Variables in Python – Complete Beginner’s Guide with Examples (2025)

If you’re new to Python, one of the first concepts you’ll encounter is variables. Variables are essential in programming — they let you store, manipulate, and reference data in your code.

In this beginner’s guide, you’ll learn:

  • What is a variable in Python?

  • How to declare and assign variables

  • Python variable naming rules

  • Types of variables and data types

  • Best practices when working with variables

What Is a Variable in Python?

A variable in Python is a name that refers to a value stored in the computer’s memory. Think of it as a container for data.

name = "John"
age = 25

In the above code:

  • name stores a string "John"

  • age stores an integer 25

How to Declare Variables in Python

Unlike other programming languages, Python doesn’t require you to declare the variable type. You just assign a value, and Python understands the type automatically.

message = "Hello, World!"     # String
count = 10                    # Integer
price = 5.99                  # Float
is_valid = True              # Boolean

You can reassign variables with new values or even change their types:

count = "Ten"  # No error, but not recommended

Python Variable Naming Rules (with Examples)

Python has specific rules and conventions for naming variables:

✅ Valid names:

  • Must begin with a letter or underscore (_)

  • Can contain letters, numbers, and underscores

  • Are case-sensitive (Age and age are different)

❌ Invalid names:

python
 
2value = 10 # Invalid: starts with a number user-name = "A" # Invalid: dash is not allowed

✅ Good examples:

python
 
first_name = "Alice" total_amount = 100 is_logged_in = False

Types of Variables in Python

Python supports multiple data types. The most common are:

TypeExample
intx = 10
floatpi = 3.14
strname = "Bob"
boolis_ready = True
listcolors = ["red", "blue"]
dictperson = {"name": "Alice", "age": 25}
NoneTypevalue = None

Use the type() function to check a variable’s type:

 
print(type(age)) # Output: <class 'int'>

Variable Scope: Local vs Global

Local Variables: Defined inside functions and only accessible within them.

Global Variables: Defined outside functions and accessible throughout the script.

# Global x = 5 def show(): # Local y = 10 print(x, y) show()

Best Practices for Python Variables (2025 Standards)

✅ Use meaningful names – make your code readable:

 
x = 5 # ❌ Bad num_students = 5 # ✅ Good

✅ Use snake_case for variable names (as per PEP 8):

 
userAge = 21 # ❌ camelCase (used in JavaScript) user_age = 21 # ✅ snake_case

✅ Keep variables consistent in style and type:

 
count = 10 count = "ten" # ❌ Avoid changing types unnecessarily

✅ Avoid reserved keywords as variable names:

 
class = "Math" # ❌ 'class' is a Python keyword

FAQs About Python Variables

Q1. Do I need to declare the type of a variable in Python?
A: No. Python uses dynamic typing, so you just assign a value.

Q2. Can variable names start with a number?
A: No. They must start with a letter or underscore.

Q3. What is the difference between = and ==?
A: = is for assignment, == is for comparison.