Python += Operator: A Guide (2024)

The Python += operator lets you add two values together and assign the resultant value to a variable. This operator is often referred to as the addition assignment operator. It is shorter than adding two numbers together and then assigning the resulting value using both a + and an = sign separately.

There may be a case where you want to add two values and assign the resulting value to a variable. That’s where Python’s addition assignment operator can come in handy.

Python += Operator: A Guide (1)

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

This tutorial will discuss using the += operator to add two values and assign the final value to a variable. We’ll walk through two examples to illustrate this operator in action.

Python Operators

In Python, an operator is a symbol that represents a predefined operation. For instance, the plus sign (+) performs an addition operation, and the asterisk (*) performs a multiplication operation.

Suppose we want to keep a running total of two numbers in Python. We could do so using this code:

a = 10a = a + 7.5print(a)

Our code returns: 17.5. We assigned the value 10 to the variable a, and we assigned the value 7.5 to the variable b. Then, we added the two numbers using a + b. We printed out the result to the console.

The Python += Operator

The Python += operator adds two values together and assigns the final value to a variable. This operator is called the addition assignment operator. This operator is often used to add values to a counter variable that tracks how many times something has happened.

When you use the addition assignment operator, two numbers will be added. The resultant value will be assigned to a variable.

The syntax for this arithmetic operator is:

variable_name += value

The value of the variable you specify must be either a Python string or a number. A number can be an integer or a floating-point value.

Say you specify a string value. The value that comes after the equals sign will be added to the end of the value stored in the variable you specify. If you specify a numeric value, the two numbers will be added.

Let’s look at two examples of using to use the += operator.

+= Operator Python: Numerical Example

Suppose we want to keep a running total of a few numbers. We could do so using the += assignment operator, like this:

a = 10a += 7.5print(a)

Our code returns: 17.5. In our code, you can see that we have used the += operator. This operator is more “Pythonic” than the alternative. The alternative is using a plus sign to add two values and the equals sign to assign the value to a variable.

First, we declare a Python variable called a. This variable stores the value 10. Then, we use a += 7.5 to add 7.5 to the a variable. Finally, we print out the value of a to the console, which returns 17.5.

Using the addition assignment operator in this example is like writing out a = a + 7.5. The result is the same, but the addition assignment operator is easier to read.

+= Operator Python: String Example

We have a variable that stores a user’s first name. At the moment, this variable is equal to “John”. We also have a variable that stores John’s surname. The value of this variable is “Swanson”. You can see this variable below:

forename = "John"surname = " Swanson"

We want to add these two names together. Notice that the user’s surname begins with a space. This is important because the += operator does not add a space to a value. We can merge our two names together using the following code:

forename += surname

Our code returns:

John Swanson

We have successfully merged the contents of our two variables.

Other Python Assignment Operators

The Python programming language offers a range of other assignment operators. You can also get membership operators, comparison operators, and identify operators in Python.

Assignment operators let you perform a mathematical function, then assign the resultant value to a variable. Here are a few of the main assignment operators offered by Python:

OperatorTypeExampleEquivalent
=Equalsa = 2a = 2
+=Additiona += 2a = a + 2
-=Subtractiona -= 2a = a – 2
*=Multiplicationa *= 2a = a * 2
/=Divisiona /= 2a = a / 2
**Powera ** 2a = a ** 2

To learn more about how mathematical operators work in Python, read our beginner’s guide to Python math operators.

Conclusion

The Python += operator performs an addition operator and then assigns the result of the operation to a variable. The += operator is an example of a Python assignment operator. This operator is called the addition assignment operator.

This tutorial discussed, with reference to an example, the basics of Python operators and how to use the += assignment operator. Now you’re equipped with the knowledge you need to use the =+ operator like a professional Python developer!

Are you interested in learning more about the Python programming language? Check out our complete How to Learn Python guide with top learning resources and online courses.

4 Ratings

Python += Operator: A Guide (2024)
Top Articles
Candle Making as a Side Hustle
17 ASDA Interview Questions [+Includes Best Answers]
Krua Thai In Ravenna
Target Dummies 101 - The Dummy Research/Tutorial Thread
Busted Newspaper Pulaski County
Tony's Delicatessen & Fresh Meats
Non-Identity Functions
Miramar Water Utility
Fbsm Berkeley
Which is better, bonds or treasury bills?
Oil filter Cross Reference - Equivafiltros
The Blind Showtimes Near Merchants Walk Cinemas
Guide:Guide to WvW Rewards
Entegra Forum
Jinx Manga Vyvy
Tenkiller Dam Release Schedule
Anime Souls Trello
Gdp E239 Bts
Fisher-Cheney Funeral Home Obituaries
O'reilly's El Dorado Kansas
Buncensored Leak
Aaa Saugus Ma Appointment
Telegram Voyeur
Hannaford Weekly Flyer Manchester Nh
The Quiet Girl Showtimes Near Landmark Plaza Frontenac
Oscillates Like A Ship
321 Flea Market Gastonia Nc
Joshua Efird Net Worth
Bollywood Movies 123Movies
Societe Europeenne De Developpement Du Financement
Crimson Draughts.
Best Upscale Restaurants In Denver
Plastic Bench Walmart
Probation中文
Rush Copley Swim Lessons
2005 Lund Boat For Sale in Ham Lake, MN Lot #67597***
Boostmaster Lin Yupoo
Intelligent intranet overview - SharePoint in Microsoft 365
Grupos De Cp Telegram
Sayuri Pilkey
Moviesverse 2023
QuiBids Review: Legit Penny Auction or a Scam? The Truth... - MoneyPantry
Legend Of Krystal Forums
Sour Power OG (Karma Genetics) :: Cannabis Strain Info
Currently Confined Coles County
Webworx Call Management
Madrigal Pharmaceuticals, Inc. (MDGL) Stock Forum & Discussion - Yahoo Finance
Gen 50 Kjv
Dollar General Penny List July 18 2023
Gelöst – Externe Festplatte kann nicht formatiert werden
Osrs Nex Mass
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6577

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.