Week 5
Week 5
Lists and
Tuples
• Name (String)
• Age (Integer)
• Grade (Float)
What if we also need to store more complex details? (e.g., Address, Phone number, Courses)
Primitive data types (e.g., integers, floats, strings) are useful for simple data.
Solution: Data structures can hold multiple types of data and scale with size.
Lists:
• Lists are ordered collections of items, and they are one of the most versatile data structures in Python.
• Lists can hold items of different data types and allow for dynamic resizing.
• You can access elements by their index, append, insert, remove, and perform various other operations
on lists.
Tuples:
• Tuples are similar to lists but are immutable, meaning their items cannot be changed after creation.
• They are often used to represent collections of related data.
Here, the list mixed_list contains an integer, a string, a float, and a boolean.
The index is zero-based, meaning the first element has an index of 0, the second element has
an index of 1, and so on.
Slicing is done by specifying a start index (inclusive) and an end index (exclusive) separated by
a colon : inside the square brackets.
If you omit the start or end index, Python assumes that you want to start from the beginning
or go to the end of the list, respectively.
Lists are mutable, which means you can modify them in place.
Lists are flexible data structures, making them suitable for various programming tasks.
Python offers different ways to iterate over a list, meaning we can go through each element in
the list one by one.
The loop goes through each element (student) in the students list.
This method is simple and commonly used.
enumerate() returns both the index and the element in each iteration.
Cleaner than using range() and len().
To access elements within a nested list, you need to provide the index for each level of nesting.
You start by accessing a sublist using an index, and then you access elements within that
sublist using another index.
This process can be repeated to access elements at deeper levels of nesting within the list.
This code will iterate through each sublist in the outer list and then iterate through each
element within the sublists, printing all elements.
2. The inner loop for element in sublist: iterates through each element within the current
sublist.
• In the first iteration of the inner loop, element will be 1, then 2, and finally 3 as it goes through [1, 2, 3].
• In the second iteration of the outer loop, element will be 4, then 5, and finally 6 as it goes through [4, 5, 6].
• In the third iteration of the outer loop, element will be 7, then 8, and finally 9 as it goes through [7, 8, 9].
Tuples can be iterated over just like lists, but they are immutable.
2. The inner loop for element in subtuple: iterates through each element within the current
inner tuple.
• In the first iteration of the inner loop, element will be 1, then 2, and finally 3 as it goes through (1, 2, 3).
• In the second iteration of the outer loop, element will be 4, then 5, and finally 6 as it goes through (4, 5, 6).
• In the third iteration of the outer loop, element will be 7, then 8, and finally 9 as it goes through (7, 8, 9).
# Example:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Creating a list of integers
result = list_sum_even(my_list) # Calling the function and passing the list of integers
print(f"Sum of even integers: {result}") # Output: Sum of even integers: 30
# Example:
my_strings = ["fiat", "volvo", "hyundai", "mercedesbenz", "nissan"] # Creating a list of strings
lengths = string_lengths(my_strings) # Calling the function and passing the list of strings
print(f"Lengths of strings: {lengths}") # Output: Lengths of strings: [4, 5, 7, 12, 6]
# Example:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Creating a list of integers
result = double_odd_numbers(my_list) # Calling the function and passing the list of integers
print(result) # Output: [2, 2, 6, 4, 10, 6, 14, 8, 18]
# Example:
my_list = [12, 45, 78, 23, 56, 89, 34] # Creating a list of integers
largest_number = find_largest_element(my_list) # Calling the function and passing the list of
integers
print(f"The largest number is: {largest_number}") # Output: The largest number is: 89
# Example:
my_tuple = (10, 20, 30, 40, 50) # Creating a tuple of integers
result = tuple_contains(my_tuple, 30) # Calling the function and passing the tuple and a value
print(result) # Output: True
# Example:
nested_lists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] # Creating a nested list of integers
result = sum_nested_lists(nested_lists) # Calling the function and passing the nested list
print(f"Sums of sublists: {result}") # Output: Sums of sublists: [6, 9, 30]
# Example:
nested_lists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] # Creating a nested list of integers
result = sum_all_elements(nested_lists) # Calling the function and passing the nested list
print(f"Sum of all elements: {result}") # Output: Sum of all elements: 45