0% found this document useful (0 votes)
182 views4 pages

Python Random Module Practice Problems

The document contains a series of Python coding problems focused on the Random module, providing examples of possible outputs and identifying correct and incorrect options. It discusses outcomes based on random selections and iterations over lists, along with specifying maximum and minimum values for certain variables. Each problem is followed by an answer indicating the correct or incorrect outcomes.

Uploaded by

iamyashvi027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views4 pages

Python Random Module Practice Problems

The document contains a series of Python coding problems focused on the Random module, providing examples of possible outputs and identifying correct and incorrect options. It discusses outcomes based on random selections and iterations over lists, along with specifying maximum and minimum values for certain variables. Each problem is followed by an answer indicating the correct or incorrect outcomes.

Uploaded by

iamyashvi027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practice short problems on Python Random

Module
PYTHON MODULES

Python Random Module Examples:


[Link] are the possible outcome(s) from the following code?
import random
PICK=[Link] (1,3)
CITY= ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY:
for J in range (0, PICK):
print (I, end = "")

(i) (ii) (iii) (iv)

DELHIDELHI DELHI DELHI DELHI


MUMBAIMUMBAI DELHIMUMBAI MUMBAI MUMBAIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI CHENNAI KOLKATAKOLKATAKOLKATA
KOLKATAKOLKATA KOLKATA
print ()

Ans. Option (i) and (iii) are possible

[Link] are the possible outcome(s) from the following code? Also, specify the maximum and
minimum values that can be assigned to variable SEL.

import random
SEL=random. randint (1, 3)
ANIMAL = ["DEER", "Monkey", "COW", "Kangaroo"]
for A in ANIMAL:
for AA in range (0, SEL):
print (A, end ="")
print ()

(i) (ii) (iii) (iv)

DEERDEER DEER DEER DEER


MONKEYMONKEY DELHIMONKEY MONKEY MONKEYMONKEY
COWCOW DELHIMONKEYCOW COW KANGAROOKANGAROOKANGAROO
(i) (ii) (iii) (iv)

KANGAROOKANGAROO KANGAROO
Ans. Maximum value of SEL is 3 and minimum is 1
(iv) is the correct option.

[Link] are the possible outcome(s) executed from the following code ?

import random
def main():
p = "MY PROGRAM"
i = 0
while p[i] != "R":
l = [Link](0,3) + 5
print (p[l],end ="_")
i += 1
main()

(i) (ii) (iii) (iv)


M_M_Y_P R_G_A_G G_G_R_O O_G_G_A
Ans. (i) will not be printed

[Link] is the incorrect outcome executed from the following code? Also, specify the maximum
and minimum values that can be assigned to variable
x = 3
N = [Link] (1, x)
for i in range (N):
print (i, "#", i + 1)

(i) (ii) (iii) (iv)


0#1 0#1 0#1 0#1
1#2 1#2 1#2
2#3 2#3
3#4
Ans. (iii) will not be printed
The maximum value of N is 3 and minimum is 1

[Link] is the incorrect outcome executed from the following code?


