Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • šŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • šŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • šŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • šŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • šŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • šŸ“ For Each Assignments
    • šŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • šŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • šŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • šŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • šŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • šŸ“ Assignments
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • šŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • šŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • šŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • šŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • šŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • šŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • šŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • šŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • šŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • šŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • šŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • šŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • šŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • šŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

šŸ“ Python Assignment Statements: Complete Guide

Python Assignment Statements - Complete Guide

Assignment statements are the foundation of storing and managing data in Python programs.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
We are a team of Python developers, data engineers and data scientists with years of real-world experience. We have built production applications. We have tackled complex problems. We have helped teams ship better code. What drives us? Teaching — especially that moment when things finally click. Our tutorials focus on practical examples and honest guidance. Whether you are just starting out or leveling up we are here to make Python genuinely useful.
šŸ“‘ On this page:
  • Introduction to Assignment Statements
  • Syntax of Assignment Statement
  • Basic Form
  • Tuple Assignment
  • List Assignment
  • Sequence Assignment
  • Extended Sequence Unpacking
  • Multiple-Target Assignment
  • Augmented Assignment
  • Try It Yourself
  • Quick Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
šŸ“š What You'll Learn Here
  • What assignment statements are and why they are fundamental
  • Syntax — the assignment operator (=) and its rules
  • Basic Form — simple variable assignment
  • Tuple Assignment — assigning multiple variables at once
  • List Assignment — using lists in assignment
  • Sequence Assignment — assigning sequences to variables
  • Extended Sequence Unpacking — using the * operator
  • Multiple-Target Assignment — chain assignment
  • Augmented Assignment — shorthand operators
  • Hands-on practice with the interactive editor

Introduction to Assignment Statements

An assignment statement is a fundamental concept in Python programming used to set a value to a variable name. During the lifetime of a program, assignment statements allow variables to hold different types of values. It stores the value at a memory location which can be identified or referred to by the variable name.

Assignment statements are very common in programming and are used frequently. They form the foundation of how data is stored and manipulated in Python.

šŸ’” Key insight: The assignment operator = should never be confused with the equality operator ==. Assignment sets a value, while equality compares values.

šŸ“Œ Topics covered in this article:
1. Syntax
2. Assignment Statement Forms
   2.1 Basic Form
   2.2 Tuple Assignment
   2.3 List Assignment
   2.4 Sequence Assignment
   2.5 Extended Sequence Unpacking
   2.6 Multiple-Target Assignment (Chain Assignment)
   2.7 Augmented Assignment

1. Syntax of Assignment Statement

The symbol = is used in an assignment statement and is called the assignment operator.

āš ļø Important Note: The assignment operator assigns the value of the right-hand operand to the left-hand side operand. This operator should never be used for equality purposes — that is the double equal sign ==.

# Basic Syntax
variable = expression

# Examples
a = 50           # Assign integer 50 to variable a
b = 34.25        # Assign float 34.25 to variable b
a = 25           # Reassign value 25 to variable a (a now holds 25)

2.1 Basic Form

This is one of the most common forms of assignment statements. Here, the variable name is defined, initialized, and assigned a value in the same statement. This form is generally used when we want to use the variable multiple times and do not want to change its value frequently.

# Basic Form Example
language = "python"
print("Language =", language)

# Output:
# Language = python

2.2 Tuple Assignment

In Python, if we need to define and assign values for more than one variable at the same time, tuple assignment is useful. This is an easy method that saves time. Each individual variable gets a different value assigned to it.

# Tuple Assignment Examples
language1, language2 = "python", "java"
a, b = 30, 40

print("Language1 =", language1)
print("Language2 =", language2)
print("a =", a)
print("b =", b)

# Output:
# Language1 = python
# Language2 = java
# a = 30
# b = 40

šŸ“– Explanation: When we declare a tuple, Python pairs objects on the right side with targets on the left by position and assigns them from left to right. Therefore, language1 and language2 get "python" and "java" respectively.

2.3 List Assignment

List assignment works in the same way as tuple assignment.

# List Assignment Example
[v1, v2] = [12, 14]

print('v1 =', v1)
print('v2 =', v2)

# Output:
# v1 = 12
# v2 = 14

2.4 Sequence Assignment

