Published on

# Introduction to Python Operators

Authors ## Basic Operators

Operators in Python allow you to perform various operations on values and variables, enabling you to manipulate data and control program flow. In this post, we will explore some of the basic operators in Python.

### Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. They include addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), floor division (`//`), modulo (remainder) (`%`), and exponentiation (`**`).

For example:

python

``````x = 10
y = 3

result = x + y  # 10 + 3 = 13

# Subtraction
result = x - y  # 10 - 3 = 7

# Multiplication
result = x * y  # 10 * 3 = 30

# Division
result = x / y  # 10 / 3 = 3.333...

# Floor Division
result = x // y  # 10 // 3 = 3 (returns the quotient as an integer)

# Modulo (Remainder)
result = x % y  # 10 % 3 = 1 (returns the remainder)

# Exponentiation
result = x ** y  # 10 ** 3 = 1000
``````

### Comparison Operators

Comparison operators are used to compare values and determine the relationship between them. They include equal to (`==`), not equal to (`!=`), greater than (`>`), less than (`<`), greater than or equal to (`>=`), and less than or equal to (`<=`).

For example:

python

``````x = 5
y = 3

# Equal to
result = x == y  # False

# Not equal to
result = x != y  # True

# Greater than
result = x > y  # True

# Less than
result = x < y  # False

# Greater than or equal to
result = x >= y  # True

# Less than or equal to
result = x <= y  # False
``````

### Assignment Operators

Assignment operators are used to assign values to variables. They combine the assignment operator (`=`) with other operators. For example:

python

``````x = 10
y = 5

x += y  # Equivalent to x = x + y

# Subtraction assignment
x -= y  # Equivalent to x = x - y

# Multiplication assignment
x *= y  # Equivalent to x = x * y

# Division assignment
x /= y  # Equivalent to x = x / y

# Modulo assignment
x %= y  # Equivalent to x = x % y

# Exponentiation assignment
x **= y  # Equivalent to x = x ** y
``````

### Logical Operators

Logical operators are used to combine conditions and perform logical operations. They include logical AND (`and`), logical OR (`or`), and logical NOT (`not`).

For example:

python

``````x = True
y = False

# Logical AND
result = x and y  # False

# Logical OR
result = x or y  # True

# Logical NOT
result = not x  # False
``````

### Boolean Logic

Boolean logic is an essential part of programming and decision-making. It involves combining conditions using logical operators to determine the outcome. The logical operators `and` and `or` evaluate multiple conditions and return `True` or `False` based on the result.

• The logical AND operator (`and`) returns `True` only if both conditions are `True`.
• The logical OR operator (`or`) returns `True` if at least one condition is `True`.

python

``````x = 10
y = 5

# Logical AND
result = (x > 5) and (y < 10)  # True

# Logical OR
result = (x > 5) or (y > 10)  # True
``````

Understanding and utilizing these basic operators and boolean logic is fundamental for performing calculations, making comparisons, and controlling the flow of your Python programs. Stay tuned for more exciting Python concepts in our upcoming posts!

### Want an email when we post?

Free, weekly at most. We hate spam too.