1.赋值
1.1序列解包
将多个值的序列解开,然后放到变量的序列中。当函数或者方法返回元组时,这个特性尤其重要。
values=1,2,3
print(values)#(1, 2, 3)
x,y,z=values
print(x)#1
scoundrel={'name':'Robin','girlfriend':'Marion'}
key,value=scoundrel.popitem()
print(key)#girlfried
print(value)#Marion
1.2链式赋值
将同一个值赋给多个变量的捷径
1.3增量赋值
x=2
x+=1
x*=2
print(x)#6
2.语句块
语句块时在条件为真(条件语句)时执行或者执行多次(循环语句)的一组语句,在代码前放置空格来缩进语句即可创建语句块。:(冒号)来表示语句块的开始。
3.条件和条件语句
name=input('what is your name?')#what is your name?Gumby/Jack
if name.endswith('Gumby'):
print('Hello,Gumby')#Hello,Gumby
else:
print('Hello,stranger')#Hello,stranger
检查多个条件可以使用elif,if语句里也可以嵌套if语句
比较运算符
相等运算符 ==
同一性运算符 is
字符串与序列比较 按照字母顺序进行比较
布尔运算符 and 类似于C++中的&& or类似于C++中的|| not
4.循环
4.1 while循环
name=''
while not name:
name=input('Please input your name:')#Please input your name:Jack
print('Hello,',name)#Hello, Jack
4.2 for循环
words=['this','is','an','ex','parrot']
for word in words:
print(word)#this is an ex parrot
4.3一些迭代工具
Zip函数 当短的迭代完之后自动停止