条件 循环及其他语句
import的更多信息
import可以从模块导入函数,可以在语句末尾增加as子句,提供别名(可为函数或模块提供别名)
>>> import math
>>> math.sqrt(4)
2.0
>>> import math as foobar
>>> foobar.sqrt(4)
2.0
>>> from math import sqrt as foobar
>>> foobar(4)
2.0
赋值
- 序列解包
将多个值的序列解开,然后放到变量的序列中。python3中有另外的特性可以像在函数的参数列表中一样使用星号运算符。
>>> d1 = {"d1":"1","d2":"2"}
>>> key,value = d1.popitem()
>>> key
'd2'
>>> value
'2'
>>> a,b,*test = [1,2,3,4,5]
>>> a
1
>>> b
2
>>> test
[3, 4, 5]
- 链式赋值
将同一个值赋给多个变量 - 增量赋值
将表达式下x = x + 1写成x += 1这种写法叫增量赋值,对于*,/,%等标准运算符都适用,使代码紧凑,易读。
>>> x = 3
>>> x *= 2
>>> x
6
>>> fnord = "foo"
>>> fnord += "bar"
>>> fnord *= 2
>>> fnord
'foobarfoobar'
条件和条件语句
- 布尔值
以下值在作为布尔表达式的时候,会被解释器看作假(False)
False None 0 " " () [] {}(都为假值,本身不相等)
换句话说,也就是标准的False和None、所有类型的数字0(浮点型,长整型和其他类型)、空序列以及空的字典都为假,其他一切都被解释为真。也就是python中的所有值都能被解释为布尔值
>>> bool("test")
True
>>> bool("")
False
>>> bool(0)
False
>>> bool([] == "")
False
- 条件执行和if语句
age = 3
if age >= 18:
print('adult')
elif age >= 6:
print('teenager')
else:
print('kid')
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容器的成员 |
- 断言
你可以要求某些条件必须为真,语句的关键字就是assert
>>> age = 10
>>> assert 0 < age <10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert 0 < age <100
>>>
循环
- while循环
x =1
while x<= 10:
print(x)
x +=1
- for循环
words = ["this","is","an","parrot"]
for word in words:
print(word)
for number in range(0,10):
print(number)
- 循环遍历字典元素
# 遍历字典元素
d = {"x":"1","y":"2","z":"3"}
for key in d:
print(key,"=",d[key])
#使用序列解包 便利
for key, value in d.items():
print (key,"=",value)
- 迭代
# 并行迭代
names = ["a1","a2","a3","a4"]
ages = [22,24,26,23]
for i in range(len(names)):
print(names[i], "is", ages[i], "years oid")
# 使用内建函数zip来并行迭代
for name, age in zip(names,ages):
print(name, "is", age, "years old")
# zip可以用来处理不等长的数列
for x, y in zip(range(5), range(100)):
print (x,y)
- 跳出循环
break
from math import sqrt
# break跳出循环
for n in range(99, 0, -1):
root = sqrt(n)
if root == int(root):
print(n)
break
continue
# continue 结束当前循环,跳到下一轮循环
for n in range(0, 10):
if n%2 == 0:
continue
else:
print(n)
列表推导式
利用其他列表创建新的列表的一种方式,类似于for循环
# 列表推导式
a = [x*x for x in range(10)]
print(a)
b = [x*x for x in range(10) if x % 3 == 0]
print(b)
c = [(x, y) for x in range(3) for y in range(2)]
print(c)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 9, 36, 81]
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
pass、del和exec
- pass
程序什么事情都不用做,就需要pass语句。python中空代码块是非法的
# pass
flage = -1
if flage < 0:
print("小于0")
elif flage >0:
# 还未完成。。。
pass
else:
print("0")
- del
del语句会移除一个对象的引用,也会移除那个名字(删除的只是名字,而不是本身的值,python没办法删除值,有解释器负责内存回收)
>>> x = 1
>>> del x
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = ["hello python"]
>>> y =x
>>> del x
>>> y
['hello python']
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
- exec和eval
exec执行一个字符串语句。动态的创造python代码
eval会计算python表达式(以字符串形式书写)
>>> exec("print('hello!')")
hello!
>>> eval("6+8*2")
22