List:
[ ] --> Symbol
-->Collection of Data
-->Collection of Heteregeneous 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=' ')
Output:
Guru Prasanna B.Com 23 True 5.6
Guru Prasanna B.Com 23 True 5.6
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
Output:
0 Guru Prasanna
1 B.Com
2 23
3 True
4 5.6
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)
Output:
['Guru Prasanna', 'B.Com', 23, True, 5.6]
['Guru Prasanna', 'M.Com', 23, True, 5.6]
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)
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]
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)
Output:
[10, 20, 50, 60]
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)
Output:
427
percentage: 85.4
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)
Output:
98
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)
Output:
65
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)
Output:
abcd
pqrs
xyz
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())
Output:
ABCD
PQRS
XYZ
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])
Output:
AB
PQ
XY
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=' ')
Output:
dhoni rohit virat
rohit virat
sachin dhoni
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 == 1:
players.append(name[i].upper())
else:
players.append(name[i].lower())
print(''.join(players), end=' ')
Output:
SaChIn DhOnI rOhIt vIrAt
Top comments (0)