数据类型
字符串类
删除空白
rstrip
删除右边空格
lstrip
删除左边空格
strip
删除两边空格
1 2 3 4 5 6 7 8 9 10
| In [1]: str=" python "
In [2]: str.rstrip() Out[2]: ' python'
In [3]: str.lstrip() Out[3]: 'python '
In [4]: str.strip() Out[4]: 'python'
|
列表
列表的索引从0开始
元素添加和删除
1.在列表末尾添加元素
1 2 3 4 5 6
| In [5]: bicycles = ['trek', 'cannondale', 'redline', 'specialized'] In [6]: bicycles[1] Out[6]: 'cannondale' In [7]: bicycles.append("app") In [8]: bicycles Out[8]: ['trek', 'cannondale', 'redline', 'specialized', 'app']
|
2.在列表任意位置插入元素
1 2 3
| In [9]: bicycles.insert(0,"app") In [10]: bicycles Out[10]: ['app', 'trek', 'cannondale', 'redline', 'specialized', 'app']
|
3.从列表末尾或任意位置删除元素
pop()会返回弹出的元素
1 2 3 4 5 6 7 8 9
| In [11]: bicycles.pop() Out[11]: 'app' In [12]: bicycles Out[12]: ['app', 'trek', 'cannondale', 'redline', 'specialized']
In [14]: bicycles Out[14]: ['trek', 'cannondale', 'redline', 'specialized'] In [15]: bicycles.pop(0) Out[15]: 'trek'
|
4.从列表删除任意位置元素
1 2 3 4 5
| In [12]: bicycles Out[12]: ['app', 'trek', 'cannondale', 'redline', 'specialized'] In [13]: del bicycles[0] In [14]: bicycles Out[14]: ['trek', 'cannondale', 'redline', 'specialized']
|
5.删除指定值
remove()
1 2 3 4 5
| In [18]: bicycles Out[18]: ['cannondale', 'redline', 'specialized'] In [19]: bicycles.remove('redline') In [20]: bicycles Out[20]: ['cannondale', 'specialized']
|
元素排序
1.sort(*, key=None, reverse=False)
永久性排序
1 2 3 4
| In [26]: num=[1,3,2,4] In [27]: num.sort() In [28]: num Out[28]: [1, 2, 3, 4]
|
2.sorted(iterable, /, *, key=None, reverse=False)
暂时性排序
1 2 3 4
| In [1]: app=['A','C','B']
In [2]: sorted(app) Out[2]: ['A', 'B', 'C']
|
列表操作
列表解析
要使用这种语法,首先指定一个描述性的列表名;然后,指定一个左方括号, 并定义一个表达式,用于生成你要存储到列表中的值。
1 2 3 4
| In [3]: values=[_**2 for _ in range(1,11)]
In [4]: values Out[4]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| t1=values t2=values[:]
In [16]: t1 Out[16]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [17]: t2 Out[17]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [18]: values[2]=3
In [19]: t1 Out[19]: [1, 4, 3, 16, 25, 36, 49, 64, 81, 100]
In [20]: t2 Out[20]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
元组使用小括号,列表使用方括号
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可
用途:一般情况下用于自己写的程序能存下数据,但是又希望这些数据不会被改变,比如:数据库连接信息等
1 2 3 4 5 6 7 8 9 10 11
| >>> tup1 = (12, 34.56); >>> tup2 = ('abc', 'xyz')
>>> tup3 = tup1 + tup2; >>> print (tup3) (12, 34.56, 'abc', 'xyz')
|
虽然不能修改元组的元素,但是可以给存储元组的变量赋值
字典
删除键值对
del()
1 2 3 4 5 6 7
| In [25]: dic Out[25]: {'a': 1}
In [26]: del dic['a']
In [27]: dic Out[27]: {}
|
遍历字典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| In [36]: for name,value in dic.items(): ...: print(name) ...: a b c
In [37]: for name,value in dic.items(): ...: print(value) ...: 1 2 3
In [38]: for _ in dic.items(): ...: print(_) ...: ('a', 1) ('b', 2) ('c', 3)
|
函数
传递实参
位置实参
调用函数时,Python必须将函数调用中的每个实参都关联到函数定义中的一个形参。为此, 最简单的关联方式是基于实参的顺序。这种关联方式被称为位置实参。
1 2 3 4 5 6
| def describe_pet(animal_type, pet_name): """显示宠物的信息""" print("\nI have a " + animal_type + ".") print("My " + animal_type + "'s name is " + pet_name.title() + ".") describe_pet('hamster', 'harry')
|
位置实参的顺序对函数的运行很重要
关键字实参
关键字实参是传递给函数的名称—值对。你直接在实参中将名称和值关联起来了,因此向函数传递实参时不会混淆
1 2 3 4 5 6
| def describe_pet(animal_type, pet_name): """显示宠物的信息""" print("\nI have a " + animal_type + ".") print("My " + animal_type + "'s name is " + pet_name.title() + ".") describe_pet(animal_type='hamster', pet_name='harry')
|
Q:让实参变为可选
可给实参指定一个默认值——空字符串
传递任意数量的实参
1 2 3 4 5 6
| def make_pizza(*toppings): """打印顾客点的所有配料""" print(toppings) make_pizza('pepperoni') make_pizza('mushrooms', 'green peppers', 'extra cheese')
|
形参名*toppings中的星号让Python创建一个名为toppings的空元组,并将收到的所有值都封装到这个元组中。
面向对象OOP
根据约定,在Python中首字母大写的是类
使用isinstance
来测试一个对象是否为某个类的实例
方法__init__
初始化方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Dog(): '''A simple attempt to model a dog.''' def __init__(self,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!") my_dog=Dog('willie',6) my_dog.sit()
|
每个与类相关联的方法 调用都自动传递实参self
,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。
我们将通过实参向Dog()传递名字和 年龄;self会自动传递,因此我们不需要传递它。每当我们根据Dog类创建实例时,都只需给最 后两个形参(name和age)提供值。
继承
1 2 3 4 5 6 7 8 9 10 11
| class Car(): def __init__(self, make, model, year) class ElectricCar(Car): """Represent aspects of a car, specific to electric vehicles.""" def __init__(self, make, model, year): """ 电动汽车的独特之处 初始化父类的属性,再初始化电动汽车特有的属性 """ super(ElectricCar,self).__init__(make, model, year) self.battery_size = 70
|
重写父类方法
直接定义覆盖父类的函数即可,Python将忽略父类中的同名方法
todo :列表推导式