Python Arithmetic Operators
Arithmetic Operators in Python
Basic Arithmetic Operators
Python's arithmetic operators largely follow standard mathematical rules.
Addition: Uses the plus sign +.
Subtraction: Uses the minus sign −.
Multiplication: Uses the asterisk sign ∗.
Division: Uses the forward slash /.
Order of Operations
Python adheres to the mathematical order of operations.
Example: Multiplication happens before addition.
Parentheses can be used to override the default order.
Exponentiation
The exponentiation operator is represented by two asterisks: **$.
Example: 3**2calculates3tothepowerof2,resultingin9.</p></li></ul><h4collapsed="false"seolevelmigrated="true">BitwiseXOROperator</h4><ul><li><p>Thecaretsign^is<strong>not</strong>forexponentiation;it′sabitwiseXORoperator.</p></li><li><p>BitwiseXORisanarithmeticoperatorthatdoesn′tfollowstandardmathematicalrules.</p></li><li><p>It′snotcoveredinthiscourse,butfurtherinformationisavailableinthenotes.</p></li><li><p>Important:Using^insteadof∗∗ for exponentiation can lead to unexpected results.
Modulo Operator
Represented by the percent sign .
Returns the remainder after division.
Example: 9 (nine modulo two) results in 1 because 9 divided by 2 is 4 with a remainder of 1.
Integer Division
Denoted by two forward slashes: //$.
Divides one integer by another and rounds the result down to the nearest integer.
Example: 7 // 2(sevenintegerdividedbytwo)resultsin3,because7dividedby2is3.5,whichisroundeddownto3.</p></li><li><p>Roundingbehavior:Integerdivisionroundsdownevenfornegativenumbers.</p></li><li><p>Example:-7 // 2$$ (negative seven integer divided by two) results in -4, because -3.5 is rounded down to -4.
Other Operator Categories