Python Variables Types Conditionals v1
Python Variables Types Conditionals v1
>>> This symbol means "write your code here" >>> 30.5
8 5
15 5
8 -5
Integers and Floats
Here’s a little Python terminology.
35 30.5
An int is a A float is a
decimal number
whole number
Order of Operations
These are the calculations happening first.
>>> ( 5 + 7 ) * 3 >>> 5 + 7 * 3
Behind 12 * 3 5 + 21
the
scenes
36 26
What we know: A swallow weighs 60g and can carry 1/3 of its weight.
>>> 60 / 3
20
What we know: A swallow weighs 60g and can carry 1/3 of its weight.
>>> 1450 / 60 / 3
8.06
That number seems way too low.
Let’s look at what happened behind the scenes:
1450 / 60 / 3
24.17 / 3
What we know: A swallow weighs 60g and can carry 1/3 of its weight.
>>> 1450 / ( 60 / 3 )
1450 / ( 20 )
72.5
Great!
# swallows per cherry: >>> 8 / ( 60 / 3 ) 0.4 We found one that
works!
>>> swallow_limit = 60 / 3
>>> swallow_limit = 60 / 3
>>> swallows_per_cherry
num_per_cherry == 8 / swallow_limit
>>> swallows_per_cherry
0.4
What Should I Name My Variables?
As long as you’re not breaking these rules, you can name variables anything you want!
>>> macaw_limit
300
Can a Macaw Carry a Coconut?
Step 2: Divide the coconut’s weight by the limit.
# macaws needed to carry a coconut
>>> num_macaws
4.8
Importing Modules — Extra Functionality
Occasionally, we will want to use modules containing functionality that is not built in to
the Python language.
>>> math.ceil(num_macaws)
>>> math.ceil(num_macaws)
5
Level 2
>>> full_name
'MontyPython'
Concatenating a Space
We can
concatenate a
>>> first_name = 'Monty' space character
bet ween the words
>>> last_name = 'Python'
>>> full_name
'Monty Python'
Moving Our Code to a Script File
>>> first_name = 'Monty' Each line is entered
on the command line
>>> last_name = 'Python'
>>> full_name
script.py
first_name = 'Monty'
last_name = 'Python'
full_name = first_name + ' ' + last_name
first_name = 'Monty'
last_name = 'Python'
full_name = first_name + ' ' + last_name
print(full_name)
Prints whatever is
Monty Python
inside the ()
print(first_name, last_name)
Monty Python
In Python 2,
print() as many things as you want, print() automatically adds spaces
print doesn’t need ()
just separate them with commas bet ween arguments
print full_name
Running a Python Script
script.py
first_name = 'Monty'
last_name = 'Python'
full_name = first_name + ' ' + last_name
print(full_name)
Monty Python
This is the command to run Python
scripts
Demo: Running a Python Script From the Console
Comments Describe What We’re Doing
Let’s write a script to describe what Monty Python is.
script.py
script.py
# Introduce them
sentence = name + ' is a ' + description + ' formed in ' + year
script.py
# Introduce them
sentence = name + ' is a ' + description + ' formed in ' + year
This will work, but it really makes sense to keep numbers as numbers.
Turning an Int Into a String
script.py
# Introduce them
print(name, 'is a', description, 'formed in', year)
script.py
Hell's Grannies
Special Characters for Formatting
We want to add some more sketches and print them out
script.py
print(famous_sketch1, famous_sketch2)
script.py
Famous Work:
Hell's Grannies
The Dead Parrot
script.py
Famous Work:
Hell's Grannies
The Dead Parrot
Strings Behind the Scenes — a List of Characters
A string is a list of characters, and each character in this list has a position or an index.
'H E L L O W O R L D !'
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
script.py
12
The Man Who Only Says Ends of Words
od
Using Slices to Access Parts of a String
Boundary Boundary
starts before ends before
index 2 index 5
Start End
boundary boundary
Slice Formula and Shortcuts
slice formula: variable [start : end+1]
Go Go
od od
Go script.py
od
word1 = 'Good'
od end1 = word1[2] + word1[3] word1[2:4]
print(end1)
Go script.py
od
Ev
en
ing
word1 = 'Good'
end1 = word1[2:]
od ning Improved this with a
word2 = 'Evening'
end2 = word2[3:] shortcut
print(end1, end2)
od ning
It would be great if we didn’t have to know the halfway points were at 2 and 3…
The Index at the Halfway Point
The len() function will let us calculate the halfway index of our word.
Go
od
script.py Ev
en
ing
# Calculate the halfway index
word1 = 'Good' od ning
half1 = len(word1)/2 half1 is 2.0
'E v e n i n g'
PROBLEM: indexes have to be [0] [1] [2] [3] [4] [5] [6]
integers — floats won’t work!
The Index at the Halfway Point Integer division vs floor
division. Be consistent if
we cover in Level 1.
word2 = 'Evening'
half2 is
half2 = len(word2)//2 // Also rounds down to the
7//2 = 3
nearest integer
In Python 2,
single division /
is int division unless
there are floats involved
Making Our Program More Reusable
Calculating the halfway index makes our logic reusable.
Go
od
Ev
en
script.py ing
word2 = 'Evening'
half2 is
7//2 = 3 half2 = len(word2)//2 'G o o d'
end2 = word2[3:] word2[half2:]
[0] [1] [2] [3]
print(end1, end2)
'E v e n i n g'
od ning [0] [1] [2] [3] [4] [5] [6]
Level 3
otherwise
Comparison Operators
There are 6 comparators in Python:
>>> is_raining
True
Conditional — if, then
An if statement lets us decide what to do: if True, then do this.
script.py
# Battle Rules
num_knights = 2
script.py
# Battle Rules
num_knights = 4
p p e n s…
Because the h i n g h a
N o t t s *
conditional is False, k e
*c r i c
this block doesn’t run
The Program Continues After the Conditional
The code continues to run as usual, line by line, after the conditional block.
script.py
# Battle Rules
num_knights = 4
if num_knights == 1:
# Block start
Irregular spacing is 2 spaces print('Do this')
a no-no! 4 spaces print('And this')
print('Always this')
otherwise
Conditional — if False else
script.py
# Battle Rules
num_knights = 4
If this statement is True,
if num_knights < 3: then run the indented code below
print('Retreat!')
else:
print('Truce?') Otherwise,
then run the indented code below
Truce?
Conditionals — How to Check Another Condition?
script.py
# Battle Rules
num_knights = 10
if num_knights < 3:
print('Retreat!')
else:
print('Truce?')
Trojan Rabbit
Combining Conditionals With and/or
King Arthur has decided:
OR AND
if it’s a Monday it’s Wednesday
If we have less than 3 knights OR If it’s a Monday Spelled out Monday, and
Wednesday on the next
slide.
script.py
# Battle Rules
num_knights = 10
day = 'Wednesday' Checking day of the week
In Python 2,
raw_input() is used
instead of input()
Receiving User Input From Console
Just like we can print() text to the console, we can also get user input from the console.
script.py
User enters 10
Enter the number of knights 10
You entered: 10
Different Input and Conditionals Changes Output
script.py
1st run How many knights? 2 2nd run How many knights? 12 2
num_knights < 3
killer bunny
otherwise
Nested Conditionals — Ask Follow-up Questions
script.py
# Battle Rules
num_knights = int(input('Enter number of knights')
day = input('Enter day of the week') First, find out if our enemy
enemy = input('Enter type of enemy') is the killer bunny
# Battle Rules
num_knights = int(input('Enter number of knights')
day = input('Enter day of the week')
enemy = input('Enter type of enemy')
Our final
if enemy == 'killer bunny':
Rules of Engagement
print('Holy Hand Grenade!')
else:
# Original Battle Rules
if num_knights < 3 or day == 'Monday':
print('Retreat!')
if num_knights >= 10 and day == 'Wednesday':
print('Trojan Rabbit!')
else:
print('Truce?')