CMIS 102 for loops --for loop preferred over while loop when know at start of loop execution how many iterations are to be done. "definite iteration". vs. --while loop preferred over for when don't know at start of loop execution how many iterations are to be done. "indefinite iteration". Typical use of for for variable in range(number): statement(s) of body will loop 'number' times, with 'variable' going from 0 to number-1. 'Number' is usually an int variable or an int expression. ---------------------------------- #user tells how many numbers to input and sum n = eval(input("Enter number of numbers: ")) sum = 0 for count in range(n): #loop n times, the most common loop in the universe num = eval(input("Enter a number: ")) sum += num print("Sum is", sum) #Do: display the value of count each iteration #range(start,end) loops from start to end-1 n = eval(input("Enter number of numbers: ")) sum = 0 for count in range(1,n+1): #loop n times, the most common loop in the universe num = eval(input("Enter number #"+str(count)+": ")) sum += num print("Sum is", sum) ------------------------------------ #sum of the first N integers n = eval(input("Enter n: ")) sum = 0 for i in range(1,n+1): #'i' index, iteration variable sum += i print(sum) #display the first N perfect squares. n = eval(input("Enter n: ")) for i in range(1,n+1): print(i, ":", i*i) #input numbers from user. Display that number of *'s, one per line. #Negative number indicates end. num = eval(input("Enter a number: ")) #first number from user while num >= 0: #until a negative number is entered for stars in range(num): print("*") num = eval(input("Enter a number: ")) -------------------------------------------------- #Fahrenheit to Celsius table. range has 3rd argument: step print("F C") for tempF in range(-40,130,10): #by 10s print(tempF, round(5.0/9.0*(tempF-32))) #round to integer Counting down loop: step argument negative print("F C") for tempF in range(120,-50,-10): #by -10s print(tempF, round(5.0/9.0*(tempF-32))) #round to integer #sum of the first N Harmonic numbers (1/1 + 1/2 + 1/3 + 1/4 + ... + 1/N) n = eval(input("Enter N: ")) sum = 0 for i in range(1,n+1): sum += 1 / i print(sum) #diverges (goes to infinity) #sum of the reciprocals of powers of 2 (1/2^0 + 1/2^1 + 1/2^2 + 1/2^3 + ... + 1/2^N) # (1/1 + 1/2 + 1/4 + 1/8 + 1/16 #Achilles/arrow paradox n = eval(input("Enter N: ")) sum = 0 for i in range(0,n+1): sum += 1 / 2**i print(sum) #converges to what? ******************************************* Can loop over the characters of a string: s = "yabba dabba doo" for c in s: print(c) #count categories of characters s = "yab@#$%^ba daBBa D12oo" uppers = lowers = digits = others = 0 for c in s: if c.isupper(): #str.isupper() true if is/are uppercase letter(s) uppers += 1 elif c.islower(): lowers += 1 elif c.isdigit(): digits += 1 else: others += 1 print("#uppers:",uppers," #lowers:",lowers," #digits:",digits," #others:",others) *************************************************************** if the loop control variable is not used in the body of the loop, can use _ instead for _ in range(n): num = eval(input("Enter a number: ")) sum += num