List :
- A built in python sequence
- Despite its name it is more akin to an array in other language
Group of Individual object into a single entity
we can have duplicates object / elements in list
we can have different datatypes in list
List are mutable
grocery_list = ['soap', 12, True, 'rice', 'veggies',4.5]
print(grocery_list)
output
['soap', 12, True, 'rice', 'veggies', 4.5]
```grocery_list = ['soap', 12, True, 'rice', 'veggies',4.5]
grocery_list[0] = 'shampoo'
print(grocery_list)
**Output**
['shampoo', 12, True, 'rice', 'veggies', 4.5]
List Creation: []
`append() function: adding at the end. `
```python
l=[]
l.append('abcd')
l.append(1234)
l.append(True)
l.append(5.4)
print(l)
output:
['abcd', 1234, True, 5.4]
How to add element at specific position(index) ---> use insert(index,value)
l = ['abcd',1234,True, 5.4]
# 0 1 2 3
l[-1] = 'pqrs'
print(l)
Output:
['abcd',1234,True, 5.4,'pqrs']
Task
q = [90,87,65,67,89]
h = [96,97,95,69,99]
a = [99,98,100,76,49]
marks = [q,h,a]
Task:
1) Print q, h, a marks in separate lines
2) Calculate Total and Percentage of q, h and a
3) Calculate Average of Tamil (first subject)
4) Calculate highest total among these three
1) Print q, h, a marks in separate lines
for inner_list in marks:
for elem in inner_list:
print(elem, end=' ')
print()
output :
90 87 65 67 89
96 97 95 69 99
99 98 100 76 49
2) Calculate Total and Percentage of q, h and a
for inner_list in marks:
total = 0
for elem in inner_list:
total = total + elem
print('total is ' ,total)
print('percentage is ',(total/len(inner_list)))
print()
Output:
398
79.6
456
91.2
422
84.4
3) Calculate Average of Tamil (first subject)
print('Task - 3')
total = 0
avg = 1
for inner_list in marks:
total+=inner_list[0]
avg *= total / 3
print('Total of Tamilsubject :',total)
print('Avg of the Tamilsubject :',avg)
Output:
285
95.0
4) Calculate highest total among these three
```print('Task - 4')
highest = 0
for subject in marks:
total = 0
for mark in subject:
total+=mark
if highest < total :
highest = total
else:
print('Highest Total is ',highest)
output:
Highest Total is 456
Top comments (0)