Python Operators
Python Operators:
In python, operators can perform different operations like logical, comparison, arithmetic, etc., on the defined variables and values. For example, the addition (+) operator is useful to perform the addition operation on defined operands.
?In python, operators are categorized into different groups. Those are
Python Arithmetic Operators:
In python, arithmetic operators perform the necessary arithmetic calculations like addition, subtraction, division, etc., on the defined variables and values.
The following table lists the different types of arithmetic operators in python.
Python Arithmetic Operators Example:
Following is the example of using arithmetic operators in the python programming language.
a =?10
b =?3
print("a + b =",a+b)
print("a - b =",a-b)
print("a b =",ab)
print("a / b =",a/b)
print("a % b =",a%b)
If you observe the above example, we used arithmetic operators (+, -, , /, %, *, //) to perform different operations on defined operands based on our requirements.
When you execute the above python program, you will get the result as shown below.
a + b = 13
a - b = 7
a * b = 30
a / b = 3.3333333333333335
a % b = 1
Python Comparison Operators:
In python, comparison operators are useful to compare variables and values to determine whether the variables equal or not, etc., based on our requirements. The comparison operators will return either?True?or?False?based on the defined condition.
The following table lists the different types of comparison operators in python.
Python Comparison Operators Example:
Following is the example of using comparison operators in the python programming language.
?a =?6
b =?3
print("a == b =",a == b)
print("a > b =",a > b)
print("a < b =",a < b)
print("a >= b =",a >= b)
领英推荐
print("a <= b =",a <= b)
print("a != b =",a != b)
If you observe the above example, we used comparison operators (==, >, <, >=, <=, !=) to perform different operations on the defined operands based on our requirements.
When you execute the above python program, you will get the result as shown below.
a == b = False
a > b = True
a < b = False
a >= b = True
a <= b = False
a != b = True
Python Logical Operators:
In python, logical operators are useful to combine operands or conditional statements based on our requirements. The logical operators will work with Boolean expressions (True?or?False) and return Boolean values.
The following table lists the different types of logical operators in python.
Python Logical Operators Example:
Following is the example of using logical operators in the python programming language.
a =?15
b =?10
# AND operator:
result = (a <= b) and (a >?10)
print("AND Operator: ", result)
# OR operator :
result = (a >= b) or (a <?5)
print("OR Operator: ", result)
# NOT operator :
result = not((a <= b) and (a >?10))
print("NOT Operator: ", result)
If you observe the above example, we used logical operators (and, or, not) with conditional statements to evaluate the expressions based on our requirements.
?When you execute the above python program, you will get the result as shown below.
AND Operator: False
OR Operator: True
NOT Operator: True
Python Assignment Operators:
In python, assignment operators can assign a new value to the operand, and these operators will work with only one operand.
The following table lists the different types of assignment operators in python.