while语句
number=23
Running=True
while Running:
guess=int(raw_input('Enter an integer:'))
if guess==number:
print'C ongratulations,you guessed it'#New block starts here
Running=False
elif guess<number:
print'C ,you guessed it.'
else :
print'C dayu number'
else:
print"loop is over"
print'Done'
2.格式化
width = input('Please enter width: ')
price_width = 10
item_width = width - price_width
header_format = '%-*s%*s'
format = '%-*s%*.2f'
print '=' * width
print header_format % (item_width, 'Item', price_width, 'Price')
print '-' * width
print format % (item_width, 'Apples', price_width, 0.4)
print format % (item_width, 'Pears', price_width, 0.5)
print format % (item_width, 'Cantaloupes', price_width, 1.92)
print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8)
print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)
3.for循环
for i in range(1,5):
print i
else:
print'The for loop is over'
4.
while True:
s=raw_input('Enter som ething:')
if s=='quit':
break
print 'Length of the string is',len(s)
print 'Done'
5.
while True:
s=raw_input('Enter some thing:')
if s=='quit':
break
if len(s)<3:
continue
print 'Input is of sufficent length'
6.
def sayHelb():
print 'Helb Word!'
sayHelb()
7.
def printMax(a,b):
if a>b:
print a,'is maximum'
else:
print b,'is maximum'
printMax(3,4)
x=5
y=7
printMax(x,y)
8.
def func(x):
print'x is',x
x=2
print 'C hanged local x to',x
x=50
func(x)
print'x is till',x
9.
def func():
global x
print 'x is',x
x=2
print'C hanged localx to ',x
x=50
func()
print 'Value of x is',x
10.
def say(message,times=1):
print message*times
say('Hello')
say('World',5)
11.
def func(a,b=5,c=10):
print'a is ',a,'and b is' ,b,'and c is',c
func(3,7)
func(25,c=24)
func(c=50,a=100)
12.
def maximum(x,y):
if x>y:
return x
else:
return y
print maximum(2,3)
13.
def printMax(x,y):
'''Prints the maximum of two numbers.
The two values must be in tegers.'''
x=int(x)
y=int(y)
if x>y:
print x,'is maximum'
else:
print y,'is maximum'
printMax(3,5)
print printMax.__doc__