day-at payilagam [Lists]
what is list:
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks) that is used to store an ordered collection of items. We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe stored at different locations.
here list is mutable and string is immutable
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6] print(student_data) output: ['Guru Prasanna', 'B.Com', 21, True, 5.6]
- list is a collection of data
- list has heterogeneous(various)data
- list is index based
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6] i = 0 while i<len(student_data): print(student_data[i],end=' ') i+=1 output: Guru Prasanna B.Com 21 True 5.6
in for loop:
for data in student_data: print(data)
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6] index=0 for index,data in enumerate(student_data): print(index,data) index+=1 output: 0 Guru Prasanna 1 B.Com 2 21 3 True 4 5.6
enumerate method index the values.
student_data = ['Guru Prasanna', 'B.Com', 21, True, 5.6] print(student_data) student_data[1] = 'M.Com' print(student_data) output: ['Guru Prasanna', 'B.Com', 21, True, 5.6] ['Guru Prasanna', 'M.Com', 21, True, 5.6]
just removes and adds the new position value what we have just coded. we have to mention which positional value.
employee = [] employee.append('Raja') employee.append('Madurai') employee.append('B.Sc.,') employee.append(5.2) employee.append(True) print(employee) output: ['Raja', 'Madurai', 'B.Sc.,', 5.2, True]
It is called as empty list.By,append method it
appends the value of the list.
employee.insert(2,'Tamilnadu') print(employee) employee.remove('Madurai') print(employee) employee.pop(3) print(employee) output: ['Raja', 'Madurai', 'Tamilnadu', 'B.Sc.,', 5.2, True] ['Raja', 'Tamilnadu', 'B.Sc.,', 5.2, True] ['Raja', 'Tamilnadu', 'B.Sc.,', True] ['Raja', 'Tamilnadu', 'B.Sc.,', True]
In this case,append means adding at the end and insert means inbetween the values as we are giving specific index to fit the value.
- remove(value based removal)
- pop(index based removal)
l = [10,20,30,40,50,60] del l[2] print(l) output: [10, 20, 40, 50, 60] l = [10,20,30,40,50,60] del l[2:4] print(l) output: [10, 20, 50, 60] l = [10,20,30,40,50,60] print(l.pop(2)) output: 30
in first case del is a keyword,
in second case we are using indexing
in third case we are taking out second index and printing it out.
l = [10,20,30,40,50,60] print(l) print(l.pop(2)) print(l) print(l.pop()) print(l) print(l.pop(123)) output: [10, 20, 30, 40, 50, 60] 30 [10, 20, 40, 50, 60] 60 [10, 20, 40, 50] Traceback (most recent call last): File "/home/main.py", line 15, in <module> print(l.pop(123)) IndexError: pop index out of range
in this case, pop(2) takes the index value and prints the value.
pop() prints the value of the last.
pop(123) prints error. there is no value assigned to it.
l = [10,20,30,40,50,60] print(l) del l[:] # del l print(l) output: [10, 20, 30, 40, 50, 60] []
after adding del keyword without any indexing inside it deletes everything and gives an empty list.
data_list = ['abcd','pqrs','xyz',1234, 1.234,True] for data in data_list: if isinstance(data,str): print(data.upper()) output: ABCD PQRS XYZ
task:
names_list = ['sachin','dhoni','rohit','virat'] for name in names_list: if len(name)==5: print(name,end=' ') print() for name in names_list: if name[-1] == 't': print(name,end=' ') print() Output: dhoni rohit virat rohit virat
The above is the detailed content of day-at payilagam [Lists]. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
