0
点赞
收藏
分享

微信扫一扫

Python元组,妈妈再也不会担心我不会Python了(九)

霸姨 2022-01-31 阅读 54


###元组


  • 元组和列表相似,不同点元组定义后不能改变,列表可以做改变。
  • 元组用小括号,列表用中括号。

#tuple
#元组定义
students = ("张三","李四","王五")

###列表常用操作

  • 打印元素
#tuple
#元组定义
students = ("张三","李四","王五")

print(students[0])
print(students[1])
print(students[2])

上述代码运行结果:

Python元组,妈妈再也不会担心我不会Python了(九)_元组

  • 遍历元素
#tuple
#元组定义
students = ("张三","李四","王五")

for i in students:
print(i)
'''
用while循环也可以
i = 0
while i < len(students):
print(students[i])
i+=1
'''

上述代码运行结果:

Python元组,妈妈再也不会担心我不会Python了(九)_while循环_02

  • 统计
#tuple
#元组定义
students = ("张三","李四","王五","李四")
print("叫李四的人有%d个"%students.count("李四"))

上述代码运行结果:

Python元组,妈妈再也不会担心我不会Python了(九)_python_03

  • 查索引
#tuple
#元组定义
students = ("张三","李四","王五","李四")
print("王五的索引是%d"%students.index("王五"))

上述代码运行结果:

Python元组,妈妈再也不会担心我不会Python了(九)_while循环_04

Python元组,妈妈再也不会担心我不会Python了(九)_python_05



举报

相关推荐

0 条评论