CSE 1010 Lecture Notes - Lecture 8: 5,6,7,8
CSE 1010 verified notes
8/29View all
Document Summary
Cse 1010 - introduction to computing for engineers. Example generating a list of 10 random numbers with each number being within the range 0 to 20: Let"s try this first with a while loop: import random nums = [] n = 0 while n<10: num = random. randint(0, 20) nums. append(num) n=n+1 print(nums) Nums = [] says that the list starts out empy. N = 0 is the loop control variable. Num = random. randint(0, 20) says to generate a random number between 0 and 20 and stores it in the num variable. Nums. append(num) says to put this number at the end of the. Nums list (which is empty at the start of this loop) Note: remember, that the values in the output can range anywhere from 0 to 20! Given a list of random numbers, calculate the average of the numbers in that list: