- 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.
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:
a, b = 10, 20; print(a, b)?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? ā¼
a, b = b, a. This is a classic example of tuple assignment.
š What is the star operator * in assignment?
ā¼
* 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? ā¼
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