Chapter 2 · Decide, repeat, reuse

Decisions and loops

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Questions with True or False answers

By the end of this lesson your programs decide and repeat.

Both start with a question whose answer is True or False (lesson 3): Python calls it a condition.

conditionmeanswith a = 7, b = 10
a < bless thanTrue
a > bmore thanFalse
a <= bat mostTrue
a >= bat leastFalse
a == bequalFalse
a != bnot equalTrue
temp = 20
if temp > 25:
    print("Hot")
else
print("Not hot")
score = 72
if score >= 90:
    print("Grade: A")
elif score >= 70:
    print("Grade: B")
else:
    prnt("Grade: C")
x = 7
print(x >= 7 and x < 10)
print(x < 0 or x > 10)
print(not (x > 10))
answer = "no"
if answer == "y" or "yes":
    print("You said yes")
score = 120
if score >= 90:
    print("Grade: A")
elif score >= 70:
    print("Grade: B")
else:
    print("Grade: C")
for i in range(5):
    if i % 2 == 0:
        print(i, "is even")
    else:
        print(i, "is odd")
total = 0
# add every number from 1 to 100 to total
print("Sum:", total)
n = 1
while n < 100:
    print(n)
    # something is missing here
print("Past 100:", n)
n = 6
print(n)
# while n is not 1:
#   halve n if it is even, else make it 3 * n + 1
#   then print n
for n in range(1, 16):
    print(n)
# 1. loop i from 1 to 7: n = 7 * i
# 2. print n, with a * when n is even or over 40
# 3. print Marked: and how many got a *
print("Liftoff!")
Terminal
Step 1 of 11Try it: fix the block

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)