记录下python学习中,钻牛角尖的一些脑残点

一、类的初始化:

为什么用__init__(self)不直接写变量赋值
1.用__init__(self)可控制属性变量
2.__init__(self)自动执行代码,初始化类
class CocaCola:
formula = ['caffeine','sugar','water','soda']
def __init__(self):
for element in self.formula:
print('Coke has {}!'.format(element))
def drink(self):
print('Energy!')
coke = CocaCola()
二、类的超继承
class father:
def jiachan(self):
print(jin,yin,zhubao)
class son(father):
def jiachan(self):
super(son,self).jiachan()
三、list的删除
list=[1,2,3,4,5]
1.del按索引删除列表的元素,del.list[1]
2.pop按索引取出元素的值可以赋值。无索引默认删除最后一个,list.pop[1]
3.remove按值删除列表里的值,list.remove("1")
四、判断字符串是否全为数字
1.mobile_phone.isdigit()全数字返回Ture
五、index查找指定元素第一次在字符串中出现的索引位置,找不到报错
list.index(“1”)

猜你喜欢

转载自www.cnblogs.com/zhifeiji822/p/11798206.html