python笔记-4

上一篇 / 下一篇  2012-06-08 13:04:25 / 个人分类:Python笔记

1、<  >  !=  == is in
>>> 9 < 7  //操作符
False
>>> 9 <=9
True
>>> 9!=3
True
>>> 9!=9
False
>>> ne =[21,22,23]
>>> tow = [21,22,23]
>>> ne == tow
True
>>> one is tow
False
>>> one is one
True
>>> three = four = [3,4,5]
>>> three is four
True
>>> pizza = "pizzahut"
>>> 's' in pizza
False
>>> 'p' in pizza
True

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}


TAG: Python python

 

评分:0

我来说两句

Open Toolbar