Table of contents | |
Introduction | |
Arithmetic Operators | |
Comparison Operators | |
Logical Operators |
In Python, operators are symbols that perform various operations on variables or values. They allow you to manipulate data and perform calculations. Understanding operators is essential for writing effective and efficient Python code. In this article, we will explore different types of operators in Python, along with examples and explanations.
Arithmetic operators are used to perform basic mathematical operations. Here are the commonly used arithmetic operators in Python:
Let's see some examples:
x = 10
y = 3
# Addition
print(x + y) # Output: 13
# Subtraction
print(x - y) # Output: 7
# Multiplication
print(x * y) # Output: 30
# Division
print(x / y) # Output: 3.3333333333333335
# Floor Division
print(x // y) # Output: 3
# Modulus
print(x % y) # Output: 1
# Exponentiation
print(x ** y) # Output: 1000
Comparison operators are used to compare two values. They return a Boolean value (True or False) based on the comparison result. Here are the comparison operators in Python:
Let's see some examples:
x = 5
y = 10
# Equal to
print(x == y) # Output: False
# Not equal to
print(x != y) # Output: True
# Greater than
print(x > y) # Output: False
# Less than
print(x < y) # Output: True
# Greater than or equal to
print(x >= y) # Output: False
# Less than or equal to
print(x <= y) # Output: True
Logical operators are used to combine multiple conditions and perform logical operations. They return a Boolean value based on the evaluation result. Here are the logical operators in Python:
Let's see some examples:
x = 5
y = 10
z = 3
# Logical AND
print(x < y and y < z) # Output: False
# Logical OR
print(x < y or y < z) # Output: True
# Logical NOT
print(not x < y) #
49 videos|38 docs|18 tests
|
|
Explore Courses for EmSAT Achieve exam
|