0% found this document useful (0 votes)
6 views4 pages

Python Programming 4

Uploaded by

shraddhavinod1
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)
6 views4 pages

Python Programming 4

Uploaded by

shraddhavinod1
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/ 4

List-:

#list = list can store elements of different data types (integer,float,string)

marks=[67,89,90,75,97,90,80,56]

print(marks) #output [67, 89, 90, 75, 97, 90, 80, 56]

print(type(marks)) #output <class 'list'>

#indexing start from 0

print(marks[2]) #output 90

print(marks[4]) #output 97

print(marks[0]) #output 67

#length of list (start from 1)

print(len(marks)) #output 8

#different data type store

student=["Shruti",95,"Pune","College ccs Wakad"]

print(student) #output ['Shruti', 95, 'Pune', 'College ccs Wakad']

print(student[0]) #output Shruti

print(student[2]) #output Pune

#string and list are same but basic difference is string is inmutable and list is mutable.

student=["Shruti",95,"Pune","College ccs Wakad"]

print(student[0]) #output Shruti

student[0]="Shreya"

print(student[0]) #output Shreya

print(student) #output ['Shreya', 95, 'Pune', 'College ccs Wakad']

#List Slicing (Same as string)

# list_name[starting_idx : ending_idx] ending index is not included

student_name=["Shruti","Disha","Nikita","Manisha","Arti"]
print(student_name[1:4]) #output ['Disha', 'Nikita', 'Manisha']

print(student_name[:3]) #[0:3] #output ['Shruti', 'Disha', 'Nikita']

print(student_name[2:]) #[2:len(student_name)] output ['Nikita', 'Manisha', 'Arti']

#list methods or function

# 1)list.append() == adds one element at the end

# 2) list.sort() == sorts in ascending order

# 3) list.sort(reverse=True) == sorts in descending order

# 4) list.reverse() == reverse the list

# 5) list.insert(idx,el) == insert element at index

# 1)list.append() == adds one element at the end

list=[2,4,6,3]

list.append(5)

print(list) #output [2, 4, 6, 3, 5]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.append("Disha")

print(list1) #output ['Arti', 'Shreya', 'Riya', 'Manisha', 'Akanksha', 'Disha']

# 2) list.sort() == sorts in ascending order

list=[4,7,12,9,67,34,56]

list.sort()

print(list) #output [4, 7, 9, 12, 34, 56, 67]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.sort()

print(list1) #output ['Akanksha', 'Arti', 'Manisha', 'Riya', 'Shreya']

# 3) list.sort(reverse=True) == sorts in descending order

list=[4,7,12,9,67,34,56]
list.sort(reverse=True)

print(list) #output [67, 56, 34, 12, 9, 7, 4]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.sort(reverse=True)

print(list1) #output ['Shreya', 'Riya', 'Manisha', 'Arti', 'Akanksha']

# 4) list.reverse() == reverse the list

list=[4,7,12,9,67,34,56]

list.reverse()

print(list) #output [56, 34, 67, 9, 12, 7, 4]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.reverse()

print(list1) #output ['Akanksha', 'Manisha', 'Riya', 'Shreya', 'Arti']

# 5) list.insert(idx,el) == insert element at index

list=[4,7,12,9,67,34,56]

list.insert(3,10)

print(list) #output [4, 7, 12, 10, 9, 67, 34, 56]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.insert(1,"Ankita")

print(list1)

# 6) list.remove()== remove element

list=[4,7,12,9,67,34,56]

list.remove(34)
print(list) #output [4, 7, 12, 9, 67, 56]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.remove("Riya")

print(list1) #output['Arti', 'Shreya', 'Manisha', 'Akanksha']

#7) list.pop(idx) == removes elements at index

list=[4,7,12,9,67,34,56]

list.pop(3)

print(list) #output[4, 7, 12, 67, 34, 56]

list1=["Arti","Shreya","Riya","Manisha","Akanksha"]

list1.pop(4)

print(list1) #output ['Arti', 'Shreya', 'Riya', 'Manisha']

-----------------------------------------------------------------------------------------------------------------

You might also like