python中代码是有意义的,一个制表符或者4个空格代表一行代码段
AAAAAAAAAA
BBBBBBBBBBB
BBBBBBBBBBB
CCCCCCCCC
CCCCCCCCC
BBBBBBBBBBB
BBBBBBBBBBB
上面共有三个代码块,包含关系如下,C行被B行包含,B代码块有被A包含。
最简单的if –then –else
>>> age = 12
>>> if age == 12:
print('It\'s %s years old ' % age)
else:
print("haha,too young!")
It's 12 years old
>>>
if 和elif
python总没有else if的说法,只能用elif
>>> age =12
>>> if age == 10:
print("too young")
elif age == 11:
print("Also too yount")
elif age == 12:
print("Good ,you are lucky %s years old !" % age )
elif age == 13:
print("Too old!")
else:
print("Very old,haha!")
Good ,you are lucky 12 years old !
>>>
常用运算比较符号
符号 | 定义 |
---|---|
== | 等于 |
!= | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
循环
for循环
while循环
#打印一个变量十次
>>> print(age)
10
>>> for x in range(0,5):
print(age)
10
10
10
10
10
range和list作用
range相当于一个迭代器,list十个列表
>>> print(list(range(1,20)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
while循环
>>> while i <= 5 and j <= 100:
i=i+1
j=j+1
print(i,j)
>>>