Python Sixth day Study summary

Source: Internet
Author: User

1. Recursive function: Call yourself
Count = 0
def func ():
Global Count
Count + = 1
Print (count)
Func ()

Func () #默认递归深度998, can be modified by Sys.setrecursionlimit (100000)
# #用递归解决年龄问题
D: Two years older than C 4 age (3) +2
C: Two years older than B 3 age (2) +2
B: Greater than a two years old 2 age (1) +2
A: This year 23 1 23
def age (N):
if n = = 1:
return 23
Else
Return Age (n-1) + 2

2. Two points Search
L1 = [2,3,5,10,15,16]
# #不能改变列表的大小, so you need to adjust the start position each time
def two_search (L,aim,start=0,end=none):
end = Len (l)-1 if End is None else end
If End >= start:
Mid_index = (End-start)//2 + start
If aim > L[mid_index]:
Return Two_search (L,aim,start=mid_index+1,end=end)
Elif Aim < L[mid_index]:
Return Two_search (l,aim,start=start,end=mid_index-1)
elif aim = = L[mid_index]:
Return Mid_index
Else
Return ' No this value '
Else
Return ' No this value '
Print (Two_search (l1,15))

3. Object-oriented
Class: A category of things with the same attributes and skills
Object: A class that is instantiated, which is the concrete embodiment of a class
Class Person: #class定义类的关键字 people class name, first letter capitalization
‘‘‘
The comment content of the class
‘‘‘
Animal = ' Advanced animals ' #静态变量
Soup = ' have thought '
def Init(self,name,sex,eye): #构造方法
Self.name = Name
Self.sex = Sex
Self.eye = Eye
Print (111)
def work (self): #动态变量, dynamic method, method
Print (' work ')

# #通过类查看静态变量, dynamic variables (typically not viewed by class, generally viewed and manipulated by objects)
#类操作静态变量
# # #类名. dict Method (can only view, can not be deleted and modified)
Print (person. Dict) #查看全部
Print (person. Dict[' Animal ']) #查看具体的
# # #类名. Variable names can be added and censored
Print (Person.animal) #查看具体的
Person.kind = ' have character ' #增加一个变量
Person.animal = ' Lower animal ' #修改原有变量
Del Person.kind #删除一个变量

#类操作方法
# # #类名. Dict [Method Name] ()
Print (person. Dict' work ') #操作work方法
# # #类名. Method name
Person.work (1)

