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 the fundamental building blocks of any programming language. They act as named containers for storing data values in your computer's memory. In Python, you can think of a variable as a sticky note with a name written on it, attached to a piece of data. Whenever you refer to that name, Python retrieves the attached data.
Unlike statically typed languages such as Java or C++, Python is dynamically typed. This means you do not need to explicitly declare a variable's type (like `int age = 25;`) before assigning a value to it. A variable is created the exact moment you first assign a value to it using the equals sign `=`. The Python interpreter automatically infers the data type based on the value provided.
Python has several built-in core data types. The most common are Numeric types (`int` for whole numbers, `float` for decimals), Text types (`str` for string sequences), and Boolean types (`bool` representing `True` or `False`). Understanding these types is crucial because different types support different operations. For example, you can mathematically add two integers, but adding two strings concatenates (joins) them together.
Because Python variables are simply references to objects in memory, a single variable can change its type over its lifetime. If you initialize `data = 100`, it is an integer. If on the next line you write `data = "Hello"`, it becomes a string. While this flexibility makes Python fast to write, developers must be careful to keep track of variable types to prevent runtime errors like attempting to add a string to a number.
Naming your variables correctly is an art form. Python enforces strict naming rules: variable names must start with a letter or an underscore, cannot start with a number, and can only contain alpha-numeric characters and underscores. Furthermore, Python developers conventionally use "snake_case" (e.g., `user_account_balance`) for variables, ensuring code remains highly readable.
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.
Keep Learning
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
How to Swap Two Variables in Python
Learn how to swap two variables in Python. Understand Pythonic tuple packing and unpacking, and compare it with traditional temporary variable swapping.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.