Python快速教程Vamei博客 笔记(未完)

来源:互联网 发布:卖家怎么加入淘宝联盟 编辑:程序博客网 时间:2024/05/16 00:57

最近看基础,找一个不错的博客

博客地址:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html

以下是我自己的一些记录和笔记


序列:
list 和 tuple 都是有序的,区别是list可变,tuple不可变
a=['a','b','c','d']print(a[1])print(a[:2])print(a[:-1])print(a[-1:])print(a[::-1])
关于[::-1]曾经面试吃亏过,问我逆向一个字符串,当时懵逼,自己写了一个函数实现,问我还有简单的方法吗,我说不知道
结果:
b
['a', 'b']
['a', 'b', 'c']
['d']
['d', 'c', 'b', 'a']


循环:
continue
break


函数:

a=1def xx(a):    a=a+1    return aprint(xx(a))print(a)

结果:
2
1
函数参数传递基础类型是值传递,内存中复制一份再传给函数

a=[1,2,3,4,5]def xx(a):    a[0]=a[0]+1    return aprint(xx(a))print(a)
结果:
[2, 2, 3, 4, 5]
[2, 2, 3, 4, 5]
函数参数是列表之类是引用传递

面向对象基础概念:
定义一个带属性的类
#定义一个类class Bird(object):    have_feather = True    way_of_reproduction = "egg"#实例化类summer = Bird()print(summer.way_of_reproduction)
结果;
egg


定义一个带函数的类

#定义一个类class Bird(object):    #类属性    have_feather = True    way_of_reproduction = "egg"    #类函数    def move(self, dx, dy):        position = [0,0]        position[0] = position[0] + dx        position[1] = position[1] + dy        return position#实例化类summer = Bird()print(summer.move(5,8))

结果:
[5, 8]


继承

#定义一个类class Bird(object):    #类属性    have_feather = True    way_of_reproduction = "egg"    #类函数    def move(self, dx, dy):        position = [0,0]        position[0] = position[0] + dx        position[1] = position[1] + dy        return position#类的继承class Chicken(Bird):    #定义自身类的属性    way_of_move = "walk"    possible_in_KFC = Trueclass Oriole(Bird):    way_in_move = "fly"    possible_in_KFC = False#实例化类summer = Chicken()#调用自身类的属性print(summer.way_of_move)#调用父类的函数print(summer.move(5,8))

结果:
walk
[5, 8]



python类可以单继承也可以多继承,这并不矛盾,小明即是人又是学生还是男人,很合理
有些java程序员经常用伦理说明继承问题,一个人只有一个爹吧,多继承是一个人有多个爹,这就不合适吧
单继承 和 多继承 到底哪个合理,反正我没有到这么高的境界来讨论这个问题

类内部函数调用类属性和类方法
#创建一个类class Human(object):    #类属性    laugh = "hahahaha"    #类函数    def show_laugh(self):        #类函数调用类属性        print(self.laugh)    def laugh_100th(self):        for i in range(5):            #调用类函数            self.show_laugh()#实例化li_lei = Human()li_lei.laugh_100th()
结果:
hahahaha
hahahaha
hahahaha
hahahaha
hahahaha

__init__()函数

#定义一个类class Bird(object):    #类属性    have_feather = True    way_of_reproduction = "egg"    #类函数    def move(self, dx, dy):        position = [0,0]        position[0] = position[0] + dx        position[1] = position[1] + dy        return position#类的继承class HappyBird(Bird):    #初始化函数,类似java构造函数    def __init__(self, more_word):        print(more_word)#实例化summer = HappyBird("happy,happy!")

结果:
happy,happy!


对象的性质

#创建类class Human(object):    #初始化    def __init__(self, input_gender):        self.gender = input_gender    def printGender(self):        print(self.gender)#实例化li_lei = Human("male")print(li_lei.gender)li_lei.printGender()

结果:
male
male


我对于对象性质的理解是对象实例化后才会产生的属性


#创建类class Human(object):    #初始化    def __init__(self, input_gender):        self.gender = input_gender    def printGender(self):        print(self.gender)#继承class Huuman(Human):    def __init__(self, input_gender):        self.gender = input_gender    #子类的函数与父类相同,子类实例化后调用的是子类自己的函数    def printGender(self):        print(self.gender+"! hahahah")#实例化li_lei = Huuman("male")li_lei.printGender()

结果:
male
male! hahahah









0 0
原创粉丝点击