《Python编程从入门到实践》3

本文介绍了Python中列表的基本操作,包括创建、访问、修改、添加、删除列表元素的方法,以及如何使用sort()、sorted()和reverse()等函数来组织列表。同时,文章还展示了如何避免常见的索引错误。
部署运行你感兴趣的模型镜像

1. f字符串的使用方法。
2. 创建列表:列表名[‘’,‘’,‘’,…]
3. 访问列表元素:列表名[索引值]
4. 列表的索引从0开始,倒数时从-1开始。
5. 使用列表中的各个值:列表名[索引值]
6. 修改、添加、和删除元素:

修改添加删除
列表名[索引] = ‘新元素’末尾添加 :数组名.append(新元素’),这里要注意:append()方法不能直接在print语句中打印出来,要先添加元素,再打印被修改后的列表del语句:del 数组名[索引]
插入:insert(索引值,‘新元素’)弹出方法pop():数组名.pop(括号内添加索引,默认为数组中最后一个数据)
根据值删除,只删除第一个指定的数值,如果后面还有,需要通过循环判断:方法remove(‘元素’)

7. 组织列表:

永久性按字母顺序排序临时性排序倒着打印列表确定列表长度
方法sort()函数sorted(列表名)方法reverse(),reverse()不是按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序函数len(列表名)
#3.1————列表是是什么
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
#3.1.1————访问列表元素
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())
print(bicycles[1].title())
print(bicycles[2].title())
print(bicycles[3].title())
#print(bicycles[4])
#3.1.2————索引从0开始而不是1开始
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
print(bicycles[-2])
print(bicycles[-3])
print(bicycles[-4])
#3.1.3————使用列表中的各个值
first_name = "ada"
last_name = "lovelace"
#f字符串:f"引号内为字符串和{变量名},无先后顺序,无运算符号"
full_name = f"{first_name}{last_name}"
print(full_name)
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicyle was a {bicycles[0].title()}."
print(message)
#3.2.1————修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'coolboy'
print(motorcycles)
#3.2.2————在列表中添加元素
#1.在列表末尾添加元素-append
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
motorcycles = []
print(motorcycles)
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
#2.在列表末尾添加元素-insert
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.insert(100, 'ducati')
print(motorcycles)
#3.2.3————在列表中删除元素
#1.使用del语句删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[2]
print(motorcycles)
#2.使用方法pop(索引,默认为0)删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
popped_motorcycle = motorcycles.pop(0)
print(f"The last motorcyle I owned was a {popped_motorcycle}.")
#3.根据值删除元素
motorcycles = ['ducati', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
#3.3————组织列表
#3.3.1.使用方法sort()对列表永久排序
#按字母排序
cars = ['BYD', 'BMW', 'AUDI', 'TOYOTA']
print(cars)
cars.sort()
print(cars)
#按字母顺序相反的顺序排序
cars.sort(reverse=True)
print(cars)
#3.3.2使用函数sorted()对列表临时排序
cars = ['BYD', 'BMW', 'AUDI', 'TOYOTA']
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print(cars)
#3.3.3倒着打印列表——reverse()永久修改
#reverse()不是按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序
cars = ['SUBARU', 'BMW', 'AUDI', 'TOYOTA']
print(cars)
cars.reverse()
print(cars)
print(cars)
cars.reverse()
print(cars)
#3.3.4确定列表的长度——len()
cars = ['SUBARU', 'BMW', 'AUDI', 'TOYOTA']
print(len(cars))
#3.4————使用列表时避免索引错误
motorcycles = ['ducati', 'yamaha', 'suzuki']
print(len(motorcycles))
print(motorcycles[2])

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值