Search Header Logo
Python Data types and arithmetic

Python Data types and arithmetic

Assessment

Presentation

Computers

6th - 8th Grade

Medium

Created by

N Ellis

Used 27+ times

FREE Resource

16 Slides • 9 Questions

1

Data Type and Arithmetic

2

media

• 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

Objectives

3

media
media

Data types and arithmetic
Introduction to Python

The input statement

• In the last lesson, you used two statements: one to

give the user a prompt, and a second to accept
user entry

print(" Enter your name: ")

name = input()

• You can combine these two statements to give a

prompt and accept input:

name = input(" Enter your name: ")

4

Multiple Choice

Variables and constants in programs need to be assigned a data type.
1
True
2
False

5

media
media

Data types and arithmetic
Introduction to Python

Concatenating text

• Concatenating means “joining together”

• A plus sign (+) is used to join two strings

• What will this Python code print?

age = 14

print ("Kumar " + " Ahmad are " +
str(age) + "years old")

• Caution: It won’t print quite correctly! Try it out…

6

media
media
media
media

Data types and arithmetic
Introduction to Python

What does this program do?

• Look at the code below and describe what each

line does

#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")

7

media
media

Data types and arithmetic
Introduction to Python

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

8

media
media

Data types and arithmetic
Introduction to Python

The importance of data types

• Python will always assign a data type of string when

data is entered using the input() function

mynumber = input("Enter a number")

print (mynumber + mynumber)

• What do you think happens?

• Try it!

• Convert the data type: mynumber = int(mynumber)

• Where would you put this line?

9

media
media

Data types and arithmetic
Introduction to Python

Defining variable data types

• Python automatically assigns a data type to a

variable

• You can override this to manually define or change

the data type using:

str()

str(age)

int()

int(goals_scored)

float() float(share_price)

• This is called Casting

10

media
media

Data types and arithmetic
Introduction to Python

Choosing data types

Data

Example

Data Type

Code

Age

14

Integer

int(age)

Shoe Size

6.5

Float

Price

14.99

Postcode

Height

Quantity in Stock

Telephone Number

11

Multiple Choice

What data type would be best used to record the number of people at a party?
1
Integer
2
Real
3
String
4
Boolean

12

Multiple Choice

What data type would be best to record the exact length of a piece of string in meters?
1
Real
2
Boolean
3
Decimal
4
Integer

13

Multiple Choice

Integers cannot store negative numbers.
1
False
2
True

14

Multiple Choice

What data type would be best used to record an email address?
1
String
2
Integer
3
Real
4
Character

15

Multiple Choice

What is an integer?

1

A number

2

A positive number

3

A whole number

4

A number with a decimal part

16

Multiple Choice

Which of these is a floating point number?

1

3

2

3.0

3

7

4

5.6

17

media
media

Data types and arithmetic
Introduction to Python

Assignment statements

• Assignment statements assign a value to a variable

• If the variable doesn’t already exist, it’s created

Examples:

name ="Jo"+" Smith"

number = 10

number =input("Enter a number")

18

media
media
media

Data types and arithmetic
Introduction to Python

BIDMAS

• The order of arithmetic operations matters

• Brackets , Indices, Division and Multiplication,

Addition and Subtraction

19

media
media

Data types and arithmetic
Introduction to Python

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

20

Multiple Choice

Which of these is the symbol for divide in python?

1

÷

2

/

3

>

21

Fill in the Blank

The symbol for multiply is

*

22

media
media

Data types and arithmetic
Introduction to Python

Sleep calculator

• Create a program to calculate how many hours per

week you sleep

• Try this code and find the problem!

hourspernight =input("How many hours per night do
you sleep? ")

hoursperweek = hourspernight * 7

print ("You sleep",hoursperweek,"hours per week")

23

media
media

Data types and arithmetic
Introduction to Python

Sleep calculator

• Extend 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 =float(hourspernight) * 7

print ("You sleep",hoursperweek,"hours per week")

hourspermonth =hoursperweek * 4.35

print("You sleep",hourspermonth,"hours per month")

24

media
media
media

Data types and arithmetic
Introduction to Python

Sleep calculator

• Now find the equivalent number of days per month

you sleep for!

25

media
media

Data types and arithmetic
Introduction to Python

Rounding

hourspermonth = 228.37499999999997
round(hourspermonth) = 228
round(hourspermonth,2) = 228.37

hourspernight = input("How many hours per night do you sleep? ")
hoursperweek = float(hourspernight) * 7
print ("You sleep",hoursperweek,"hours per week")
hourspermonth = hoursperweek * 4.35
hourspermonth = round(hourspermonth)
print (“\nYou sleep",hourspermonth,"hours per month")

Data Type and Arithmetic

Show answer

Auto Play

Slide 1 / 25

SLIDE