Python Variables

Sriparno08's avatar
Published May 6, 2021Updated Aug 11, 2025
Contribute to Docs

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.

  • 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

Creating Python Variables

The assignment operator (=) is used to create a Python variable:

x = 10
name = "Alice"
is_active = True

In this example:

  • x stores an integer (10)
  • name stores a string ("Alice")
  • is_active stores 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 variable
x = 10
# Changing the value of the variable
x = 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, and NUM are three different variables).

These are some valid variable names:

my_var = 1
_myvar = 2
myVar3 = 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 = 5
b = 3
sum_result = a + b
print("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 = 10
y = 20
x, y = y, x
print("x =", x)
print("y =", y)

The output generated by this code is:

x = 20
y = 10

Codebyte Example: Using Python Variables in Strings

Python variables can be used within strings using f-strings (Python 3.6+):

Code
Output
Loading...

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, and SCORE are different).
  • Avoid using Python reserved keywords like if, while, or class.
  • Follow PEP 8 style guidelines and use lowercase_with_underscores for 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.

All contributors

Contribute to 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