Object: Class name () instantiates an object
P1 = person (' AA ', ' Male ', ') #只要实例化一个对象, auto Trigger __init___
#实例化一个对象, an object space is generated in memory.
#自动执行init方法 and pass this space object to self
#通过构造方法里的代码给空间对象添加一些属性, and return to the object

# #通过对象查看静态变量, dynamic variable
#对象操作属性变量
# # #对象. dict Method (can only view, can not be deleted and modified)
Print (P1. Dict) #查看p1的属性
# # #对象. Variable names can be added and censored
Print (P1.name) #查看name属性
P1.color = ' Yellow skin ' #增加一个属性
P1.name = ' CC ' #改变一个原有属性
Print (P1.animal) #访问类的静态变量

#对象操作方法
# # #对象. Method Name ()
P1.work () #操作work方法
# # #类名. Method Name (object)
Person.work (p1) #操作work方法

4. Class space and object space
Class Person:
Animal = ' Advanced animal ' # static variable
Soup = ' have thought ' # static variable
def Init(Self,name,age,sex,): # construction method
Self.age = Age
Self.name = Name # Property Self.name Property name
Self.sex = Sex

def work(self,job): # 动态变量,动态方法,方法    # self.job = job    print(‘人会工作....‘)

P1 = person (' AA ', 18, Male ') #实例化一个对象
Print (P1.name) #在对象空间查找
Print (P1.animal) #通过对象空间的指示牌从类空间查找

5. Combination
Encapsulates an object of another class to a property of a class object
Class Game_person: #定义一个类
def Init(SELF,NICKNAME,SEX,HP,AD):
Self.nickname = Nickname
Self.sex = Sex
SELF.HP = HP
Self.ad = AD

def attack(self,p):    p.hp -= self.ad    print(‘%s×××了%s,%s还剩%s血量‘ %(self.nickname,p.nickname,p.nickname,p.hp))def weapon_attack(self,wuqi):    self.wuqi = wuqi

Class Weapon: #定义一个类
def Init(SELF,NAME,AD):
Self.name = Name
Self.ad = AD

def fight(self,p1,p2):    p2.hp -= self.ad    print(‘%s使用%s打了%s%s血,%s还剩%s血‘ %(p1.nickname,self.name,p2.nickname,self.ad,p2.nickname,p2.hp))

ts = Game_person (' Tyson ', ' Male ', 200,50) #定义一个实例化对象
AA = Game_person (' AA ', ' Male ', 100,10) #定义一个实例化对象
Fuzi = Weapon (' Axe ', Max.) #定义一个实例化对象
Ts.attack (AA) #对象调用方法
Aa.weapon_attack (Fuzi) #aa调用weapon_attack方法将fuzi对象封装到aa的属性中
Aa.wuqi.fight (Aa,ts) #aa. Wuqi=fuzi aa.wuqi.fight (aa,ts) =fuzi. Fight (Aa,ts)

6. Inheritance
# #面向对象三大特点: Encapsulating inherited polymorphism
Class Animal: #父类 base class
Soup = ' soul '
def Init(self,varieties, Sex, color):
Self.varieties = Varieties
Self.sex = Sex
Self.color = Color

Class Cat (Animal):
Pass

Class Dog (Animal):
Pass

CAT1 = Cat (' Persian cat ', ' male ', ' orange ') #子类 derived class
Dog1=dog (' Teddy ', ' Male ', ' black ') #子类 derived classes

# #类分为两种: New class, Classic class. However, there are only new classes in python3x, classes that inherit the object class are called modern classes, and in python3x all classes inherit the object class by default. Classic class is default in PYTHON2X
New class: The breadth first to follow
Classic class: Depth first to follow
Inheritance: Single inheritance, multiple inheritance

# #个性化封装同时继承父类的属性
Class Animal:
Soup = ' soul '
def Init(self,varieties, Sex, color):
Self.varieties = Varieties
Self.sex = Sex
Self.color = Color
Def eat (self):
Print (' Eat ')

Class Bird (Animal):
def Init(self,varieties, Sex, color,fly):
# animal.init (self,varieties, sex, Color,) # The method of executing the parent class the first way (not commonly used)
# super (animal,self). Init (varieties, sex, color,) # The method of executing the parent class the second way
Super (). Init (varieties, sex, Color,) # methods for executing a parent class The second way to omit the notation (common)
Self.fly = Fly # Personalized Package
Def eat (self):
Super (). Eat ()
Print (' Bird eat ') # Personalized Package

B1 = Bird (' Parrot ', ' Male ', ' green ', 800)
B1.eat () #先执行父类的方法 and then execute your own method

# #单继承: a line
Class A:

def func(self):    print(‘A‘)

Class B (A):
Pass
def func (self):
Print (' B ')

Class C (B):
Pass
def func (self):
Print (' C ')

C1 = C ()
C1.func () #C--b--a

# #多继承: Diamond Inheritance (new Class) Find all classes with a minimum number of times
Class A:
def func (self):
Print (' A ')

Class B (A):
Pass
def func (self):
Print (' B ')

Class C (A):
Pass
def func (self):
Print (' C ')

Class D (B,C):
Pass

D1 = D ()
D1.func () #B--c--a
Print (D.mro ()) #打印查找顺序

# #多继承: Depth First (classic class, default to Classic class in python2x, can be modified by inheriting object)
Class A:
def func (self):
Print (' A ')

Class B (A):
Pass
def func (self):
Print (' B ')

Class C (A):
Pass
def func (self):
Print (' C ')

Class D (B,C):
Pass

D1 = D ()
D1.func () #B--a--c

Python Sixth day Study summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.