Tuple
Tuple is like list, but we can;t modify it, they are immutable. Tuple immutable 直,就是他的直不無法修改的,有些語法是用const類似這概念。
Create Tuple 建立tuple
1 | # Empty tuple |
created without using parentheses
1 | my_tuple = 3, 4.6, "dog" |
Access Tuple Elements
1 | # Accessing tuple elements using indexing |
# Negative indexing for accessing tuple elements
1 | my_tuple = ('p', 'e', 'r', 'm', 'i', 't') |
Slicing
1 | # Accessing tuple elements using slicing |
change tuple
1 | # Changing tuple values |
delete tuple
1 | # Deleting tuples |
Method
Tuple only have two method:
Method | Description |
---|---|
count() | Return the number of times a specifies value occurs in tuple |
index() | search the tuple for a specfied value ad returns the position of where it’s found |
1 | my_tuple = ('a', 'p', 'p', 'l', 'e',) |
Iterating Through a Tuple
1 | # Using a for loop to iterate through a tuple |