In Python, a list is a versatile data structure that can hold a collection of items. You can create, access, and modify lists easily.
4.4.1 Creating Lists
A list is created by placing items inside square brackets [ ], separated by commas. Lists can contain items of different types, such as numbers, strings, or even other lists.
Example: Create a list of your favorite fruits.
fruits = ["Mango", "Apple" "Banana"] print(fruits) # Output: ['Mango' 'Apple' 'Banana' ]
Explanation: This code creates a list named 'fruits containing three elements and then prints the list.
4.4.2 Accessing List Items
You can access items in a list by referring to their index, starting from 0. Example: Access and print the second item from the list of fruits.
fruits = ["Mango", "Apple", "Banana" print(fruits [1]) # Output: Apple
Explanation: The code initializes a list 'fruits' containing 'Mango', 'Apple', and 'Banana', then prints the second item, 'Apple', using the index '1'.
ACTIVITY
Write a Python program that imports the random and statistics libraries. Use random to generate a list of 10 random numbers between 1 and 50. Then, use statistics to calculate and print the mean (average) of those numbers.