Cheat Sheet For Python Tuples GitHub

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Search… All gists Back to GitHub Sign in Sign up

Instantly share code, notes, and snippets.

ShyamaSankar / python_tuples.py Star 1 Fork 4

Last active 22 days ago

Code Revisions 2 Stars 1 Forks 4 Download ZIP

Cheat sheet for Python tuples

python_tuples.py Raw

1 #### Tuple creation #####


2 # Create a tuple, also called tuple packing.
3 numbers = 1, 2
4 print(numbers) # Output: (1, 2) <- Note that it is represented with an enclosing paranthesis
5 # Create tuple with paranthesis.
6 numbers = (1, 2, 3)
7 print(numbers) # Output: (1, 2, 3)
8 # Create an empty tuple.
9 numbers = ()
10 print(numbers) # Output: ()
11 # Create a tuple with one item. Note that the trailing comma is necessary
12 numbers = 1,
13 print(numbers) # Output: (1,)
14 # Create a tuple with heterogenous items.
15 random_tuple = "Hey", (1, 2), 1, ["you"]
16 print(random_tuple) # Output: ("Hey", (1, 2), 1, ["you"])
17 # Create tuple with tuple() constructor.
18 numbers = tuple()
19 print(numbers) # Output: ()
20 numbers = tuple([1, 2]) # Takes any sequence as input
21 print(numbers) # Output: (1, 2)
22
23 #### Methods on tuples #####
24 # Get length of list by using len() method.
25 numbers = 5, 8, 8
26 print(len(numbers)) # Output: 3
27 # Get index of an element using the index() method.
28 numbers = 5, 8, 8
29 print(numbers.index(8)) # Output: 1
30 # Count occurences of an item in a tuple.
31 numbers = 5, 8, 8
32 print(numbers.count(8)) # Output: 2
33
34 # Access elements of a tuple by indexing.
35 str_tuple = "hey", "there!", "how", "are", "you?"
36 print(str_tuple[0]) # Output: "hey"
37 print(str_tuple[len(str_tuple) - 1]) # Output: "you?"
38 print(str_tuple[-1]) # Output: "you?"
39
40 # Slicing a tuple.
41 str_tuple = "hey", "there!", "how", "are", "you?"
42 print(str_tuple[2:]) # Output: ("how", "are", "you?")
43 print(str_tuple[:2]) # Output: ("hey", "there!")
44 print(str_tuple[-3:]) # Output: ("how", "are", "you?")
45 print(str_tuple[:-3]) # Output: ("hey", "there!")
46 print(str_tuple[1:4]) # Output: ("there!", "how", "are")
47 # Get a copy of the tuple by slicing.
48 print(str_tuple[:]) # Output: ("hey", "there!", "how", "are", "you?")
49
50 # Concatenate tuples.
51 numbers = (1, 2)
52 strings = ("Hey", "there")
53 print(numbers + strings) # Output: (1, 2, "Hey", "there")
54
55 # Tuples are immutable, that is, we cannot change its contents.
56 numbers = (1, 2, 3)
57 numbers[0] = 100 # Gives error: TypeError: 'tuple' object does not support item assignment
58 del numbers[0] # Gives error: TypeError: 'tuple' object doesn't support item deletion
59
60 # Looping through tuple using 'in'.
61 numbers = 1, 2
62 for number in numbers:
63 print(number)
64
65 # Check if element is present in tuple.
66 numbers = 1, 2
67 print(1 in numbers) # Output: True
68 print(5 in numbers) # Output: False
69
70 # Delete tuple using del keyword.
71 numbers = 1, 2
72 del numbers
73
74 # Tuple packing.
75 # We are packing two items 1 and 2 into the tuple.
76 numbers = 1, 2
77 # Tuple sequence unpacking.
78 # Number of variables used has to be same as the number of items in the tuple.
79 # Unpacking the tuple and assigning its items to x and y.
80 x, y = numbers
81 # Note that this is also packing the args as a tuple which gets unpacked as the print method's argument
82 print(x, y) # Output # (1, 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Terms Privacy Security Status Docs Contact GitHub Pricing API Training Blog About

© 2022 GitHub, Inc.

You might also like