You are on page 1of 15

UNIT 2 PROGRAMMING

LESSON 3:
DATA TYPES IN TEXT-BASED PROGRAMS
LEARNING OBJECTIVES

• 8P.02 Identify and describe data types in text-based


programs, including Integer, Real and Boolean.
• 8P.05 Know how to develop text-based programs using
data types, including Integer, Real, String and Boolean.
LESSON FOCUS /
SUCCESS CRITERIA

• Learners will able to:


• Identify and describe data types in text-based programs,
including Integer, Real and Boolean.
• Know how to develop text-based programs using data types,
including Integer, Real, String and Boolean.
DATA TYPES IN TEXT-BASED
PROGRAMS

• Integer stores whole numbers


• Real stores decimal numbers
• String stores characters that can include letters, symbols
and numbers that cannot be used mathematically.
DATA TYPES IN TEXT-BASED
PROGRAMS- BOOLEAN

• The concept of a Boolean data type as ‘true’ or ‘false’


because it can only be one of two values.
• The Boolean data type ‘true’ represent with the binary
number ‘1’, and ‘false’ with ‘0’.
• We usually spell ‘Boolean’ with a capital B. This is
because it is named after a person: George Boole.
WARM-UP

What is a data type in programming?


Answer: data that is put into categories, so the program knows what is expected

What data types do you already know?


Answer: Integer, Real and String

What is the difference between an Integer and a Real number?


Answer: an Integer is a whole number, a Real number is a decimal number

What is a string?
Answer: one or more characters that can include letters, symbols and numbers that
are not used in mathematics calculations, such as telephone numbers or credit card
numbers.
Identify the most appropriate data type
for each
Data Description Data type
Purple colour String
23 Age Integer
22.5 The price of the game Real/Float
True The result from 10>5 Boolean
001182738 the ID number of the String
book

Integer data types cannot start with a 0. Any leading 0s will be removed.
Therefore, any integer that needs to have leading 0s, such as an ID number,
will need to be stored as a string. In this table, if the ID number of the book
was stored as an integer, it would be 1182738, which might be the ID number
of a different book.
BOOLEAN DATA

The use of Boolean data as a ‘flag’. It is used to indicate if


something has occurred, or is valid or correct.
If the flag is:
• true then it is positive, meaning that it has occurred, it is valid
or it is correct
• false it is negative, meaning that it has not occurred, it is not
valid or it is incorrect.
HANDS ON ACTIVITY
A SET OF PROGRAMS THAT WILL REQUIRE THE USE
OF DATA OF DIFFERENT DATA TYPES.
Ask the user a series of questions that require answers as integers,
reals and strings. Store whether they answered each question correctly
in a different variable as true for correct, and false for incorrect.
answer1 = int(input("What is 1 + 2?"))
if answer1 == 3:
answer1Result = True
print (answer1Result)
else:
answer1Result = False
print (answer1Result)

answer2 = float(input("What is 3 / 2?"))


if answer2 == 1.5:
answer2Result = True
print (answer2Result)
else:
answer2Result = False
print (answer2Result)

answer3 = input("Enter True if 2 = 3? Or False otherwise")


if answer3 == "True":
answer3Result = True
print (answer3Result)
else:
answer3Result = False
print (answer3Result)
Ask the user to enter true or false. Output a different
message depending on their answer.

entered = input("Enter True or False")


if entered == "True":
result = True
else:
result = False

if result == True:
print("Yes you entered True")
else:
print("Oh no, you did not enter True")
Design a questionnaire and write a program to ask
the user the questions and store their answers.

firstName = input("Enter your first name")


age = int(input("Enter your age"))
pocketMoney = float(input("Enter the amount
of pocket money you get"))
likeGames = input("Enter True if you like
video games")
if likeGames == "True":
likeGames = True
else:
likeGames = False
Write a program that displays some data and asks the user
for the most appropriate data type for each. Output
whether they are correct or incorrect.
print("What data type is more appropriate for:")
answer1 = input("True or False")
if answer1 == "Boolean":
print("Correct")
else:
print("Wrong it is Boolean")

answer2 = input("1, 4, 6, 12")


if answer2 == "Integer":
print("Correct")
else:
print("Wrong they are integers")

answer3 = input("horse, spider, rabbit")


if answer3 == "String":
print("Correct")
else:
print("Wrong it is a string") Challenge
answer4 = input("1.22, -2.93, 99.00001")
if answer4 == "Real":
print("Correct") Extend this activity using OR conditions to
else: allow different spellings, for example by
print("Wrong they are real numbers")
checking if the user has entered "Integer" or
"integer" or "int" or "Int".
REFLECTION

• Did you encounter any problems while writing the programs?


• What were the problems?
• Were these to do with the data types?
• How did you fix them?
• Allow learners to discuss their responses in pairs and then ask:
• Did you both encounter any problems that were similar?
• Did your partner offer any useful solutions to the problems?
CONCLUSION

• What is the difference between a String and a Boolean


data type?
Boolean is only ‘true’ or ‘false’, while a String can be any
combination of characters.

You might also like