import random
movie_list = ['The Jungle Book', 'Finding Nemo', 'Harry Potter', 'The
Incredibles', 'Frozen']

movie1 = [Link](movie_list)
movie2 = [Link](movie_list)
print ( movie1 ," or " ,movie2)
1. Finding Nemo or Harry Potter
2. Harry Potter or Toy Story
3. Frozen or The Jungle Book
4. Toy Story or Spider Man
Ans. (ii) and (iv) are incorrect option.

[Link] are the possible outcome(s) executed from the following code ?

import random
color_list = ["Pink", "Red", "Sky blue", "Yellow", "Green"]
[Link](color_list)
print ( color_list )

1. [‘Sky blue’, ‘Green’, ‘Yellow’, ‘Pink’, ‘Red’]


2. [‘Blue’, ‘Green’, ‘Pink’, ’Brown’, ‘Red’, ‘Black’]
3. [Yellow, Sky blue, Green, Pink, Red]
4. (‘Yellow’, ‘Red’, ‘Sky blue’, ‘Green’, ‘Pink’)
Ans. (i) , (iv) is a correct output

[Link] is the incorrect outcome(s) executed from the following code?


import random
list=[14, 62, 30, 57, 91]
str= ('Simply')
print ( [Link](list),"and" , [Link](str) )

1. 57 and i
2. 62 and S
3. 20 and p
4. 30 and M
Ans. (iii) and (iv) are incorrect options

[Link] are the possible outcome(s) executed from the following code ? Also specify the maximum
and minimum values that can be assigned to variable PICKER.

import random
N=3
PICKER = [Link] (1, N)
COLOR = ["BLUE", "PINK", "GREEN", "RED"]
for I in COLOR :
for J in range (0, PICKER):
print (I, end = " ")
print ()

(i) (ii) (iii) (iv)

BLUE BLUE PINK BLUEBLUE


PINK BLUEPINK PINKGREEN PINKPINK
GREEN BLUEPINKGREEN GREENRED GREENGREEN
(i) (ii) (iii) (iv)

RED BLUEPINKGREENRED GREENGREEN


Ans. Option (i) and (iv) are possible
The maximum value of PICKER is 3 and minimum is 1

[Link] are the possible outcome(s) for “Jumble String”, when following code will be executed?

import random
stringN = "SimplyCoding"
char_list = list(stringN)
[Link](char_list)
stringN = ''.join(char_list)
print ( stringN )

1. ( CmodlnpgiSyi )
2. miSiypgoCdln
3. ‘lCSgpimiondy’
4. kpCyodigntS
Ans. (ii) will be printed

[Link] is the correct outcome executed from the following code?


import random
n1= [Link](1, 10, 2)
n2= [Link](1, 10, 3)
print (n1,"and",n2)

1. 2 and 7
2. 3 and 4
3. 8 and 10
4. 8 and 9
Ans. (ii) is correct

Common questions

Powered by AI

The inconsistency arises when options not included in the original list are shown as selection outcomes, such as 'Harry Potter or Toy Story'. This indicates that outcomes (ii) and (iv) are incorrect since 'Toy Story' and 'Spider Man' are absent from the existing movie list, demonstrating how logical inconsistencies can occur if list elements are incorrectly stated .

random.shuffle rearranges items in a list randomly, changing the order but not the elements themselves. This function is useful in scenarios where a random ordering of elements is required, preserving the original dataset's count and composition. The examples demonstrate its usage by showing lists of colors shuffled into different orders, proving its effect on sequence but not content .

The code uses random.randint(1,3) to determine the number of times (1 to 3) each city name is printed sequentially. Possible outputs depend on the random value chosen, with some cities possibly repeating. As given instances show, outputs like 'DELHIDELHIDELHI MUMBAIMUMBAI' and 'DELHIMUMBAI MUMBAI MUMBAI' can occur, illustrating different PICK values causing varied repetitions .

Option (iii) is marked impossible since the range determined by random.randint and list logic cannot produce this specific sequence under the constraints given (e.g., the union of number pairs beyond '2#3'). The flawed expectation contradicts the maximum reach of original values, resulting in unfeasible pairings like '3#4', highlighting incorrect assertions in conceptualizing bounds within nested outputs .

random.randrange allows generating numbers within a specified range but with a predefined step, unlike random.randint which picks any integer within a full range, or random.choice which selects non-numeric items. This constraint (e.g., choosing only odd numbers) limits outcomes to specific sequences, such as in 'n1=7 and n2=4' (evenly stepping choices).

Predicting outcome validity involves understanding random function limits and exact list definitions. Challenges include maintaining accurate number range interpretations (as with randint/range settings) and correctly identifying potential list elements (in selection functions). Misalignments between expected and plausible results, exemplified by absent or logically inconsistent items e.g., invalid movie picks, showcase difficulties in ensuring all operations accurately reflect intended use .

random.choice selects a random element from a non-empty sequence like a list, whereas random.randint selects an integer from a specified numeric range, inclusive of both endpoints. For instance, in a list of movies, random.choice directly selects a movie title, while random.randint determines numeric values as seen in index picking .

random.shuffle disrupts character order within a list, ensuring original characters persist through reshuffling while their positions are rearranged. Applied to string data, this function pseudorandomly modifies sequence without element loss, maintaining consistent character count with varied alignments. Jumbled string outcomes ('miSiypgoCdln') reflect such integrity-preserved permutations of 'SimplyCoding' .

Max-min bounds largely rely on specified range inputs and encompassing definitions within functions like random.randint or random.choice, incorporating inclusive endpoint rules (randint) and available elements (choice). Factors like list lengths, explicit value settings (e.g., 'x = 3'), or overridden logic define achievable outcomes, bounding valid integers or elements to structured framework (e.g., max-min SEL as 1-3).

Nested loops, as seen in these Python examples, iterate over two dimensions, combining a list of elements with a random count defined by random.randint. Each outer loop iteration (e.g., a city or color) triggers multiple executions of an inner block (controlled by random value), resulting in varied repetitions and patterns depending on inner loop extent. Thus, outputs vary significantly in repetition and structure within city/color lists .

You might also like