Python Variables and Data Types
To build any meaningful Python program, you need to store, manage, and manipulate data. In Python, this is done using variables and data types. This lesson teaches you how to create variables, understand common data types, and follow best practices.
What You'll Learn
- What variables are and how to create them
- Python data types: int, float, str, bool
- Variable naming rules and best practices
- How to check a variable's type using type()
- How to convert between data types (type casting)
What Are Variables?
A variable is like a container that stores data. It's a kind of named memory location that holds the data for that variable. You assign a value to a variable and use it later in your program. You assign a value to a variable using the = operator.
Syntax:
variable_name = value
Example:
name = "Alice"
age = 25
Python Data Types
Data types define the type of data a variable can hold — such as numbers, text, or boolean values. Every variable in Python has a specific data type that tells the interpreter how to handle and store that value in memory.
| Data Type | Description | Example |
|---|---|---|
int
|
Integer numbers |
10, -5, 2025
|
float
|
Decimal numbers |
3.14, 0.99, -10.5
|
str
|
Text (string) |
"hello", 'Python'
|
bool
|
Boolean (True/False) |
True, False
|
Note: Python also provides complex types like lists, dictionaries, and more — but we’ll cover those in the next lessons.
Let’s explore the data types in Python.
Integer (int)
Used to store whole numbers (positive or negative).
age = 30
temperature = -5
print(age,temperature)
# Output: 30 -5
Use int when working with quantities like age, count, year, etc.
Floating Point (float)
Used for decimal or real numbers.
height = 5.9
price = 99.99
print(height,price)
# Output: 5.9 99.99
Use float when precision matters — e.g., prices, measurements, averages.
String (str)
Used to store sequences of characters — text.
name = "Alice"
message = 'Hello, World!'
print(name)
# Output: Alice
Use str for text, names, sentences, symbols.
Boolean (bool)
Used for logical values: True or False.
is_logged_in = True
is_admin = False
print(is_logged_in, is_admin)
# Output: True False
Use bool for flags, switches, and control flows like if conditions.
Key Features of Python Variables
- No need to declare data types explicitly – Python automatically detects the type based on the assigned value.
- Dynamically typed – You can change a variable’s type after it has been assigned.
Example
x = 10
print(x) # x is an integer
x = "Hello"
print(x) # Now x is a string
This flexibility makes Python easy to use, especially for beginners — but it also means you need to be careful with variable reassignment to avoid unexpected errors.
Checking Variable Types
Use the type() function to check a variable’s data type:
i = 10
f = 20.99
x = "Python"
b = True
print(type(i)) # <class 'int'>
print(type(f)) # <class 'float'>
print(type(x)) # <class 'str'>
print(type(b)) # <class 'bool'>
# Output:
# <class 'int'>
# <class 'float'>
# <class 'str'>
# <class 'bool'>
Multiple Assignment in Python
Python allows you to assign values to multiple variables in a single line, making your code more concise and readable. To do this, ensure the number of variables on the left side of the assignment operator (=) matches the number of values on the right.
name, age, is_student = "Ali", 22, True
print(name, age, is_student)
Assigning the Same Value to Multiple Variables
Python lets you assign a single value to multiple variables simultaneously by chaining the assignment operator (=). This is useful when initializing several variables with the same default value.
a = b = c = 0
Variable Naming Rules
- Start with a letter or underscore
- Can contain letters, numbers, and underscores
- Case-sensitive
- Cannot start with a number or use keywords
-
Cannot use Python keywords (like
for,if,class)
Best Practices
-
Use descriptive names like
user_name - Use lowercase with underscores for readability
Type Conversion (Casting)
Type conversion, also known as type casting, is the process of converting a value from one data type to another. Python provides built-in functions to do this explicitly.
There are two types of type conversion in Python:
- Implicit Conversion – done automatically by Python
- Explicit Conversion – done manually using functions like int(), float(), str(), etc.
Implicit Type Conversion
Python automatically converts one data type to another when it's safe to do so. For example, converting an integer to a float during a calculation.
a = 10 # int
b = 3.5 # float
result = a + b
print(result) # Output: 13.5
print(type(result)) # Output: <class 'float'>
Python converts a (an int) to float automatically to match b.
Explicit Type Conversion (Casting)
You manually convert one data type to another using built-in functions.
Common Casting Functions:
| Function | Description | Example |
|---|---|---|
int()
|
Converts to integer |
int("10") → 10
|
float()
|
Converts to float |
float("3.14") → 3.14
|
str()
|
Converts to string |
str(25) → "25"
|
bool()
|
Converts to boolean (True/False) |
bool(0) → False
|
Examples:
String to Integer:
num_str = "100"
num_int = int(num_str)
print(num_int + 50) # Output: 150
Integer to String:
age = 30
message = "Age is " + str(age)
print(message) # Output: Age is 30
Float to Integer (rounds down):
pi = 3.14159
pi_int = int(pi)
print(pi_int) # Output: 3
Boolean Conversion:
print(bool(0)) # Output: False
print(bool(5)) # Output: True
print(bool("")) # Output: False
print(bool("Hi")) # Output: True
Be careful. Not all conversions are valid. Trying to convert a non-numeric string to an integer will cause an error.
value = "abc"
num = int(value) # Error: ValueError
Always validate or check the input before converting it.
Practice Example
x = "100"
y = float(x)
z = bool(x)
print(x, type(x))
print(y, type(y))
print(z, type(z))
Mini Quiz
-
What is the type of:
"123"? -
What does
bool("")return? -
How would you convert
3.14to a string? -
What happens if you try
int("hello")?
Summary
- Variables store data, and Python detects the type automatically
-
Common data types:
int,float,str,bool -
Use
type()to check types, and casting functions to convert - Follow naming rules and best practices for clean code
What’s Next?
Next, we’ll explore Python Operators and Expressions — how to perform calculations, make comparisons, and build logic in your code.