Slides badge

Data types

Write the formula in Python

  • Write programs that calculates and displays the answers to the following formulas. Remember that when you see something like P = IV, this is the same as P = I × V. Create your own values to test each of them (e.g a = 10, b = 20):
  1. x = a + b
  2. P = IV
  3. F = ma
  4. E = mc2

Write the formula in Python

  • Write programs that calculates and displays the answers to the following formulas. Remember that when you see something like P = IV, this is the same as P = I × V. Create your own values to test each of them (e.g a = 10, b = 20):
  1. x = a + b
  2. P = IV
  3. F = ma
  4. E = mc2
a = 10
b = 20
c = 15
I = 1
V = 2
m = 20

x = a + b
P = I * V
F = ma
E = m * c * c

print(x)
print(P)
print(F)
print(E)
  • Develop our understanding of variables

  • Understand how to use comments within our programs

  • Know the different data types

  • I can develop my own program using variables
  • I can develop a program that performs checking the data types of variables

Variables in Python

print("Please insert a number")
y = int(input())
z = 2

x = y * z

print(x)

Write a very short three-line program that will ask the user "Please insert your name" then take in a user’s name and stores it in a variable and then display "Hello " and the user’s name.

Step 1: Print to the screen "Please insert your name"
Step 2: Get the user’s input
Step 3: Display the "Hello " and the user’s input

Task

Comments

  • One of the most important things in programming, especially if you choose to do Computing Science in National 4/5, is internal comments.
  • A comment is a piece of text within the program that describes what your code does. Python ignores comments when it is run.
  • In Python, a comment line starts with a hashtag sign #
  • It is important to note that comments only go across one line, so each line after that will need a # at the start too

Using comments

  • We should get used to using comments so that when we look back at our programs, we can understand what each piece of code does without needing to look it up.

#Display to the user Please insert a number
print("Please insert a number")
#Get the number from the user and put it in y
y = int(input())
z = 2

#Do the calculation
x = y * z
#Print the result
print(x)

Data types

  • A data type is the type of the data you are storing in a variable.
    For instance, when we are storing a number using the int(input()) function we are getting an integer.
    Python has 5 data types we need to know about.

Name Description Example
Integer Whole numbers 1, 200, -128, 5
Float (real number) Numbers with decimal places, numbers with exponents (powers) 6.23, 5.00, -
Char A single character from the keyboard (note the quotes) "Y", "N", "O", "1", "@"
String Multiple characters (note the quotes) "Hello World", "Mr Smith", "Computing"
Boolean Either True or False True, False

Why do we need to know about data types?

  • Understanding how a computer stores data is important for us because we sometimes need to convert the data to another type. For example, if we use the input() function we have been using in the past examples the computer expects text even if we want a number.

  • To convert the text into a number so that we can use it in calculations we use the int() function. This tells the computer that it should now consider that string as an integer.

Data type conversion

  • We have multiple ways of converting data that we are going to use for the next part of this lesson:

Function name Description Example
int() Convert data to an integer int(input())
float() Converts data to a floating point number (real number or decimal)  float(input())
str() Converts data to a string of text str(50)

Data type conversion

  • Converting the data is possible using the functions str, int and float.

x = "10"
print(x)

y = int(10)
print(y)

Data types

  • For each of the following pieces of data, write down the data type you would expect the value variable to be:

 

 

 

 

 

 

 

String

value = "Hello world"

Character

value = "0"

Boolean

value = True

Float

value = 0.5

String

value = "101Paris"

Character

value = "c"

Why do we need to know about data types?

  • Understanding how a computer stores data is important for us because we sometimes need to convert the data to another type. For example, if we use the input() function we have been using in the past examples the computer expects text even if we want a number.

  • To convert the text into a number so that we can use it in calculations we use the int() function. This tells the computer that it should now consider that string as an integer.

  • Computers store data in the memory, but without the program knowing what data type is being used it's just a bunch of 1s and 0s.

10101100 10111100 01110111

Checking the type

  • Python has a very useful way of checking the data type of a variable using the type() function.
  • For example:
name = "Jack"
print(type(name))
age = 12
print(type(age))

Your task is to create a program that displays the type of three different variables. 

 

To do this, you will need to create three variables and use three type functions to get their types. What do you notice about their types when they are displayed?

 

 

 

 

 

 

tESTING THE TYPE FUNCTION

var1 = 11.3
print(type(var1))

Work through the worksheet.

 

Once you have done that you can go back to your computer and create a program to test these types using the type() function.

Task

Programming Challenges

Reviewing our progress

Let’s build a calorie counter.  The NHS recommends that an adult male takes on board 2,500 calories per day and an adult woman takes on 2000 calories per day.  Build your program for a woman or a man.  The pseudocode below should help you.
 
  • Display "Welcome to the calorie counter"
  • Ask user to enter how many calories they have eaten today
  • Calculate how many remaining calories they can eat today
  • Display "You can eat", calories, "calories today"

Programming challenges - Calorie counter

Write a program that asks the user to enter how much they have spent on their school dinner.  Store this in a variable called dinnerCost.  Then make the program subtract the dinnerCost from the money that the user had at the start of the day.  Display this result on the screen

Programming challenges - School dinners

Jennifer wants to carpet her new room with pink carpet.  Create a program that will ask the user for the two dimensions of her room (length and width) and then calculate the area of the room (length x width), before displaying the answer.

Programming challenges - Area Calculator

Write a program to work out how long a user has been alive to the nearest year.  There are 365 days in a year.  The pseudocode below will help you with this:
 
  1. Ask for user’s name
  2. Ask for user’s age
  3. Use the age variable to work out how many hours that is (24 hours in a day)
  4. Use the hours to work out the number of minutes and seconds a person has lived for (60 minutes per hour and 60 seconds per minute)
  5. Display all information on the screen.

Programming challenges - How long has the user been alive?

Write a program that monitors a user’s shopping trip and calculates how much was spent.  
 
  1. Ask for the firstItem bought
  2. Ask for the firstItemCost
  3. Ask for the secondItem bought
  4. Ask for the secondItemCost
  5. Calculate the total amount spent
  6. Display the total amount spent to the user

Programming challenges - Going shopping

Pizzas come in different shapes and sizes.  We can write a program to work out which pizza is the best in terms of price and size.
 
The formula for area is 3.142 times the radius squared if it is a circle and length times width if it is a rectangle.
 
Imagine a pizza 20cm in diameter costing £4.99 and another 20cm in length by 15cm wide and costing £6.99.
 
Write a program which calculates the surface area per £ and displays to the user which is the best value pizza, the round one of the rectangular one.

Programming challenges - Pizza

Ask user how much pocket money they get per week. The program should ask the user how much they've spent and display how much is left. Steps for this:
 
  1. Ask how much they have spent.
  2. Calculate and display how much they have left

Programming challenges - Pocket money

Ask user for distance travelled and speed, then calculate how long they've travelled for.
 
  1. Calculate the time taken using time = distance / speed.
  2. Display the answer.

Programming challenges - Journey Time Calculator

Presentation Overview
Close
JB
Data types
© 2020 - 2026 J Balfour
22:45 | 18-01-2026
Join Live Session
Go Live
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Bullet Only Mode
Generate Quiz
Generate Lesson Plan
Widget Screen
Canvas Controls
Fullscreen
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Set a timer for how long?
    10:00
    5:00
    3:00
    2:30
    2:00
    1:00
    ...
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      Apply theme

      Blue theme
      White theme

      More effects:

      ×
      Loading
      All slideshow files