Python Variables
Python variables help in storing data that can be used and manipulated throughout a program. Unlike some other languages, Python doesn’t require explicit declaration of a variable’s data type. Whether storing a number, a string of text, or even a complex data structure like a list, variables act as containers for that information, allowing code to reference and reuse it.
Creating Python Variables
The assignment operator (=) is used to create a Python variable:
x = 10name = "Alice"is_active = True
In this example:
xstores an integer (10)namestores a string ("Alice")is_activestores a Boolean value (True)
Python automatically infers the type of data being assigned.
Modifying Python Variables
The assignment operator (=) can also be used to change the value of a Python variable after its creation:
# Creating a variablex = 10# Changing the value of the variablex = 15
Python Variable Naming Conventions
A Python variable can have a short name (x, y, etc.) or a more descriptive name (age, grade, grocery_list, etc.).
Here are the rules to follow while naming Python variables:
- A variable name should begin with a letter or the underscore character. It cannot start with a number.
- A variable name can only include alpha-numeric characters and underscores (
A-z,0-9, and_). - Variable names are case-sensitive (
num,Num, andNUMare three different variables).
These are some valid variable names:
my_var = 1_myvar = 2myVar3 = 3
These are some invalid variable names:
3var = 10 # Starts with a number (invalid)my-var = 20 # Contains a hyphen (invalid)if = "hello" # 'if' is a reserved keyword (invalid)
Example 1: Basic Arithmetic with Python Variables
This example adds two Python variables:
a = 5b = 3sum_result = a + bprint("Sum:", sum_result)
The output generated by this code is:
Sum: 8
Example 2: Swapping Python Variables
Python makes it easy to swap values between two variables:
x = 10y = 20x, y = y, xprint("x =", x)print("y =", y)
The output generated by this code is:
x = 20y = 10
Codebyte Example: Using Python Variables in Strings
Python variables can be used within strings using f-strings (Python 3.6+):
Frequently Asked Questions
1. What are the four types of variables in Python?
Python defines four main types of variables based on their scope: local, global, instance and class. Here’s how they work:
- Local variables: Declared inside a function and accessible only within that function.
- Global variables: Declared outside all functions and accessible throughout the program.
- Instance variables: Belong to a specific object created from a class.
- Class variables: Shared across all instances of a class.
2. How to name Python variables?
When naming variables in Python follow these rules:
- Begin with a letter or underscore (
_) and never with a number. - Use only letters, numbers, and underscores (no spaces or special characters).
- Variable names are case-sensitive (
score,Score, andSCOREare different). - Avoid using Python reserved keywords like
if,while, orclass. - Follow PEP 8 style guidelines and use
lowercase_with_underscoresfor readability.
3. How do you declare variable types in Python?
Python uses dynamic typing, so you don’t have to declare types explicitly, the type is inferred when you assign a value.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours