In Python, operators are specialized symbols that perform different computations. To create an expression, we combine operators and operands. Expressions are merely values’ representations. The fundamental building blocks of a program that define its functionality are relation and logic.
Value comparisons are done using relational operators, also known as comparison operators. It responds to the condition by returning either True or False.
Operator | Description | Syntax |
---|---|---|
> | Greater than: True if the left operand is greater than the right | x > y |
< | Less than: True if the left operand is less than the right | x < y |
== | Equal to: True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to True if the left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to True if the left operand is less than or equal to the right | x <= y |
a = 9
b = 5
print(a < b)
print(a > b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
False
True
False
True
True
False
To decide on an expression, logical operators are mainly used. The following logical operators are supported by Python.
Operator | Description | Example |
---|---|---|
and | If both expressions are true, then the condition will be true. If a and b are the two expressions | a → true, b → true then a and b → true. |
or | If one of the expressions is true, then the condition will be true. If a and b are the two expressions, | a → true, b → false => a or b → true. |
not | If an expression a is true, then not (a) will be false and vice versa. | a → true=> not a → false. |
a = 5
print(2 < 3) and (2 < 5)
print(2 < 3) or (2 < 5)
print(not a)
True
True
False
Note: also read about Python Tuple
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…