for i in range()

Using for loop and range to repeat code
Repeating a print command
for i in range(4):
    print("hello")
This would output to the interpreter as
 Repeating two commands
double = 2
for i in range(6):
    print(double)
    double = double * 2
#Placing 2 inside the variable double
#Making Python loop through commands underneath six times
#Print whatever is inside the variable double
#Multiply double variable by two
This would output as
 
Double is printed as 2 and then multipled by 2
Double is then printed as 4 and then multiplied by 2
Double is then printed as 8 and then multiplied by 2
etc until it has gone through the loop 6 times


Repeating randomly chosen items from two lists

(Code to copy and paste)
import random
for i in range(5):
    name = random.choice(["Herbert","John","Maria","Dave","Rachael"])
    greeting = random.choice(["live long and prosper","live forever","live life to the full"])
    print(name,greeting)

(Code as it looks in Portable Python)
This could output as
See list or random for more information.
Comments