Learn How to Code with Python: Beginner to Advanced Guide

Arithmetic operators are the foundation of programming logic. In Python, they allow us to perform mathematical computations using symbols instead of words. The basic arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation (**). Each operator performs a specific mathematical task. Addition and subtraction behave exactly as expected. Multiplication multiplies numbers, while division (/) always produces a floating-point result, even if the division is exact. For example, 10 / 2 returns 5.0, not 5. Floor division (//) removes the decimal part and returns the integer portion of the result. Understanding these operators is essential before moving to more advanced concepts, because nearly every program performs arithmetic operations in some form. 1 An integer result only A floating-point result A string representation of the division What does the / operator return in Python? Operator precedence determines the order in which operations are evaluated in an expression. Python follows a strict hierarchy, similar to mathematics. If you ignore precedence rules, your results will be incorrect. Exponentiation (**) has higher precedence than multiplication and division. Multiplication (*), division (/), and floor division (//) come next. Addition (+) and subtraction (-) are evaluated after those. When multiple operators have the same precedence, Python evaluates them from left to right. For example, in the expression 2 + 3 * 4, multiplication happens first, resulting in 2 + 12, which equals 14. Not 20. 1 20 14 24 In the expression 2 + 3 * 4, what is the result? Parentheses override operator precedence. They allow programmers to explicitly control the order of evaluation. Anything inside parentheses is evaluated before the rest of the expression. For example, (2 + 3) * 4 equals 5 * 4, which equals 20. Without parentheses, the result would be 14. Parentheses eliminate ambiguity and make code easier to read. Using parentheses is considered good practice when clarity is more important than relying on implicit precedence rules. 1 14 20 24 What is the result of (2 + 3) * 4? The modulus operator (%) returns the remainder of a division. It does not return the quotient. This operator is frequently used to determine whether a number is even or odd. For example, 10 % 3 equals 1 because 3 goes into 10 three times with a remainder of 1. If a number % 2 equals 0, the number is even. If it equals 1, the number is odd. The modulus operator is also useful for cycling through values, such as wrapping around indices in loops. 1 3 1 0 What is the result of 10 % 3? Floor division (//) divides two numbers and rounds the result down to the nearest whole number. It does not behave the same as regular division. For example, 7 // 2 equals 3 because 7 divided by 2 is 3.5, and floor division removes the decimal part. This is especially useful when working with indices or discrete units. Keep in mind that floor division with negative numbers rounds down toward negative infinity, not toward zero. 1 3.5 3 4 What is the result of 7 // 2? Exponentiation (**) raises a number to the power of another number. It has higher precedence than multiplication and division. For example, 2 ** 3 equals 8. In the expression 2 * 3 ** 2, exponentiation happens first, so 3 ** 2 equals 9, and then 2 * 9 equals 18. This operator is critical for scientific calculations, growth formulas, and algorithmic complexity analysis. 1 36 18 12 What is the result of 2 * 3 ** 2? When multiple operators of the same precedence appear in an expression, Python evaluates them from left to right. This is called left associativity. For example, 10 - 3 - 2 is evaluated as (10 - 3) - 2. First 10 - 3 equals 7, then 7 - 2 equals 5. It is not evaluated as 10 - (3 - 2). Understanding associativity prevents logical errors in chained calculations. 1 9 5 3 What is the result of 10 - 3 - 2? Division by zero is not allowed in Python. If you attempt to divide by zero using /, //, or %, Python raises a ZeroDivisionError. For example, 5 / 0 produces an error immediately. This rule exists because division by zero is mathematically undefined. Programs must handle such cases using conditional logic to prevent runtime crashes. 2 It returns 0 It returns infinity It raises an error What happens if you execute 5 / 0? Complex expressions combine multiple operators and require careful attention to precedence rules. Consider the expression 5 + 2 * 3 ** 2. First, exponentiation: 3 ** 2 equals 9. Next, multiplication: 2 * 9 equals 18. Finally, addition: 5 + 18 equals 23. Breaking down expressions step by step ensures accuracy and avoids careless mistakes. 1 21 23 27 What is the result of 5 + 2 * 3 ** 2? Unary operators operate on a single operand. The most common arithmetic unary operator is the negative sign (-), which changes the sign of a number. For example, -5 + 3 equals -2. The unary minus has higher precedence than addition and subtraction but lower than exponentiation. In expressions like -3 ** 2, exponentiation happens first. So it becomes -(3 ** 2), which equals -9. 1 9 -9 6 What is the result of -3 ** 2?

Edit