0% found this document useful (0 votes)
21 views8 pages

Variables

Uploaded by

reneezhou3621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Variables

Uploaded by

reneezhou3621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

VARIABLES

BOOK PAGES 6-7


VARIABLE
When the computer is running it stores data values in its memory. A computer’s memory is made up
of millions of different storage areas called locations. You can set aside a memory location to store
a value. You give the memory location a name. A named memory location is a variable.
The electrical signals of computer memory can change. That means you can change the value stored
in the variable.
When you close the program, or turn off your computer, the electrical signals will stop. That means
the computer will lose the value stored in the variable when the program stops.
It would be best if you chose an appropriate name for each variable. The name of this variable must
be meaningful; it must show the purpose of this variable.
TIPS FOR NAMING A VARIABLE

 The name must not include spaces.


 You can use only letters, numbers and the underscore character (_).
 The name must start with a letter of the alphabet.
 It is important to choose identifiers carefully. Well-chosen identifiers will help other programmers to read and
understand your code. Well-chosen identifiers make it easier for you to write code without making mistakes.
Choose a name that reminds you of what value the variable stores. This will make your program easier to
write and easier for other people to understand.
 Many Python programmers use only lower-case letters for variable names. In the exams many variable names
start with a capital (upper-case) letter to make them stand out in the text.
ASSIGNMENT
Assignment is the process of storing a value in a variable.
To assign a value to a variable, you use the following pattern:
 the name of the variable
 an equals sign
 a value.
For instance:
Age = 15
CodeName = ‘James Bond’
DECLARING A VARIABLE

In some programming languages, you must declare a variable before you can use it. With the term
declare, we have to define the variable’s name and data type.
DECLARE name AS string
In Python, we don’t have to declare the variables.
OUTPUT A VARIABLE
We already learned how to output a value. Let’s see now how to output a variable. Run the following
code
Age = 15
CodeName = "James Bond"

print(Age)
print(CodeName)

print('Your code name is: ' + Codename)


print("You age is:", Age)
TASK 1 – BOOK (page 7)
1. Give the command to assign the value 19.99 to the variable TicketCost.
2. Give the command to output the value stored in the variable TicketCost.
3. Here are four variable names. For each one say whether it is a good or bad variable name and say why.
a. Character Name
b. firstName
c. 5Star
d. Myvariable
e. Star*Ratin
CODING TASK – BOOK (page 7)

Open the computer game program you started writing in the previous lesson.
1. Create a variable called Score.
2. Assign the value 0 to this variable.
3. Add a command to print out the variable.

You might also like