DEV Community

Cover image for Operators In Python
Introschool
Introschool

Posted on • Updated on

Operators In Python

Subscribe to our Youtube Channel To Learn Free Python Course and More

What are the Operators
The operator in a programming language is a symbol that represents an action. When I write an expression 2 + 8, It means to perform addition on operands 2 and 8. Operands are objects that are being manipulated.

Any expression is a combination of operators and operands. Python supports many operators that can be combined with operands to form an expression. Let’s see them in detail.

Types Of Operators

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplications and other such operations. We will explore them with the example.
Image description
Let’s see the examples

print('Addition(+) :', 10 + 2)

print('Subtraction(-) :', 10 - 7)

print('Multiplication(*) :', 3 * 4)

print('Float Division(/) :', 10 / 3)

print('Floor Division(//) :', 10 // 3)

print('Modulus(%) :', 10 % 3)

print('Exponent(**) :', 3 ** 2)

# Output

# Addition(+) : 12

# Subtraction(-) : 3

# Multiplication(*) : 12

# Float Division(/) : 3.3333333333333335

# Floor Division(//) : 3

# Modulus(%) : 1


# Exponent(**) : 9

Enter fullscreen mode Exit fullscreen mode

Comparison Operators
As you can guess from the name, comparison operators are used to compare values. The result will always be True or False. These are reserved Python keywords.
Image description
Let’s see the examples

# Comparison Operators

print('10 Greater than (>) 5:', 10 > 5)

print('10 Greater than or equal to (>=) 11:', 10 >= 11)

print('20 Less than (<) 19:', 20 < 19)

print('7 Less than or equal to (<=) 9:', 7 <= 9)

print('10 equal to (==) 5:', 10 == 5)

print('5 equal to (==) 5:', 5 == 5)

print('10 not equal to (!=) 5:', 10 != 5)

# Output

# 10 Greater than (>) 5: True

# 10 Greater than or equal to (>=) 11: False

# 20 Less than (<) 19: False

# 7 Less than or equal to (<=) 9: True

# 10 equal to (==) 5: False

# 5 equal to (==) 5: True

# 10 not equal to (!=) 5: True
Enter fullscreen mode Exit fullscreen mode

Logical Operators
True and False are logical Boolean values. There are three logical operators and, or, not in Python.
Image description
Let’s see the example

# Logical Operators

x = True
y = False

print('x and y:', x and y)

print('x or y:', x or y)

print('not x:', not x)

print('not y:', not y)

# Output

# x and y: False

# x or y: True

# not x: False

# not y: True
Enter fullscreen mode Exit fullscreen mode

Here you see the output depends on the value of the boolean operands. But what if the operands are not boolean? How would logical operators work on non-boolean operands?

# Logical Operators with non-boolean operands
x = 4
y = 0
print('x and y:', x and y)

print('x or y:', x or y)

# Output

# x and y: 0

# x or y: 4
Enter fullscreen mode Exit fullscreen mode

Here the result of x and y is 0 whereas the result of x or y is 4. How did it possible? To understand this, first, you have to understand the concept of truthy and falsy values.

Truthy and Falsy Values
As you already know that in Python there are only two boolean values which are True and False. But other values are also concluded in a boolean context. There is a built-in function in Python called bool() that helps you to find the boolean context of non-boolean values. The bool() function gives either True or False. According to the result of the bool function, you can identify truthy and falsy values.

Let’s understand it by example.
Numeric Values

# Numeric Value

bool(1)
# Output: True

bool(7)
# Output: True

bool(0)
# Output: False
Enter fullscreen mode Exit fullscreen mode

String Values

# String Value

bool('')
# Output: False

bool('Hello World')
# Output: True
Enter fullscreen mode Exit fullscreen mode

Truthy and Falsy values are well defined by Python. Below are all the values that are considered false in a boolean context.

  • A boolean value False
  • Numeric value zero(0, 0.0)
  • An empty string.
  • The Python keyword None
  • All the other data structure with empty values( like empty list, dictionary, tuples, set) Except for these falsy values, others are considered as truthy value Now you can understand below examples
print(not 5)
# Output: False
"""
In boolean context 5 is a truthy value so not 5 will give False
"""

print(7 and '')
# Output: ''
"""
In boolean context 7 is truthy value and an empty string is a falsy value so 7 and '' expression will result into ''.
"""

print(5 or 0)
# Output: 5
"""
In boolean context 5 is a truthy value and 0 is a falsy value so 5 or 0 will give 5 as output.
"""
Enter fullscreen mode Exit fullscreen mode

Note: If both values are true

  • and gives the last truthy value.
  • or gives the first truthy value.

Assignment Operators
Assignment Operators are used to assigning values to variables. You have already seen a simple assignment (=) operator. The assignment operator evaluates the right side of the expression and assigns to the left side variable. See the below table.
Image description
Let’s understand it by example.

#Assignment Operators

x = 15
x += 5
"""
Output: 20
Because x += 5 is equivalent to x = x + 5
"""

y = 8
y *= 2

"""
Output: 16
Because y *= 2 is equivalent to x = y * 2
"""
Enter fullscreen mode Exit fullscreen mode

Other Operators
There are other two types of operators, one is Identity Operators and other is Membership Operators.

Identity Operators
In Python is and is not are Identity Operators. These Operators are used to find whether two values are located at the same memory location or not. If two values are equal, it doesn’t mean that these are present at the same memory location.

# Identity Operators 
# Identity Operators 
x = 23
y = 23

a = 'Python'
b = 'Python'

t = [1, 2, 3, 4, 5]  # list data structure
v = [1, 2, 3, 4, 5]  # list data structure

x is y
# Output: True

a is not b
# Output: False

t is v
# Output: False

t is not v
# Output: True
Enter fullscreen mode Exit fullscreen mode

Here you see that x and y are located at the same memory, similarly, a and b are located at the same memory. But two list objects are different even if the values inside them are identical. We will know about the lists data structure in detail later in the course.

Membership Operators
In python in and not in are Membership operators. These operators are used to check whether value or variable present in the sequence like string, list, tuple, dictionary or set. You will see detailed examples of membership operator with each data structure later in the course but for current understanding see the below example.

# Membership Operator

a = 'Hello'
b = 'Hello World!'
c = 'Python'
d = 'He'

a in b
# Output: True

c in b
# Output: False

d in b
# Output: True

d not in c
# Output: True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)