30 FIRST STEPS
Ride the rollercoaster You can’t ride –
A sign at the theme park says you must be over 8 years you’re too small!
old and taller than 4 feet 7 inches to ride the
rollercoaster. Mia is 10 years old and 5 feet tall. Let’s use But I’m 100
years old!
the shell to check whether she can go for a ride. Type
the following lines of code to create variables for Mia’s
age and height and assign the correct values to them.
Type the rules for going on the rollercoaster as a
Boolean expression, then hit the enter/return key.
This is a Boolean
>>> age = 10 expression meaning
These two lines >>> height = 1.5 “older than 8 and more
assign values to than 4 ft 7 in tall”.
the variables. >>> (age > 8) and (height > 53 inches)
True
Mia can go on the
rollercoaster!
Branching
Computers often need to make decisions about
which parts of a program to run. This is because
most programs are designed to do different
things in different situations. The route through
the program splits like a path branching off into
side paths, each leading to a different place.
LINGO ▷ School or park?
Imagine you have to decide what route
Condition to walk each day based on the answer
to the question “Is today a weekday?” If
A condition is a Boolean
it’s a weekday, you take the route to
expression (a True-or- school; if it’s not, you take the route to
False comparison) that the park. In Python, the different routes
helps a computer decide through a program lead to different
which route to take blocks of code. A block can be one
when it reaches a branch statement or several, all indented by
in the code. four spaces. The computer uses a test
called a condition to figure out which
blocks it should run next.