突然进度开始加快了,一章夹杂了这么多内容。条件、循环还有其它语句,看来是要分上中下了。
第五章 条件、循环和其它语句
5.1 print和import的更多消息
print在python3.0中不再是语句,而是函数,基本功能不变。
5.1.1 使用逗号输出
打印多个表达式时,用逗号隔开。
print('Age:',42) #返回值Age: 42,打印出的结果会自动在每个参数之间插入一个空格符。
print(1,2,3) #返回值1 2 3,不会构成元组。
print((1,2,3)) #返回值(1, 2, 3)
name='Gumby'
salutation='Mr.'
greeting='Hello,'
print(greeting,salutation,name) #返回值Hello, Mr. Gumby
print(greeting,',',salutation,name) #返回值逗号前会自动插入空格符
prrint(greeting+',',salutation,name) #返回值Hello, Mr. Gumby
#print在3.0以后有了变化
print('hello',
print('world')) #返回值world hello None,不但打印顺序变了,而且会在第一个打印的结果后面加None,说明先执行括号里面的函数。
5.1.2 把某件事作为另一件事导入
从模块导入函数的格式:
import somemodule,
from somemodule import somefunction,
from some module import somefunction,anotherfunction,yetanotherfunction
from somemodule import *(只有确定自己想要从给定的模块导入所有功能时,才使用最后一个版本。
如果两个模块都有open函数
只需要使用第一种方式导入,然后按下面方式使用:
module1.open(...)
module2.open(...)
也可以在语句末尾加一个as子句,在该子句后给出名字,或为整个模块提供别名:
import math as foobar
foobar.sqrt(4) #返回值2.0
from math import sqrt as foobar #为整个模块提供别名
foobar(4) #返回值2.0
对于open函数可以像下面这样用:
from module1 import open as open1
from module2 import open as open2
5.2 赋值魔法
5.2.1 序列解包
x,y,z=1,2,3 #多个赋值同时进行
print(x,y,z) #返回值1 2 3
print(y,z,x) #返回值2 3 1,自动将x,y,z和值一一对应
x,y=y,x
print(x,y,z) #返回值2 1 3
#序列解包
values=1,2,3
values #返回值(1, 2, 3)
x,y,z=values
x #返回值1。
scoundrel={'name':'Robin','girlfriend':'Marion'}
key,value=scoundrel.popitem()
key #返回值'girlfriend'
value #返回值'Marion'
#所解包的序列中的元素数量必须和放置在赋值符号=左边的变量数量完全一致,否则会引起异常
x,y,z=1,2 #报错
a,b,rest*=[1,2,3,4] #操作也会报错illegal expression for augmented assignment。
5.2.2 链式赋值(chained assignment)
将同一个值赋给多个变量。
x=y=somefunction()跟下面语句是一样的效果
y=somefunction()
x=y
不等同于
x=somefunction()
y=somefunction()
5.2.3.增量赋值(augmented assignment)
表达式运算符放在赋值运算符左边。
x=2
x+=1 #意思是x=x+1
x #返回值3
x*=2
x #返回值6
#只要二元运算符本身适用于这些数据类型都可以使用
fnord='foo'
fnord+='bar'
fnord*=2
fnord #返回值'foobarfoobar'
5.3 语句块:缩排的乐趣
语句缩排是在条件为真时执行或者执行多次的一组语句。代码前可以通过放置空格来缩进语句即可创建语句块。类似于写作文分段会在段首加两个空格,看起来比较清楚。
5.4 条件和条件语句
5.4.1 这就是布尔变量的作用
布尔值用真(True)假(False)来表示。
下面的值在作为布尔表达式时,会被看作假(false)
False None 0,"",(),[],{}
其它一切都为真(True)
bool函数可以用来将其它值转换成布尔值
bool('I think,therefore I am') #返回值True,这里可以调戏电脑了,随便写什么电脑都会回答你true,因为返加值既不是空值也不是各种形式的0.
bool(42) #返回值True
bool('') #返回False
bool(0) #返回False
虽然[]和""都是假值,它们本身却不相等,对于其它不同的假值对象也是如此。
5.4.2 条件执行和if语句
#运行脚本
name=input("What is your name?")
if name.endswith('Gumby'): #条件判定语句如果为真,执行冒号后面的语句。如果为假,不执行。
print('Hello,Mr.Gumby')
5.4.3 else子句
#else不是独立语句,只能作为if语句的一部分,增加一种选择。
name=input("What is your name?")
if name.endswith('Gumby'):
print('Hello,Mr.Gumby')
else: #if语句为假,执行else冒号后面的语句。
print('Hello,stranger')
5.4.4 elif子句
需要检查多个条件时,就可以用elif。
num=int(input("Enter a number:")) #这里必须指定数据类型
if num>0:
print('The number is positive')
elif num<0:
print('The number is negative')
else:
print('the number is zero')
5.4.5 嵌套代码块
name=input('What is your name?')
if name.endswith('Gumby'):
if name.startswith('Mr.'): #if语句中嵌套if语句
print('Hello,Mr.Gumby')
elif name.startswith('Mrs.'):
print('Hello,Mrs.Gumby')
else:
print('Hello,Gumby')
else:
print('hello, stranger')
5.4.6 更复杂的条件
1.比较运算符
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的成员 |
2.相等运算符
单个等号是赋值,两个等号才表示相等
3. is:同一性运算符
x=[1,2,3]
y=[2,4]
x is not y #返回值True
del x[2]
y[1]=1
y.reverse()
x==y #返回值True,x和y的值相等了
x is y #返回值False
4.in:成员资格运算符
name=input('What is your name?')
if 's' in name:
print('You name contains the letter "s".')
else:
print('Your name does not contains the letter "s".')
5.字符串和序列比较
字符串可按字母顺序进行比较。
'alpha'<'beta' #返回True
'FnOrD'.lower()=='Fnord'.lower() #返回值True,如果有大小写就比较混乱,可以用upper和lower忽略大小写再比较。
[1,2]<[2,1] #返回值True。
[2,[1,4]]<[2,[1,5]] #返回True。
6.布尔运算符
number=int(input('Enter a number between 1 and 10:'))
if number <=10 and number >=1: #and 就是布尔运算符,连接两个布尔值,并且在两者都为真是返回真
print('Great!')
else:
print('Wrong!')
5.4.7 断言
assert语句在程序中置入检查点,条件后添加字符串,用来解释断言。