Python Programming Task Week 2
Hello Folks!
We have been on python lessons (datatype). We have discussed the following datatypes so far; integer (int) - see video, string (str) - see video and list which is also called array (one dimensional) - see video.
Below are tasks or exercises for us this week based on list:
- Data = [2,4,2,1,4,5,6,1,11]. Multiply the elements in the list using
- for loop
- list comprehension
- map function
- Data = [5,4,2,1,2,6,8,9,10,11,13,17]. Sort the list in a way that all the odd numbers comes before the even numbers.
- Data = [1,2,3,[4,6,9],2,[1,6]] and Data2 = [1,2,3,4,5,6,7,8,9]. Find the progressive sum of the lists.
Please kindly solve and drop your solutions as a comment on this blog or on our YouTube channel. Remember, there is always a prize attached to our task.
To Win:
- Drop your answer as comment.
- Like this post and any other four on the blog.
- Like three videos on our YouTube channel.
- Subscribe to our blog and also our channel.
- Share our blog and channel to three social media sites with the hashtag (#hamplustech).
Thanks.
ReplyDelete#for loop
data = [2,4,2,1,4,5,6,1,11]
mult = 1
for x in data:
mult *= x
print(mult)
#list comprehension
a = range(10)
data = [2,4,2,1,4,5,6,1,11]
data = [str(x) for x in data]
mult = ["*".join(data)]
print(eval(*mult))
#map function
data = [2,4,2,1,4,5,6,1,11]
v=1
def x(a):
global v
v*=a
return v
lst = list(map(x,data))
print('progressive multiplied list', '==>')
print(lst, '\n')
print('Final multiplied ans', lst[8])
#2. sort
data = [5,4,2,1,2,6,8,9,10,11,13,17]
even = []
odd =[]
for x in data:
if x%2==0:
even.append(x)
else:
odd.append(x)
even.sort()
odd.sort()
sort = odd + even
print(sort)
#3.progressive sum
from itertools import accumulate,chain
data = [1,2,3,[4,6,9],2,[1,6]]
for x in data:
if type(x)== int:
data[0:3] = [[1,2,3]]
data[2] = [2]
break
print(data)
print(list(accumulate(chain(*data))))
data = [1,2,3,4,5,6,7,8,9]
print(list(accumulate(data)))
Thanks for the prize
ReplyDelete