Home Backend Development Python Tutorial day-at payilagam [Lists]

day-at payilagam [Lists]

Jan 04, 2025 pm 09:08 PM

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]

Copy after login
  • 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 
Copy after login

in for loop:

for data in student_data:
    print(data)

Copy after login
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

Copy after login

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]

Copy after login

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]

Copy after login

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]

Copy after login

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
Copy after login

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

Copy after login

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]
[]

Copy after login

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

Copy after login

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 

Copy after login

The above is the detailed content of day-at payilagam [Lists]. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

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 by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

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 in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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 without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

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...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

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

See all articles