Arithmetic and Assignment Operators
Arithmetic operators are used to perform mathematical calculations in Python, such as addition, subtraction, multiplication, division, finding remainders, floor division, and calculating powers. They form one of the most fundamental parts of Python programming because numerical calculations are required in almost every type of program. Assignment operators are used to assign values to variables and to update the values stored in variables. The basic assignment operator is =, while augmented assignment operators such as +=, -=, *=, and /= combine an arithmetic operation with assignment. Understanding these operators is essential for writing calculations, updating counters, processing values, and developing more efficient Python programs.
<!--StartFragment--><h1><strong>Topic 8: Arithmetic and Assignment Operators</strong></h1>
<h2><strong>Summary</strong></h2>
<p><strong>Arithmetic operators</strong> are used to perform mathematical calculations on values in Python. They allow programs to perform operations such as addition, subtraction, multiplication, division, finding remainders, floor division, and exponentiation. These operations are fundamental to programming because numerical calculations are required in applications ranging from simple mathematical programs to scientific computing, financial software, simulations, data processing, and automation.</p>
<p><strong>Assignment operators</strong> are used to assign values to variables and to update values that are already stored in variables. The basic assignment operator is <code data-start="713" data-end="716">=</code>, while augmented assignment operators such as <code data-start="763" data-end="767">+=</code>, <code data-start="769" data-end="773">-=</code>, <code data-start="775" data-end="779">*=</code>, <code data-start="781" data-end="785">/=</code>, <code data-start="787" data-end="791">%=</code>, <code data-start="793" data-end="798">//=</code>, and <code data-start="804" data-end="809">=</code> combine an arithmetic operation with assignment. Together, arithmetic and assignment operators provide the basic mechanism through which a program can calculate, store, and continuously update information.</p>
<h1><strong>1. Operators in Python</strong></h1>
<p>An <strong>operator</strong> is a special symbol or keyword that tells Python to perform a particular operation on one or more values. The values or expressions on which an operator operates are called <strong>operands</strong>. Operators form an important part of Python expressions because they allow programmers to manipulate data, perform calculations, compare values, make decisions, and work with collections.</p>
<p>For example, in the expression below, <code data-start="1476" data-end="1479">+</code> is the operator and <code data-start="1500" data-end="1503">a</code> and <code data-start="1508" data-end="1511">b</code> are the operands.</p>
<pre data-start="1531" data-end="1550"><pre><code>a + b</code></pre></pre>
<p>Python provides several categories of operators. These include arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, membership operators, and identity operators. Each category performs a different kind of operation.</p>
<p>In this topic, the focus is specifically on <strong>arithmetic operators and assignment operators</strong>.</p>
<h1><strong>2. Arithmetic Operators</strong></h1>
<p><strong>Arithmetic operators</strong> are used to perform mathematical operations on numerical values. They are primarily used with integers and floating-point numbers, although some arithmetic symbols can also have meanings with other Python data types.</p>
<p>Arithmetic operations are essential whenever a program needs to calculate a total, difference, average, percentage, distance, measurement, rate, financial amount, scientific value, or any other numerical result. Arithmetic expressions can contain constants, variables, and multiple operators.</p>
<p>Python provides the following fundamental arithmetic operators:</p>
<table><thead><tr><th>Operator</th><th>Name</th><th>Purpose</th></tr></thead><tbody><tr><td><code data-start="2595" data-end="2598">+</code></td><td>Addition</td><td>Adds values</td></tr><tr><td><code data-start="2628" data-end="2631">-</code></td><td>Subtraction</td><td>Subtracts one value from another</td></tr><tr><td><code data-start="2685" data-end="2688">*</code></td><td>Multiplication</td><td>Multiplies values</td></tr><tr><td><code data-start="2730" data-end="2733">/</code></td><td>Division</td><td>Performs ordinary division</td></tr><tr><td><code data-start="2778" data-end="2781">%</code></td><td>Modulus</td><td>Returns the remainder</td></tr><tr><td><code data-start="2820" data-end="2824">//</code></td><td>Floor Division</td><td>Returns the floor of the quotient</td></tr><tr><td><code data-start="2882" data-end="2886"></code></td><td>Exponentiation</td><td>Raises a value to a power</td></tr></tbody></table>
<h1><strong>3. Addition Operator <code data-start="2960" data-end="2963">+</code></strong></h1>
<p>The <strong>addition operator (<code data-start="2992" data-end="2995">+</code>)</strong> is used to add numerical values. It produces the mathematical sum of its operands and is one of the most frequently used arithmetic operators in Python.</p>
<p>Addition is used whenever quantities need to be combined. Common applications include calculating totals, adding marks, combining measurements, calculating accumulated values, and processing numerical data.</p>
<p>The operator can also be used with some non-numeric data types. For example, Python allows strings to be joined using <code data-start="3479" data-end="3482">+</code>. However, within arithmetic operations, its primary purpose is numerical addition.</p>
<pre data-start="3567" data-end="3600"><pre><code>total = price + tax</code></pre></pre>
<p>The expression on the right side is evaluated first, and its result is then assigned to <code data-start="3690" data-end="3697">total</code>.</p>
<p>Addition is a <strong>commutative operation</strong> for numerical values, meaning that changing the order of the operands does not change the mathematical result. However, this property should not automatically be assumed for every type that supports the <code data-start="3943" data-end="3946">+</code> operator because the meaning of an operator can depend on the data type involved.</p>
<h1><strong>4. Subtraction Operator <code data-start="4058" data-end="4061">-</code></strong></h1>
<p>The <strong>subtraction operator (<code data-start="4093" data-end="4096">-</code>)</strong> is used to subtract one numerical value from another. The right-hand operand is subtracted from the left-hand operand.</p>
<p>Subtraction is commonly used to determine differences, remaining quantities, changes in values, profit or loss, distance between numerical measurements, and many other relationships between values.</p>
<p>Unlike addition, subtraction is <strong>not commutative</strong>. This means that changing the order of the operands generally changes the result. Therefore, the order in which values appear in a subtraction expression is important.</p>
<pre data-start="4640" data-end="4678"><pre><code>remaining = total - used</code></pre></pre>
<p>Here, Python subtracts the value of <code data-start="4716" data-end="4722">used</code> from the value of <code data-start="4741" data-end="4748">total</code> and stores the resulting value in <code data-start="4783" data-end="4794">remaining</code>.</p>
<p>Subtraction can produce positive, zero, or negative results depending on the relationship between the operands. This makes it useful for representing changes and differences where a value may decrease below zero.</p>
<h1><strong>5. Multiplication Operator <code data-start="5042" data-end="5045">*</code></strong></h1>
<p>The <strong>multiplication operator (<code data-start="5080" data-end="5083">*</code>)</strong> is used to multiply numerical values. The result of multiplication is called the <strong>product</strong>.</p>
<p>Multiplication is useful when a value needs to be scaled or when the same quantity is considered multiple times. It is commonly used for calculating total prices, areas, distances, quantities, mathematical formulas, and many other numerical relationships.</p>
<pre data-start="5439" data-end="5477"><pre><code>total = price * quantity</code></pre></pre>
<p>In this expression, the values represented by <code data-start="5525" data-end="5532">price</code> and <code data-start="5537" data-end="5547">quantity</code> are multiplied, and the resulting product is assigned to <code data-start="5605" data-end="5612">total</code>.</p>
<p>The multiplication operator can also be used with certain non-numeric types. For example, Python allows a string or sequence to be repeated by multiplying it by an integer. However, this behavior is separate from numerical multiplication and should be understood according to the data type involved.</p>
<h1><strong>6. Division Operator <code data-start="5941" data-end="5944">/</code></strong></h1>
<p>The <strong>division operator (<code data-start="5973" data-end="5976">/</code>)</strong> is used to divide one numerical value by another. It represents ordinary mathematical division and preserves the fractional part of the result.</p>
<p>One important characteristic of Python's <code data-start="6166" data-end="6169">/</code> operator is that it produces a <strong>floating-point result</strong>. Even when both operands are integers and the mathematical result is a whole number, ordinary division still produces a floating-point value.</p>
<pre data-start="6370" data-end="6399"><pre><code>result = 10 / 2</code></pre></pre>
<p>The value of <code data-start="6414" data-end="6422">result</code> is <code data-start="6426" data-end="6431">5.0</code>.</p>
<p>This behavior distinguishes ordinary division from floor division. Ordinary division is appropriate when the exact quotient, including its fractional portion, is required.</p>
<p>Division is widely used in calculations involving averages, ratios, rates, percentages, measurements, scientific formulas, and proportional relationships.</p>
<h2><strong>Division by Zero</strong></h2>
<p>Division by zero is mathematically undefined and is not permitted by Python. If a program attempts to divide a number by zero, Python raises a <code data-start="6931" data-end="6950">ZeroDivisionError</code>.</p>
<pre data-start="6953" data-end="6982"><pre><code>result = 10 / 0</code></pre></pre>
<p>Therefore, when a divisor can come from user input, a file, or another external source, a program may need to verify that the divisor is not zero before performing the operation.</p>
<h1><strong>7. Modulus Operator <code data-start="7188" data-end="7191">%</code></strong></h1>
<p>The <strong>modulus operator (<code data-start="7219" data-end="7222">%</code>)</strong> returns the remainder obtained after dividing one numerical value by another. Unlike ordinary division, which produces the quotient, modulus focuses on what remains after the division.</p>
<p>The modulus operation is particularly important in programming because remainders are useful for identifying patterns and controlling repeated operations.</p>
<pre data-start="7568" data-end="7600"><pre><code>remainder = 17 % 5</code></pre></pre>
<p>The result is <code data-start="7616" data-end="7619">2</code> because dividing 17 by 5 leaves a remainder of 2.</p>
<p>The modulus operator has many practical applications. It can be used to determine whether a number is even or odd, check whether one number is exactly divisible by another, work with repeating cycles, create periodic behavior, and perform calculations where only the remainder is important.</p>
<p>For example, when checking whether a number is even, the remainder after division by 2 can be examined. If that remainder is zero, the number is even.</p>
<p>The modulus operator also follows Python's rules for negative operands, so its behavior should be understood in relation to Python's definition of division and remainder rather than being treated as simple digit extraction.</p>
<h1><strong>8. Floor Division Operator <code data-start="8371" data-end="8375">//</code></strong></h1>
<p>The <strong>floor division operator (<code data-start="8410" data-end="8414">//</code>)</strong> performs division and returns the <strong>floor of the resulting quotient</strong>.</p>
<p>Flooring means moving to the greatest integer that is less than or equal to the mathematical result. This is an important distinction because floor division does not simply remove the decimal portion of a number.</p>
<p>For positive values, floor division often appears similar to truncating the decimal portion. However, the difference becomes particularly important with negative values because flooring moves toward negative infinity.</p>
<pre data-start="8923" data-end="8953"><pre><code>result = 17 // 5</code></pre></pre>
<p>The mathematical quotient is 3.4, so the floor of the quotient is 3.</p>
<p>With negative values, the distinction is more noticeable. For example, the mathematical quotient of <code data-start="9125" data-end="9134">-17 / 5</code> is <code data-start="9138" data-end="9144">-3.4</code>, and its floor is <code data-start="9163" data-end="9167">-4</code>.</p>
<p>Therefore, floor division should be understood as <strong>flooring the quotient</strong>, not merely deleting everything after the decimal point.</p>
<p>Floor division is useful when a program needs a whole-number result based on the floor of a division operation, such as determining complete groups, positions, intervals, or other quantities where fractional portions are not required.</p>
<h1><strong>9. Exponentiation Operator <code data-start="9571" data-end="9575"></code></strong></h1>
<p>The <strong>exponentiation operator (<code data-start="9610" data-end="9614"></code>)</strong> is used to raise one value to the power of another. The left operand acts as the <strong>base</strong>, while the right operand acts as the <strong>exponent</strong>.</p>
<p>Exponentiation represents repeated multiplication for positive integer exponents and is widely used in mathematical, scientific, engineering, and computational applications.</p>
<pre data-start="9935" data-end="9964"><pre><code>result = 2 3</code></pre></pre>
<p>The result is 8 because the calculation represents 2 raised to the third power.</p>
<p>Python also supports zero, negative, and fractional exponents. Depending on the operands, the resulting value may be an integer or a floating-point number.</p>
<p>Exponentiation is useful for calculating powers, squares, cubes, exponential growth, scientific formulas, geometric calculations, and mathematical algorithms.</p>
<h1><strong>10. Arithmetic Operator Summary</strong></h1>
<table><thead><tr><th>Operator</th><th>Operation</th><th>General Purpose</th></tr></thead><tbody><tr><td><code data-start="10462" data-end="10465">+</code></td><td>Addition</td><td>Calculates the sum</td></tr><tr><td><code data-start="10502" data-end="10505">-</code></td><td>Subtraction</td><td>Calculates the difference</td></tr><tr><td><code data-start="10552" data-end="10555">*</code></td><td>Multiplication</td><td>Calculates the product</td></tr><tr><td><code data-start="10602" data-end="10605">/</code></td><td>Division</td><td>Calculates the ordinary quotient</td></tr><tr><td><code data-start="10656" data-end="10659">%</code></td><td>Modulus</td><td>Calculates the remainder</td></tr><tr><td><code data-start="10701" data-end="10705">//</code></td><td>Floor Division</td><td>Calculates the floor of the quotient</td></tr><tr><td><code data-start="10766" data-end="10770"></code></td><td>Exponentiation</td><td>Calculates a power</td></tr></tbody></table>
<h1><strong>11. Arithmetic Expressions</strong></h1>
<p>An <strong>arithmetic expression</strong> is a combination of values, variables, and arithmetic operators that produces a result. Expressions are one of the fundamental building blocks of Python programs because they allow mathematical calculations to be represented directly in code.</p>
<p>An expression may contain a single operation or several operations combined together. Python evaluates the expression according to its operator precedence and associativity rules.</p>
<pre data-start="11300" data-end="11332"><pre><code>result = a + b * c</code></pre></pre>
<p>In this expression, Python does not simply perform the operations from left to right. It follows the rules that determine which operation should be performed first.</p>
<p>Arithmetic expressions make programs more powerful because complex calculations can be represented using a compact and structured form. They are commonly used on the right side of assignment statements, inside function arguments, in conditions, and in calculations throughout a program.</p>
<h1><strong>12. Operator Precedence in Arithmetic Expressions</strong></h1>
<p><strong>Operator precedence</strong> determines the order in which different operators are evaluated when they appear together in an expression.</p>
<p>Without precedence rules, an expression containing several operators could have multiple possible interpretations. Python therefore follows a defined order so that the same expression produces a predictable result.</p>
<p>For the arithmetic operators covered in this topic, the important precedence order is generally:</p>
<ol>
<li>
Parentheses
</li>
<li>
Exponentiation <code data-start="12325" data-end="12329"></code>
</li>
<li>
Multiplication <code data-start="12348" data-end="12351">*</code>, division <code data-start="12362" data-end="12365">/</code>, floor division <code data-start="12382" data-end="12386">//</code>, and modulus <code data-start="12400" data-end="12403">%</code>
</li>
<li>
Addition <code data-start="12416" data-end="12419">+</code> and subtraction <code data-start="12436" data-end="12439">-</code>
</li>
</ol>
<p>For example, multiplication has a higher precedence than addition. Therefore, in an expression containing both operations, multiplication is performed before addition unless parentheses change the order.</p>
<pre data-start="12646" data-end="12679"><pre><code>result = 10 + 5 * 2</code></pre></pre>
<p>The multiplication is performed first, followed by the addition.</p>
<p>Parentheses can be used to explicitly control the order of evaluation. They are particularly important when an expression represents a mathematical formula or when the intended order is not immediately obvious.</p>
<pre data-start="12959" data-end="12994"><pre><code>result = (10 + 5) * 2</code></pre></pre>
<p>Here, the expression inside the parentheses is evaluated first.</p>
<p>Using parentheses can improve both correctness and readability because it makes the programmer's intended order of calculation clear.</p>
<h1><strong>13. Associativity of Arithmetic Operators</strong></h1>
<p>When multiple operators with the same precedence appear in an expression, Python uses <strong>associativity rules</strong> to determine their evaluation order.</p>
<p>Most arithmetic operators at the same precedence level, including multiplication, division, floor division, modulus, addition, and subtraction, are evaluated from <strong>left to right</strong>.</p>
<p>Exponentiation is an important exception. The exponentiation operator has <strong>right-to-left associativity</strong>, which means chained exponentiation is evaluated from the right side.</p>
<p>Understanding associativity is important when expressions contain several operators with the same precedence. It allows programmers to predict how Python will interpret an expression without relying on guesswork.</p>
<h1><strong>14. Assignment Operators</strong></h1>
<p>An <strong>assignment operator</strong> is used to assign a value to a variable. Assignment is one of the most fundamental operations in Python because programs constantly need to store information so that it can be used later.</p>
<p>The basic assignment operator is <code data-start="14248" data-end="14251">=</code>. It takes the value or expression on the right side, evaluates it, and assigns the resulting value to the name on the left side.</p>
<pre data-start="14382" data-end="14406"><pre><code>marks = 85</code></pre></pre>
<p>Here, Python evaluates the right-hand side and associates the resulting value with the variable <code data-start="14504" data-end="14511">marks</code>.</p>
<p>Assignment is different from mathematical equality. The <code data-start="14570" data-end="14573">=</code> symbol tells Python to perform an assignment; it does not ask whether two values are equal.</p>
<h1><strong>15. Basic Assignment Operator <code data-start="14701" data-end="14704">=</code></strong></h1>
<p>The basic assignment operator <code data-start="14738" data-end="14741">=</code> is used to assign a value to a variable.</p>
<p>The general structure is:</p>
<pre data-start="14811" data-end="14841"><pre><code>variable = value</code></pre></pre>
<p>The right-hand side can contain a literal value, another variable, or a complete expression. Python evaluates the right-hand side first and then performs the assignment.</p>
<pre data-start="15014" data-end="15052"><pre><code>total = price * quantity</code></pre></pre>
<p>Here, Python first evaluates <code data-start="15083" data-end="15101">price * quantity</code> and then assigns the resulting value to <code data-start="15142" data-end="15149">total</code>.</p>
<p>This means assignment is not limited to storing simple values. It can also be used to store the results of calculations and other expressions.</p>
<p>Assignment statements are therefore central to Python programming because they allow information to be stored, reused, and modified throughout the execution of a program.</p>
<h1><strong>16. Multiple Assignment</strong></h1>
<p>Python supports <strong>multiple assignment</strong>, which allows several variables to receive values within a single statement.</p>
<pre data-start="15617" data-end="15651"><pre><code>a, b, c = 10, 20, 30</code></pre></pre>
<p>The values are assigned to the corresponding variables in order.</p>
<p>Python also allows the same value to be assigned to multiple variables using a chained assignment.</p>
<pre data-start="15819" data-end="15846"><pre><code>a = b = c = 0</code></pre></pre>
<p>Multiple assignment can make initialization more concise and readable when several related variables need to be created or updated together.</p>
<p>It is important to understand that multiple assignment does not remove the concept of individual variables. Each variable still refers to its assigned value; the syntax simply allows several assignments to be expressed in a compact form.</p>
<h1><strong>17. Augmented Assignment Operators</strong></h1>
<p>Python provides <strong>augmented assignment operators</strong> that combine an operation with assignment. They are especially useful when an existing variable needs to be modified using its current value.</p>
<p>For example:</p>
<pre data-start="16479" data-end="16499"><pre><code>x += 5</code></pre></pre>
<p>This is conceptually equivalent to:</p>
<pre data-start="16538" data-end="16561"><pre><code>x = x + 5</code></pre></pre>
<p>The augmented form communicates clearly that the existing value of <code data-start="16630" data-end="16633">x</code> is being increased by 5 and that the updated result is stored back in <code data-start="16704" data-end="16707">x</code>.</p>
<p>The arithmetic augmented assignment operators are:</p>
<table><thead><tr><th>Operator</th><th>Meaning</th><th>Conceptual Equivalent</th></tr></thead><tbody><tr><td><code data-start="16825" data-end="16829">+=</code></td><td>Add and assign</td><td><code data-start="16849" data-end="16864">x = x + value</code></td></tr><tr><td><code data-start="16869" data-end="16873">-=</code></td><td>Subtract and assign</td><td><code data-start="16898" data-end="16913">x = x - value</code></td></tr><tr><td><code data-start="16918" data-end="16922">*=</code></td><td>Multiply and assign</td><td><code data-start="16947" data-end="16962">x = x * value</code></td></tr><tr><td><code data-start="16967" data-end="16971">/=</code></td><td>Divide and assign</td><td><code data-start="16994" data-end="17009">x = x / value</code></td></tr><tr><td><code data-start="17014" data-end="17018">%=</code></td><td>Modulus and assign</td><td><code data-start="17042" data-end="17057">x = x % value</code></td></tr><tr><td><code data-start="17062" data-end="17067">//=</code></td><td>Floor divide and assign</td><td><code data-start="17096" data-end="17112">x = x // value</code></td></tr><tr><td><code data-start="17117" data-end="17122">=</code></td><td>Exponentiate and assign</td><td><code data-start="17151" data-end="17167">x = x value</code></td></tr></tbody></table>
<p>Augmented assignment is particularly useful in programs where values are repeatedly updated, such as counters, totals, accumulated results, scores, quantities, and numerical calculations.</p>
<h1><strong>18. Addition Assignment <code data-start="17388" data-end="17392">+=</code></strong></h1>
<p>The <strong><code data-start="17402" data-end="17406">+=</code> operator</strong> adds a value to the current value of a variable and assigns the resulting value back to that variable.</p>
<p>Conceptually:</p>
<pre data-start="17537" data-end="17557"><pre><code>x += 5</code></pre></pre>
<p>means:</p>
<pre data-start="17567" data-end="17590"><pre><code>x = x + 5</code></pre></pre>
<p>The important idea is that the existing value of <code data-start="17641" data-end="17644">x</code> participates in the calculation. The result then replaces the previous value associated with <code data-start="17738" data-end="17741">x</code>.</p>
<p>This operator is commonly used when a numerical quantity needs to increase over time. Counters, accumulated totals, scores, and running calculations are common situations in which addition assignment is useful.</p>
<h1><strong>19. Subtraction Assignment <code data-start="17987" data-end="17991">-=</code></strong></h1>
<p>The <strong><code data-start="18001" data-end="18005">-=</code> operator</strong> subtracts a value from the current value of a variable and assigns the resulting value back to that variable.</p>
<p>Conceptually:</p>
<pre data-start="18143" data-end="18163"><pre><code>x -= 5</code></pre></pre>
<p>means:</p>
<pre data-start="18173" data-end="18196"><pre><code>x = x - 5</code></pre></pre>
<p>This operator is useful when an existing quantity needs to decrease. It can be used for counters, remaining quantities, resource tracking, balances, and other situations where a value is progressively reduced.</p>
<p>Using <code data-start="18415" data-end="18419">-=</code> also makes the intention of the operation clear because the statement directly communicates that the existing value is being decreased.</p>
<h1><strong>20. Multiplication Assignment <code data-start="18591" data-end="18595">*=</code></strong></h1>
<p>The <strong><code data-start="18605" data-end="18609">*=</code> operator</strong> multiplies the current value of a variable by another value and assigns the result back to the same variable.</p>
<p>Conceptually:</p>
<pre data-start="18747" data-end="18767"><pre><code>x *= 5</code></pre></pre>
<p>means:</p>
<pre data-start="18777" data-end="18800"><pre><code>x = x * 5</code></pre></pre>
<p>Multiplication assignment is useful when an existing value needs to be scaled. It can appear in mathematical calculations, repeated scaling operations, growth calculations, and numerical transformations.</p>
<p>The important feature of this operator is that it performs both the multiplication and the assignment as one operation.</p>
<h1><strong>21. Division Assignment <code data-start="19156" data-end="19160">/=</code></strong></h1>
<p>The <strong><code data-start="19170" data-end="19174">/=</code> operator</strong> divides the current value of a variable by another value and assigns the resulting quotient back to the variable.</p>
<p>Conceptually:</p>
<pre data-start="19316" data-end="19336"><pre><code>x /= 5</code></pre></pre>
<p>means:</p>
<pre data-start="19346" data-end="19369"><pre><code>x = x / 5</code></pre></pre>
<p>Because ordinary division using <code data-start="19403" data-end="19406">/</code> produces a floating-point result, using <code data-start="19447" data-end="19451">/=</code> may change the resulting value to a floating-point representation.</p>
<p>Division assignment is useful when an existing quantity needs to be scaled down through division or when a sequence of calculations repeatedly reduces a value.</p>
<p>As with ordinary division, the divisor must not be zero.</p>
<h1><strong>22. Modulus Assignment <code data-start="19766" data-end="19770">%=</code></strong></h1>
<p>The <strong><code data-start="19780" data-end="19784">%=</code> operator</strong> performs a modulus operation using the current value of a variable and then assigns the resulting remainder back to that variable.</p>
<p>Conceptually:</p>
<pre data-start="19943" data-end="19963"><pre><code>x %= 5</code></pre></pre>
<p>means:</p>
<pre data-start="19973" data-end="19996"><pre><code>x = x % 5</code></pre></pre>
<p>This operator is useful when the remainder of a calculation is required and the variable should subsequently contain that remainder.</p>
<p>It can be useful in cyclic calculations, remainder-based processing, and numerical algorithms where the original value is no longer required after the modulus operation.</p>
<h1><strong>23. Floor Division Assignment <code data-start="20337" data-end="20342">//=</code></strong></h1>
<p>The <strong><code data-start="20352" data-end="20357">//=</code> operator</strong> performs floor division on the current value of a variable and assigns the resulting floor value back to that variable.</p>
<p>Conceptually:</p>
<pre data-start="20505" data-end="20526"><pre><code>x //= 5</code></pre></pre>
<p>means:</p>
<pre data-start="20536" data-end="20560"><pre><code>x = x // 5</code></pre></pre>
<p>It is useful when a program needs to repeatedly perform floor division while updating the same variable.</p>
<p>Because floor division rounds toward negative infinity rather than simply removing the decimal portion, its behavior should be understood carefully when negative values are involved.</p>
<h1><strong>24. Exponentiation Assignment <code data-start="20886" data-end="20891">=</code></strong></h1>
<p>The <strong><code data-start="20901" data-end="20906">=</code> operator</strong> performs exponentiation on the current value of a variable and assigns the result back to that variable.</p>
<p>Conceptually:</p>
<pre data-start="21039" data-end="21060"><pre><code>x = 2</code></pre></pre>
<p>means:</p>
<pre data-start="21070" data-end="21094"><pre><code>x = x 2</code></pre></pre>
<p>This operator is useful when an existing value needs to be raised to a particular power. It provides a concise way to perform exponentiation while updating the original variable.</p>
<p>Exponentiation assignment can be useful in mathematical calculations and algorithms where a value is repeatedly transformed using powers.</p>
<h1><strong>25. Arithmetic and Assignment Operators Together</strong></h1>
<p>Arithmetic and assignment operators are frequently used together because calculations normally need to produce results that are stored somewhere.</p>
<p>A common programming pattern is to assign an initial value, perform an arithmetic operation, and then update the variable with the result.</p>
<pre data-start="21758" data-end="21807"><pre><code>total = 100
total += 50
total -= 20</code></pre></pre>
<p>In this type of operation, the variable is continuously updated. There is no need to create a separate variable for every intermediate result.</p>
<p>This pattern is particularly important in loops, counters, simulations, calculations, data processing, and programs that continuously modify information. It represents one of the most common relationships between arithmetic and assignment in practical programming.</p>
<h1><strong>26. Difference Between <code data-start="22246" data-end="22249">=</code> and <code data-start="22254" data-end="22258">==</code></strong></h1>
<p>The assignment operator <code data-start="22286" data-end="22289">=</code> must be clearly distinguished from the equality comparison operator <code data-start="22358" data-end="22362">==</code>.</p>
<p>The <code data-start="22369" data-end="22372">=</code> operator assigns a value to a variable.</p>
<pre data-start="22414" data-end="22434"><pre><code>x = 10</code></pre></pre>
<p>The <code data-start="22440" data-end="22444">==</code> operator, on the other hand, checks whether two values are equal. It belongs to the category of <strong>comparison operators</strong>, which is a separate topic.</p>
<p>Therefore, the two symbols have completely different purposes:</p>
<table><thead><tr><th>Operator</th><th>Purpose</th></tr></thead><tbody><tr><td><code data-start="22694" data-end="22697">=</code></td><td>Assignment</td></tr><tr><td><code data-start="22715" data-end="22719">==</code></td><td>Equality comparison</td></tr></tbody></table>
<p>Understanding this distinction is essential because confusing assignment with comparison can lead to incorrect programs or syntax errors depending on how the symbols are used.</p>
<h1><strong>27. Arithmetic Operators with Variables</strong></h1>
<p>Arithmetic operators are most useful in practical programs when they operate on values stored in variables. Variables allow calculations to work with changing information rather than fixed values.</p>
<p>For example, a program calculating the total cost of products does not need to know the exact price and quantity in advance. It can store these values in variables and apply the multiplication operator to them.</p>
<pre data-start="23379" data-end="23441"><pre><code>price = 50
quantity = 4
total = price * quantity</code></pre></pre>
<p>The same calculation can then work with different values without changing the structure of the calculation itself.</p>
<p>This illustrates an important principle of programming: <strong>the operation and the data can be separated</strong>. The program defines how the calculation should be performed, while variables provide the values used in that calculation.</p>
<h1><strong>28. Important Considerations When Using Arithmetic Operators</strong></h1>
<p>Arithmetic operators are generally straightforward, but several details are important for writing correct Python programs.</p>
<h3><strong>Division by Zero</strong></h3>
<p>Division and modulus operations cannot use zero as the divisor. Attempting such an operation raises a <code data-start="24107" data-end="24126">ZeroDivisionError</code>.</p>
<pre data-start="24129" data-end="24162"><pre><code>result = number / 0</code></pre></pre>
<p>Programs that receive divisors from users or external sources should therefore account for the possibility of zero.</p>
<h3><strong>Result Data Types</strong></h3>
<p>The type of a result depends on the operation and its operands. Ordinary division using <code data-start="24396" data-end="24399">/</code> produces a floating-point result, while floor division has different behavior because it calculates the floor of the quotient.</p>
<p>Understanding result types is important because later calculations may depend on whether a value is an integer or floating-point number.</p>
<h3><strong>Floating-Point Precision</strong></h3>
<p>Floating-point numbers are represented internally using binary approximations. As a result, some decimal values cannot be represented with perfect precision.</p>
<p>This means that certain floating-point calculations may produce a result that appears slightly different from the exact decimal value expected mathematically. This is a normal characteristic of computer arithmetic rather than a problem specific to Python.</p>
<p>When extremely precise decimal calculations are required, especially in areas such as financial software, appropriate numerical techniques or specialized decimal types may be necessary.</p>
<h3><strong>Large Numerical Calculations</strong></h3>
<p>Python integers can represent very large whole numbers, which makes Python suitable for many calculations involving large integer values. However, extremely large calculations can still require considerable memory and processing time.</p>
<p>Therefore, the ability to represent large numbers does not mean that every extremely large calculation is computationally inexpensive.</p>
<h1><strong>29. Arithmetic and Assignment Operator Comparison</strong></h1>
<table><thead><tr><th>Category</th><th>Operator</th><th>Main Purpose</th></tr></thead><tbody><tr><td>Arithmetic</td><td><code data-start="25838" data-end="25841">+</code></td><td>Addition</td></tr><tr><td>Arithmetic</td><td><code data-start="25870" data-end="25873">-</code></td><td>Subtraction</td></tr><tr><td>Arithmetic</td><td><code data-start="25905" data-end="25908">*</code></td><td>Multiplication</td></tr><tr><td>Arithmetic</td><td><code data-start="25943" data-end="25946">/</code></td><td>Division</td></tr><tr><td>Arithmetic</td><td><code data-start="25975" data-end="25978">%</code></td><td>Remainder</td></tr><tr><td>Arithmetic</td><td><code data-start="26008" data-end="26012">//</code></td><td>Floor division</td></tr><tr><td>Arithmetic</td><td><code data-start="26047" data-end="26051"></code></td><td>Exponentiation</td></tr><tr><td>Assignment</td><td><code data-start="26086" data-end="26089">=</code></td><td>Assign a value</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26134" data-end="26138">+=</code></td><td>Add and assign</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26183" data-end="26187">-=</code></td><td>Subtract and assign</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26237" data-end="26241">*=</code></td><td>Multiply and assign</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26291" data-end="26295">/=</code></td><td>Divide and assign</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26343" data-end="26347">%=</code></td><td>Modulus and assign</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26396" data-end="26401">//=</code></td><td>Floor divide and assign</td></tr><tr><td>Augmented Assignment</td><td><code data-start="26455" data-end="26460">=</code></td><td>Exponentiate and assign</td></tr></tbody></table>
<h1><strong>30. Importance of Arithmetic and Assignment Operators</strong></h1>
<p>Arithmetic and assignment operators are fundamental to Python programming because almost every meaningful program needs to calculate values and store or update information.</p>
<p>Arithmetic operators provide the mathematical operations required to process numerical information. They allow programs to calculate totals, differences, averages, percentages, distances, measurements, rates, financial values, scientific quantities, and many other results.</p>
<p>Assignment operators provide the mechanism for storing those results in variables. They also allow existing values to be updated as a program executes. Without assignment, the results of many calculations would not be available for later operations.</p>
<p>The combination of these two operator categories creates a basic computational pattern:</p>
<p><strong>Store a value → Perform a calculation → Update or store the result → Use the result in further calculations.</strong></p>
<p>This pattern appears throughout programming. It can be found in simple mathematical programs as well as in complex applications involving simulations, data processing, games, scientific computing, automation, and software systems.</p>
<h1><strong>31. Key Points</strong></h1>
<ul>
<li>
<strong>Arithmetic operators</strong> perform mathematical operations on values.
</li>
<li>
<strong><code data-start="27782" data-end="27785">+</code></strong> performs addition.
</li>
<li>
<strong><code data-start="27811" data-end="27814">-</code></strong> performs subtraction.
</li>
<li>
<strong><code data-start="27843" data-end="27846">*</code></strong> performs multiplication.
</li>
<li>
<strong><code data-start="27878" data-end="27881">/</code></strong> performs ordinary division and produces a floating-point result.
</li>
<li>
<strong><code data-start="27953" data-end="27956">%</code></strong> returns the remainder after division.
</li>
<li>
<strong><code data-start="28001" data-end="28005">//</code></strong> performs floor division.
</li>
<li>
<code data-start="28037" data-end="28041"></code> performs exponentiation.
</li>
<li>
<strong><code data-start="28071" data-end="28074">=</code></strong> is the basic assignment operator.
</li>
<li>
<strong>Augmented assignment operators</strong> combine an operation with assignment.
</li>
<li>
<strong><code data-start="28190" data-end="28194">+=</code></strong> adds and assigns.
</li>
<li>
<strong><code data-start="28219" data-end="28223">-=</code></strong> subtracts and assigns.
</li>
<li>
<strong><code data-start="28253" data-end="28257">*=</code></strong> multiplies and assigns.
</li>
<li>
<strong><code data-start="28288" data-end="28292">/=</code></strong> divides and assigns.
</li>
<li>
<strong><code data-start="28320" data-end="28324">%=</code></strong> performs modulus and assigns.
</li>
<li>
<strong><code data-start="28361" data-end="28366">//=</code></strong> performs floor division and assigns.
</li>
<li>
<strong><code data-start="28410" data-end="28415">=</code></strong> performs exponentiation and assigns.
</li>
<li>
<strong>Parentheses</strong> can be used to control the order of arithmetic operations.
</li>
<li>
<strong>Division by zero</strong> raises <code data-start="28562" data-end="28581">ZeroDivisionError</code>.
</li>
<li>
<strong><code data-start="28587" data-end="28590">=</code> and <code data-start="28595" data-end="28599">==</code> have different purposes</strong>: <code data-start="28627" data-end="28630">=</code> performs assignment, while <code data-start="28658" data-end="28662">==</code> performs equality comparison.
</li>
<li>
Arithmetic operators calculate values, while assignment operators store or update values.
</li>
</ul>
<h2><strong>Quick Revision</strong></h2>
<p><strong>Arithmetic Operators:</strong> <code data-start="28835" data-end="28838">+</code>, <code data-start="28840" data-end="28843">-</code>, <code data-start="28845" data-end="28848">*</code>, <code data-start="28850" data-end="28853">/</code>, <code data-start="28855" data-end="28858">%</code>, <code data-start="28860" data-end="28864">//</code>, <code data-start="28866" data-end="28870"></code></p>
<p><strong>Assignment Operators:</strong> <code data-start="28898" data-end="28901">=</code>, <code data-start="28903" data-end="28907">+=</code>, <code data-start="28909" data-end="28913">-=</code>, <code data-start="28915" data-end="28919">*=</code>, <code data-start="28921" data-end="28925">/=</code>, <code data-start="28927" data-end="28931">%=</code>, <code data-start="28933" data-end="28938">//=</code>, <code data-start="28940" data-end="28945">=</code></p>
<p><strong>Main idea:</strong> <strong>Arithmetic operators perform calculations, while assignment operators assign, store, or update values.</strong></p><!--EndFragment--><!--EndFragment-->
