Control Flow

Conditional Statements

if

name = 'Arthur'
if name == 'Arthur':
    print("I didn't know we 'ad a king!")

NOTE

  • Indentation creates a block

  • A colon, :, is required at the beginning of a block

  • Use 4 spaces per indentation level (from the official style guide)

elif

name = 'Galahad
if name == 'Arthur':
    print("I didn't know we 'ad a king!")
elif name == 'Galahad':
    print("Sir Galahad! You would not be so ungallant as to refuse our hospitality.")

else

name = 'Galahad
if name == 'Arthur':
    print("I didn't know we 'ad a king!")
elif name == 'Galahad':
    print("Sir Galahad! You would not be so ungallant as to refuse our hospitality.")
else:
    print("Who are you?")

Loops

while loop

for loop

range range(start, stop, step)

The range function returns a sequence of numbers that starts from start (0 by default) and stops before stop, in increments of step (1 by default).

break & continue You can stop a loop with break or skip to the next iteration with continue

Last updated

Was this helpful?