首页 > 编程语言 >python编程从入门到实践--第4章 操作列表

python编程从入门到实践--第4章 操作列表

时间:2022-10-10 16:34:02浏览次数:43  
标签:dimensions python 编程 列表 -- foods print my

一。遍历整个列表

  注意缩进,与不要遗漏冒号。

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    #print(magician)
    print(f"{magician.title()}, that waw a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")

print("Thank you, everyone. That waw a great magic show!")
print("This is test indentationError: unexpected indent")

二。创建数值列表

# 创建数值列表--实际最大输出4
for value in range(1, 5):
    print(value)

# 创建数字列表--实际最大输出5
numbers = list(range(1, 6))
print(numbers)

# 创建偶数
even_numbers = list(range(2, 11, 2))
print(even_numbers)

# 创建平方值--1~10的平方值
squares = []
for value in range(1, 11):
     squares.append(value**2)

print(squares)

# 统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))

#列表解析
squares1 = [value**2 for value in range(1, 11)]
print(squares1)

三。使用列表的一部分

  切片与遍历切片

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])     # 起始0,终止3前元素--['charles', 'martina', 'michael'] 
print(players[1:4])     # 起始1,终止4前元素--['martina', 'michael', 'florence']
print(players[:4])      # 起始0,终止4前元素--['charles', 'martina', 'michael', 'florence']
print(players[2:])      # 起始2到最末尾元素--['michael', 'florence', 'eli']
print(players[-3:])     # 起始倒数第三个至最末尾一个元素--['michael', 'florence', 'eli']

# 遍历切片
print("Here are the first three plyers on my team:")
for player in players[:3]:
    print(player.title())

四。复制列表

  深拷贝与浅拷贝

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

#friend_foods = my_foods[:]      # 深拷贝--两个不同列表
friend_foods = my_foods          # 浅拷贝--相同一个列表,同一个引用


print("My favorite foods are:")
print(my_foods)

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

my_foods.append('cannoli')
friend_foods.append('ice cream')

print("My favorite foods are:")
print(my_foods)

print("\nMy friends favoirte foods are:")
print(friend_foods)

五。元组

相当于不可变数组,只在构造时可以修改值,后的不能修改。

dimensions = (200, 50)  # 只有二个元素
print(dimensions[0])
print(dimensions[1])

my_t = (3,)     # 单个元素,也要加逗号
print(my_t[0])

#dimensions[0] = 300    # 运时报错,'tuple' object does not support item assignment

# 遍历元组中所有元素
for dimension in dimensions:
    print(dimension)


# 修改元组变量--构造时
dimensions = (400, 100, 50)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

 

标签:dimensions,python,编程,列表,--,foods,print,my
From: https://www.cnblogs.com/duju/p/16776150.html

相关文章

  • 超声波探伤器上位机设计
    1.主菜单栏:文件&视图&工具2.文件/保存&另存为&打开&退出;视图/矩形扇形转换&扇形矩形转换&插值并重新成像&图像播放&暂停播放&匹配滤波;工具/放大&缩小&选中区域放大&恢复原图......
  • 智慧树(知到)习惯分问答生成器结合ai改写 需要自己的cookie,我找了半天,也没找到完全免费
    仅作用于问答详情界面,未设置自动发布 //==UserScript==//@nameWhee1-智慧树(知到)习惯分问答生成器结合ai改写//@namespacehttp://tampermonkey.net/......
  • FPGA串口模块
     然后串口的时钟实时50MHz,波特率为115200.  下载成像后,先用 进行串口检测,(上位机有的时候可能会不兼容电脑,先用串口工具检测串口是否在正常工作。)    如上所示,接收计......
  • Java 我的第一个hello word
         项目—右键—选中添加框架的支持 选中“WebApplication”,然后确定  右键src,新建一个类          ......
  • MySQL练习题2
    6,取得平均工资最高的部门的部门名称。selectd.dname,avg(e.sal)asavgsalfromempejoindeptdone.deptno=d.deptnogroupbyd.dnameorderbyavgsaldescli......
  • JQuery中常用的选择器
    jQuery常用选择器基础选择器基本选择器语法功能ID选择器$('#ID')找到匹配指定ID的元素元素(标签)选择器$('element')找到指定的元素class选择器$('.cla......
  • emd分解
    %此版本为ALAN版本的整合注释版functionimf=emd(x)%EmpiricialModeDecomposition(Hilbert-HuangTransform)%imf=emd(x)%Func:findpeaksx=transpose(x(......
  • fpga的就业工资大约是多少,就业前景怎样?
     随便找了一个招聘网站,搜索“FPGA工程师”得到如下结果,可以看到,三年的工作经验大部分月薪都有20k左右,也有更高的30k、40k的;身边做FPGA的朋友也都是在这个范围内,所以FPGA......
  • 10.10
    #include<stdio.h>intmain(){inta,b,c;scanf("%d%d%d",&a,&b,&c);printf("%d\n",a+b+c);inta,b; scanf("%d,%d",&a,&b); printf("%d\n",a+b);inta,b,c,d;scanf("%d......
  • matlab中的三维可视化实现
    图形三维立体可视化在Matlab中的实现和处理三维图形的绘制除了常用的网格图、表面图和等高线等方法外,Matlab还提供了一些立体可视化函数用于绘制更为复杂的立体和向量对象。......