What are variables in Python?
A variable is a named storage location that holds a value. It allows you to give data a descriptive name so you can reference and use it later. You can write flexible and readable code without having to hardcode values.
For example, instead of writing the number 35
over and over again, you can store it in a variable user_age
and use user_age
everywhere.
How to Create (Declare) Variables in Python
Creating variables in Python is very simple. You don’t need to explicitly declare a type. Just assign a value using the assignment operator =
.
Structure:
variable_name = value
Example:
first_name = "Alice"
user_age = 30
print(first_name)
print(user_age)
In this example:
- We created the
first_name
variable and assigned it the text value “Alice”. - We created the
user_age
variable and assigned it the number 30.
Python automatically understands that “Alice” is text (string) and 30 is a number (integer), looking at the value.
Python Fundamentals for Beginners Free Course
Master Python basics, from variables to data structures and control flow. Solve real-time problems and build practical skills using Jupyter Notebook.
Naming Rules and Conventions
There are certain rules for naming variables in Python, and some community-accepted conventions that make the code more readable.
Rules (Must Follow)
- Variable names must start with a letter (a-z, A-Z) or an underscore (
_
). - Variable names cannot start with a number.
- Variable names can only contain alphanumeric characters and underscores (A-Z, 0-9, and
_
). - Variable names are case-sensitive. Python treats
age
,Age
, andAGE
as three separate variables. - Python reserved keywords (such as
if
,for
,while
,class
) cannot be used for variable names.
Conventions (Recommended)
The standard convention in Python is snake_case. Write all letters in lowercase and separate words with underscores. This is best for readability. Other formats work as well, but snake_case
is preferred.
Good Examples:
user_first_name
total_amount
is_active
Not Recommended:
userFirstName
# camelCaseUserFirstName
# PascalCase
Common Data Types of Variables
A variable can hold different types of data. Python automatically assigns a data type when you create a variable. The most common types are:
- String (
str
)Used for text. To create a string, enclose the text in single or double quotes.
user_greeting = "Hello, World!"
- Integer (
int
)Used for whole numbers, positive or negative, without decimals.
item_count = 100
- Float (
float
)Used for numbers with a decimal point.
price = 19.99
- Boolean (
bool
)Represents a value of either
True
orFalse
. Booleans are often the result of comparisons.is_logged_in = True
How to Check a Variable’s Type
If you’re ever in doubt about the data type stored in a variable, use Python’s built-in type()
function. This is very helpful for debugging or understanding other people’s code.
How to Use:
- Call the
type()
function. - Pass the variable’s name within parentheses.
Example:
user_name = "John Doe"
items_in_cart = 5
total_cost = 124.75
print(type(user_name)) # Output: <class 'str'>
print(type(items_in_cart)) # Output: <class 'int'>
print(type(total_cost)) # Output: <class 'float'>
This immediately identifies which variable is a string, which is an integer, and which is a float.
How to Reassign Variables
You can change the value of any variable at any time by simply assigning a new value to it. This process is called reassigning. Python is a dynamically-typed language, meaning you can also change the data type of a variable.
Example:
# First assign an integer value
user_id = 123
print(user_id) # Output: 123
# Now assign a new integer value
user_id = 456
print(user_id) # Output: 456
# Now assign a different data type (string)
user_id = "user-abc-456"
print(user_id) # Output: user-abc-456
Deleting Variables
If you no longer need a variable, you can delete it with the del
keyword. If you try to access it after deleting it, you will get an error.
Syntax:
del variable_name
Example:
customer_name = "temporary user"
print(customer_name)
# Delete the variable
del customer_name
# The next line will give a NameError because the variable no longer exists.
# print(customer_name)
Key Points
- Create variables by assigning a value with the
=
operator. - Use
snake_case
to give them readable names and follow Python’s naming rules. - Check the variable’s data type with the
type()
function. - You can assign new values to variables or delete them completely.