You are on page 1of 40

Get Into

Programming
with Python
Programming is being
in control
If you can answer the following
questions, then you can write a
computer program:
1. What comes next 3,6,9…?
2. Can you operate a microwave?
3. Can you name the steps to bake a
cake, change a tire or assemble a
piece of furniture?
Info about Presentation and presenter name below

Variables,
Constants,
Types and
Axle Barr Name Here Name here
Casting
• Details about the • Details about the • Details about the
presenter/instructor presenter/instructor presenter/instructor
• Details about the • Details about the • Details about the
presenter/instructor presenter/instructor presenter/instructor
• Details about the
presenter/instructor
Why Use Variables
What is a variable:

• A name that represents a value, just like my weight has a value, it


is 85 Kgs, my job has a value, it is Trainer.

• Variables are at the core of every programming language

• A variable is like a bucket, it stores something for use later

• Think of three buckets: red, blue and green


Named Buckets
What if each bucket had a name:
red_bucket

Let’s pretend that the red bucket has


300ml of water, the blue has 200 and the
blue_bucket green is completely empty

green_bucket
Filling Buckets
Pour the contents of the red and blue buckets into the green:
red_bucket

If the red bucket had 300 ml of water, the


blue bucket had 200 ml of water and we
blue_bucket poured both buckets of water into the
green bucket, we would expect the green
bucket to have 500 ml of water.
green_bucket
Algebra: Typical Statement

red_bucket + blue_bucket = green_bucket


Algorithm: Computer Program

green_bucket = red_bucket + blue_bucket


R.A.M

Blue bucket
Red bucket

Green bucket
Algorithm to
Program
Translating this to an actual computer program, it will look like this
red_bucket = 300
blue_bucket = 200
green_bucket = 0

Then, to add the blue and red buckets together we do this:


green_bucket = blue_bucket + red_bucket

Now, we can print green_bucket with a print statement


print green_bucket

On the computer screen will be the number 500


Forget about algebra, for now:

• In algebra, we find statements like these:


5+x=9

Computer • And we are supposed to solve for x


programs are not • In computers, when we use x in any part of our program, x
like algebra represents something - it could be 10, zero or nothing at

• Also remember the = sign means assignment, so the above


statement won’t work in programming - it produces an
error
Variable Reuse

What if we did not have a green_bucket?

blue_bucket = red_bucket + blue_bucket


Variable Reuse

What if we did not have a green_bucket?

red_bucket = red_bucket + blue_bucket


Algebra vs Computer Program

Algebra Computer
Program

Statement X=5+5 X=5+5

Interpretation Solve for x Add 5 to 5, then store the sum in


the variable called x

Result X = 10 The number/value 10 is stored in x


A Special Variable called Constant:
• Although a variable, as the name suggests, varies, there is one type
of variable that does not vary, the constant variable

• There are certain values that do not change, such as the number of
days in a week, degrees in a circle and wheels on a tricycle. The constant
• Some mathematical constants, like pi and Euler’s number (e), do not variable?
change

• We refer to these values in computer science as constant variables


(an oxymoron)

• Since these values are needed, we assign the various values to


constant variables. For example pi = 3.142, normally written as
PI = 3.142
Life of a Variable

Line Statement/Expression Notes


Number
1 x=5 The value of 5 is assigned to a variable called x. this is also
known as initialization. In some languages it will be both
declaration and initialization.
2 print x The number 5 appears on the screen
3 x=x+1 The original value of 5 is used to add 1 to it, so x now stores
the value of 6
4 print x The number 6 appears on the screen
5 x=3 The variable x is being used again to store a new value. So
the old value of 6 is gone, x now represents a new value, 3
6 x++ Some programming languages allow the variable itself to be
operated on, in this case x is incremented by 1
7 print x The number 4 appears on the screen
Specialized
Storage

01
Use storage based on purpose:

• A wallet is used for storing paper


money
• Only gas goes into the gas tank of
your car
• A ceramic cup for hot drinks, but a
glass for cold drinks
Variable Types:

• Computers use memory (RAM) space to store the value that a variable
represents

• Some variables require a small amount of bytes others require a larger


Each variable is amount of bytes
stored in a type of • Names have been given for these various types - integer for example is
bucket used for whole numbers like 0, 200, 50000

