constants & variables

Download Constants & Variables

If you can't read please download the document

Upload: juggle333

Post on 19-Nov-2015

217 views

Category:

Documents


1 download

DESCRIPTION

Constants & Variables

TRANSCRIPT

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

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

Constants & Variables

Memory AddressesEach 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