Assignment Operators

Python · Operators

Theory

= binds a name to a value. Augmented assignment operators combine an operation with assignment: total += 50 means total = total + 50, and the same pattern exists for -=, *=, /=, //=, %=, **= and others. For immutable values like numbers and strings, Python computes a new object and re-points the name at it. Python has no ++ or -- operators; write count += 1.

For mutable objects the story changes. On a list, x += [3] modifies the existing list in place (just like x.extend([3])), so any other name pointing at the same list sees the change. x = x + [3] instead builds a brand-new list and rebinds only x. Assignment also supports unpacking: a, b = 1, 2 assigns both at once, a, b = b, a swaps without a temporary variable, and a = b = 0 points both names at the same object.

Why we use it

Updating a running total, counter or accumulator is one of the most common things code does. Augmented assignment keeps it short and readable, and understanding in-place behaviour avoids nasty aliasing bugs with lists.

When to use it

Use += for counters and running totals in loops. When a list is shared between names (or passed into a function) and you do not want the original changed, use new = old + [item] instead of +=.

How it works

  1. Python meets total += 50
  2. Look up the current value of total (100)
  3. Is the object a mutable list? Yes: Extend it in place. No: Compute total + 50 as a new object.
  4. Bind the name total to the result
  5. print(total) shows 150

Syntax

=  +=  -=  *=  /=

Example

total = 100
total += 50
print(total)

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