2、and
复制代码 代码如下:
example = 5
>>> if example > 3 and example <10:
print 'number is between 3 and 10'
KeyboardInterrupt
>>> if example > 3 and example <10:
print 'number is between 3 and 10'
number is between 3 and 10
3、or
复制代码 代码如下:
if example >3 or example <4:
print "this works"
this works
>>> if example >3 or example <10:
print 'hey now'
hey now
4、while
复制代码 代码如下:
b = 1
>>> b
1
>>> while b <= 10:
print b
b += 1
1
2
3
4
5
6
7
8
9
10
5、for
复制代码 代码如下:
gl=['bread','milk','meat','beef']
>>> gl
['bread', 'milk', 'meat', 'beef']
>>> for food in gl:
print 'I want'+food
I wantbread
I wantmilk
I wantmeat
I wantbeef
6、
break
复制代码 代码如下:
>>> while 1:
name = raw_input('Enter name: ')
if name == 'quit':break
Enter name: grep
Enter name: tom
Enter name: lisa
Enter name: quit
7、函数
复制代码 代码如下:
def whatsup(x):
return ' whats up '+x
>>> print whatsup('tony')
whats up tony
>>> def plusten(y):
return y+10
>>> print plusten(2)
12
8、参数
复制代码 代码如下:
def name(first,last):
print '%s %s' %(first,last)
>>> name('bucky','roberts')
bucky roberts
>>> def name(first='tom',last='smith'):
print '%s %s' % (first,last)
>>> name()
tom smith
>>> name('bucky','roberts')
bucky roberts
>>> name('bucky')
bucky smith
>>> name(last='roberts')
tom roberts
9、混合参数
复制代码 代码如下:
def list(*food):
print food
>>> list('apple')
('apple',)
>>> list('apples','peaches','beef')
('apples', 'peaches', 'beef')
>>> def profile(name,*ages):
print name
print ages
>>> profile('bucky',42,43,76,56,89)
bucky
(42, 43, 76, 56, 89)
10、
复制代码 代码如下:
def cart(**items):
print items
>>> cart(apples=4,peaches=6,beef=60)
{'peaches': 6, 'apples': 4, 'beef': 60}
>>> def profile(first,last,*ages,**items):
print first,last
print ages
print items
>>> profile('bucky', 'roberts', 32,43,76,65,76, bacon=4, saus=64)
bucky roberts
(32, 43, 76, 65, 76)
{'saus': 64, 'bacon': 4}