Question

Python: Could you please say step by step how the output is coming? I understand about...

Python:

Could you please say step by step how the output is coming? I understand about importing and getting random number but how the inner loop is working?

Code:

import random

li1 = [[random.randrange(5) for i in range(3)] for j in range(4)]
print("First output : {}".format(li1))

ncol = 4
nrow = 5
li2 = [ [random.randrange(2) for i in range(ncol) ] for j in range(nrow) ]
print("Second output : {}".format(li2))

Output:

First output : [[3, 2, 4], [2, 3, 2], [4, 2, 0], [4, 2, 4]]
Second output : [[0, 1, 1, 0], [1, 1, 1, 0], [1, 0, 1, 1], [0, 1, 1, 0], [1, 1, 1, 0]]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

li1 = [[random.randrange(5) for i in range(3)] for j in range(4)]
inner loop:
[random.randrange(5) for i in range(3)] //it generates a list of 3 random numbers in range 0-4 // for example : [0,4,2]
now let x = [random.randrange(5) for i in range(3)]
outer loop:
[[random.randrange(5) for i in range(3)] for j in range(4)]
means
[x for j in range(4)] //it generates a list of 4 x's // here x is a list of 3 random numbers in range 0-4
//so,it generates a 2d 4x3 list //4 lists of list , where each list have 3 random numers in range 0-4

Add a comment
Know the answer?
Add Answer to:
Python: Could you please say step by step how the output is coming? I understand about...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python: how would I format the grid below so that it is aligned correctly? as you...

    python: how would I format the grid below so that it is aligned correctly? as you see, I tried to center it but it is still not correct. import random row=random.randint(1,10) col=random.randint(1,10) for r in range(row): for c in range(col): if r %2==0 and c %2==0: print(format('a','^3'),end=' ') elif r %2!=0 and c %2!=0: print(format("/x\\",'^3'),end=' ') elif c%2!=0: print(format('-','^3'),end='')    else: print(format('s','^3'),end=' ')    print() #go to next line current Output is: (example if random function choses col=4 and row=3)...

  • PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting...

    PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting and selection sorting. My program only compares insertion and selection once. It is supposed to compare increasing, decreasing and an array of random values for each at 5 different lengths (without looping). Please help me. I have included my code below. import time import random def insertion_sort(arr):     for i in range(1, len(arr)):         key = arr[i]         j = i - 1         while j >= 0...

  • In Python: What is the output of the following code? for i in range(5): if i...

    In Python: What is the output of the following code? for i in range(5): if i == 2: continue print(i) Question 22 options: 0 1 2 4 0 1 3 4 0 1 3 0 1 4

  • python beginner question! Hello, i am trying to allign the two variable below next to each...

    python beginner question! Hello, i am trying to allign the two variable below next to each other using a for loop, like this table below: 1 3 2 7 3 1 4 2 5 0 heres the code I use: balls = [1,2,3,4,5] count = [3,7,1,2,0] for j in numbers: print("{:}".format(j)) for i in count: print('{:10}'.format(i)) but the output looks like this: 1 2 3 4 5 3 7 1 2 0 i need my output to look like the...

  • Select the Python math module function that give you the Euclidean norm, square root of x*x...

    Select the Python math module function that give you the Euclidean norm, square root of x*x + y*y. This is the length of the vector from the origin to point (x, y). sin hypot cos sqrt radians What would be the value printed from the code below? import math print(math.ceil(math.e)) 7 2.718281 3.141592 2 3 What would be the value printed from the code below? import math print(math.floor(math.pi)) 2.718281 3.141592 2 3 6 ex is e raised to the power...

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • Using a sort method and my previous code (put inside of addHours method if possible), how...

    Using a sort method and my previous code (put inside of addHours method if possible), how would I get the list of each employee's total hours to display in order of least amount of hours to greatest. An explanation for each step would also be great. import random #Create function addHours def addHours(lst): #Display added hours of employees print("\nEmployee# Weekly Hours") print("----------------------------") print(" 1 ",sum(lst[0])) print(" 2 ",sum(lst[1])) print(" 3 ",sum(lst[2])) #Create main function def main(): #Create first empty list...

  • How to remove all occurrences of 2 from list2. Then display the list. Python import random...

    How to remove all occurrences of 2 from list2. Then display the list. Python import random list1 = [] for i in range (10): list1.append(random.randint(1,4)) print('List 1:') print(list1) list2 = [] list2.append(list1[5:]) print('List 2:') print(list2) list2.remove ******** (This is the part I am having trouble on!!) print('List 2 with all 2s removed:') print(list2) Sample output: List 1: [4, 1, 1, 3, 3, 3, 2, 4, 2, 4] List2: [3, 2, 4, 2, 4] List 2 with all 2s removed: [3,...

  • Need in Python! Consider the following program and expected output. Rewrite it using a for loop...

    Need in Python! Consider the following program and expected output. Rewrite it using a for loop and a conditional break statement such that it has the same output.   HINT: Stay with i as the main loop counter and let j control the break behavior.   i = 0 while i < 10 and j < 10 : j = i * 2 print(i, j) i += 1 # Output below, for reference - not part of the program! 0 0 1...

  • Need in Python! Consider the following program and expected output. Rewrite it using a for loop...

    Need in Python! Consider the following program and expected output. Rewrite it using a for loop and a conditional break statement such that it has the same output.   HINT: Stay with i as the main loop counter and let j control the break behavior.   i = 0 while i < 10 and j < 10 : j = i * 2 print(i, j) i += 1 # Output below, for reference - not part of the program! 0 0 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT