DEV Community

A K I L A N
A K I L A N

Posted on

Python task

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)

Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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

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]

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Top comments (0)