函数
def say_hello():
# 该块属于这一函数
print('hello world')
# 函数结束
say_hello() #调用函数
say_hello() #再次调用函数
函数参数
def print_max(a,b):
if a > b:
print(a,'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
# 直接传递字面值 print_max(3, 4)
x = 5
y = 7
# 以参数的形式传递变量
print_max(x,y)
局部变量
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is still', x)
结果:
$ python function_local.py
x is 50
Changed local x to 2
x is still 50
global 语句
x = 50
def func():
global x
print('x is', x)
x = 2
print('Changed global x to', x)
func()
print('Value of x is', x)
结果:
$ python function_global.py
x is 50
Changed global x to 2
Value of x is 2
默认参数值
def say(message, times=1):
print(message * times)
say('Hello')
say('World', 5)
结果
$ python function_default.py
Hello World
WorldWorldWorldWorld
关键字参数
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)
结果:
$ python function_keyword.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
可变参数
def total(a=5, *numbers, **phonebook):
print('a', a)
#遍历元组中的所有项目
for single_item in numbers:
print('single_item', single_item)
#遍历字典中的所有项目
for first_part, second_part in phonebook.items():
print(first_part,second_part)
print(total(10,1,2,3,Jack=1123,John=2231,Inge=1560))
结果:
$ python function_varargs.py
a 10
single_item 1
single_item 2
single_item 3
Jack 1123
John 2231
Inge 1560
None
return 语句
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
DocStrings
def print_max(x, y):
#第一行以某一大写字母开始,以句号结束。第二行为空,后跟的第三行开始时任何详细的解释说明
'''打印两个数值中的最大数。
这两个数都应该是整数'''
# 如果可能,将其转换至整数类型
x = int(x)
y = int(y)
if x > y:
print(x, 'is maximum')
else:
print(y, 'is maximum')
print_max(3, 5)
print(print_max.__doc__)
结果
5 is maximum
打印两个数值中的最大数。
这两个数都应该是整数
该文档字符串所约定的是一串多行字符串,其中第一行以某一大写字母开始,以句号结束。 第二行为空行,后跟的第三行开始是任何详细的解释说明。 在此强烈建议你在你所有重要功 能的所有文档字符串中都遵循这一约定。
模块
import sys
print('The command line arguments are:')
for i in sys.argv:
print(i)
print('\n\nThe PYTHONPATH is', sys.path, '\n')
结果
The command line arguments are:
C:/Users/shenyonghui/PycharmProjects/untitled1/venv/module_using_sys.py
The PYTHONPATH is ['C:\\Users\\shenyonghui\\PycharmProjects\\untitled1\\venv', 'C:\\Users\\shenyonghui\\PycharmProjects\\untitled1', 'C:\\Users\\shenyonghui\\PycharmProjects\\untitled1\\venv\\Scripts\\python37.zip', 'C:\\Users\\shenyonghui\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\shenyonghui\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\shenyonghui\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\shenyonghui\\PycharmProjects\\untitled1\\venv', 'C:\\Users\\shenyonghui\\PycharmProjects\\untitled1\\venv\\lib\\site-packages', 'C:\\Users\\shenyonghui\\PycharmProjects\\untitled1\\venv\\lib\\site-packages\\setuptools-39.1.0-py3.7.egg', 'C:\\Users\\shenyonghui\\PycharmProjects\\untitled1\\venv\\lib\\site-packages\\pip-10.0.1-py3.7.egg']
from…import 语句
from math import sqrt
print("Square root of 16 is", sqrt(16))
模块的 name
if __name__ == '__main__':
print('This program is being run by itself')
else:
print('I am being imported from another module')