Python Variables

Python · Variables & Data Types

Theory

A variable in Python is a name that refers to an object in memory. When you write sales = 250, Python first evaluates the right side, creating an int object holding 250, and then binds the name sales to that object. There is no declaration step and no type attached to the name; the variable exists from the moment it is first assigned, and using it before that raises NameError.

Think of a variable as a label tied to a box, not the box itself. Assigning b = a ties a second label to the same object, it does not copy anything. For immutable values like numbers and strings this never surprises you, because changing b really means re-pointing b at a new object. For mutable objects like lists, both names see every change made through either one. Reassigning sales = 'high' later is allowed; the name simply points at a different object of a different type.

Why we use it

Variables let you store a value once and reuse it by name, so calculations are readable (total = price * quantity) and easy to change. Understanding names-as-references explains many beginner surprises with lists and dictionaries.

When to use it

Create a variable whenever a value is used more than once or deserves a meaningful name. If you need an independent copy of a list or dict, do not just assign it to a new name; use .copy() or list(old) so changes do not leak between them.

How it works

  1. Python meets sales = 250
  2. Evaluate the right-hand side first: create the int object 250
  3. Does the name sales already exist? Yes: Re-point it at the new object. No: Create the name in the current scope.
  4. Bind the name to the object
  5. Later, print(sales) looks the name up and fetches its object
  6. Output: 250

Syntax

variable_name = value

Example

sales = 250
city = "Mumbai"

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic