Last active
April 1, 2024 10:13
-
-
Save ShyamaSankar/557992a80aa87895d7b02f2b2d71c763 to your computer and use it in GitHub Desktop.
Cheat sheet for Python tuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Tuple creation ##### | |
# Create a tuple, also called tuple packing. | |
numbers = 1, 2 | |
print(numbers) # Output: (1, 2) <- Note that it is represented with an enclosing paranthesis | |
# Create tuple with paranthesis. | |
numbers = (1, 2, 3) | |
print(numbers) # Output: (1, 2, 3) | |
# Create an empty tuple. | |
numbers = () | |
print(numbers) # Output: () | |
# Create a tuple with one item. Note that the trailing comma is necessary | |
numbers = 1, | |
print(numbers) # Output: (1,) | |
# Create a tuple with heterogenous items. | |
random_tuple = "Hey", (1, 2), 1, ["you"] | |
print(random_tuple) # Output: ("Hey", (1, 2), 1, ["you"]) | |
# Create tuple with tuple() constructor. | |
numbers = tuple() | |
print(numbers) # Output: () | |
numbers = tuple([1, 2]) # Takes any sequence as input | |
print(numbers) # Output: (1, 2) | |
#### Methods on tuples ##### | |
# Get length of list by using len() method. | |
numbers = 5, 8, 8 | |
print(len(numbers)) # Output: 3 | |
# Get index of an element using the index() method. | |
numbers = 5, 8, 8 | |
print(numbers.index(8)) # Output: 1 | |
# Count occurences of an item in a tuple. | |
numbers = 5, 8, 8 | |
print(numbers.count(8)) # Output: 2 | |
# Access elements of a tuple by indexing. | |
str_tuple = "hey", "there!", "how", "are", "you?" | |
print(str_tuple[0]) # Output: "hey" | |
print(str_tuple[len(str_tuple) - 1]) # Output: "you?" | |
print(str_tuple[-1]) # Output: "you?" | |
# Slicing a tuple. | |
str_tuple = "hey", "there!", "how", "are", "you?" | |
print(str_tuple[2:]) # Output: ("how", "are", "you?") | |
print(str_tuple[:2]) # Output: ("hey", "there!") | |
print(str_tuple[-3:]) # Output: ("how", "are", "you?") | |
print(str_tuple[:-3]) # Output: ("hey", "there!") | |
print(str_tuple[1:4]) # Output: ("there!", "how", "are") | |
# Get a copy of the tuple by slicing. | |
print(str_tuple[:]) # Output: ("hey", "there!", "how", "are", "you?") | |
# Concatenate tuples. | |
numbers = (1, 2) | |
strings = ("Hey", "there") | |
print(numbers + strings) # Output: (1, 2, "Hey", "there") | |
# Tuples are immutable, that is, we cannot change its contents. | |
numbers = (1, 2, 3) | |
numbers[0] = 100 # Gives error: TypeError: 'tuple' object does not support item assignment | |
del numbers[0] # Gives error: TypeError: 'tuple' object doesn't support item deletion | |
# Looping through tuple using 'in'. | |
numbers = 1, 2 | |
for number in numbers: | |
print(number) | |
# Check if element is present in tuple. | |
numbers = 1, 2 | |
print(1 in numbers) # Output: True | |
print(5 in numbers) # Output: False | |
# Delete tuple using del keyword. | |
numbers = 1, 2 | |
del numbers | |
# Tuple packing. | |
# We are packing two items 1 and 2 into the tuple. | |
numbers = 1, 2 | |
# Tuple sequence unpacking. | |
# Number of variables used has to be same as the number of items in the tuple. | |
# Unpacking the tuple and assigning its items to x and y. | |
x, y = numbers | |
# Note that this is also packing the args as a tuple which gets unpacked as the print method's arguments. | |
print(x, y) # Output # (1, 2) |
This is very helpful...many thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice Wor6