Python Operators Introduction
Operators are the symbols that are used to perform some operations on one or more values. These values are known as the operands.
Types of Operators
In Python, we have the following types of operators:
Arithmetic Operators
Operator | Name | Example | Precedence | Associativity |
---|
+ | Addition | a + b | 3 | Left to Right |
– | Subtraction | a-b | 3 | Left to Right |
* | Multiplication | a * b | 2 | Left to Right |
/ | Division | a / b | 2 | Left to Right |
% | Modulus | a % b | 2 | Left to Right |
// | Floor Division | a//b | 2 | Left to Right |
** | Exponential | a ** b | 1 | Right to Left |
Assignment Operators
Comparison Operators
Operator | Name | Example | Description |
---|
== | Equal to | a == b | Returns True if a is equal to b else False. |
!= | Not Equal to | a != b | Returns True if a is not equal to b else False. |
> | Greater than | a > b | Returns True if a is greater then b else False. |
< | Less than | a < b | Returns True if a is less then b else False. |
>= | Greater than or equal to | a >= b | Returns True if a is greater then equal to b else False. |
<= | Less than or equal to | a <= b | Returns True if a is less then equal to b else False. |
Logical Operators
Operator | Example | Details |
---|
and | a > 5 and b < 20 | Returns True only if both statements are True, else False. |
or | a > 5 or b < 20 | Returns True if any statement is True, and returns False only if both statements are False. |
not | not(a > 5 and b < 20) | Returns True if expression inside parenthesis is False. |
Identity Operators
Operator | Example | Description |
---|
is | a is b | Returns True if both variables share same memory location, i.e. are same objects. |
is not | a is not b | Returns True if both variables do not share the same memory location, i.e. are not the same objects. |
Membership Operators
Operator | Example | Description |
---|
in | a in b | Returns True if the variable is present in the sequence, else False. |
not in | a not in b | Returns True if the variable is not present in the sequence, else False. |
Bitwise Operators
Resources
References:
- Python Operators.
- Examples on Operators.
- Python Operators and Expressions.
© Copyright 2024 www.logicalpython.com. All rights reserved.
We constantly review Tutorials, references, and examples to avoid errors, but we cannot warrant full correctness
of all content.