![]() ![]() A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions: Order of operations: You might have a long way to go, but as long as you continue learning, experimenting, and repeating, you will keep making progress and becoming a better programmer.An expression is a combination of values, variables, and operators. You're now one step closer to mastering Python. You can also experiment with the other operators mentioned in the table above. With this guide handy, you're prepared to use the addition assignment operator to its full potential. Here's a list of what are considered the "main" assignment operators in Python: Some of the most interesting ones include comparison operators, membership operators, and identity operators.Īssignment operators allow you to perform mathematical functions and assign the results to a variable. Other Assignment Operators You Should Know Aboutīesides the addition assignment operator, Python offers a variety of other assignment operators. So, the associativity property of the addition assignment operator is from left to right. The final statement prints the A value, showing that it equals 10. The bit-shifted result is then added to A and stored in A. Then, the value of B is shifted to the right by one bit. A = 5Īs you can see, variables A and B are assigned values 5 and 10. Let's use an example to determine the associativity of the addition assignment operator. The code will return the value "Chloe Reid." Associativity of the += Operator ![]() To merge the two names, we'd use this code: forename += surname We do this because the addition operator will not add a space to the stored string. We can use the addition operator to do this.Īs you can see, the surname has a space at the beginning. We now want to add these names, or rather, append them and store it as one value. The code would look like this: forename = "Chloe" Then, we define a second variable to hold the user's surname. Let's say we define a variable that stores the user's first name. Using this operator instead of basing the code on the basic logic used in the previous program makes it more "Pythonic." Also, the program is much easier to read and quicker to write. The second statement will then add 7.5 to runningTotal.įinally, the runningTotal value will be printed, and you will see 17.5 appear in the console. Running this code will give you the same result as the program we wrote earlier.įirst, the value 10 will be assigned to runningTotal. Here's how you would use the addition assignment operator in the program: runningTotal = 10 # Assigning initial value Let's rewrite the program we discussed at the beginning of this post. Let's look at an example of both cases: Numerical Example ![]() On the other hand, if a number is initially assigned to a variable, the number after the equals sign will be added to the initially assigned number. Then, the value you write after the equals sign will be appended to the end of the string. Let's say you initially assign a string value to the variable. The number may be an integer or a floating-point number. It's important to note that the variable you set needs to either be a number or a string. Here's how you'll need to use it so it works correctly: exampleVariable += someValue Like any other operator, this one also has syntax associated with it. The operator is typically used to store sums of numbers in counter variables to keep track of the frequency of repetitions of a specific operation. For this reason, it's termed the "addition assignment" operator. The += operator is a pre-defined operator that adds two values and assigns the sum to a variable. The Python += Operator: Explanation and Examples Now, let's look at how the += operator can make this program easier to write. ![]() Then, we added 7.5 to the value in runningTotal and saved this sum in runningTotal.įinally, we printed the value of runningTotal.Īs you can guess, this program would print 17.5. In the first line, we assigned 10 to the runningTotal variable. Here's a simple program you could write: runningTotal = 10 # Assigning initial value Let's say you want to keep a running total of a couple of numbers. Of course, there are several operators in Python. The first thing that will likely strike you is the plus sign, which represents addition. Operators are symbols in Python that represent a pre-defined operation in the language. In this brief tutorial, we'll discuss what this operator is and illustrate how you can use it to add values and assign the result with examples. But the nice thing is that Python provides an operator to accomplish this quickly. When writing a program in Python, there's a good chance you will need to add two values and save the resulting value in a variable at some point.ĭoing this is pretty straightforward, even if you don't have much experience programming. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |