📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Variables in Python: Complete Guide

Quick answer: Learn how variables work in Python, naming rules, dynamic typing, multiple assignment, constants, and common interview questions.

What is a Variable?

A variable is a name that refers to a value stored in memory. In Python, you create a variable simply by assigning a value to a name, with no separate declaration step required.

name = "Fireblaze"
price = 89000
is_active = True

Variable Naming Rules

  • Must start with a letter or an underscore, not a digit

  • Can contain letters, digits and underscores

  • Case sensitive: price and Price are different variables

  • Cannot use a reserved keyword like if, for or class as a variable name

Dynamic Typing

Python variables do not have a fixed type. The same name can be reassigned to a value of a different type.

x = 10        # x is an integer
x = "ten"     # now x is a string
x = [1, 2, 3] # now x is a list

Use type(x) at any point to check a variable's current type.

Multiple Assignment

a, b, c = 1, 2, 3

x = y = z = 0   # all three point to the same initial value

# Swapping values without a temporary variable
a, b = b, a

Constants in Python

Python has no true built-in constant type. By convention, variables meant to stay unchanged are written in all capital letters, though nothing technically prevents reassignment.

MAX_STUDENTS = 60
COMPANY_NAME = "Fireblaze AI School"

Global vs Local Variables

total = 100   # global variable

def update():
    total = 50    # this creates a NEW local variable, does not change the global one
    print(total)  # 50

update()
print(total)      # still 100

Use the global keyword inside a function if you genuinely need to modify a global variable from within it.

Common Interview Questions

Does Python require you to declare a variable's type?

No. Python is dynamically typed, so a variable's type is determined by the value currently assigned to it, and can change over its lifetime.

What happens if you use a variable before assigning it a value?

Python raises a NameError, since the name does not yet exist in memory.

FAQ

Frequently Asked Questions

Do you need to declare a variable's type in Python?

No. Python is dynamically typed, meaning a variable's type is determined by its current value and can change during the program.

What are the naming rules for Python variables?

A variable name must start with a letter or underscore, can contain letters, digits and underscores, is case sensitive, and cannot be a reserved keyword.

How do you swap two variables in Python without a temporary variable?

Using tuple assignment: a, b = b, a.

What is the difference between a global and a local variable?

A global variable is defined outside any function and accessible throughout the file. A local variable is defined inside a function and only exists within it, unless declared global.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles