Arithmetic Operators in Python

 


In Python, arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, etc. Below are the common arithmetic operators in Python:

Arithmetic Operators:

  • Addition (+) – Adds two numbers.

    a = 10

  • b = 5

  • result = a + b

  • print(result)  # Output: 15


  • Subtraction (-) – Subtracts the second number from the first.

    a = 10

  • b = 5

  • result = a - b

  • print(result)  # Output: 5


  • Multiplication (*) – Multiplies two numbers.

    a = 10

  • b = 5

  • result = a * b

  • print(result)  # Output: 50


  • Division (/) – Divides the first number by the second number (always returns a float).

    a = 10

  • b = 3

  • result = a / b

  • print(result)  # Output: 3.3333333333333335


  • Floor Division (//) – Divides the first number by the second number and returns the largest integer less than or equal to the result (rounded down).

    a = 10

  • b = 3

  • result = a // b

  • print(result)  # Output: 3


  • Modulus (%) – Returns the remainder of the division of two numbers.

    a = 10

  • b = 3

  • result = a % b

  • print(result)  # Output: 1


  • Exponentiation (**) – Raises the first number to the power of the second number.

    a = 2

  • b = 3

  • result = a ** b

  • print(result)  # Output: 8


Examples:

Example 1: Performing all arithmetic operations

  • a = 15

  • b = 4


  • addition = a + b

  • subtraction = a - b

  • multiplication = a * b

  • division = a / b

  • floor_division = a // b

  • modulus = a % b

  • exponentiation = a ** b


  • print("Addition:", addition)           # Output: 19

  • print("Subtraction:", subtraction)     # Output: 11

  • print("Multiplication:", multiplication) # Output: 60

  • print("Division:", division)           # Output: 3.75

  • print("Floor Division:", floor_division) # Output: 3

  • print("Modulus:", modulus)             # Output: 3

  • print("Exponentiation:", exponentiation) # Output: 50625


Example 2: Modulus and Floor Division

  • # Using modulus to find the remainder

  • x = 29

  • y = 5

  • remainder = x % y

  • print("Remainder:", remainder)  # Output: 4


  • # Using floor division

  • quotient = x // y

  • print("Quotient:", quotient)  # Output: 5


Order of Precedence:

Arithmetic operations follow a specific order of precedence (similar to the standard mathematical order of operations):

  1. Exponentiation (**)

  2. Multiplication, Division, Floor Division, Modulus (*, /, //, %)

  3. Addition, Subtraction (+, -)

Example:

  • # Parentheses can be used to alter the order of operations

  • result = 3 + 2 * 5

  • print(result)  # Output: 13 (Multiplication happens before addition)


  • # Using parentheses to force addition first

  • result = (3 + 2) * 5

  • print(result)  # Output: 25


Key Points:

  • Division (/) always returns a floating-point number, even if the result is a whole number.

  • Floor division (//) returns an integer, rounding down the result to the nearest whole number.

  • Modulus (%) provides the remainder of a division operation.

  • Exponentiation (**) is used to raise a number to a power.

Let me know if you'd like more details or further examples on any of these! 😊


Post a Comment

0 Comments