In recent versions of Python, tuple and list assignment have been generalized into what we now call sequence assignment. Python assigns items in the sequence on the right to variables in the sequence on the left by position.

# Sequence Assignment Example
a, b, c, d, e, f = 'Python'

print('a =', a)
print('b =', b)
print('c =', c)
print('d =', d)
print('e =', e)
print('f =', f)

# Output:
# a = P
# b = y
# c = t
# d = h
# e = o
# f = n

2.5 Extended Sequence Unpacking

Extended sequence unpacking allows us to be more flexible in how we select portions of a sequence to assign. Using the star operator (*), it's possible to have different numbers of elements on each side of the assignment operator.

# Extended Sequence Unpacking Example
p, *q = 'Python Programming'

print('p =', p)
print('q =', q)

# Output:
# p = P
# q = ['y', 't', 'h', 'o', 'n', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']

šŸ“– Explanation: Here, p is matched with the first character in the string on the right, and q gets the rest. The starred name (*q) is assigned a list containing all items not assigned to other names.

2.6 Multiple-Target Assignment (Chain Assignment)

Multiple-target assignment assigns a reference to the same object (the rightmost object) to all targets on the left.

# Multiple-Target Assignment (Chain Assignment)
x = y = 75

print(x, y)

# Output:
# 75 75

2.7 Augmented Assignment

Augmented assignment is a shorthand assignment that combines an expression and an assignment.

# Augmented Assignment Example
x = 2

# Equivalent to: x = x + 1
x += 1

print(x)

# Output:
# 3

# Other augmented assignment forms:
# -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, >>=

Try It Yourself!

Experiment with assignment statements directly in your browser. Modify the code and see the results in real time.

Loading Pyodide... 0%
Python Code Editor
========================================
BASIC ASSIGNMENT
========================================
Language: Python
Version: 3.12

========================================
TUPLE ASSIGNMENT
========================================
lang1: Python, lang2: Java
a: 30, b: 40

========================================
LIST ASSIGNMENT
========================================
v1: 12, v2: 14

========================================
SEQUENCE ASSIGNMENT
========================================
p: C, q: o, r: d, s: e

========================================
EXTENDED SEQUENCE UNPACKING
========================================
first: P
rest: ['y', 't', 'h', 'o', 'n']

========================================
CHAIN ASSIGNMENT
========================================
x: 100, y: 100

========================================
AUGMENTED ASSIGNMENT
========================================
z after += 3: 8

āœ… Assignment statements are the foundation of Python!
šŸ†

šŸŽ‰ You've Mastered Python Assignment Statements!

You understand basic assignment, tuple assignment, list assignment, sequence assignment, extended unpacking, chain assignment, and augmented assignment.

Quick Quiz – Test Your Knowledge

Let's see what you've learned about Python assignment statements:

1. What symbol is used as the assignment operator in Python?
2. What is the output of: a, b = 10, 20; print(a, b)?
3. What does x += 5 mean?

Frequently Asked Questions

šŸ¤” What's the difference between = and ==? ā–¼

= is the assignment operator used to assign values to variables. == is the equality operator used to compare two values. Never confuse them!

šŸ”„ Can I swap two variables in Python using assignment? ā–¼

Yes! Python makes variable swapping simple: a, b = b, a. This is a classic example of tuple assignment.

šŸ“ What is the star operator * in assignment? ā–¼

The star operator * is used in extended sequence unpacking. It collects all remaining items from a sequence into a list. For example, first, *rest = "Python" assigns 'P' to first and all remaining characters to rest as a list.

šŸ”— What is chain assignment? ā–¼

Chain assignment (x = y = 75) assigns the same value to multiple variables. Both x and y point to the same object (75).

šŸ“š Where to Go From Here

Now that you understand Python assignment statements, here are some related topics to explore:

šŸ“ Variables & Identifiers

Learn about variable naming rules

šŸ”§ Operators

Explore all Python operators

šŸ“Š Data Types

Understand Python data types

šŸ“– Related Tutorials
  • Variables and Identifiers
  • Operators and Expressions
  • Data Types in Python
  • Constants in Python
šŸ“– Interview & FAQ Resources
  • Python Data Types Interview Questions & Answers
  • SQL Restricting & Sorting Data FAQ
Previous: Constants Next: Input/Output →