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.
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:
tuple2 = 1,2,3,4,5,6
print(tuple2[3])
print(tuple2[0:4])
When we want to count some elements in a tuple:
print(tuple2.count(1))
We can also use a for loop to output each element in the tuple:
for idx in tuple2:
print(idx)