1 global变量 2 x=50 3 def func(): 4 global x 5 print(‘x is ‘,x) 6 x=2 7 print(‘changed global x to ‘,x) 8 func() 9 print(‘value of x is‘,x) 10 11 def say(message,times=1): 12 print(message*times) 13 say(‘hello‘) 14 say(‘world‘,5) 15 hello 16 worldworldworldworldworld 17 函数参数列表中的默认参数必须位于括号的末尾 18 19 关键字参数 20 dint(‘a is ‘,a,‘and b is‘,b,‘and c is ‘,c) 21 func(3,7) 22 func(25,c=24) 23 func(c=50,a=100) 24 25 def total(a=5,*numbers,**phonebook): 26 print(‘a‘,a) 27 for i in numbers: 28 print(‘i‘, i) 29 for first_part,second_part in phonebook.items(): 30 print(first_part, second_part) 31 print(total(10,1,2,3,jack=1123,john=2231,Inge=1560)) 32 33 每一个函数都在结尾默认地加上了一句,return none 34 35 模块类似导包 36 import math 37 print("Squr is",math.sqrt(16)) 38 39 class Person: 40 def __init__(self,name)://类似构造函数的功能 41 self.name=name 42 def say_hi(self): 43 print(‘hello‘,self.name) 44 p=Person(‘ghjghj‘) 45 p.say_hi()