Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
Overview
Variables are containers for storing data values. In Python, you do not need to declare a variable before using it or declare its type.
A variable is created the moment you first assign a value to it.
Python has various built-in data types, such as Text Type (str), Numeric Types (int, float, complex), and Boolean Types (bool).
Code Example
This script shows different variable types and how to check a variable type using the built-in type() function.
age = 25 # Integer
height = 5.9 # Float
name = "Jane Doe" # String
is_developer = True # Boolean
print(f"{name} is {age} years old.")
print(f"Type of height: {type(height)}")
print(f"Is developer? {is_developer}")Jane Doe is 25 years old.
Type of height: <class 'float'>
Is developer? TrueReal-world Use Cases
- Storing user input for later use
- Holding state in an active application
- Defining configuration settings for scripts
Frequently Asked Questions
Do I need to declare variables like in C++ or Java?
No, Python is dynamically typed. The type is determined at runtime based on the assigned value.
Can a variable change its type?
Yes, if you assign a string to a variable that previously held an integer, the variable simply becomes a string.