0% found this document useful (0 votes)
4 views12 pages

Class 10 Python Programming List

Uploaded by

harshu.transform
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)
4 views12 pages

Class 10 Python Programming List

Uploaded by

harshu.transform
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/ 12

CLASS 10

Python Programming
LIST
LIST

When we want to assign multiple values to a variable, we use list.


Python knows a number of compound data types, used to group together
other values. The most versatile is the list, which can be written as a list of
comma-separated values (items) between square brackets.
LIST

• List is a collection of elements which is ordered and changeable.


• Allows duplicate values.
• A list contains items separated by commas and enclosed within square
brackets ([ ]).
• All items belonging to a list can be of different data type.
• The values stored in a list can be accessed using the slice operator ([ ] and
[:]) with indexes starting at 0 in the beginning of the list.
• CREATING A LIST

To create a list, enclose the elements of the list within square brackets
and separate the elements by commas.
Syntax:
list-name= [item-1, item-2, …….., item-n]
Example:
mylist=["apple","banana","cherry"] #a list with three items
• Accessing lists:
• The values stored in a list can be accessed using the slice operator ([ ] and [:]) with
indexes.
• List-name[start:end] will give you elements between indices start to end-1.
• The first item in the list has the index zero (0).
• Examples:
>>> mylist=[25,12,36,95,14]
>>> mylist[3]
95
>>> mylist [-2]
95
>>> mylist [-5]
25
>>> mylist[-10] #IndexError: list index out of range
LIST METHODS
FUNCTION NAME DESCRIPTION
append( ) The append() method in python adds a single item to the existing list. It doesn't return a new list of
items but will modify the original list by adding the item to the end of the list

extend( ) We can add several items to the list using extend.

len( ) Find the length of the list.

index( ) Returns the index of the first occurrence of the element with the specified value. If not found it
raises a value error.

insert( ) Adds an element at the specified position.

reverse() The reverse() method reverses the elements of the list

clear() The clear() method removes all items from the list
remove( ) To remove a single element from the list by passing the value itself.

pop( ) Removes the element at the specified position using index number and returns the deleted
element.
sort( ) Sorts the list. By default in ascending order

sort(reverse=True) sorts the list in the descending order

max() max returns the elements from the list with maximum value

min() min() returns the elements from the list with minimum value

To remove multiple values from list using range ( del l1 [2:] )


Del
List: Slicing method

mylist=[34,53,14,64,37,25,18]
print(mylist[3])
print(mylist [-2])
print(mylist [-5])
print(mylist[2:6])
Example: List Functions
l1.sort(reverse=True) #sort(reverse=True) descending order
l1=[122,33,144,55,66,77] print(l1)
print(l1) l1.insert(2,500) #insert()

print(len(l1)) #len()
print(l1)
l1.remove(55) #remove()
#l1.reverse() #Reverse()
print("Removed 55 from the list",l1)
#print(l1) print(l1.pop()) #pop()
#l1.clear() #clear() l2=[100,200,300]

#print(l1) l1.append(l2) #append()


print(l1)
print(l1.index(25)) #index()
l1.extend(l2) #extend()
print(max(l1)) #max()
print(l1)
print(min(l1)) #min() print(len(l1))

l1.sort() #sort() ascending order print(l1[5])

print(l1)
#WAP to display list elements with index number in different line.

l1=[23,45,56,78,99,100]
for i in l1: #method1
print("The element is ",i,"and its index number is:",l1.index(i))
#WAP find the sum and average of list l1.

l1=[23,45,56,78,99,100]
s=0
for i in l1:
s=s+i
print("Sum=",s)
print("Average=",s/len(l1))
THANK YOU

You might also like