Ask Question

Write a Python program to do the following:

(a) Use a for loop and a random integer generator to generate 10 random integers in 1 through 15. Store the random integers in a tuple. Display the tuple. [Hint: you may want to store the random integers in a list first and then convert the list to a tuple].

(b) Create a new tuple. Copy the first three elements of the tuple in part (a) to this tuple. Display this tuple.

(c) Create a new tuple. Copy the last three elements of the tuple in part (a) to this tuple. Display this tuple.

(d) Concatenate the two tuples in part (b) and part (c). Display the concatenated tuple.

(e) Sort the concatenated tuple. Display the sorted tuple. The following is an example. There is no user input in this program.

+4
Answers (1)
  1. 2 March, 20:52
    0
    (b) Tb = Ta[:3]

    (c) Tc = Ta[-3:]

    (d) Td = Tb + Tc

    Explanation:

    In order to generate random number in python, we need to first import random () function from random module

    This function will generate a float number between 0 and 1, to make it between 1 and 15, we add 1 to the multiplication between the random number and the range differences

    (a)

    from random import random ()

    L = []

    for i in range (10):

    L. append (1 + round (random () * 14))

    Ta = tuple (L)

    (b) To copy the first 3 elements of tuple Ta, we simply go from : to 3

    Tb = Ta[:3]

    (c) To copy the last 3 elements of tuple Ta, we simply go from - 3 to:

    Tc = Ta[-3:]

    (d) To concatenate the 2 tuples, we simply add them up

    Td = Tb + Tc
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a Python program to do the following: (a) Use a for loop and a random integer generator to generate 10 random integers in 1 through ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers