做影视网站版权问题,石家庄核酸检测,媒介星软文平台官网,登陆国外网站速度慢Python List
# python 列表可以加入所有类型 如列表#xff0c;字典#xff0c;数字#xff0c;字符串等bicycles [trek, cannondale, redline, specialized]
print(bicycles)# 访问列表元素#xff0c;使用索引
print(bicycles[0])# 访问最后一个元素下标-1. 以此类推
p…Python List
# python 列表可以加入所有类型 如列表字典数字字符串等bicycles [trek, cannondale, redline, specialized]
print(bicycles)# 访问列表元素使用索引
print(bicycles[0])# 访问最后一个元素下标-1. 以此类推
print(bicycles[-1])# 修改、添加和删除元素
bicycles[1] Hello
print(bicycles)bicycles.append(Hallo) # 末尾添加
bicycles.insert(0, world) # 指定位置添加del bicycles[2] # 删除指定元素
value bicycles.pop() # 删除末尾元素
value bicycles.pop(2) # 删除指定位置元素
bicycles.remove(world)# 列表永久性排序
bicycles.sort()# 暂时排序
sortList sorted(bicycles)# 反转列表
bicycles.reverse()
# 列表长度
len(bicycles)# 操作列表# 1. 遍历操作 formagicians [alice, david, carolina]
for magician in magicians:print(magician)# 数值列表 range
for value in range(1, 5): #[1, 5)print(value)# 转化为数字列表list(range(1, 6))# 列表切片 list[start:end] 得到[start, end)的元素 切片得到新的listplayers [charles, martina, michael, florence, eli]
print(players[0:3]) # players[0] players[1] players[2]
print(players[:3]) # from start
print(players[1:]) # to end.copyList players[:] # 复制整个列表Python dict
# 相关信息关联起来的Python字典 {key: value, key: value} key value can be any.
# 字典用放在花括号{}中的一系列键—值对表示
alien_0 {color: green, point: 5}
print(alien_0[color])# 添加键值对
alien_0[x_position] 1
alien_0[y_position] 2
print(alien_0)# 修改值
alien_0[color] yellow
print(alien_0)# 删除键值对
del alien_0[color]
print(alien_0)# 遍历字典for key, value in alien_0.items():print(key: key)print(value: str(value))for key in alien_0.keys():print(key: key)for value in alien_0.values():print(value: str(value))python class
# Python class# 类定义中的括号是空的因为我们要从空白创建这个类 括号里面写的是基类
class Dog(object):def __init__(self, name, age):# 构造方法中直接定义类属性# __init__()方法来创建Dog实例时将自动传入实参self。每个与类相关联的方法# 调用都自动传递实参self它是一个指向实例本身的引用让实例能够访问类中的属性和方法。# self为前缀的变量都可供类中的所有方法使用我们# 还可以通过类的任何实例来访问这些变量。self.name name获取存储在形参name中的值并将# 其存储到变量name中然后该变量被关联到当前创建的实例self.name nameself.age ageself.height 0 # 指定初始值def sit(self):print(self.name.title() is now sitting)def roll_over(self):print(self.name.title() rolled over!)class Battery(object):def __init__(self):self.battery 50class Car(object):def __init__(self, make, model, year):self.make makeself.model modelself.year yearself.odometer_reading 0 # 默认初始值def update_odometer(self, meter):self.odometer_reading meterdef get_descriptive_name(self):long_name str(self.year) self.make self.modelreturn long_name.title()class ElectricCar(Car):def __init__(self):super().__init__(2023, TS12, 12) # 调用父类构造self.battery Battery()def update_odometer(self, meter):print(override.)my_new_car Car(audi, a4, 2016)
print(my_new_car.get_descriptive_name())my_new_car.odometer_reading 23
my_new_car.update_odometer(43)