Home > Backend Development > Python Tutorial > Day - List and List functions

Day - List and List functions

Mary-Kate Olsen
Release: 2025-01-04 03:33:46
Original
1029 people have browsed it

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)
Copy after login
Copy after login
['Pritha', 'B.E', 30, True, 5.6]
Copy after login
Copy after login

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

Copy after login
Copy after login
Pritha B.E 30 True 5.6
Copy after login
Copy after login
Copy after login
Copy after login

Example using for loop:

student_data = ['Pritha', 'B.E', 30, True, 5.6]
for data in student_data:
    print(data, end=" ")
Copy after login
Copy after login
Pritha B.E 30 True 5.6
Copy after login
Copy after login
Copy after login
Copy after login

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

Copy after login
Copy after login
0 Pritha
1 B.E
2 30
3 True
4 5.6

Copy after login
Copy after login

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)
Copy after login
Copy after login
['Pritha', 'B.E', 30, True, 5.6]
['Pritha', 'M.E', 30, True, 5.6]

Copy after login
Copy after login

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)
Copy after login
['Raja', 'Madurai', 'B.Sc.,', 5.2, True]

Copy after login

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)

Copy after login
['Raja', 'Madurai', 'Tamil Nadu', 'B.Sc.,', 5.2, True]
Copy after login

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)

Copy after login
['Raja', 'B.Sc', 5.2, True]
Copy after login

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)

Copy after login
['Raja', 'Madurai', 'B.Sc', True]
Copy after login

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)
Copy after login
[10, 20, 40, 50, 60]
Copy after login

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)

Copy after login
[10, 20, 50, 60]
Copy after login

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())

Copy after login
[]
60
Copy after login

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)

Copy after login
427
percentage: 85.4

Copy after login

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)

Copy after login
98
Copy after login

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)
Copy after login
65
Copy after login
scores = [90,167, 208,45,32]
lowest = scores[0]

for score in scores:
    if score<lowest:
        lowest = score 

print(lowest)

Copy after login
32
Copy after login

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)
Copy after login
Copy after login
['Pritha', 'B.E', 30, True, 5.6]
Copy after login
Copy after login

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

Copy after login
Copy after login
Pritha B.E 30 True 5.6
Copy after login
Copy after login
Copy after login
Copy after login

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=" ")
Copy after login
Copy after login
Pritha B.E 30 True 5.6
Copy after login
Copy after login
Copy after login
Copy after login

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

Copy after login
Copy after login
0 Pritha
1 B.E
2 30
3 True
4 5.6

Copy after login
Copy after login

# SaChIn DhOnI rOhIt vIrAt

student_data = ['Pritha', 'B.E', 30, True, 5.6]
print(student_data)

student_data[1] = 'M.E'
print(student_data)
Copy after login
Copy after login
['Pritha', 'B.E', 30, True, 5.6]
['Pritha', 'M.E', 30, True, 5.6]

Copy after login
Copy after login

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template