Python Learning(4) 操作列表

本文详细介绍了Python编程中的列表操作,包括创建、修改、遍历、排序以及高级用法,适合Python初学者和进阶者提升技能。
摘要由CSDN通过智能技术生成

Python编程从入门到实践:

# -*- coding: utf-8 -*-

"""
chapter4 操作列表

"""

import math

#  4.1 遍历列表 for循环
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)

#  4.1.1 深入的研究循环
#  4.1.2 在for循环中执行更多的操作
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician.title() + ', that was a great trick!')

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print("i can't wait to see your next trick" + magician.title() + '.\n')

# 4.1.3 在for循环结束后执行一些操作
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician.title() + ', that was a great trick!')
    print("i can't wait to see your next trick" + magician.title() + '.\n')
print('thanks everyone,that was a great show')  # 不缩进 将在循环结束后执行

# 4.2 避免缩进错误
'''
一些常见的缩进错误:
1.忘记缩进-python使用缩进代码行与前一个代码行的关系;
2.忘记缩进额外的代码行-注意你的逻辑,你要对便利出来的每个元素执行操作,还是要在循环结束后,执行操作;
3.不必要的缩进
4.遗忘了for之后的冒号
'''

# 4.3 创建数值列表
# 4.3.1 使用函数range()生成一系列数字
for number in range(1, 5):  # 左闭右开 ,实际上会打印1,2,3,4 而不会打印出5
    print(number)
for number2 in range(5):  # 会打
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值