Programming · StudentHub Lesson
Variables & Data Types
Learn how programs store data in variables using types like integers, strings, and booleans.
What you will learn
- Define what a variable is
- Identify common data types
- Declare and assign variables in Python
- Distinguish between integers, floats, strings, and booleans
- Understand type conversion basics
Watch the lesson
Python for Beginners – Full Course [Programming Tutorial] · freeCodeCamp.org
Watch on YouTubeTopic notes
Main Idea
Variables are named storage locations in a program that hold data, and each variable has a data type describing what kind of value it stores.
Key Concepts
- Variables can be created and reassigned during a program
- Common data types: integer, float, string, boolean
- Python is dynamically typed, so you don't declare types explicitly
- Type conversion changes data from one type to another
Definitions
- Variable: a named container for storing data values
- Data type: a classification of what kind of value a variable holds
- Boolean: a data type with only True or False values
Syntax
```python
age = 15 # integer
height = 5.6 # float
name = "Alex" # string
is_student = True # boolean
```
Examples
score = 100creates an integer variablestr(100)converts the integer 100 to the string "100"
Common Mistakes
- Forgetting quotes around strings
- Mixing types without converting (e.g., adding a string and an integer)
Key concepts
Important terms
- Variable
- A named container that stores a data value
- Data type
- The category of value a variable can hold, e.g., integer or string
- Type conversion
- Changing a value from one data type to another
Worked examples
Problem
Create a variable storing your age and print it
- 1. age = 15
- 2. print(age)
Answer: 15
Quick revision
- Variables store data with names
- Python infers type automatically
- Common types: int, float, str, bool
- Use type() to check a variable's type
- Convert types using int(), str(), float()
Check your understanding
Question 1 · Multiple choice
Which of these is a valid Python string assignment?
Question 2 · Multiple choice
What data type is the value True?
Question 3 · True or false
In Python, you must declare a variable's type before using it.
Question 4 · Short answer
What function converts a string to an integer in Python?
Question 5 · Short answer
What is stored in the variable x after x = 3.14?
Done with Variables & Data Types?
Sign in to save your progress across the library.