Python Programming Task 1 - Answer

Hello!

Below is the answers to last week's task. To see the task, please visit Here.


The code is seen below:
print("Answers to online task by Hampo, JohnPaul A.C.")
print()
print("Week 1 Answers")
print("Answer to last week's task No.1\
      Hello Dear! Here is a little task for us in python.\
\
1] Write a program to find the sum of the data structure below\
[[1,2,3],[4,5,6,7],[8,9]]\
\
2] Write a program to convert the tuple to a dictionary where\
name is the key and age is the value. Example: x = ('John', 23, 'm'), ('Peter', 43, 'm')\
then the output will be y = {'John': 23, 'Peter': 43}.")
print()
print("Solution")
print("Task Number 1")
a = [[1,2,3],[4,5,6,7],[8,9]]
print("Using a for loop")
b = []
for i in range(len(a)):
    for j in range(len(a[i])):
        b.append(a[i][j])
print("The sum of the list of lists = ", sum(b))
print()
print("Alternatively: using list comprehension")
c =[sum(a[i]) for i in range(len(a))]
print("The sum of the list of lists = ", sum(c))
print()
print("Or")
print()
d = sum([sum(a[i]) for i in range(len(a))])
print("The sum of the list of lists = ", d)
print()
print("Solution")
print("Task Number 2")
x = ('John', 23, 'm'), ('Peter', 43, 'm')
print("Using a for loop")
y = dict()
for name, age, sex in x:
    y[name] = age
print("The new dictioary is:\n", y)
print()
print("Alternatively: using dictionary comprehension")
print()
y2 = {name:age for name, age, sex in x}
print("The new dictioary is:\n", y2)


The screen above should be the answer you get when you run the code.

Thanks.

Please subscribe to this blog and the YouTube channel. Also share, like and comment!

Comments

Popular posts from this blog

Nigerian Air Force DSSC 2018 - Information

Facebook Job - Secrets and Tips (Applicants)