Python3 Basics语法

UnderstandPython3 basic语法, variableclass型 and 运算符

1. Python3 Introduction

Python is a解释型, 面向object, 动态dataclass型 advanced程序designlanguage. Python3 is Python 最 new version, 于2008年12月3日release, 它不完全兼容Python2.xversion. Python3引入了许how newfeatures and improvement, 使其成 for 现代programming 理想选择.

1.1 Python3 特点

  • simple 易学: Python 语法简洁明了, code readable 性 high , 适合初学者Learning
  • 面向object: support面向objectprogramming, 但也可以using过程式 or function式programming风格
  • 丰富 library: 拥 has 强 big 标准library and 第三方library, 几乎可以application于任何领域
  • 跨平台: 可以 in Windows, Mac OS and Linuxetc. many 个平台 on run
  • 解释执行: 不需要编译, 直接解释执行, Developmentefficiency high

2. Python3 Basics语法

2.1 comment

in Pythonin, comment以#开头, 用于解释code functions and 作用.

# 这 is 一行comment
print("Hello, Python!")  # 这 is 行尾comment

2.2 variable

Pythonin variable不需要声明class型, 直接赋值即可. variable名必须以字母 or under 划线开头, 不能以number开头, 且区分 big small 写.

# variable赋值
name = "Python"
age = 3
price = 99.99
is_valid = True

# 打印variable值
print(name)
print(age)
print(price)
print(is_valid)

2.3 dataclass型

Python3 has 以 under basicdataclass型:

  • number (Number) : 整数, 浮点数, 复数
  • string (String) : 用单引号 or 双引号括起来 文本
  • boolean值 (Boolean) : True or False
  • list (List) : has 序 可变序列
  • 元组 (Tuple) : has 序 不可变序列
  • collection (Set) : 无序 不重复元素collection
  • dictionary (Dictionary) : 无序 键值 for collection
# number
num1 = 10         # 整数
num2 = 3.14       # 浮点数
num3 = 1 + 2j     # 复数

# string
str1 = "Hello"     # 双引号string
str2 = 'World'     # 单引号string
str3 = """Multi
line
string"""     # 三引号 many 行string

# boolean值
bool1 = True
bool2 = False

# list
list1 = [1, 2, 3, 4, 5]
list2 = ["apple", "banana", "cherry"]

# 元组
tuple1 = (1, 2, 3)
tuple2 = ("a", "b", "c")

# collection
set1 = {1, 2, 3, 4, 5}
set2 = {"apple", "banana", "cherry"}

# dictionary
dict1 = {"name": "Python", "version": 3, "type": "programming language"}
dict2 = {1: "one", 2: "two", 3: "three"}

2.4 运算符

Python3support many 种运算符:

2.4.1 算术运算符

a = 10
b = 3

print(a + b)  # 加法
print(a - b)  # 减法
print(a * b)  # 乘法
print(a / b)  # 除法
print(a % b)  # 取余
print(a ** b) # 幂运算
print(a // b) # 整除

2.4.2 比较运算符

a = 10
b = 3

print(a == b)  # etc.于
print(a != b)  # 不etc.于
print(a > b)   #  big 于
print(a < b)   #  small 于
print(a >= b)  #  big 于etc.于
print(a <= b)  #  small 于etc.于

2.4.3 逻辑运算符

a = True
b = False

print(a and b)  #  and 运算
print(a or b)   #  or 运算
print(not a)    # 非运算

2.5 输入输出

Pythoninusingprint()function输出 in 容, usinginput()function获取user输入.

# 输出
print("Hello, Python!")
print("The answer is", 42)

# 输入
name = input("请输入你 名字: ")
print("你 good , " + name + "!")

age = input("请输入你 年龄: ")
print("你今年" + age + "岁. ")

实践case: 计算圆 面积

writing一个Python程序, 计算圆 面积. 程序应该:

  1. 提示user输入圆 半径
  2. 计算圆 面积 (面积 = π × 半径²)
  3. 输出计算结果
# 计算圆 面积
import math

# 提示user输入半径
radius = float(input("请输入圆 半径: "))

# 计算面积
area = math.pi * radius ** 2

# 输出结果
print("圆 面积 is : ", area)

3. Python3 basic语句

3.1 indent

Pythonusingindent来表示code块, 而不 is using big 括号. indent通常 for 4个空格 or 一个制表符.

# indentexample
if 5 > 2:
    print("5 big 于2")
    print("这 is indent code块")
print("这 is indent out  code")

3.2 many 行语句

Pythonin, 一行可以写 many 条语句, 用分号分隔. such as果语句太 long , 可以using反斜杠换行.

#  many 行语句example
x = 1; y = 2; z = 3  # 一行 many 条语句

#  long 语句换行
a = 1 + 2 + 3 + \n    4 + 5 + 6 + \n    7 + 8 + 9

print(a)

3.3 variable命名规则

  • variable名必须以字母 or under 划线开头
  • variable名不能以number开头
  • variable名只能package含字母, number and under 划线
  • variable名区分 big small 写
  • 不能usingPython关键字serving asvariable名

互动练习: variable and 运算符

1. 请creation一个variablestore你 姓名, 然 after 打印出来.
2. 请creation两个variablestore两个number, 然 after 计算它们 and , 差, 积, 商并打印出来.
3. 请writing一个程序, 将user输入 温度 from 摄氏度转换 for 华氏度 (公式: 华氏度 = 摄氏度 × 9/5 + 32) .

4. Python3 关键字

Python3 has 以 under 关键字, 这些关键字不能用作variable名:

False    class    finally  is       return
None     continue for      lambda   try
True     def      from     nonlocal while
and      del      global   not      with
as       elif     if       or       yield
assert   else     import   pass
break    except   in       raise