- Tuples = ( x, y, (m,n), k)
- Tuples are ordered
- Tuples allows duplicate elements
- Tuples are immutable -> tuple cannot be changed
- You can not add an Item to a tuple
- You can not remove an Item from a tuple
- Tuples can contain string, integer, float and even tuples (called nesting)
- Type of the variable is Tuple
- Index of first element of my_tuple (x) is 0
- Index of last element of my_tuple (k) is -1
- Creating a Tuple
- Usign tuple([ ])
- Using ()
- Index of Tuples
- Range of Indexes (Slicing)
- Step Index
- Returning Index of Elements
- Concatenating Tuples (Combining Tuples)
- Converting a tuple to a list
- Converting a list to a tuple
- Count() Method
- Index() Method
- Type
- Lenghth of Tuples
- Sorting Tuples
- Tuples and Loop
- Tuples and if Statements
- Unpacking Single element
- Unpacking multiple elements using *
- Comparing Tuples with Lists
- sys module
- timeit Module
- Usign tuple([ ])
- Using ()
my_tuple = tuple(["Hello"])
my_tuple
- If you creating a Tuple with one element:
- It is essential to add a comma after the item to be considered as a tuple
- Otherwise, it will be considered as a string
my_tuple = ("Hello",)
print(type(my_tuple))
my_variable = ("Hello")
print(type(my_variable))
- Showing an element of a Tuple
- Index of the first element is always 0
- Index of the last element is always -1
my_tuple = ( 10 , "hello", 1.50 , ("X is",3))
print(my_tuple[0])
print(my_tuple[1])
print(my_tuple[2])
print(my_tuple[3])
print(my_tuple[3][0])
print(my_tuple[3][1])
print(my_tuple[-1])
print(my_tuple[-1][0])
print(my_tuple[-1][1])
- Showing multiple elements
my_tuple = ( 1, 3, 5, 7, 9)
print(my_tuple[0:1])
print(my_tuple[0:2])
print(my_tuple[3:5])
print(my_tuple[:])
- step: the amount by which the index increases (defaults: 1)
my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
print(my_tuple[::2])
my_tuple = ( 1, 3, 5, 7, 9)
check_tuple = my_tuple.index(7)
print(check_tuple)
my_tuple = ( 2 , "bye", 100)
new_tuple = my_tuple + (0.5 , "Added", 150 ,)
print(new_tuple)
my_tuple = ( 2 , "bye", 100)
my_list = list(my_tuple)
my_list
my_list = [2, 'bye', 100]
my_tuple = tuple(my_list)
my_tuple
my_tuple = ( 1, "hi", 5, 7, "hi", "hi")
check_tuple = my_tuple.count("hi")
print(check_tuple)
- Return index of the first occurrence of the element
my_tuple = ( 1, "hi", 5, 7, "hi", "hi")
check_tuple = my_tuple.index("hi")
print(check_tuple)
my_tuple = ( 2 , "bye", 100)
type(my_tuple)
my_tuple = (1, 3, 5, 7, 9)
len(my_tuple)
my_tuple = ( 100, 3, 55, 70, 99)
sorted_tuple = sorted(my_tuple)
print(sorted_tuple)
my_tuple = ( 100, 3, 55, 70, 99)
for x in my_tuple:
print(x)
my_tuple = ( 100, 3, 55, 70, 99)
if "hello" in my_tuple:
print("Yes, You won!")
else:
print("No, You lost")
my_tuple = ("John", "Smith", "50")
name, familyname, age = my_tuple
print(name)
print(familyname)
print(age)
my_tuple = ( 100, 3, 55, 70, 99)
x1, *x2, x3 = my_tuple
print(x1)
print(*x2)
print(x3)
- sys module
- timeit Module
- Tuples are smallers
- Tuples are more efficient
import sys
my_tuple = ( 100, 3, 55, 70, 99)
my_list = [100, 3, 55, 70, 99]
print(sys.getsizeof(my_tuple), "bytes")
print(sys.getsizeof(my_list), "bytes")
- It takes longer time to create Lists than Tuples
- Tuples are more efficient
import timeit
print(timeit.timeit(stmt = "(100, 3, 55, 70, 99)" , number = 1000000 ))
print(timeit.timeit(stmt = "[100, 3, 55, 70, 99]" , number = 1000000))