Python Basics: Additional and Special Operators
Welcome back to our Python foundational series! In our previous session, we explored the core mathematical engines of Python: Arithmetic, Comparison, and Logical operators.
However, as you begin writing algorithms for Machine Learning and Data Science, you will quickly realize that you are constantly updating the state of your variables. Whether you are incrementing an epoch counter during neural network training or accumulating the total loss over a dataset, writing x = x + 1 repeatedly becomes tedious.
To make your code more concise, efficient, and readable (“Pythonic”), Python offers Assignment Operators. In this session, we will explore these special operators and see how they update variable values in place.
Assignment Operators
Assignment operators combine a standard arithmetic operation with variable assignment. They take the current value of a variable, apply a mathematical calculation to it, and immediately save the new result back into the exact same variable.
Here is the complete list of assignment operators available in Python:
| Operator | Example | Equivalent to |
|---|---|---|
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
**= | x **= 3 | x = x ** 3 |
//= | x //= 3 | x = x // 3 |
Code Example: Basic Assignment
Let’s look at a simple, everyday example of incrementing and decrementing a variable:
x = 10
# Incrementing
x += 5 # Equivalent to x = x + 5. The value of x is now 15.
# Decrementing
x -= 3 # Equivalent to x = x - 3. The value of x is now 12.
Code Example: Comprehensive Operator Usage
To fully understand how these operators transform a variable sequentially, let’s run through a continuous script where we apply different assignment operators to x. Notice how the value of x carries over from one operation to the next.
# Initial value
x = 10
print("Initial value of x:", x)
# += operator (Addition Assignment)
x += 3
print("After x += 3, x =", x) # Equivalent to x = x + 3 -> Output: 13
# -= operator (Subtraction Assignment)
x -= 3
print("After x -= 3, x =", x) # Equivalent to x = x - 3 -> Output: 10
# *= operator (Multiplication Assignment)
x *= 3
print("After x *= 3, x =", x) # Equivalent to x = x * 3 -> Output: 30
# /= operator (Division Assignment)
x /= 3
print("After x /= 3, x =", x) # Equivalent to x = x / 3 -> Output: 10.0 (Float)
# %= operator (Modulus Assignment)
x %= 3
print("After x %= 3, x =", x) # Equivalent to x = x % 3 -> Output: 1.0
# Resetting x to a whole number for the final examples
x = 10
# **= operator (Exponentiation Assignment)
x **= 3
print("After x **= 3, x =", x) # Equivalent to x = x ** 3 -> Output: 1000
# //= operator (Floor Division Assignment)
x //= 3
print("After x //= 3, x =", x) # Equivalent to x = x // 3 -> Output: 333
Conclusion
Assignment operators are fundamental tools for controlling the flow of your programs. Whenever you write a for loop or a while loop to process data, you will rely on these operators to track progress, sum values, or dynamically update model parameters.
By mastering variables, casting, and all categories of operators, you now have a solid grasp of how to manipulate raw data. In our next session, we will shift our focus to text processing by exploring String Methods—a crucial skill for Natural Language Processing (NLP) and data cleaning!
Notebook
Enjoy Reading This Article?
Here are some more articles you might like to read next:
- Python Basics: Operators (Arithmetic, Comparison, and Logical)
- Python Basics: Postscript – Python Memory Management and Immutability
- Python Basics: Advanced Assignment and Type Casting
- Python Basics: The print() Function and String Formatting
- Python Basics: Variables and Data Types
- Python Roadmap: What to Learn, In What Order, and Which Tools to Use
- Introduction to Google Colab and Kaggle Notebooks: Cloud-Based Machine Learning
- Python Basics: Common String Methods for Data Cleaning and NLP
- Setting Up Your AI and Machine Learning Environment: Python, Conda/venv, VS Code, Jupyter, and Git