欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python study notes Day 4 tuple

程序员文章站 2022-05-27 22:08:12
...

Python study notes Day 4 tuple

Tuples are basically the same as lists, lists use brackets to contain elements, while tuples use parentheses or no parentheses.
But yesterday’s list blog about adding, deleting, modifying, and inserting lists does not apply to tuples.
Python study notes Day 4 tuple

Tuple1 = (1,2,3,4,5,6)
tuple2 = 1,2,3,4,5,6
print(Tuple1)
print(tuple2)

When we need to extract certain elements from a tuple according to certain requirements:
Python study notes Day 4 tuple

tuple2 = 1,2,3,4,5,6
print(tuple2[3])
print(tuple2[0:4])

When we want to count some elements in a tuple:
Python study notes Day 4 tuple

print(tuple2.count(1))

We can also use a for loop to output each element in the tuple:
Python study notes Day 4 tuple

for idx in tuple2:
    print(idx)