python基础04 运算
数学运算
>>> print 3+3 6 >>> print 1.3-3 -1.7 >>> print 3*5 15 >>> print 4.5/2 2.25 >>> print 2/1.5 1.33333333333 >>> print 3**2 9 >>> print 10%3 1
判断
返回ture False
>>> print 5 == 6 False >>> print 5 == 5 True >>> print 4>6 False >>> print 7>6 True >>> print 5 in [1,3,5] True >>> print 4<6 True >>> print 3 is 5 False >>> print 3 is 344543 False >>> print 3 in (1,2) False >>> print 3 in (1,2,3) True >>> print 'a' in '344543' False >>> print 'a' in '344543a' True
逻辑运算
>>> print True and False False >>> print True or False True >>> print not True or False False >>> print not True False
数学 +, -, *, /, **, %
判断 ==, !=, >, >=, <, <=, in
逻辑 and, or, not
缩进
python用缩进来写模块
>>> i=4 >>> if i>3: ... print i 4
循环
用于重复执行一些程序块,
for
预先设定好循环的次数n,让后执行属于for的语句n次
for 元素 in 序列:
statement
>>> for a in [1,2,3,4,'life']: ... print a ... 1 2 3 4 life
表是一种序列,
>>> for a in range(1,3): ... print a ... 1 2
while
while的用法
while 条件:
statement
不停的循环执行statement,直到条件为假False
>>> while i<10: ... print i ... i+=1 ... 3 4 5 6 7 8 9
中断循环
continue 跳出本次循环,执行下次循环
break 停止执行整个循环