0% found this document useful (0 votes)
16 views

Python KS3 Lesson 2 - 1 Variables

The document provides a step-by-step guide for beginners to write their first Python program, which greets the user by name. It explains the use of variables, how to create and manipulate them, and introduces basic concepts like strings and simple calculations. The document emphasizes the importance of debugging and encourages users to experiment with their code.

Uploaded by

myatkhant431
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python KS3 Lesson 2 - 1 Variables

The document provides a step-by-step guide for beginners to write their first Python program, which greets the user by name. It explains the use of variables, how to create and manipulate them, and introduces basic concepts like strings and simple calculations. The document emphasizes the importance of debugging and encourages users to experiment with their code.

Uploaded by

myatkhant431
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

First

steps
22 FIRST STEPS

Your first program Hello Cedric!

Now that you’ve installed Python and IDLE, it’s


time to write your first program in Python.
Follow these steps to create a simple program
that greets the user with a cheery message.

How it works 1 Launch IDLE


A shell window appears when you start IDLE.
The program first displays the message “Hello, Ignore it and click on File in the IDLE menu.
World!” and then asks your name. Once you’ve Choose New File to create an empty editor
window where you can write your program.
typed in your name, it says hello again, but this
time it includes your name in the greeting. The
program uses something called a variable to New File
remember your name. A variable is used in Open
coding to store information.
Open Module

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!')

Say hello, adding


user’s name
Hello, World!
3 Save your file
Before you can run the code, you must save
it. Go to the File menu and choose Save.

End Close

Save

Save As...
YO U R F I R S T P RO G R A M 23

4 Save the file


A pop-up box will appear. Type in a name for your
LINGO
program, such as “helloworld.py”, and click Save. .py files
Python programs usually have
Save As: helloworld.py
a name ending with “.py”,
Tags: which makes them easy to
Type the recognize. When you save a
Where: Documents name of program, Python automatically
your
program
adds “.py” at the end, so you
here. don’t need to type it in.
Cancel Save

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.

7 Add more lines


Go back to the editor window and add two more lines to
print('Hello, World!')
your script. Now the middle line asks for your name and person = input('What’s your name?')
then stores it in a variable. The last line uses your name print('Hello,', person)
to print a new greeting. You can change it to a different
greeting if you prefer—as polite or as rude as you like!
This line asks for the user’s name and
stores it in a variable called “person”.

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.

How to create a variable △Storage box


A variable is like a box with a
A variable needs a name. Think of a name that will remind name label. You can store data
you what’s inside the variable. Then decide what you want in the box and then use the
to store in the variable. This is the variable’s value. Type name to find the data again
when you need to use it.
the name, followed by an equals sign, followed by the
value. We call this “assigning a value” to the variable.
This value will be stored in the variable.

1 Assign a value
In the shell window, type this line of code to
>>> age = 12

create the variable age and assign a value


to it. Use your own age if you want. This is the variable’s name.

2 Print the value


Now type the line of code shown on the right >>> print(age)
into the shell window. Hit the enter/return 12
key to see what happens.
The value of age The print() function prints the value
of the variable between the brackets.

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)

Using numbers Symbol Meaning


Variables can be used to store numbers and do sums. + add
You can use them with symbols to do calculations, – subtract
just like you do in maths. Some of these symbols will * multiply
be familiar, but watch out for the symbols meaning / divide
“multiply” and “divide”—they’re slightly different
from the ones you use in class. Some of the Python math symbols

Create a new variable, x, and give it the value 6.

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

Print the Multiply x by 7 and


value of y. store the result in y.
The result of the calculation
Change the value of x.

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?

The result hasn’t changed;


next we’ll find out why. Update the value of y.

3 Update the value


The value of y needs to be updated to get the >>> x = 10
correct result. Type these lines. Now the code >>> y = x * 7
assigns the new value to y after x has been
>>> print(y)
changed. If you update the value of one
variable in your own programs, always check 70
to see if you need to update any others.
26 FIRST STEPS

Working with strings


Coders use the word “string” for any data
Y 0
made up of a sequence of letters or other P T H N
characters. Words and sentences are
stored as strings. Almost all programs use
strings at some point. Every character that A string is simply a
sequence of characters.
you can type on your keyboard, and even
those you can’t, can be stored in a string. The quote marks show that
the variable contains a string.

1 Strings in variables
Strings can be put into variables. Type this
>>> name = 'Ally Alien'

code into the shell window. It assigns the >>> print(name)


string 'Ally Alien' to the variable name Ally Alien
and then displays it. Strings must always have
quotation marks at the beginning and end.
Hit the enter/return
Remember the
key to print the string.
quote marks.

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

You might also like