Python 条件、循环和其他语句
1、print在Python3.0以后不再是语句而是一个函数
2、False、None、0、""、''、()、[]、{} 在作为布尔表达式的时候,会被解释器看着假(false),其他的一切都被结束为真
3、为模块和模块中的函数重命名
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 为整个模块提供别名 import math as chen print(chen.sqrt(9)) # 3.0 # 为函数提供别名 from math import sqrt as yan print(yan(16)) # 4.0
4、序列解包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
## 序列解包(sequence unpacking) x,y,z = 1,2,3 print(x,y,z) # 1 2 3 x,y = y,x print(x,y,z) # 2 1 3
5、Python3.0以后没有 raw_input 这个函数,input实现了 raw_input 有的作用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = input('what is your name:')
print("hello,"+ name + "!")
6、链式赋值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
## 链式赋值
a = b = 4
print(a,b)
# 4 4
7、条件语句的用法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = input('what is your name:') if name == 'Chen': print('Welcome,Mr.%s' % name) else: print('Hello,stranger') print("hello,"+ name + "!") num = input('Enter a number:') if int(num)>0: print('正数') elif int(num)<0: print('负数') else: print('零')
8、Python中的比较运算符
x == y x等于y
x < y x小于y
x > y x大于y
x>=y x大于等于y
x<=y x小于等于y
x!=y x不等于y
x is y x和y是同一对象
x is not y x和y是不同的对象
x in y x是y容器的成员
x not in y x不是y容器的成员
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
x = y = [1,2,3]
z = [1,2,3]
print(x==y) # True
print(x is y) # True
print(x is z) # False
a = [1,2,3]
b = [2,4]
print( a is not b) # True
del a[2]
b[1] = 1
b.reverse()
print( a== b ) # True
print( a is b ) # False
9、断言
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
age = 10 assert 0 < age < 100 age = -1 assert 0 < age < 100 # 如果条件为假,则提示异常
10、break 结束循环,continue结束本次循环跳到下一轮循环开始
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 打印1 -- 100 x = 1 while x<=100: print(x) x += 1 # 打印列表中的每个元素 words = ['this','is','an','ex','parrot'] for word in words: print(word) dic = {'x':1,'y':2,'z':3} for key in dic: print(key,'corresponds to',dic[key]) # x corresponds to 1 # y corresponds to 2 # z corresponds to 3 # 并行迭代 names = ['anne','brth','george','damon'] ages = [12,45,32,102] # anne is 12 years old for i in range(len(names)): print(names[i],'is',ages[i],'years old')
11、列表推导式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
## 列表推导式 print([x*x for x in range(10)]) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] girls = ['alice','bernice','clarice'] boys = ['chris','aenold','bob'] print( [b+'+'+g for b in boys for g in girls if b[0]==g[0]] ) # ['chris+clarice', 'aenold+alice', 'bob+bernice'] print([(x,y) for x in range(3) for y in range(3)]) # [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
12、pass 有的时候程序什么事情都不用做,就需要使用pass语句,在代码中做占位符使用,因为Python中执行空代码块是非法的
使用 del 删除对象
exec 用来执行存储在字符串或者文件中的Python语句
eval 会计算Python表达式(以字符串的形式书写),并且返回结果值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
## pass num = 3 if num > 0: print('%d 是正数' % num) elif num == 0: # 不执行 pass else: print('%d 是负数' % num) ## del person = {'age':42, 'first name':'Robin', 'last name':'Lorsy'} print(person) # {'age': 42, 'first name': 'Robin', 'last name': 'Lorsy'} someBody = person print(someBody) # {'age': 42, 'first name': 'Robin', 'last name': 'Lorsy'} person = None print(someBody) # {'age': 42, 'first name': 'Robin', 'last name': 'Lorsy'} someBody = None print(someBody) # None # 将someBody和person绑定到同一个字典上,当person设置为None的时候,someBody还是可以用的, # 当我把someBody也设置为None的时候,字典就飘在内存里面了,Python会直接删除那个字典 x = 1 del x # print(x) # error # 使用del语句,它不仅会移除一个对象的引用,也会移除那个名字本身 x = ['Hello', 'World'] y = x y[1] = 'Python' print(x) # ['Hello', 'Python'] del x print(y) # ['Hello', 'Python'] ## exec exec("print('hello world')") # hello world # 使用命名空间(命名域) from math import sqrt scope = {} exec('sqrt = 1',scope) # exec("sqrt = 1") in scope print(sqrt(4)) # 2.0 print(scope['sqrt']) # 1 # 潜在的破坏性代码并不会覆盖sqrt函数,原来的函数可以正常工作,而通过exec赋值的变量sqrt只在它的作用域内有效 ## eval print(eval(input("Enter an arithmetic expression: "))) # Enter an arithmetic expression: 6+18*2 # 42 # 给eval使用命名空间,可以提供两个命名空间,一个全局的一个局部的,全局的必须是字典,局部的可以是任何形式的映射 space = {} space['x'] = 2 space['y'] = 3 print(eval('x*y',space)) # 6