Watch the video and take notes, pay attention to how you can change the type of data held in a variable while in a running program.
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
# Example code (casting data)
# strings to integer casting, this can be used for turning integers or floats into strings.
a = "10"ย # variable a is assigned the string value "10"
b = "20"ย # varaible b is assigned the string value "20"
total = a + b ย # performing addition with strings will not work
print(total)ย ย # output will just be one string attached to the other "1020"
a = "10"ย # variable a is assigned the string value "10"
b = "20"ย # varaible b is assigned the string value "20"
total = int(a) + int(b) ย # placing int() round a varible will cast the contents to become an integer for that operation (this is not permanent)
print(total)ย ย # output will be 30 as expected
# Permanent casting
a = "10" # variable a is assigned the string value "10"
b = "20" # varaible b is assigned the string value "20"
a = int(a)ย # variable a will be reassigned to the contents of a when it cast as an integer
b = int(b)ย # variable b will be reassigned to the contents of b when it cast as an integer
total = a + bย
print(total)
Apply your knowledge and use casting to complete the following code to allow the programs to execute (run) correctly.ย
Exercise 1:
# Code exercise (casting data)
# Exercise 1
a = 10
b = "5"
c = "3"
total = a + b * c
print(total)
Exercise 2:
# Code exercise (casting data)
# Exercise 2:ย
houseNumber = 67
streetName = "Huzzah Avenue"
print(houseNumber + streetName)
Exercise 3:
# Code exercise (casting data)
# Exercise 3:
num1 = str(input("Enter a number :"))
num2 = str(input("Enter a number :"))
total = num1 * num2
print(total)
Exercise 1a:
a = 10
b = "5"
c = "3"
total = a + int(b) * int(c)
print(total)
Exercise 1b: (alternate solution)
a = 10
b = "5"
c = "3"
b = int(b)
c = int(c)
total = a + b * c
print(total)
Exercise 2a:
houseNumber = 67
streetName = " Huzzah Avenue"
print( str(houseNumber) + streetName)
Exercise 2b: (alternate solution)
houseNumber = 67
streetName = " Huzzah Avenue"
houseNumber = str(houseNumber)
print( houseNumber + streetName)
Exercise 3a:
num1 = int(input("Enter a number :"))
num2 = int(input("Enter a number :"))
total = num1 * num2
print(total)
Exercise 3b: (alternate solution)
num1 = str(input("Enter a number :"))
num2 = str(input("Enter a number :"))
num1 = int(num1)
num2 = int(num2)
total = num1 * num2
print(total)
Describe why casting is useful.
Identify and describe the parts in the following Python instruction.
num = int( "10" )
Describe why understanding what data is used in your program is important; from data inputted by the user to the data outputted to the user.ย
Written Exercise
Describe why casting is useful.
Casting is useful as it allows a developer the ability to change the type of data held in the variables of a program.ย e.g. integer to string and vice versa.ย This would be useful when performing math orย outputting / formatting data.ย
Identify and describe the parts in the following Python instruction.
num = int( "10" )
variable is being assigned a value that has been cast as an integer.ย "10" has been cast to 10.
Describe why planning what data is used in your program is important; from data inputted by the user to the data outputted to the user.ย
Having an understanding of what data is being used throughout your program is vital.ย As the way your program processes data will change depending on if you have to recast your data in order to use it.ย e.g. You would not have a user input string data into a calculator as it would have to be recast to a float before you can perform any calculations.ย Plan for the data you need for your program, input the correct data on input and then it will not need to be recast.ย