In Python, a list is a versatile data structure used to store collections of items. Lists are mutable, meaning you can modify, add, or remove elements from them after creation. Lists are denoted by square brackets [] and elements inside a list are separated by commas.
Common List Operations:
Using a While Loop:
numbers = [1, 2, 3, 4, 5]
i = 0
while i < len(numbers):
print(numbers[i])
i += 1
Using a For Loop:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
Instructions:
To work on the exercises, you can write Python code in your favorite text editor or IDE (Integrated Development Environment). Save the code in a .py file and run it using the Python interpreter. Observe the outputs and verify that the results match the expected outputs mentioned in each exercise.
Input: [1, 2, 3, 4, 5]
Output: 15
Input: [10, 5, 25, 8, 20]
Output: Maximum: 25, Minimum: 5
Input: [1, 2, 3, 4, 5]
Output: [1, 4, 9, 16, 25]
Input: [1, 2, 2, 3, 4, 4, 5]
Output: [1, 2, 3, 4, 5]
Input: [1, 2, 3, 4, 5, 6]
Output: [2, 4, 6]
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Input: [1, 2, 2, 3, 4, 4, 5], Element: 2
Output: 2
Input: [1, 2, 3], [4, 5, 6]
Output: [1, 2, 3, 4, 5, 6]