您的位置:首页 > 编程语言 > Python开发

Python控制流和函数--Python学习笔记二

2016-04-30 21:42 686 查看
print 'hello world'
i = 5
print i
i = i +1
print i

s = '''This is
you'''
print s

控制流

if语句

#!/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循环

可以在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'


for循环

!/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#中

的foreach循环十分类似。Java程序员会注意到它与Java 1.5中的for (int i : IntArray)相似。

在C/C++中,如果你想要写for (int i = 0; i < 5; i++),那么用Python,你写成for i in range(0,5)。你

会注意到,Python的for循环更加简单、明白、不易出错。

break语句

break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,

也停止执行循环语句。

一个重要的注释是,如果你从for或while循环中 终止 ,任何对应的循环else块将不执行。

#!/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'


continue语句

continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。

使用continue语句

#!/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


使用global语句

如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,

而是 全局 的。我们使用global语句完成这一功能。没有global语句,是不可能为定义在函数外

的变量赋值的。

你可以使用定义在函数外的变量的值(假设在函数内没有同名的变量)。然而,我并不鼓励你

这样做,并且你应该尽量避免这样做,因为这使得程序的读者会不清楚这个变量是在哪里定义

的。使用global语句可以清楚地表明变量是在外面的块定义的。

你可以使用同一个global语句指定多个全局变量。例如global x, y, z。

#!/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

return语句

return语句用来从一个函数 返回 即跳出函数。我们也可选从函数 返回一个值 。

#!/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
Python有一个很奇妙的特性,称为 文档字符串 ,它通常被简称为 docstrings 。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.


文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,

从第三行开始是详细的描述。 强烈建议 你在你的函数中使用文档字符串时遵循这个惯例。

你可以使用__doc__(注意双下划线)调用printMax函数的文档字符串属性(属于函数的名

称)。请记住Python把 每一样东西 都作为对象,包括这个函数。我们会在后面的类一章学习

更多关于对象的知识。

如果你已经在Python中使用过help(),那么你已经看到过DocStings的使用了!它所做的只是抓

取函数的__doc__属性,然后整洁地展示给你。你可以对上面这个函数尝试一下——只是在你

的程序中包括help(printMax)。记住按q退出help。

#!/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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: