Class 2
Class 2
Variables
Types
Arithmetic operators
Boolean logic
Strings
Printing
Exercises
AGENDA
Lists
Dictionaries
Sets
If Else
Loops
Functions
Classes
Exercises
BUT FIRST A FEW TASKS
Create a program that tells us how many days, weeks, months we have left if we live until
90 years old.
It will take your current age as the input and output a message with our time left in this
format:
You have x days, y weeks, or z months left.
Where x, y and z are replaced with the actual calculated numbers.
Warning your output should match the Example Output format exactly, even the positions
of the commas and full stops.
Example Input
56
Example Output
You have 12410 days, 1768 weeks, OR 408 months left.
Instructions
If the bill was $150.00, split between 5 people, with 12% tip.
Each person should pay (150.00 / 5) * 1.12 = 33.6
Format the result to 2 decimal places = 33.60
Thus, everyone's share of the total bill is $30.00 plus a $3.60 tip.
.
>>> format(math pi, '.2f') # give 2 digits after the point
Example Input '3.14'
Welcome to the tip calculator!
What was the total bill? $124.56
How much tip would you like to give? 10, 12, or 15? 12
How many people to split the bill? 7
Example Output
Each person should pay: $19.93
LISTS
One of the most useful concepts
Group multiple variables together (a kind of container!)
INDEXING A LIST
• Indexing – accessing items within a data structure
Tanmoy2
DICTIONARY DEFINITION
Defined as comma separated key : value pairs:
Comma separated
Curly brackets
DICTIONARY PROPERTIES
Values are mapped to a key
Values are accessed by their key
Key are unique and are immutable
Values cannot exist without a key
DICTIONARIES
Let us define the one from the previous image
ACCESSING A DICTIONARY
Values are accessed by their keys (just like a dictionary)
Conditional statement
Executed if answer is True
elif = else + if which means that the previous statements must be false for the current one
to evaluate to true
Write a program that works out whether if a
given number is an odd or even number.
Multiple if if and elif Nested if
If condition If condition
code code If condition
If condition elif condition If condition
code code If condition
If condition elif condition code
code code
If condition elif condition else:
code code code
else: else:
code code
BITCOIN BROKER EXAMPLE
QUICK QUIZ
What would happen if both conditions are True?
FOR LOOP
Allows us to iterate over a set amount of variables within a data structure. During
that we can manipulate each item however we want
What if we have much more than 4 items in the list, say, 1000?
FOR EXAMPLE
• Now with a for loop
Warning you should round the result to the nearest whole number. The interpretation message needs to include the words from
the interpretations above. e.g. underweight, normal weight, overweight, obese, clinically obese.
Example Input
weight = 85
height = 1.75
Example Output
85 ÷ (1.75 x 1.75) = 27.755102040816325
This is how you work out whether if a particular year is a leap year.
Example Input
size = "L"
add_pepperoni = "Y"
extra_cheese = "N"
Example Output
Your final bill is: $28.
Instructions
You are going to write a program that tests the compatibility between two
people. To work out the love score between two people:
Take both people's names and check for the number of times the letters in the
word TRUE occurs. Then check for the number of times the letters in the word LOVE
occurs. Then combine these numbers to make a 2-digit number.
HINT: use count() to count the number of a particular letter in a string
For Love Scores less than 10 or greater than 90, the message should be:
"Your score is **x**, you go together like coke and mentos."
For Love Scores between 40 and 50, the message should be:
"Your score is **y**, you are alright together."
Otherwise, the message will just be their score. e.g.:
"Your score is **z**."
Example
name1 = "Angela Yu"
name2 = "Jack Bauer"
Love Score = 53
Print: "Your score is 53.“
T occurs 0 times
R occurs 1 time
U occurs 2 times
E occurs 2 times
Total = 5
L occurs 1 time
O occurs 0 times
V occurs 0 times
E occurs 2 times
Total = 3