自定义一个函数,返回一个序列。序列中每个数字都是前两个数字之和(斐波那契数列). def fibs(num): 廉位置参数 xesu1t=[0,1 年新建列表存储数列的值 for i in range(2,num): #循环num-2次 a result[i-1]+result[i-2] result.append(a) 量将值追加至列表 return result ·返回列表 fibs(5) 输出结果 [0,1,1,2,3] 步骤2参数 自定义函数,打印不同方式传入的参数。 def hello(greeting='hello',name='world'): 默认参数 print('s,&s!(greeting,name)) 格式化输出 hello() #he11o,wor1d默认参数 hello('Greetings') Greetings,world位置参数 hello('Greetings','universe') #Greetings,universe 位置参数 he11o(name='Gumby') he1lo,Gumby关键字参数 输出结果: hello,world! Greetings,world! Greetings,universe hello,Gumby! 1.2.12面向对象 步骤1创健和使用类 创建Dog类. 根据D0g类创建的每个实例都将存储名字和年龄。我们将赋予了每条小狗蹲下(st0)和打 滚(roll_over(O)的能力 class Dog(): """一次模拟小狗的简单尝试"" def (self,ne ne,age) 初始化属性nae和age self.namename self.age age def sit(self): ■"“模拟小狗被命令时蹲下m
自定义一个函数,返回一个序列。序列中每个数字都是前两个数字之和(斐波那契数列)。 def fibs(num): # 位置参数 result = [0,1] # 新建列表存储数列的值 for i in range(2,num): # 循环 num-2 次 a = result[i-1] + result[i-2] result.append(a) # 将值追加至列表 return result # 返回列表 fibs(5) 输出结果: [0, 1, 1, 2, 3] 步骤 2 参数 自定义函数,打印不同方式传入的参数。 def hello(greeting='hello',name='world'): # 默认参数 print('%s, %s!' % (greeting, name)) # 格式化输出 hello() # hello,world 默认参数 hello('Greetings') # Greetings,world 位置参数 hello('Greetings','universe') # Greetings,universe 位置参数 hello(name='Gumby') # hello,Gumby 关键字参数 输出结果: hello, world! Greetings, world! Greetings, universe! hello, Gumby! 1.2.12 面向对象 步骤 1 创建和使用类 创建 Dog 类。 根据 Dog 类创建的每个实例都将存储名字和年龄。我们将赋予了每条小狗蹲下(sit())和打 滚(roll_over())的能力: class Dog(): """一次模拟小狗的简单尝试""" def __init__ (self,name,age): """初始化属性 name 和 age""" self.name = name self.age = age def sit(self): """模拟小狗被命令时蹲下
print(self.name.title()+"is now sitting") def roll_over(self): ·"“模拟小狗被命令时打滚”“ print (self.name.title()+"rolled over!") 实例化类 dog=Dog("哈士奇",2) dog.sit() oll_over() 出结果 哈士奇is now sitting 哈士奇rolled over 步骤2访问属性 class Employ ·所有员工的基类 empCount -0 definit(self,name,salary): self.name name self.salary =salary Employee.empcount +=1 def displaycount(self): def print("Total Employee sd"8 Employee.empCount displayEmployee(self) print ("N se name,"Salary:"self.salary) #创建Employee类的第一个对象 empl =Employee("Zara",2000) 量创建Employee类的第二个对家” ) print ("Total Emplovee sd"s Emplovee.empcount) 输出结果: Zara,Salary 2000 1ary:5000 步骤3类的继承 面向对橡的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制。 继承完全可以理解成类之间的类型和子类型关系。 class Parent: 定义父类 parentAttr =100 def init (self):
print(self.name.title()+"is now sitting") def roll_over(self): """模拟小狗被命令时打滚""" print(self.name.title()+"rolled over!") 实例化类 dog = Dog("哈士奇",2) dog.sit() dog.roll_over() 输出结果:: 哈士奇 is now sitting 哈士奇 rolled over! 步骤 2 访问属性 class Employee: '所有员工的基类' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print("Total Employee %d" % Employee.empCount ) def displayEmployee(self): print("Name : ", self.name, ", Salary: ", self.salary) # 创建 Employee 类的第一个对象" emp1 = Employee("Zara", 2000) # 创建 Employee 类的第二个对象" emp2 = Employee("Manni", 5000) emp1.displayEmployee() emp2.displayEmployee() print("Total Employee %d" % Employee.empCount) 输出结果: Name : Zara ,Salary: 2000 Name : Manni ,Salary: 5000 Total Employee 2 步骤 3 类的继承 面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制。 继承完全可以理解成类之间的类型和子类型关系。 class Parent: # 定义父类 parentAttr = 100 def __init__(self):
pr1nt("调用父类构造函数") def parentMethod(self): print("河用父类方法) def attr): .pa ntAttratt print("父类属性:",Parent,parentAttr) class Child(Parent):定义子类 def init (self) print(调用子类构洁方法" def childMethod(self): print("调用子类方法 e-Child() 实例化子类 c.childMethod() #调用子类的方法 c.parentMethod() #调用父类方法 c.setAttr(200) 量再次调用父类的方法一设置属性值 c.getAttr() 幸再次调用父类的方法~获取属性值 输出结果: 调用子类构造方法 调用子类方法 调用父类方法 父类属性:200 步骤4类属性和方法 class Justcounter: secretcount=0#私有变量 publicCount=0 #公开变量 def count(self): self.__secretcount +=1 self.publiccount +=1 print (self.secretCount) counter =JustCounter() counter.count counter.count print(counter.publicCount) print(counter,_secretCount)报错,实例怀能访问私有变量 输出结果: 1
print("调用父类构造函数") def parentMethod(self): print('调用父类方法') def setAttr(self, attr): Parent.parentAttr = attr def getAttr(self): print("父类属性 :", Parent.parentAttr) class Child(Parent): # 定义子类 def __init__(self): print("调用子类构造方法") def childMethod(self): print('调用子类方法') c = Child() # 实例化子类 c.childMethod() # 调用子类的方法 c.parentMethod() # 调用父类方法 c.setAttr(200) # 再次调用父类的方法 - 设置属性值 c.getAttr() # 再次调用父类的方法 - 获取属性值 输出结果: 调用子类构造方法 调用子类方法 调用父类方法 父类属性 : 200 步骤 4 类属性和方法 class JustCounter: __secretCount = 0 # 私有变量 publicCount = 0 # 公开变量 def count(self): self.__secretCount += 1 self.publicCount += 1 print(self.__secretCount) counter = JustCounter() counter.count() counter.count() print(counter.publicCount) print(counter.__secretCount) # 报错,实例不能访问私有变量 输出结果: 1 2 2
AttributeError Traceback (most recent call last) kipython-input-5-5dff96b3b703>in <module> 10 counter.count() 11 print(counter.publicCount) ->12 print(counter,.一secretCount)#报份,实例不能访利私有变量 AttributeError:'JustCounter'object has no attributesecretCount' 图1-1报错信息 1.2.13标准库的使用 步骤15ys sys.exit(:此方法可以是当前程序退出,n为0时表示正常退出,其他值表示异常退出. import sys for nge(100): print (i) 1E1==5: gys.e×1t(0) 输出结果: 0 An exception has occurred,use stb to see the full traceback. Sys.path:获取模块搜索路径。 ays.path 输出结果 ['D:\\python3.6\\python36.zip', 'D:\\python3.6\\DLLs' 'D:\\python3.6\\lib' D:\\python3.6', 'D:\\python3.6\\lib\\site-packages', 'D:\\python3.6\\lib\\site-packages\\IPython\\extensions' 'C:\\Users\\xxx\\.ipython'] sys.platform:获取当前系统平台 sys.platform
图 1-1 报错信息 1.2.13 标准库的使用 步骤 1 sys sys.exit([n]):此方法可以是当前程序退出,n 为 0 时表示正常退出,其他值表示异常退出。 import sys for i in range(100): print(i) if i ==5: sys.exit(0) 输出结果: 0 1 2 3 4 5 An exception has occurred, use %tb to see the full traceback. sys.path:获取模块搜索路径。 sys.path 输出结果: ['D:\\python3.6\\python36.zip', 'D:\\python3.6\\DLLs', 'D:\\python3.6\\lib', 'D:\\python3.6', '', 'D:\\python3.6\\lib\\site-packages', 'D:\\python3.6\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\xxx\\.ipython'] sys.platform:获取当前系统平台。 sys.platform