0% found this document useful (0 votes)
11 views2 pages

Python List NOTES (1) (1)

Uploaded by

Bhavika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Python List NOTES (1) (1)

Uploaded by

Bhavika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CLASS-IX

SUBJECT: ARTIFICIAL INTELLIGENCE


Introduction to Python List

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

Example of creating List


names = ["Arjun", "Sonakshi", "Vikram", "Sandhya"]
print(names)
# Output: ['Arjun', 'Sonakshi', 'Vikram', 'Sandhya']

Concept of Indexes

# Create the list


children = ['Arjun', 'Sonakshi', 'Vikram', 'Sandhya', 'Sonal', 'Isha', 'Kartik']

# a) Print the whole list


print(children) # Output: ['Arjun', 'Sonakshi', 'Vikram', 'Sandhya', 'Sonal', 'Isha', 'Kartik']

# b) Delete the name “Vikram” from the list


children.remove('Vikram')
print(children) # Output: ['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik']

# c) Add the name “Jay” at the end


children.append('Jay')
print(children) # Output: ['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Jay']

# d) Remove the item which is at the second position


children.pop(1) # Removes 'Sonakshi'
print(children) # Output: ['Arjun', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Jay']

Working with a List of Numbers


# Create the list
num = [23, 12, 5, 9, 65, 44]

# a) Print the length of the list


print(len(num)) # Output: 6

# 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 = [10, 20, 30, 40]


# Add elements [14, 15, 12] using extend function
List_1.extend([14, 15, 12])
# Sort the final list in ascending order

List_1.sort() print(List_1)
# Output: [10, 12, 14, 15, 20, 30, 40]

You might also like