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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️