Python List NOTES (1) (1)
Python List NOTES (1) (1)
A list in Python is a versatile data structure that allows you to store and manage a
collection of items in a single variable. These items can be of any type, such as
integers, strings, or even other lists
Concept of Indexes
# b) Print the elements from second to fourth position using positive indexing
print(num[1:4]) # Output: [12, 5, 9]
# c) Print the elements from third to fifth position using negative indexing
print(num[-4:-1]) # Output: [5, 9, 65]
In Python, the for loop is commonly used to iterate over the elements of a list.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
Create a List of First 10 Even Numbers, Add 1 to Each Item, and Print
even_numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
new_list=[]
for num in even_numbers:
num=num+1
new_list.append(num)
print(new_list)
List_1.sort() print(List_1)
# Output: [10, 12, 14, 15, 20, 30, 40]