Python L2 Numbers and Arithmetic
Python L2 Numbers and Arithmetic
Introduction to Python
Learning objectives
• Understand the importance of using correct data
types: string, integer or float
• Use the int, float and round functions
• Understand how to use assignment statements
correctly
• Perform arithmetic using the BIDMAS rule
• Write a program involving input, calculation and
output
Introduction to Python
Numbers and arithmetic
ad here
#Animal
animal = input("Enter the name of an animal: ")
description = input("How would you describe a " + animal + "? ")
print ("A",animal,"is",description)
print("You will find it under",animal[0],"in the dictionary")
Introduction to Python
Numbers and arithmetic
ad here
Data types
• String holds alphanumeric data as text
• Integer holds whole numbers
• Float holds numbers with a decimal point
• Boolean holds either ‘True’ or ‘False’
Introduction to Python
Numbers and arithmetic
ad here
• str() str(animal_name)
• int() int(goals_scored)
• float() float(share_price)
Assignment statements
Assignment statements assign a value to a variable. If
the variable doesn’t already exist, it’s created.
Examples:
number = 10
number = input("Enter a number")
Introduction to Python
Numbers and arithmetic
ad here
BIDMAS
• The order of arithmetic operations
matters.
• Brackets , Indices, Division and
Multiplication, Addition and Subtraction
Introduction to Python
Numbers and arithmetic
ad here
Arithmetic operators
Arithmetic Operator Example Output
Addition + 2 + 10 12
Subtraction - 9–6 3
Multiplication * 5*4 20
Division / 5/2 2.5
Floor Division // 7 // 2 3
Remainder % 7%3 1
Introduction to Python
Numbers and arithmetic
ad here
Sleep calculator
• Create a program to calculate how many hours per
week you sleep
• Try this code and find the problem!
Sleep calculator
• Add to the problem to find the total number of hours
spent sleeping in a month
• Assume an average of 4.35 weeks per month
hourspernight = input("How many hours per night do you sleep? ")
hoursperweek = int(hourspernight) * 7
print ("You sleep",hoursperweek,"hours per week")
hourspermonth = float(hoursperweek) * 4.35
print ("You sleep",hourspermonth,"hours per month")
Introduction to Python
Numbers and arithmetic
ad here
Sleep calculator
• Now find the equivalent number of days per month
you sleep for!
Introduction to Python
Numbers and arithmetic
ad here
Rounding
• hourspermonth = 228.37499999999997
• round(hourspermonth) = 228
• round(hourspermonth,2) = 228.37
hourspernight = input("How many hours per night do you sleep? ")
hoursperweek = int(hourspernight) * 7
print ("You sleep",hoursperweek,"hours per week")
hourspermonth = float(hoursperweek) * 4.35
hourspermonth = round(hourspermonth)
print (“\nYou sleep",hourspermonth,"hours per month")
Introduction to Python
Numbers and arithmetic
ad here
Finishing off
• Display a title for your program when it first runs
• Calculate the number of days per year spent asleep
• Include the user’s name to make it more user
friendly