129k views
5 votes
Write a Python program to do the following:

(a) Use the range function to generate this sequence of integers: 5, 9, 13, 17 and 21. Save the numbers in a list. Display the list.
(b) Use a for loop to display each element in a separate line.
(c) Use the range function to generate this sequence of integers: 26, 19, 12 and 5. Save the numbers in a list. Display the list.
(d) Use a for loop to calculate the total of the elements in the second list. Display the total.

User Kien Bui
by
6.3k points

1 Answer

7 votes

Answer:

Step-by-step explanation:

(a) the range function has parameters as range(start, stop, step). Since our sequence starts at 5, stops at 21 prior to 22 and has a step side of 4

L = range(5, 22, 4)

(b)

for e in L:

print(e)

(c)Since our sequence starts at 26, stops at 5 prior to 4 and has a step side of 7 negatively

L2 = range(26, 4, -7)

(d) we can set up a total variable initially equals to 0. The add it to each element of L2 in the loop:

total = 0

for e in L2:

total += e

print(total)

User Ian Marshall
by
6.7k points