Skip to content
Learnearn.uk » Home » Arithmetic, Relational & Logical Operators

Arithmetic, Relational & Logical Operators

Arithmetic Operators

Arithmetic Operators

Arithmetic operators can be used in Python to perform various arithmetic operations on numeric values and variables.

Operator Description Example
+ Addition 3 + 5 = 8
- Subtraction 7 - 2 = 5
* Multiplication 4 * 3 = 12
/ Division 10 / 2 = 5.0
// Floor Division 10 // 3 = 3
% Modulus (Remainder) 10 % 3 = 1
** Exponentiation 2 ** 3 = 8
+= Increment x += 5
-= Decrement x -= 3
*= Multiplication Assignment x *= 2
/= Division Assignment x /= 4

Examples

# Addition
result = 5 + 3
print(result)   # 8

# Subtraction
result = 7 - 2
print(result)   # 5

# Multiplication
result = 4 * 3
print(result)   # 12

# Division
result = 10 / 2
print(result)   # 5.0

# Floor Division
result = 10 // 3
print(result)   # 3

# Modulus (Remainder)
result = 10 % 3
print(result)   # 1

# Exponentiation
result = 2 ** 3
print(result)   # 8

# Increment
x = 5
x += 1
print(x)       # 6

# Decrement
y = 7
y -= 1
print(y)       # 6

# Multiplication Assignment
z = 4
z *= 3
print(z)       # 12

# Division Assignment
w = 10
w /= 2
print(w)       # 5.0

Relational Operators

Relational Operators

Relational operators in Python are used to compare values and determine the relationship between them. These operators return Boolean values (True or False) based on the comparison result. Here are the relational operators in Python:

Operator Description Example
== Equal to 5 == 5
!= Not equal to 5 != 3
> Greater than 7 > 3
< Less than 2 < 4
>= Greater than or equal to 6 >= 4
<= Less than or equal to 3 <= 3

These operators can be used to compare numeric values, strings, variables, or expressions. The result of the comparison is a Boolean value indicating whether the comparison is true or false.

Examples

x = 5
y = 7

print(x == y)   # False
print(x != y)   # True
print(x > y)    # False
print(x < y)    # True
print(x >= y)   # False
print(x <= y)   # True

Logical Operators

Logical Operators

Logical operators in Python are used to combine or manipulate Boolean values (True or False). These operators allow you to perform logical operations and make decisions based on multiple conditions.

Operator Description
and Returns True if both operands are True
or Returns True if at least one operand is True
not Returns the opposite Boolean value of the operand

These operators are used to combine or manipulate Boolean values and are commonly used in conditional statements, loops, and Boolean expressions.

Examples

# Example 1
x = 5
y = 7
z = 3

result = (x < y) and (y < z)
print(result)   # False

# Example 2
x = 5
y = 7
z = 3

result = (x < y) or (y < z)
print(result)   # True

# Example 3
x = 5

result = not (x < 10)
print(result)   # False