print 'hello world' i = 5 print i i = i +1 print i s = '''This is you''' print s
控制流
#!/usr/bin/python #Filename:if.py number = 23 guess = int(raw_input('Enteran integer:')) if guess == number: print 'congratulations, you guessed it' print "(but you do not in any prizes!)" elif guess < number: print 'no, it is a little higher than that' else: print 'no, it is a little' print 'done'
可以在while循环中使用一个else从句
#!/usr/bin/python #Filename:while.py number = 23 running = True while running: guess = int(raw_input('enter an integer:')) if guess == number: print 'congratulations ,you guessed it ' running = False; elif guess < number: print 'no,it is a little higher than that' else: print 'no, it is a little lower than that' else: print 'the while loop is over' print 'done'
!/usr/bin/python #Filename:for.py for i in range(1,5): print i else: print 'the for loop is over'Python的for循环从根本上不同于C/C++的for循环。C#程序员会注意到Python的for循环与C#中
#!/usr/bin/python #Filename:break.py while True: s = raw_input('enter something:') if s == 'quit': break print 'Length of the string is ',len(s) print 'done'
#!/usr/bin/python #Filename:continue.py while True: s = raw_input('enter something:') if s == 'quit': break if len(s)<3: continue print 'input is of sufficient length'
continue语句对于for循环也有效。
函数通过def关键字定义。def关键字后跟一个函数的 标识符 名称,然后跟一对圆括号。圆括号
之中可以包括一些变量名,该行以冒号结尾。接下来是一块语句,它们是函数体。下面这个例
子将说明这事实上是十分简单的:
#!/usr/bin/python #Filename:function1.py def sayHello(): print 'hello world' sayHello()
#!/usr/bin/python #Filename:func_param.py def printMax(a,b): if a>b: print a,'is max num' else: print b,' is max num' printMax(3,4) x = 5 y = 7 printMax(x,y)
#!/usr/bin/python #Filename:func_local.py def func(x): print 'x is ',x x = 2 print 'changed local x to ',x x = 50 func(x) print 'x is still',x
#!/usr/bin/python #Filename:global.py def func(): global x print 'x is ',x x = 2 print 'changed local x to ',x x = 50 func() print 'value of x is ',x
<span style="font-size:18px;">[root@fengniu020 test]# python global.py x is 50 changed local x to 2 value of x is 2
#!/usr/bin/python #Filename:func_default.py def say(message,times = 1): print message*times say('hello') say('world',5)
[root@fengniu020 test]# python func_default.py hello worldworldworldworldworld
#!/usr/bin/python #Filename:func_key.py 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)运行结果:
[root@fengniu020 test]# python func_key.py a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
#!/usr/bin/python #Filename:func_return.py def func(x,y): if x>y: return x else: return y print func(2,3)D ocStrings
#!/usr/bin/python #Filename:func_doc.py def printMax(x,y): '''Prints the max of two numbers. The two values must be integers.''' x = int(x) y = int(y) if x>y: print x,' is max' else: print y,' is max' printMax(3,5) print printMax.__doc__
[root@fengniu020 test]# python func_doc.py 5 is max Prints the max of two numbers. The two values must be integers.
#!/usr/bin/python #Filename:func_doc.py def printMax(x,y): '''Prints the max of two numbers. The two values must be integers.''' x = int(x) y = int(y) if x>y: print x,' is max' else: print y,' is max' printMax(3,5) print printMax.__doc__ help(printMax)