Home >Backend Development >Python Tutorial >Day - List and List functions
List:
List is represented by
List is a collection of heterogeneous data(Different datatype).
List is index_based
List is mutable(changeable)
Example:
student_data = ['Pritha', 'B.E', 30, True, 5.6] print(student_data)
['Pritha', 'B.E', 30, True, 5.6]
Example using while loop:
student_data = ['Pritha', 'B.E', 30, True, 5.6] i = 0 while i<len(student_data): print(student_data[i],end=' ') i+=1
Pritha B.E 30 True 5.6
Example using for loop:
student_data = ['Pritha', 'B.E', 30, True, 5.6] for data in student_data: print(data, end=" ")
Pritha B.E 30 True 5.6
enumerate():
It is used to create the index in the loop.
Also used for grouping and index tracking.
student_data = ['Pritha', 'B.E', 30, True, 5.6] index = 0 for index,data in enumerate(student_data): print(index, data) index+=1
0 Pritha 1 B.E 2 30 3 True 4 5.6
List is mutable so we can change any element from the list.
student_data = ['Pritha', 'B.E', 30, True, 5.6] print(student_data) student_data[1] = 'M.E' print(student_data)
['Pritha', 'B.E', 30, True, 5.6] ['Pritha', 'M.E', 30, True, 5.6]
append():
It is used to add an element to the end of a list.
It modifies the list in-place and does not return a new list.
Create a list using append():
employee = [] employee.append('Raja') employee.append('Madurai') employee.append('B.Sc.,') employee.append(5.2) employee.append(True) print(employee)
['Raja', 'Madurai', 'B.Sc.,', 5.2, True]
insert():
It is used to insert an element at a specific position in a list.
employee = ['Raja', 'Madurai', 'B.Sc.,', 5.2, True] employee.insert(2, 'Tamil Nadu') print(employee)
['Raja', 'Madurai', 'Tamil Nadu', 'B.Sc.,', 5.2, True]
remove():
It is used to remove the first occurrence of a specific element from a list.
If the element is found, it is removed, and the list is modified in place.
If the element is not present in the list, a ValueError is raised.
employee = ['Raja', 'Madurai', 'B.Sc', 5.2, True] employee.remove('Madurai') print(employee)
['Raja', 'B.Sc', 5.2, True]
pop():
It is used to remove and return an element from a list at a specific index.
employee = ['Raja', 'Madurai', 'B.Sc', 5.2, True] employee.pop(3) print(employee)
['Raja', 'Madurai', 'B.Sc', True]
del statement:
It is used to delete an element from a list by index.
It does not return a value; it simply removes the item or object from memory.
l = [10,20,30,40,50,60] del l[2] print(l)
[10, 20, 40, 50, 60]
We can use del to remove an item from a list by specifying a slice.
l = [10,20,30,40,50,60] del l[2:4] print(l)
[10, 20, 50, 60]
Difference between pop() and del:
l = [10,20,30,40,50,60] del l[:] print(l) l = [10,20,30,40,50,60] print(l.pop())
[] 60
del[:]- Delete everything from the list
pop()- Delete last element from the list.
Write a program to calculate total mark and percentage:
marks_list = [90,97,97,65,78] total = 0 l=len(marks_list) for mark in marks_list: total+=mark print(total) percentage=total/l print("percentage:",percentage)
427 percentage: 85.4
Write a Program to find the highest mark from the given mark:
highest = 0 marks_list = [90,97,96,65,98] for mark in marks_list: if mark>highest: highest = mark #90 97 print(highest)
98
Write a program to find the lowest mark from the given mark:
lowest = 100 marks_list = [90,97,96,65,98] for mark in marks_list: if mark<lowest: lowest = mark print(lowest)
65
scores = [90,167, 208,45,32] lowest = scores[0] for score in scores: if score<lowest: lowest = score print(lowest)
32
isinstance()-used to check if an object is an instance of a specified class or any of its subclasses.
It returns True if the object matches the specified class or type, otherwise it returns False.
Write a program to find str datatype:
student_data = ['Pritha', 'B.E', 30, True, 5.6] print(student_data)
['Pritha', 'B.E', 30, True, 5.6]
Write a program to find str datatype and make them upper:
student_data = ['Pritha', 'B.E', 30, True, 5.6] i = 0 while i<len(student_data): print(student_data[i],end=' ') i+=1
Pritha B.E 30 True 5.6
Write a program to find str datatype and make them upper for first two letter:
student_data = ['Pritha', 'B.E', 30, True, 5.6] for data in student_data: print(data, end=" ")
Pritha B.E 30 True 5.6
Task:
1) contains n --> names
2) names have 5 letters
3) t --> names end with
student_data = ['Pritha', 'B.E', 30, True, 5.6] index = 0 for index,data in enumerate(student_data): print(index, data) index+=1
0 Pritha 1 B.E 2 30 3 True 4 5.6
# SaChIn DhOnI rOhIt vIrAt
student_data = ['Pritha', 'B.E', 30, True, 5.6] print(student_data) student_data[1] = 'M.E' print(student_data)
['Pritha', 'B.E', 30, True, 5.6] ['Pritha', 'M.E', 30, True, 5.6]
The above is the detailed content of Day - List and List functions. For more information, please follow other related articles on the PHP Chinese website!