Python 常用文件扩展名为 .py,可以新建一个 hello.py 文件开始下面的学习,执行使用命令 python3 hello.py
字母 或 下划线 '_'字母、数字 和 下划线 组成大小写敏感不能用作标识符名称['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
python中注释以 # 开头,如:
# 注释一
print ("Hello, world!") # 注释二
多行注释可以使用 ''' 或 " " "
'''
多行注释1
'''
"""
多行注释2
"""
缩进 来表示代码块,不需要使用大括号 {}空格数是可变的,但是 同一个代码块 的语句必须包含 相同的缩进空格数缩进相同 的一组语句构成一个代码块,我们称之 代码组if 、while 、def 和 class 这样的 复合语句,首行以 关键字 开始,以 冒号( : ) 结束# 缩进测试
if True:
print(111)
print(222)
# 缩进正确
print(333)
else:
print(000)

如果编辑器装了python的扩展,在缩进不一致时就会直接提示

# 缩进测试
if True:
print(111)
print(222)
# 缩进不一致时,会导致运行错误
print(333)
else:
print(000)

反斜杠 \ 来实现多行语句[], {}, 或 () 中的多行语句,不需要使用反斜杠 \ # 多行语句测试
a = 1
b = 2
c = 3
# 一行
num1 = a + b + c
print(num1)
# 多行
num2 = a + \
b + \
c
print(num2)
# 多行()
num3 = (a +
b +
c)
print(num3)

语句之间使用 ; 分割
#在同一行中使用多条语句
a1 = 1; a2 = 2; total = a1 + a2; print(total);

不需要声明使用前必须赋值,变量赋值以后该变量才会被创建变量所指的内存中对象的类型= 赋值= 为多个变量赋同一值, = 组合可为多个变量依次赋值#变量赋值
b1 = 1
b2 = 2
c1 = c2 = c3 = 3
d1, d2, d3 = 10, 'test', 20
print(b1, b2, c1, c2, c3, d1, d2, d3)

函数以 def 声明,格式如下:
def 函数名(参数列表):
函数体
例如:
# 函数实例
def max (a, b):
if a > b:
return a
else:
return b
num1 = 10
num2 = 20
print(max(num1, num2))

冒号 :缩进 来划分语句块,相同缩进数 的语句在一起组成一个语句块if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
没有 switch...case 语句,一连串的 if-else 不友好时可以考虑 match...case(Python 3.10增加)_ 可以匹配一切match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>
冒号 和 缩进while 循环 可以使用 else 语句,while 后语句为 false 时会执行 else后的语句while 判断条件(condition):
执行语句(statements)……
例1:
# 计算 1 到 100 的总和
n = 100
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1
print("1 到 %d 之和为: %d" % (n,sum))

例2:
# while else
count = 0
while count < 5:
print (count, " 小于 5")
count = count + 1
else:
print (count, " 大于或等于 5")

break 语句用于跳出 当前循环体continue 语句用于跳出 当次循环for <variable> in <sequence>:
<statements>
else:
<statements>
例1:
# for 循环
arr = [1, 2, 3, 4]
for item in arr:
if item == 3:
print(item)
break
print(item)
print('结束循环')

例2:
# for else
arr = []
for item in arr:
if item == 3:
print(item)
break
print(item)
else:
print('没有循环数据')
print('结束循环')

Python3 中有 六个 标准的数据类型:
Number(数字)String(字符串)List(列表)Tuple(元组)Set(集合)Dictionary(字典)六个标准数据类型中:
不可变 数据(3 个):Number(数字)、String(字符串)、Tuple(元组);可变 数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。