1、常量与变量
常量: 一旦赋值,就不可再改变,换句话说,就是不能对它重新赋值。Python不存在常量
字面常量:一个单独出现的量,未赋值给任何变量或常量
变量: 是一个名字,在赋值符号的左边,这个名字可以指代赋值符号右边的内容
In [37]: i = 3
In [38]: print(i)
3
2、系统类型
n [39]: 3 + 3
Out[39]: 6
In [40]: 3 + '3'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-1142db5d42c6> in <module>()
----> 1 3 + '3'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [41]:
In [41]: i = 3
In [42]: type(i)
Out[42]: int
In [43]: i = '3'
In [44]: type(i)
Out[44]: str
3、运算符
3、1 算术运算符--算术运算符只能对int和float运算
In [45]: 1 + 1
Out[45]: 2
In [46]: 1 + 1.2
Out[46]: 2.2
In [47]: 3 / 2
Out[47]: 1.5
In [48]: 3 // 2
Out[48]: 1
3、2比较运算符--比较运算的返回值都是bool类型
In [49]: 3 > 5
Out[49]: False
In [50]: 3 < 5
Out[50]: True
In [51]: 3 != 5
Out[51]: True
3/3逻辑运算符--逻辑运算符的操作数都是bool类型或者可以隐式转化成bool类型的类型, 返回值都是bool类型
In [52]: True and False
Out[52]: False
In [53]: True or False
Out[53]: True
In [54]: not False
Out[54]: True
4、程序结构
4、1 顺序结构
4/2 分支结构
单分支
In [56]: if a > 3:
...: print('a > 3')
...: print(a)
...:
a > 3
5
双分支结构
In [57]: a = 5
In [58]: if a < 10:
...: print('a < 10')
...: else:
...: print('a > 10')
...: print(a)
...:
a < 10
5
多分支结构
In [59]: a = 30
In [60]: if a < 10:
...: print('a < 10')
...: elif a < 20:
...: print('10 <= a < 20')
...: else:
...: print('a >= 20')
...: print(a)
...:
a >= 20
30
4/3 循环结构
while循环
In [61]: i = 0
In [62]: while i < 5:
...: print(i)
...: i += 1
...:
0
1
2
3
4
一定要有某种机制修改调制使其退出循环,通常是在循环体里修改条件
for循环
In [63]: for i in range(0,5):
...: print(i)
...:
0
1
2
3
4
for in 循环里永远不要修改可迭代对象
5、break 与continue
break:提前终止
In [64]: for i in range(0,10):
...: print(i)
...: if i % 2 != 0:
...: print('ok')
...: break
...:
0
1
ok
continue:用于跳过循环体剩下的部分
In [65]: for i in range(0,10):
...: if i % 2 != 0:
...: print(i)
...: continue
...:
1
3
5
7
9
else字句--当循环没有提前退出时,会执行else子句
In [66]: for i in range(0,5):
...: pass
...: else:
...: print('ok')
...:
ok
In [67]: for i in range(0,5):
...: break
...: else:
...: print('ok')
...:
...:
In [68]: for i in range(0,5):
...: continue
...: else:
...: print('ok')
...:
...:
...:
ok