• One or more characters are called string variables or simply strings

• There is a special type of variable called the Boolean - it can represent


a yes or no, a true or false or an on or off
Typical Variable Names and Types

Business Variable Name Type


• Hospital • patientName • String
• Vehicle • gasTankLitres • Integer
• Government • criminal_record • Boolean
• Bank • accountBalance • Float
x=7
y=3
z=x+y
print(z)
x = int(7)
y=3
z=x+y
print(z)
x = int(7)
y=3 x = int(7)
z=x+y y = int(3)
print(z) z = int(x + y)
print(z)
x = int(7)
y=3 x = int(7)
z=x+y y = int(3)
print(z) z = int(x + y) x=7
print(z) y=3
print(x+y)
name = "John"
age = 44
print("Hello " + name)
name = "John"
age = 44
print("Hello " + name + "your age is " + age)
Casting
Casting changes the variable’s
type temporarily:

01
• Printing and inputting data can only
be done with one type, Strings. To
print any data type other than a
string, we must convert that type into
a string, temporarily.
• We can cast any other type into a
string
• Casting does NOT change the type of
the original variable, only in the
process of printing does the type
change
name = "John"
age = 44
print("Hello " + name + ", your age is " + str(age))
name = "John"
print("Hello", name)
name = "John"
age = 44
print("Hello", name, age)
name = "John"
age = 44
print("Hello %s your age is %d" % (name, age))
Place holders replace Recycle bins with specific content:
content
• Think of placeholders like %d and %s
as recycle bins.
CANS PAPER PLASTIC LANDFILL
• The bins are empty at first, but they
have a label, cans, paper etc.
• As users finish their meals, they
throw the appropriate type of waste
into the appropriate bins.
• Same with %d, only numbers can be
thrown into that placeholder and
then printed out.
Place holders replace Chest of drawers with
content specific content:
t-Shirts
Another way to look at placeholders is Socks
a chest of drawers. The top drawer may
be designated
Another way to look atas the sockisdrawer,
placeholders a chest of a
drawers.
Thedifferent
top drawerone
may for underwear
be designated as theand
sockadrawer,
third a
different one for underwear
for t-shirts and athey
etc. At first, third for
aret-shirts
empty etc. At
first they are empty but once filled at anytime in the future
butyouonce filled
can open theat anytime
sock drawer andin find
thesocks.
future Shorts
you can open the sock drawer and find
socks. Open the t-shirts drawer and
find only t-shirts. v
red_bucket = 300
blue_bucket = 200
green_bucket = red_bucket + blue_bucket

print("The green bucket contains %dml of water"


% (green_bucket))
red_bucket = 300
blue_bucket = 200

print("The green bucket contains %dml of water"


% (red_bucket + blue_bucket))
Fahrenheit to Celsius

fahrenheit = 32
celsius = 0
(F − 32) × 5/9 = C celsius = (fahrenheit - 32) * (5/9)
print("The answer is ", celsius)
Fahrenheit to Celsius

fahrenheit = 32
celsius = 0
(F − 32) × 5/9 = C celsius = (fahrenheit - 32) * (5/9)
print("The answer is %d" % celsius)
Fahrenheit to Celsius

fahrenheit = 32
celsius = 0
(F − 32) × 5/9 = C OFFSET = 32
celsius = (fahrenheit - OFFSET) * (5/9)
print("The answer is %d" % celsius)
Shopping Cart-Final Price

productCost = 100
Cost 100 TAX = 1.08
Tax 8% (1.08)
finalPrice = productCost * TAX
Final price is: print(finalPrice)
100 * 0.08 = $108.00
Shopping Cart-Final Price

productCost = input("Enter price: ")


Cost 100 productCost = int(productCost)
Tax 8% (1.08)
TAX = 1.08
Final price is: finalPrice = productCost * TAX
100 * 0.08 = $108.00 print(finalPrice)
Shopping Cart-Final Price

productCost = input("Enter price: ")


Cost 100 productCost = int(productCost)
Tax 8% (1.08)
TAX = 1.08
Final price is: SHIPPING = 5.00
100 * 0.08 = $108.00 finalPrice = (productCost * TAX) + SHIPPING
print(finalPrice)

You might also like