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 print i> <pre class="brush:php;toolbar:false">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> <pre class="brush:php;toolbar:false">65
scores = [90,167, 208,45,32] lowest = scores[0] for score in scores: if score<lowest: lowest="score" print> <pre class="brush:php;toolbar:false">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 print i> <pre class="brush:php;toolbar:false">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!

The basic syntax for Python list slicing is list[start:stop:step]. 1.start is the first element index included, 2.stop is the first element index excluded, and 3.step determines the step size between elements. Slices are not only used to extract data, but also to modify and invert lists.

Listsoutperformarraysin:1)dynamicsizingandfrequentinsertions/deletions,2)storingheterogeneousdata,and3)memoryefficiencyforsparsedata,butmayhaveslightperformancecostsincertainoperations.

ToconvertaPythonarraytoalist,usethelist()constructororageneratorexpression.1)Importthearraymoduleandcreateanarray.2)Uselist(arr)or[xforxinarr]toconvertittoalist,consideringperformanceandmemoryefficiencyforlargedatasets.

ChoosearraysoverlistsinPythonforbetterperformanceandmemoryefficiencyinspecificscenarios.1)Largenumericaldatasets:Arraysreducememoryusage.2)Performance-criticaloperations:Arraysofferspeedboostsfortaskslikeappendingorsearching.3)Typesafety:Arraysenforc

In Python, you can use for loops, enumerate and list comprehensions to traverse lists; in Java, you can use traditional for loops and enhanced for loops to traverse arrays. 1. Python list traversal methods include: for loop, enumerate and list comprehension. 2. Java array traversal methods include: traditional for loop and enhanced for loop.

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
