第五章:条件、循环和其他语句
1.print和import的更多信息
1.1使用逗号输出
>>> print 'Age:', 42
Age: 42
>>>1,2,3
(1,2,3)
>>>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
如何greeting字符串不带逗号
print greeting+',' , salutation,name
如何在结尾加上逗号,接下来的语句和前一条语句在同一行打印
print 'Hello.'
print 'world!'
输出Hello.world!
1.2从模块导入函数
import somemodule
from somemodule import somefunction
from somemodule import somefunction,anotherfunction
from somemodule import *
如何两个模块都有相同的函数
module1.open(...)
module2.open(...)
或者在语句末尾增加as字句
>>>import math as foobar
>>>foobar.sqrt(4)
2.0
也可为函数提供别名
>>>from math import sqrt as foobar
>>>foobar(4)
2.0
from module1 import open as open1
from module2 import open as open2
2.赋值魔法
2.1序列解包
>>>x,y,z=1,2,3
>>>print x,y,z
1 2 3
>>>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
'grilfriend'
>>>value
'Marion'
>>>x,y,z=1,2
ValueError:
2.2增量赋值
>>>x = 2
>>>x += 1
>>>x *= 2
>>>x
6
>>>fnord = 'foo'
>>>fnordd += 'bar'
>>>fnord *= 2
>>>fnord
'foobarfoobar'
3条件和条件语句
布尔值
下面的值在作为布尔表达式的时候,会被解释器看作假(false)
False None 0 " " () [] {}
>>>True
True
>>>False
False
>>>True == 1
True
>>>False == 0
True
>>>Frue + False + 42
43
>>>bool('I think,therefore I am')
True
>>>bool(42)
True
>>>bool(' ')
False
>>>bool(0)
False
3.2条件执行和if语句
name = raw_input('What is your name? ')
if name.endswith('Gumby')
print 'Hello. Mr , Gumby'
else:
print 'Hello. stranger'
elif字句相当于else if
num = input('Enter a number: ')
if num>0:
print 'The number is positive.'
elif num0:
print 'The number is negative'
else:
print 'The number is zero'
4.更复杂的条件
4.1比较运算符
4.2赋值运算符
>>>'foo' == 'foo'
True
>>> 'foo' == 'bar'
False
4.3
同一性运算符:is
>>>x=y=[1,2,3]
>>>z=[1,2,3]
>>>x == y
True
>>>x == z
True
>>> x ix y
True
>>>x is z
False
4.4成员资格运算符:in
name = raw_input('What is your name?')
if 's' in name:
print 'Your name contains the letter "s"'
else:
print 'Your name does not contains the letter "s"'
4.5字符串和序列比较
字符串可以按照字母顺序排列进行比较
>>>'alpha'
True
>>>[1,2]
True
>>>[2,[1,4]]
True
4.6布尔运算符
number = input('Enter a number between 1 and 10: ')
if number <= 10 andd number >= 1:
print 'Great!'
else:
print 'Wrong!'
5.断言:与其让程序在晚些时候崩溃,不如在错误条件时崩溃
>>>age = -1
>>>assert 0
AssertError: The age must be realistic
6.循环:
6.1while循环
x = 1
while x <= 100:
print x
x += 1
name=''
while not name:
name = raw_input('Please enter your name:')
print 'Hello,%s!' % name
6.2for循环
numbers= [0,1,2,3,4,5,6,7,8,9]
for number in number:
print number
>>>range(0,10)
[0,1,2,3,4,5,6,7,8,9]
或者range(10)
for number in range(1,101):
print number
6.3循环遍历字典元素
d = {'x':1,'y':2,'z':3}
for key in d:
print key, 'corresponds to',d[key]
for key,value in d.items():
print key,'corresponds to',value
6.4一些迭代工具
6.4.1并行迭代
names = ['anne','beth','george','damon']
ages = [12,45,32,102]
for i in range(len(names)):
print names[i],'is',ages[i],'years old'
zip函数实现并行迭代
>>>zip(names,ages)
[('anne',12),('beth',45),('george',32),('damon',102)]
for name,age in zip(names,ages):
print name,'is',age,'years old'