本篇文章是 A Byte of Python 学习笔记
A Byte of Python 电子书网址:https://python.swaroopch.com
数据结构
Data Structures
There are four buit-in data structures in Python - list, tuple, dictionary and set.
列表 List(mutable易变的、可以改变的:增加、删除、查找)
表示方法:[list1, list2, list3…]
help(list):查看list的全部函数和变量
常用操作list 的函数:
append,增加,shoppinglist.append(‘rice’);
del,删除,del shoppinglist[0];
shoppinglist.sort(),排序列表;
len(shoppinglist),查找列表的元素个数;
元组 Tuple(immutable不易变的)
Note that a tuple within a tuple does not lose its identity.
表示方法:(tuple1, tuple2, tuple3…)
单元素的 tuple=('apples', )
字典 Dictionary
key不可更改,value 可以更改
表示方法:d=
key-value pairs in a dictionary are not order in any manner.
序列 Sequence
List, tuple and strings are examples of sequence. The major feathers are membership test and and indexing operators.
切片操作 slicing operation (两个参数或者三个参数)
集合 Set
NOTE:When you create an object and assign it to a variable, the variable only refers to the object and does not represent the object itself ! That is, the variable name points to that part of your computer’s memory where the object is stored. This is called binding the name to the object.
An assignment statement for list does not create a copy.You have to use slicing operation to make a copy to the sequence.
help(str):查看字符串的全部变量和方法
面向对象编程
Object Oriented Programing
>Class and objects are the two main aspects of object oriented programing.
>The fields and methods can be referred to as the attributes of that class.
>The fields are of two types: instance variables and class variables.
创建类、创建类的实例
创建类的方法:切记后面的"()"
如:
The __init__ method
类和对象变量 Class and Object Vaviables
object variables: 被类的实例拥有
class variables: 共享性,可以被类的所有实例使用
继承
One of the major benefits of object oriented programming is reuse of code and one of the ways this is achieved is through the inheritance mechanism. Inheritance can be best imagined as implementing a type and subtype relationship between classes.
多态性
类和对象变量的实例(代码可左右滑动):
注意:
population 是类变量,是公用的,因此在使用时"Robot.poplation"
name 是对象变量,因此在使用时"self.name"
"@classmethod"是类方法的修饰符,方法是类的方法因此在使用时 "Robot.poplation"
继承的实例(代码可左右滑动):
注意:
子类Teacher,Student的方法均继承父类,因此在使用时"class Teacher(SchoolMember):"和"class Student(SchoolMember):"
子类的方法在使用时"SchoolMember.tell(self)"
类的对象:t, s 组成列表,用 for 循环实现
当没有使用类的方法时,对象仅调用类的初始化
What I discovered after this journey of learning is that it’s not the languages that matter but what you do with them. Actually, I always knew that, but I’d get distracted by the languages and forget it periodically. Now I never forget it, and neither should you.
Which programming language you learn and use doesn’t matter. Do not get sucked into the religion surrounding programming languages as that will only blind you to their true purpose of being your tool for doing interesting things.
---fromLearn Python 3 The Hard Way, Author,Zed A. Shaw
领取专属 10元无门槛券
私享最新 技术干货