Python KS3 Lesson 2 - 1 Variables
Python KS3 Lesson 2 - 1 Variables
steps
22 FIRST STEPS
Recent Files
▷ Hello World flowchart
Programmers use diagrams Start Class Browser
called flowcharts to plan their
Path Browser
programs and to show how they
work. Each step is shown in a
box, with an arrow leading to
the next step. Sometimes the Say hello
steps are questions and have 2 Type the first line
In the editor window, type this line of text.
more than one arrow leading The word “print” is a Python instruction that
onward, depending on the tells the computer to display something on
answer to the question. Ask user to type the screen, such as the words “Hello, World!”
their name
print('Hello, World!')
End Close
Save
Save As...
YO U R F I R S T P RO G R A M 23
5 Check it works
Now run the first line of the program
Python Shell >>>
to see if it works. Open the Run menu Check Module Hello, World!
and choose Run Module. You should
>>>
see the message “Hello, World!” in the Run Module
shell window.
The message will
appear in the shell.
6 Fix mistakes
If the code isn’t working, stay calm!
EXPERT TIPS
Every programmer makes mistakes, Keyboard shortcut
and finding these “bugs” is vital if you
want to become an expert at coding. A handy shortcut to run a program from
Go back and check your code for the editor window is simply to press F5 on
typing errors. Did you include the your keyboard. This is a lot quicker than
brackets? Did you spell the word selecting “Run” and then “Run Module”.
“print” correctly? Fix any mistakes,
then try running the code again.
8 Final task
Run the code again to check it. When you type in your Hello, World!
User’s
name and hit the enter/return key, the shell should show What's your name?Josh name
a personalized message. Congratulations on completing
Hello, Josh
your first Python program! You’ve taken your first steps
towards becoming a powerful programmer.
24 FIRST STEPS
Variables
If you want to write useful code, you’ll need to be able
to store and label pieces of information. That’s what
variables do. Variables are great for all sorts of things—
from tracking your score in a game to performing
calculations and holding lists of items.
1 Assign a value
In the shell window, type this line of code to
>>> age = 12
EXPERT TIPS
Naming variables
Choosing good names for your variables Dos and don’ts
will make your program easier to • Start the variable’s name with a letter.
understand. For example, a variable • Any letter or number can be used in the name.
tracking a player’s lives in a game could • Symbols such as -, /, #, or @ aren’t allowed.
be called lives_remaining, rather
• Spaces can’t be used.
than just lives or lr. Variable names
can contain letters, numbers, and • An underscore ( _ ) can be used instead of a space.
underscores, but they should begin • Uppercase (capitals) and lowercase letters are different.
with a letter. Follow the rules shown Python will treat “Score” and “score” as two different variables.
here and you won’t go wrong. • Avoid words Python uses as commands, such as “print”.
VA R I A B L E S 25
LINGO
Integers and floats
In coding, whole numbers are called “integers”,
while numbers with a decimal point in them
are known as “floats”. Programs usually count
things using integers. Floats are more often
used for measurements. 0.5 sheep
1 sheep (an integer) (a float)
1 A simple calculation
Type this code in a shell window. It uses >>> x = 6
numbers stored in two variables, named x >>> y = x * 7
and y, to carry out a simple multiplication.
>>> print(y)
Hit the enter/return key to get the answer.
42
2 Change a value
To change the value of a variable, you just >>> x = 10
assign a new value to it. In your code, change >>> print(y)
the value of x to 10 and run the calculation
42
again. What do you expect the result to be?
1 Strings in variables
Strings can be put into variables. Type this
>>> name = 'Ally Alien'
2 Combining strings
Variables become really useful when you
>>> name = 'Ally Alien'
combine them to make new variables. If you >>> greeting = 'Welcome to Earth, '
add two strings together, you can store the >>> message = greeting + name
combination in a new variable. Try this out.
>>> print(message)
Welcome to Earth, Ally Alien
EXPERT TIPS
Length of a string The + symbol
joins one string
The quote marks to another.
You can use a handy trick, len(), to aren’t shown when
count the number of characters in a string you print a string.
(including the spaces). The command
len() is an example of what coders call a
function. (You’ll use lots of functions in this
book.) To find out how many characters He doesn’t have
there are in 'Welcome to Earth, Ally a clue!
Alien', type the line below into the Take me to your
leader...
shell once you’ve created the string, then
hit enter/return.
>>> len(message)
28
The number of
characters counted