Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Y8 Python notes 3

Variables
Variables are used to store data in a program.
A variable is a named location in the computer’s memory which stores data of a
particular type. Data is entered by the user whilst the program is running.
Variables can hold data of different types. In order to store the data you must
decide on a name and a data type for the variable first. For example, to store the
player’s age you will need a variable which can hold a whole number (also known
as an integer)

To store the player’s name you will need a variable which can hold letters – this is
called a string.

The variable names have been chosen as playerage and playername but they
could have been called anything we wanted. Variables can hold different data
types. The table shows some examples of each data type.

Page 1 of 6
Input Statement
To capture data from the user the input function.
The input function captures user input as a string data type.
This contains numbers, letters and symbols. For example ‘Robot’,
‘Password123’ and ‘WWW777’ are all examples of string data type.
Input is a built-in function in Python – it is the part of the Python
language

The input statement prints a question onto the screen and captures
the user’s answer to the question in a variable called
playername…………………………….
playername = input (“What is your name?”)
print(“Welcome “+ playername)
playername is then used in the next line of code and printed onto
the screen as part of a welcome message. Can you guess what will be
printed onto the screen?

Assignment Statement
The value of a variable can also be set by using an assignment
statement. This gives the variable a value.

Page 2 of 6
An assignment statement includes an equals symbol and takes the
form:
name of variable = value
For example:
playerlives = 3
total = score1 + score2
playername = ”George”

Data Types Examples

Page 3 of 6
Constants

Example of constants:
PI = 3.14

GRAVITY = 9.8

Example Programs:

Page 4 of 6
Python Worksheet 3
Program 1
Write a program to use input function and ask two questions:
1. Person’s name
2. Favorite color
Produce the following output:

Program 2
Write a program that will ask the year when you were born and then it
will calculate and display your age.
Produce the following output:

Program 3
Write a program to input integer marks for three subjects out of 50,
calculate and print the total and average of three subject marks.

Page 5 of 6
Produce the following output:

Page 6 of 6

You might also like