PYTHON 学习笔记 day03 列表和元祖

4.2创建数值列表

4.2.1 使用函数range()

for value in range(1,5):

      print(value)

4.2.2使用函数range()创建数字列表

even_numbers = list(range(2,11,2))

print(even_numbers)

4.2.3 简单的统计

digits=[1,2,3,4,5,6,7,8,9]

print(min(digits))

print(max(digits))

print(sum(digits))

 

4.2.4 列表解析

squares=[value**2 for value in range(1,11)]

print(squares)

 

4.3 使用列表的一部分

4.3.1 切片 取得列表的一部分

players=['charles','martina','michael','florence','eli']

#取得第一位到第3位的元素的列表

print(players[0:3])

#取得第2位到第4位的元素列表

print(players[1:4])

#取得开始到第4位的元素的列表

print(players[:4])

#取得第3到最后一位的元素的列表

print(players[2:])

#取得最后3位的元素的列表

print(players[-3:])

 

4.3.2遍历切片

players=['charles','martina','michael','florence','eli']

print("Here are the first three players on my team:")

for player in players[:3]:

      print(player.title())

 

4.3.3 复制切片

my_foods=['pizza','falafel','carrot cake']

friend_foods = my_foods[:]

 

print("My favourite foods are:")

print(my_foods)

 

print("\nMy friend's favourite foods are:")

print(friend_foods)

 

4.4 元祖 存放不变的元素。

用圆括号定义

 

dimensions=(200,50)

print(dimensions[0])

print(dimensions[1])

4.4.2 遍历元祖

for dimension in dimensions:

      print(dimension)

4.4.3 元祖的重新定义 赋值

dimensions=(400,100)

print("\nModified dimensions:")

for dimension in dimensions:

      print(dimension)

4.5 PEP8格式规范

缩进 4个空格

行长 80个字符。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值