|||
Accessibility Settings

Y10 AU – CT2 – Data Types and Casting

Copy this title into a new OneNote document:

Y10 AU – CT2 – Lesson Title Text

Do It Now:
Copy the code below into a Raspberry pi

Complete the instructions under each block of arrow comments

Then, screenshot your solution back into OneNote

# Y10 AU - CT2 - DO IT NOW

#------------------------ Local Variables ---------------------------

#====> Initialise a variable called place



#------------------------ Main Program ------------------------------

#====> Assign the variable place to an input
#====> The prompt of the input will ask them to input somewhere they would like to visit



#====> Output an fString that says you would really like to visit the place as well




๐ŸŽฏ Objectives:

(1) We are learning how to work with arithmetic operations in Python

(2) We are learning how to cast values to the correct data type and why we need to cast values to the correct data type

(3) We will be able to justify the use of different data types for different purposes within a program

๐Ÿ“– Reading:

Mathematical operations are an important part of programming.

Here are the symbols we use for each operation in Python

OperationSymbolExample
Addition+answer = 2 + 2
Subtractionanswer = 2 – 1
Multiplication*answer = 2 * 2
Division/answer = 4 / 2
Powers / Indices**5 squared is… answer = 5 ** 2

โŒจ๏ธ Activity

  • Create a Folder Called Y10 AU – CT2
  • Inside, create a file called Activity 1 – Operators.py and copy the code below into it
  • Add the required code after each block of arrow comments
  • Screenshot your code into OneNote once complete
# Y10 AU - CT2 - Activity 1

#------------------------- Global Variables --------------------------------

num1 = 20
num2 = 5

#------------------------- Main Program ------------------------------------

# Addition (example done for you)
answer = num1 + num2
print(f"{num1} + {num2} = {answer}")

#====> Subtract num2 from num1



#====> Multiply num1 by num2



#====> Divide num1 by num2



#====> Output num1 to the power of num2



๐ŸŽ“ Teacher Demonstration

Your teacher will now demonstrate taking numbers as inputs and attempting to calculate them.

They will use the code below – you are welcome to code along with them.

#---------------------------- Global Variables -----------------------------------

num1 = 0         # Initialising the variable with an empty value
num2 = 0         # Initialising the variable with an empty value

#---------------------------- Main Program ---------------------------------------

print("This program will take two numbers and add them together")

num1 = input("Please enter number 1: ")

num2 = input("Please enter number 2: ")

answer  = num1 + num2

print(f"{num1} + {num2} = {answer}")

โŒจ๏ธ Activity – Casting Explained

  • Go to the folder for today’s lesson and make a file called Activity 2 – Casting.py
  • If you haven’t already done so, copy the code above into the file
  • Work with your teacher – adding their code amendments and providing meaningful comments into the code
  • Add a section to your OneNote document called Activity 2 – Casting Explained
  • Paste a screenshot of the amended code into it.
  • Annotate the screenshot to demonstrate where casting occurs
  • Explain (to the best of your abilities) what casting is and why we do it.

๐Ÿ“– Reading Source

As you have learned from the need to cast data when creating arithmetic programs, there are several different types of data.

Data TypeDescription Purpose
String ๐ŸงตString data is used to store characters (in-fact, strings of characters.

Each character is actually stored as a unique set of binary 1s and 0s and therefore can require more memory than the other types of data.

Even if a string character is a numeric symbol e.g. “15” it still has no mathematical value, and we cannot use it within a calculation.
Integer ๐Ÿ”ขInteger data is whole number data and we use this in arithmetic calculations.

Integer data is stored in binary in a different way from string data, meaning it requires less memory.
Real (sometimes called float) .Because binary numbers naturally cannot contain decimals we need a data type called Real.

This allows for decimals numbers to be stored in memory and used within calculations.
Boolean ๐Ÿ‘๐Ÿฝ๐Ÿ‘Ž๐ŸฝBoolean is actually a really useful data type.

There are only two values (True and False)

It is used to capture the response to any kind of test carried out on another item of data.

๐Ÿ”Ž Practical Example

When we initialise variables at the beginning of a program we assign a default or empty value to the variable.

This not only helps other programmers to understand what to expect from these variables, it also ensures the computer allocates the right amount of memory for the variable.

(1) Copy the code below into raspberry pi

(2) Run it

(3) Screenshot both the code and the output

(4) Paste both into OneNote Under the title Activity 3 – Data Types

# Y10 AU - CT2 - Data Types Example

#---------------------Global Variables--------------------------

var1 = 0

var2 = 0.0

var3 = ""

var4 = True

#------------------- Main Program ------------------------------

print(f"var1 = {type(var1)}")

print(f"var2 = {type(var2)}")

print(f"var3 = {type(var3)}")

print(f"var4 = {type(var4)}")

๐Ÿ“– MWB Questions

Join the MWB activity with the code your teacher will share with you

Add the subheading Mini Whiteboard Questions into OneNote

Remember to screenshot your answer and corrections beneath