Operators (arithmetic, comparison, logical) in python
Operators are some special symbols or reserved keywords in Python programming language. Python operators are used to perform programming operations on program variables and variable values. Python program operators can be divided into many types. Some of the popular operators include arithmetic operators, comparison (relational) operators, logical operators, assignment operators, and more advanced operators like membership and identity operators.
So let’s know about Python program operators, which include arithmetic, comparison and logical operators.
Python Arithmetic Operators.
Arithmetic operators in Python programs are used to perform arithmetic operator operations with variable values between multiple program variables declared in the program.
Arithmetic Operators Example.
+ Addition operator = p + q
– Subtraction operator = p – q
* Multiplication operator = p * q
/ Division operator = p / q
// Floor division operator = p // q
% Modulus (remainder) operator = p % q
** Exponentiation operator = p ** q
Arithmetic Operators program Example
p = 5
q = 2
print(p + q) # The result is – 7
print(p – q) # The result is – 3
print(p * q) # The result is – 10
print(p / q) # The result is – 2.5
print(p // q) # The result is – 2
print(p % q) # The result is – 1
print(p ** q) # The result is -25
Python Comparison Operators.
In Python programming, the comparison operator is used to compare two variable values declared in the program. This comparison operator displays the program comparison result values as (true or false) based on a boolean value.
Comparison Operator Description Example.
== p equal to == q
!= p not equal to != q
> p greater than > q
< p less than < q
>= p greater than equal to >= q
<= p less than equal to <= q
Comparison Operator Example.
p = 4
q = 6
print(p == q) # the result is – false
print(p!= q) # the result is – true
print(p > q) # the result is – false
print(p < q) # the result is – true
print(p >= q) # the result is – false
print(p <= q) # the result is – true
Python Logical Operators.
Python Logical Operators are used to combine and execute program condition statements using logical conditions in Python programs.
Python Logical Operators Example.
If both the statements in a Python program are true, then it produces true values, e.g., p < 3 and q < 7
Or If at least one of the statements in a Python program condition is true, then it produces true result, e.g., p < 9 or q < 9
Here NOT operator reverses the program result, if the result is true, then it produces false values, e.g., not(p < 9 and q < 13)
Logical Operators Example.
p = 6 q = 12 print(p < 7 and q < 13) # the result is – true, [because True here Both conditions are true]
print(p < 7 or q < 13) # the result is – true, [because (At least one condition is true]
print(not(p < 7 and q < 13)) # the result is – False [it Reverse the result of p < 7 and q < 13]