DEV Community

Guru prasanna
Guru prasanna

Posted on • Edited on

Python Day-23 Lists and list functions,Task

List:
[ ] --> Symbol
-->Collection of Data
-->Collection of Heterogeneous Data(different data types)
-->List is Index Based
-->List is Mutable(Changeable)

Ex: student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6]
indexing --> 0 1 2 3 4

Example: using while loop and for loop:

student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6]
i = 0 
while i<len(student_data):
    print(student_data[i],end=' ')
    i+=1
print()

for data in student_data:
    print(data,end=' ')
Enter fullscreen mode Exit fullscreen mode

Output:

Guru Prasanna B.Com 23 True 5.6 
Guru Prasanna B.Com 23 True 5.6
Enter fullscreen mode Exit fullscreen mode

enumerate()-->Useful for index tracking
Enumerate is a built-in function in python that allows you to keep track of the number of iterations (loops) in a loop.

Syntax: enumerate(iterable, start=0)
--> Iterable: any object that supports iteration
--> Start: the index value from which the counter is to be started, by default it is 0

Example:

student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6]
index = 0
for index,data in enumerate(student_data):
    print(index, data)
    index+=1
Enter fullscreen mode Exit fullscreen mode

Output:

0 Guru Prasanna
1 B.Com
2 23
3 True
4 5.6
Enter fullscreen mode Exit fullscreen mode

To prove list is mutable
Example:

student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6]

print(student_data)

student_data[1] = 'M.Com'

print(student_data)
Enter fullscreen mode Exit fullscreen mode

Output:

['Guru Prasanna', 'B.Com', 23, True, 5.6]
['Guru Prasanna', 'M.Com', 23, True, 5.6]
Enter fullscreen mode Exit fullscreen mode

List Functions:

1) append()-->Adds an element at the end of the list
2) insert()-->Adds an element at the specified position
3) remove()-->Removes the first item with the specified value(value based removal).
4) pop()-->Removes the element at the specified position(index based removal).

refer- https://www.w3schools.com/python/python_ref_list.asp

Example:

employee = []
employee.append('Raja') 
employee.append('Madurai')
employee.append('B.Sc')
employee.append(5.2)
employee.append(True)

print(employee)

employee.insert(2, 'Tamil Nadu')
print(employee)

employee.remove('Madurai')
print(employee)

employee.pop(3)  
print(employee)
Enter fullscreen mode Exit fullscreen mode

Output:

['Raja', 'Madurai', 'B.Sc', 5.2, True]
['Raja', 'Madurai', 'Tamil Nadu', 'B.Sc', 5.2, True]
['Raja', 'Tamil Nadu', 'B.Sc', 5.2, True]
['Raja', 'Tamil Nadu', 'B.Sc', True]
Enter fullscreen mode Exit fullscreen mode

del keyword:
The del keyword is used to delete objects.(variables, lists, or parts of a list etc..)
-->Even del can be used to delete particular range.

Example:

l = [10,20,30,40,50,60]

del l[2:4]

print(l)
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 50, 60]
Enter fullscreen mode Exit fullscreen mode

Difference between del and pop:

del will remove the specified index.(keyword)
pop() removes and returns the element that was removed.(inbuilt method)

calculate total marks and percentage

# Total, 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)
Enter fullscreen mode Exit fullscreen mode

Output:

427
percentage: 85.4
Enter fullscreen mode Exit fullscreen mode

Calculate Highest mark

# Highest Mark
marks_list = [90,97,96,65,98]
highest = marks_list[0]

for mark in marks_list:
    if mark>highest:
        highest = mark

print(highest)
Enter fullscreen mode Exit fullscreen mode

Output:

98
Enter fullscreen mode Exit fullscreen mode

Calculate lowest mark

# lowest Mark

marks_list = [90,97,96,65,98]
lowest = marks_list[0]

for mark in marks_list:
    if mark<lowest:
        lowest = mark

print(lowest)
Enter fullscreen mode Exit fullscreen mode

Output:

65
Enter fullscreen mode Exit fullscreen mode

isinstance(): The isinstance() function returns True if the specified object is of the specified type, otherwise False.
Example:1

data_list = ['abcd','pqrs','xyz',1234, 1.234,True]
for data in data_list:
    if isinstance(data,str):
        print(data)
Enter fullscreen mode Exit fullscreen mode

Output:

abcd
pqrs
xyz
Enter fullscreen mode Exit fullscreen mode

Example:2

#Find str datatype and make them to uppercase
data_list = ['abcd','pqrs','xyz',1234, 1.234,True]
for data in data_list:
    if isinstance(data,str):
        print(data.upper())
Enter fullscreen mode Exit fullscreen mode

Output:

ABCD
PQRS
XYZ
Enter fullscreen mode Exit fullscreen mode

Example:3

#Find str datatype and print only first 2 letters
data_list = ['abcd','pqrs','xyz','a','m',1234, 1.234,True]
for data in data_list:
    if isinstance(data,str):
        if len(data)>= 2:
            print(data.upper()[:2])
Enter fullscreen mode Exit fullscreen mode

Output:

AB
PQ
XY
Enter fullscreen mode Exit fullscreen mode

Tasks:
1) contains n --> names
2) names have 5 letters
3) t --> names end with

names_list = ['sachin','dhoni','rohit','virat']
#1
for name in names_list:
    if len(name)==5:
        print(name,end=' ')
print()

#2
for name in names_list:
    if name[-1] == 't':
        print(name,end=' ')
print()

#3
for name in names_list:
    if 'n' in name:
        print(name,end=' ')   
Enter fullscreen mode Exit fullscreen mode

Output:

dhoni rohit virat 
rohit virat 
sachin dhoni
Enter fullscreen mode Exit fullscreen mode

4) SaChIn DhOnI rOhIt vIrAt-->To get this output

names_list = ['sachin', 'dhoni', 'rohit', 'virat']

for index, name in enumerate(names_list):
    players = []

    if index < 2:

        for i in range(len(name)):
            if i % 2 == 0:
                players.append(name[i].upper())
            else:
                players.append(name[i].lower())
    else:

        for i in range(len(name)):
            if i % 2 != 0:
                players.append(name[i].upper())
            else:
                players.append(name[i].lower())

    print(''.join(players), end=' ')
Enter fullscreen mode Exit fullscreen mode

Output:

SaChIn DhOnI rOhIt vIrAt
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay