Computer Science
Grade 7
20 min
7. Data Types in Python: Integers, Floats, Strings, Booleans
Review the basic data types in Python and how they relate to data representation concepts.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define the four basic data types in Python: integer, float, string, and boolean.
Identify the data type of a given value (e.g., 10, 3.14, 'Hello', True).
Use the `type()` function in Python to check the data type of a variable.
Explain the difference between an integer and a float.
Explain the purpose of using quotation marks for strings.
Correctly assign values of each of the four data types to variables in Python.
How does your computer know the difference between the number 10 and the word 'ten'? 🤔
In this lesson, we'll explore the four most important 'data types' in Python. Think of them as the basic building blocks for all information in your programs. Understanding them is the first step to making your computer d...
2
Key Concepts & Vocabulary
TermDefinitionExample
Data TypeA category that tells the computer what kind of data a value is, like a number or text. This determines what operations you can perform on the data.The number `5` has the data type `integer`, while the text `"5"` has the data type `string`.
Integer (int)A whole number, without any decimal points. It can be positive, negative, or zero.Examples include `-15`, `0`, and `42`.
Float (float)A number that has a decimal point. It's used for representing fractional or 'floating-point' numbers.Examples include `98.6`, `3.14`, and `-0.5`.
String (str)A sequence of characters (letters, numbers, symbols) enclosed in single (`'`) or double (`"`) quotes. It represents text.Examples include `"Hello, world!"`, `'Python123...
3
Core Syntax & Patterns
Variable Assignment
variable_name = value
Use the equals sign (`=`) to assign a value to a variable. The value on the right is stored in the variable on the left. For example, `score = 100` stores the integer `100` in the variable `score`.
String Quotation Marks
my_string = "text" or my_string = 'text'
Strings must always be surrounded by either single quotes (`'`) or double quotes (`"`). The computer treats anything inside these quotes as plain text, even if it looks like a number.
Checking Data Types
type(variable_or_value)
Use the built-in `type()` function to ask Python for the data type of a value or a variable. It's a very useful tool for checking your work, like `type(10)` would show `<class 'int'>`.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
In the 'Sale Price' example, the final price is `19.99`. If you then run the code `is_cheap = final_price < 20.0`, what will be the value and data type of the `is_cheap` variable?
A.Value: `19.99`, Data Type: float
B.Value: `True`, Data Type: boolean
C.Value: `"True"`, Data Type: string
D.The code will cause an error.
Challenging
A program needs to store a user's 5-digit zip code, like `90210`. Although it's made of numbers, it will never be used for math (e.g., adding or subtracting). Which data type is most appropriate and why?
A.Integer, because it is a whole number.
B.Float, because it could be used in calculations later.
C.String, because it represents a sequence of characters, not a mathematical quantity.
D.Boolean, because it represents a location.
Challenging
A student stores a player's level as `player_level = "10"` and their score as `player_score = 1500.5`. Which variable assignment is most likely to cause problems for future math operations, like adding 1 to the level?
A.`player_score`, because floats can be inaccurate.
B.`player_level`, because it is a string and cannot be used directly in math.
C.Both, because you cannot mix strings and floats.
D.Neither, Python can automatically handle the math for both.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free