Slides badge

Data Types

Learning Intentions

  • Describe, use and exemplify several data types

  • Describe and use 1D arrays

Success Criteria

  • Be able to explain the different data types and what they do
  • Be able to explain what a 1D array is and use them in code

IO in Python

 

  • Input and output is one of the most basic things a computer can do. 

  • For output, we use the print function in Python. When using text (strings) we use quote marks.

print("Hello class")
  • For input, we request the user to give us data for our program using the input function in Python.

print(input())

IO in Python

  • Always take note of the number of brackets in your program.

print(input())
  • As you open a bracket, ensure you close it as well. This is the most common mistake in Python.

Variables

  • Data is stored in a computer's memory whilst a program is running within storage locations. Each of these storage locations has a unique address.

  • A variable is a small container that contains a name and a value that is used to identify a storage location in memory.

  • Before variables existed, programmers accessed data stored in the memory using the memory addresses (e.g. *9412 would access memory address 9412 in C)

Variables

  • Programmers can easily set a retrieve the data found in the memory location using the variable names within a program. 

  • A program also stores data types for each of these variables allowing it know what to expect when it reads the data. 

  • Data types also tell the computer how much memory to allocate to the value (since an integer only requires 32 bits of memory whereas a real number would require 64 bits of memory)

Variables

  • Programmers can easily set a retrieve the data found in the memory location using the variable names within a program. 

  • A program also stores data types for each of these variables allowing it know what to expect when it reads the data. 

  • Data types also tell the computer how much memory to allocate to the value (since an integer only requires 32 bits of memory whereas a real number would require 64 bits of memory)

Naming Variables - the rules

  • Must have a unique name

  • The name must be <= 255 characters

  • The name must begin with a letter

  • No spaces or many special characters (. , “ - $ # * and others) in the name

  • The _ is a valid character in the name.

  • The name cannot be a reserved word

     

Naming Variables - the rules

  • It is always good programming practice to use names that are descriptive. For example, naming a variable that contains the user’s forename bob and one that stores the users surname as alice is not good programming practice since it’s difficult to identify.

  • Use camelCase where possible to make it easier to read. For example firstname becomes firstName. camelCase involves making each ‘word’ within the variable name start with an uppercase except the first word.

Declaring variables in Python

  • To create (declare) a variable in Python you simply write the name of the variable then the equals sign (=) and then the value:

  • This is called assignment.
name = "Joe Balfour"
age = 31
favouriteColor = "orange"

Declaring variables in Python

  • Variables can also be assigned to other variables. 
  • This makes a copy of that variable so that any modification to the variable will not affect the original variable:
data = "Joe"
firstName = data

The character data type

  • The character data type can contain any character from the keyboard, ranging from A-z, 1-9, or any symbol.

  • Much like the String data type, characters are enclosed in quote marks ""

variable = "J"

The String data type

  • Strings and characters are very related since a string is a collection (array) of characters.

  • Strings are always enclosed in quote marks.

  • Numbers stored in strings have no meaning when you are trying to use mathematical operators (such as add) on them.

variable = "Joe Balfour"

#This won't do anything other than crash
variable2 = "10" + 20

The String data type

  • However, strings can be concatenated (joined) together in Python:

firstName = "Joe"
surName = "Balfour"
output = firstName + surName
print(output)

The Integer data type

  • An integer is a whole number. In Python, an integer is allocated 32 bits and store a number between -214,748,3648 and 214,748,3647. Integers cannot have decimal places.

  • Integers can have mathematical operations carried out on them.

variable = 20
variable2 = variable + 20

The Integer data type

  • When you receive an integer from the user using the input function, the default data type will actually be a string.

  • We need to cast (convert) it to an integer first using the int function with brackets surrounding the input function:

variable = int(input())
variable2 = variable + 20

The Real data type

  • A real number or a float is a number which has a decimal place.

  • Python prefers the use of the word float when referring to these kinds of numbers

  • Real and Integers can have operations applied that involve both and unless specified, the result will be a real. E.g. 3.14 + 1.

variable = 10.2
variable2 = variable + 20

The real data type

  • Like with an integer, when you receive a real from the user using the input function, the default data type will actually be a string.

  • We need to cast (convert) it to a real (or the Python equivalent, float) first using the float function with brackets surrounding the input function:

variable = float(input())
variable2 = variable + 10.2

The Real data type

  • Boolean variables can only contain True or False. They are very space efficient data types since they only require one bit to represent their value.

  • We will look at these in more detail in a future lesson.

     

variable1 = True
variable2 = False

For each of the following variable declaration, specify the type of the variable:

 

var1 = "Hello class"

var2 = 31.3

var3 = "J"

var4 = 91.0

var5 = "Hi"

var6 = 53

var7 = var1

var8 = var2 + var6

In your jotters...

Work through the data types worksheet

Task

Past Paper Questions

A club requires a program to calculate how much each member needs to pay in membership fees. 

 

Complete the table below to state which type of variable should be used.

Variable Sample data Variable type
membershipFee 37.50 real
yearsOfMembership 6
memberName Jones

 

2 marks

Review

Review

Presentation Overview
Close
JB
Data Types
© 2020 - 2024 J Balfour
13:14 | 29-04-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      All slideshow files