Computer Science Grade 5 20 min

Data Types: Numbers, Text, and More!

Understand different data types and how to choose the appropriate type for each field.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Identify the four main data types in Python: integer, string, float, and boolean. Write an `if/elif/else` statement that makes decisions based on the value of a number or text variable. Use comparison operators (==, >, <) to compare different data types inside a conditional. Write a `for` loop that iterates through a list containing different data types. Use the `type()` function inside an `if` statement to check if a variable is a number or text. Explain why mixing numbers and text with a `+` sign can cause an error. Have you ever played a game that asks for your name and then your age? 🤔 How does the computer know one is text and the other is a number? In this lesson, we'll explore the different 'types' of data a computer understa...
2

Key Concepts & Vocabulary

TermDefinitionExample Integer (int)A whole number, without any decimal points. It can be positive, negative, or zero.In `player_score = 150`, the number 150 is an integer. String (str)A sequence of characters, like letters, numbers, or symbols, wrapped in quotes.In `player_name = "Alex123"`, the text "Alex123" is a string. FloatA number that has a decimal point.In `distance = 9.5`, the number 9.5 is a float. Boolean (bool)A data type that can only have one of two values: `True` or `False`. It's like a computer's way of saying 'yes' or 'no'.In `is_game_over = False`, the value `False` is a boolean. ListA container that holds an ordered collection of items. The items can be of different data types.In `inventory = ["sword", 5, &quot...
3

Core Syntax & Patterns

The `if/elif/else` Decision Maker if condition: # code to run if condition is True elif another_condition: # code to run if first is False and this is True else: # code to run if all above are False Use this structure to make your program choose what to do. The conditions often check the value of a variable, comparing numbers with `>` or `<` and text with `==`. The `for` Loop Item Processor for item in my_list: # code to run for each item Use a `for` loop to do something for every single item in a list, one by one. The `item` variable holds the current item from the list as the loop runs. The `type()` Checker type(variable_name) This special function tells you the data type of a variable. You can use it in an `if` statement like this: `if type(p...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A program has the variable `player_level = "25"`. The code `if player_level > 10:` is written to check if the player is high-level. Why will this code cause an error or not work as expected?
A.You cannot use the `>` operator to compare a string ("25") and an integer (10).
B.The variable name `player_level` is too long.
C.The number 10 should have been written in quotes, like `"10"`.
D.The `if` statement is missing an `else` part.
Challenging
Based on the 'Sorting a Backpack' example, what will be the complete output of this code? `backpack = ["map", 12.5, 3]` `for item in backpack:` `if type(item) == str:` `print("Item:")` `else:` `print("Amount:")`
A.Item: Amount: Amount:
B.Item: Item: Item:
C.Amount: Amount: Item:
D.Item: Float: Integer:
Challenging
A programmer wrote this code to give awards based on score, but it has a logical flaw. If a player's score is 95, they should get a 'Gold medal', but they get a 'Silver medal' instead. Why? `score = 95` `if score > 50:` `print("Silver medal")` `elif score > 90:` `print("Gold medal")`
A.The `>` operator should be `<`.
B.The `elif` should be another `if`.
C.The condition `if score > 90` should be checked before `if score > 50`.
D.The scores 50 and 90 should be in quotes.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Control Structures in Python

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.