Python - Arithmetic Operators

Welcome, aspiring programmers! Today, we're diving into the world of Python arithmetic operators. Don't worry if you've never written a line of code before - we'll start from the very beginning and work our way up. By the end of this tutorial, you'll be performing calculations like a pro!

Python - Arithmetic Operators

Python Arithmetic Operators

Arithmetic operators in Python are like the basic math operations you learned in school, but now they're your tools for making computers do calculations. Let's start with the simplest ones:

Addition (+)

The addition operator is represented by the plus sign (+). It works just like you'd expect:

result = 5 + 3
print(result)  # Output: 8

In this example, we're adding 5 and 3, and storing the result in a variable called result. Then we're printing it out. Simple, right?

Subtraction (-)

Subtraction works similarly, using the minus sign (-):

result = 10 - 4
print(result)  # Output: 6

Here, we're subtracting 4 from 10. Easy peasy!

Multiplication (*)

For multiplication, we use the asterisk (*):

result = 6 * 7
print(result)  # Output: 42

In this case, we're multiplying 6 by 7. And yes, the answer to life, the universe, and everything is indeed 42!

Division (/)

Division uses the forward slash (/):

result = 20 / 5
print(result)  # Output: 4.0

Notice that the result is 4.0, not just 4. In Python 3, division with / always returns a float (decimal) number.

Floor Division (//)

Sometimes, you want to divide and get rid of the decimal part. That's where floor division comes in:

result = 17 // 5
print(result)  # Output: 3

17 divided by 5 is 3 with a remainder of 2. Floor division gives us just the 3.

Modulus (%)

Speaking of remainders, the modulus operator gives us exactly that:

result = 17 % 5
print(result)  # Output: 2

This gives us the remainder of 17 divided by 5, which is 2.

Exponentiation (**)

Last but not least, we have exponentiation, represented by two asterisks:

result = 2 ** 3
print(result)  # Output: 8

This calculates 2 to the power of 3, which is 8.

Different Arithmetic Operators in Python

Let's summarize all these operators in a handy table:

Operator Name Example
+ Addition 5 + 3 = 8
- Subtraction 10 - 4 = 6
* Multiplication 6 * 7 = 42
/ Division 20 / 5 = 4.0
// Floor Division 17 // 5 = 3
% Modulus 17 % 5 = 2
** Exponentiation 2 ** 3 = 8

Precedence and Associativity of Python Arithmetic Operators

Now, what happens when we use multiple operators in the same expression? This is where precedence and associativity come into play.

Operator Precedence

Operator precedence determines the order in which operations are performed. It's like the order of operations you learned in math class (remember PEMDAS?). In Python, the precedence from highest to lowest is:

  1. ** (Exponentiation)
  2. *, /, //, % (Multiplication, Division, Floor Division, Modulus)
  3. +, - (Addition, Subtraction)

Let's see an example:

result = 2 + 3 * 4
print(result)  # Output: 14

In this case, the multiplication (3 * 4) happens first, then the addition (2 + 12).

If we want to change the order, we can use parentheses:

result = (2 + 3) * 4
print(result)  # Output: 20

Now the addition happens first, then the multiplication.

Associativity

Associativity comes into play when you have multiple operators with the same precedence. In Python, most operators are left-associative, meaning they are evaluated from left to right.

result = 20 - 5 - 3
print(result)  # Output: 12

This is evaluated as (20 - 5) - 3, not 20 - (5 - 3).

The exception is the exponentiation operator (**), which is right-associative:

result = 2 ** 3 ** 2
print(result)  # Output: 512

This is evaluated as 2 (3 2), not (2 3) 2.

Complex Number Arithmetic

Python also supports complex numbers, which are numbers with a real and imaginary part. They're written with a 'j' or 'J' to represent the imaginary part:

z1 = 2 + 3j
z2 = 1 - 1j

# Addition
result = z1 + z2
print(result)  # Output: (3+2j)

# Subtraction
result = z1 - z2
print(result)  # Output: (1+4j)

# Multiplication
result = z1 * z2
print(result)  # Output: (5+1j)

# Division
result = z1 / z2
print(result)  # Output: (0.5+2j)

Complex numbers follow the same arithmetic rules as real numbers, with the added rule that i^2 = -1 (where i is the imaginary unit, represented by j in Python).

And there you have it! You've just taken your first steps into the world of Python arithmetic. Remember, programming is like learning a new language - it takes practice. So don't be afraid to experiment with these operators, try out different combinations, and see what happens. Happy coding!

Credits: Image by storyset