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

Constants & Variables

Constants are values that never change, while variables can be changed. A computer's memory can be thought of as drawers in a storage cabinet, with each drawer having an address. Variables act as labels for these drawers, allowing a program to keep track of what is in each drawer without needing to know the specific memory addresses. Constants are also stored in memory like variables but cannot be changed, and their values are fixed when the program is written.

Uploaded by

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

Constants & Variables

Constants are values that never change, while variables can be changed. A computer's memory can be thought of as drawers in a storage cabinet, with each drawer having an address. Variables act as labels for these drawers, allowing a program to keep track of what is in each drawer without needing to know the specific memory addresses. Constants are also stored in memory like variables but cannot be changed, and their values are fixed when the program is written.

Uploaded by

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

Constants & Variables

What are they?


Constant: a value that never changes
Variable: a value that can be changed
To get an idea of how a computer works with values,
we need to know a bit about Memory...

Constants & Variables


Memory
We can imagine a computer's memory like a kind of
electronic storage cabinet:-

Constants & Variables


Memory Addresses
Each drawer has a number, like houses on a street

Allows the computer to access data like DHL does with parcels
Lets the computer keep track of where, not what
Too fiddly & complicated to be useful to humans

Constants & Variables


Variables

Like labels on the drawers


Lets us (our program) keep track of what not where
We don't need to know anything about memory addresses!

Constants & Variables


More About Variables
Like storage cabinet drawers:

we can put values in (write)


Unlike drawers:

variables can only store one value at a time

when we take values out (read) the value stays there


Variables are so-called because they are Read/Write

their values can be changed they're variable!

Constants & Variables


Constants

Value fixed when program is written


Stored in memory like variables but are Read Only
Their value is never changed - they're constant!
In programs we put constant names in UPPER CASE

Examples:

DAYS_IN_WEEK = 7

ML_PER_TEASPOON = 4.9289216

UK_CAPITAL = London

Constants & Variables


Using Constants & Variables
Time to play! Start the IDLE software:

Constants & Variables


Introducing the Print command

The print command prints what's between ' and '


Try using your name:

It also prints sums for you. Try it:

Constants & Variables


Now let's try some text variables (also called strings):

And some numbers:

Constants & Variables


More fun with variables:

Notice how the commas are used to separate the


variables from each other.
Some more sums

Notice how Python uses * for and / for

Constants & Variables


Summary
Constants & Variables are labels for values stored in a
computer's memory by a program
Constants are fixed when a program is written

used to store values which never change


Variables can be changed by the program when it is
running

they can contain numbers or text

You might also like