Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • What Are Assignment Operators?
  • Step-by-Step Breakdown with Real Examples
  • 4. Multiply and Assign *=
  • 5. Divide and Assign /=
  • 6. Modulus and Assign %= (Remainder)
  • 7. Exponent and Assign **=
  • 8. Floor Divide and Assign //=
  • Summary Table
  • Practical Machine Learning Example
  • Output:
  • Keywords
  1. Artificial Intelligence
  2. Data Science Foundation
  3. Python Programming
  4. Operators

Assignment Operators

Nerd Cafe

What Are Assignment Operators?

In Python, assignment operators are used to assign values to variables. The most basic one is =, but there are many others that combine arithmetic with assignment, such as +=, -=, etc.

Step-by-Step Breakdown with Real Examples

1. Basic Assignment =

x = 5
print(x)

Note:

  • This assigns the value 5 to variable x.

  • In machine learning, you might initialize weights like this: weight = 0.01.

2. Add and Assign +=

x = 5
x += 3  # same as x = x + 3
print(x)  # Output: 8

Use Case in ML: Updating the learning rate or loss:

loss = 0.0
loss += 0.25  # adding batch loss to total loss

3. Subtract and Assign -=

x = 10
x -= 4  # same as x = x - 4
print(x)  # Output: 6

Use Case in ML: Gradient Descent weight update:

weight = 0.5
gradient = 0.1
learning_rate = 0.01
weight -= learning_rate * gradient

4. Multiply and Assign *=

x = 4
x *= 3  # same as x = x * 3
print(x)  # Output: 12

Use Case in ML: Scaling weights or input:

input = 0.7
scale = 2
input *= scale  # Normalize or scale input

5. Divide and Assign /=

x = 20
x /= 4  # same as x = x / 4
print(x)  # Output: 5.0

Use Case in ML: Averaging losses:

total_loss = 1.2
batches = 4
avg_loss = total_loss
avg_loss /= batches

6. Modulus and Assign %= (Remainder)

x = 10
x %= 3  # same as x = x % 3
print(x)  # Output: 1

Use Case in ML or loops: Logging every N steps:

if step % 100 == 0:
    print("Logging at step", step)

7. Exponent and Assign **=

x = 2
x **= 3  # same as x = x ** 3
print(x)  # Output: 8

Use Case in ML: Exponential learning rate decay or power-related transformations:

lr = 0.1
decay_factor = 0.9
lr **= 2  # simulate decay

8. Floor Divide and Assign //=

x = 9
x //= 2  # same as x = x // 2
print(x)  # Output: 4

Use Case in ML: Chunking data into batches:

data_size = 1000
batch_size = 128
num_batches = data_size
num_batches //= batch_size

Summary Table

Operator
Description
Example (x = 5)
Result (x)

=

Assign

x = 5

5

+=

Add and assign

x += 3

8

-=

Subtract and assign

x -= 2

6

*=

Multiply and assign

x *= 4

20

/=

Divide and assign

x /= 5

4.0

%=

Modulus and assign

x %= 3

1

**=

Exponent and assign

x **= 2

25

//=

Floor divide and assign

x //= 2

2

Practical Machine Learning Example

Let’s simulate a basic training loop to see assignment operators in action:

# Simulating training loop
epochs = 5
learning_rate = 0.1
weight = 0.5
gradient = 0.2

for epoch in range(epochs):
    weight -= learning_rate * gradient  # Gradient Descent
    print(f"Epoch {epoch+1}: weight = {weight}")

Output:

Epoch 1: weight = 0.48
Epoch 2: weight = 0.45999999999999996
Epoch 3: weight = 0.43999999999999995
Epoch 4: weight = 0.41999999999999993
Epoch 5: weight = 0.3999999999999999

Keywords

assignment operators, python assignment, python operators, augmented assignment, python basics, machine learning python, gradient descent, weight update, loss function, learning rate, += operator, -= operator, *= operator, /= operator, %= operator, **= operator, //= operator, python syntax, data science python, python tutorial, nerd cafe

PreviousLogical OperatorsNextCompound Operators

Last updated 2 months ago