Loop Control Statements in C
Introduction to HTML
How to use the Github API
The image tag, anchor tag and the button tag
Ordered and Unordered Lists in HTML
The division tag
HTML Forms
Tables in HTML
Introduction to C Programming
Introduction to Python
Varibles and Datatypes in Python
Operators in Python
Typecasting in Python
Input and Output in Python
If Else in Python
Loops in Python
Break, Continue and Pass in Python
Python practice section 1
Lists in Python
Tuple in Python
Operators in Python
Operators are used to perform computation on values. We have 7 types of Operators in Python. These are:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic operators
These are general mathematical operations, which work on more than one operand.
Operators | Meaning | Example | Result |
---|---|---|---|
+ | Addition | 10+2 | 12 |
- | Subtraction | 10-2 | 8 |
* | Multiplication | 10*2 | 20 |
/ | Division | 10/2 | 5 |
// | Floor division | 11/2 | 5 |
% | Modulus | 11%2 | 1 |
** | Exponentiation | 2**3 | 8 |
Assignment operators
As the name suggests, they are used to assign value to variables.
Operators | Example | Equivalent |
---|---|---|
= | x = 10 | x = 10 |
+= | x += 10 | x = x + 10 |
-= | x -= 10 | x = x - 10 |
*= | x *= 10 | x = x * 10 |
/= | x /= 10 | x = x / 10 |
%= | x %= 10 | x = x % 10 |
//= | x //= 10 | x = x // 10 |
**= | x **= 10 | x = x ** 10 |
Comparison operators
Comparison operators are used to compare two values. The result of the comparison is true or false.
Operators | Meaning | Example | Result |
---|---|---|---|
> | Greater than | 10>2 | True |
< | Less than | 10<2 | False |
== | Equal | 10==10 | True |
!= | Not equal | 10!=2 | True |
>= | Greater than or equal to | 11>=2 | True |
<= | Less than or equal to | 11<=2 | False |
Logical operators
Logical operators are used to combine 2 or more conditional statements:
Operators | Explanation | Example | Result |
---|---|---|---|
and | Return true only when all conditional statements are true | 10>2 and 15>5 | True |
or | Return false only when all conditional statements are false | 10>2 or 15>25 | True |
not | It reverses the result | not(5>15) | True |
Identity operators
Identity operators are used to compare objects. They compare the memory locations of two objects. We
have two identity operators:
- is :- Return true if both points to the same memory location or we can say both are same objects.
Consider the below Example for better understanding.
a = ["rapid","coders"] b = ["rapid","coders"] c = b print(a is b) # output-> False # is and == operators have different functions. print(a == b) # output-> True print(c is b) # output-> True
- is not :- It is opposite to 'is' operator. Return true if both point to different memory locations or we can say both are not same objects. Consider the below Example for better understanding.
a = ["rapid","coders"]
b = ["rapid","coders"]
c = b
print(a is not b) # output-> True
print(a == b) # output-> True
print(c is not b) # output-> False
Membership operators
Membership operators are used to check membership of a value. It checks whether a value is a member of a sequence, such as list, tuple. We have two Membership operators:
- in :- Return true if the given value is present in the sequence. Consider the below Example for better understanding.
a = ["rapid","coders"] b = "rapid" print(b in a) # output-> True print("akshay" in a) # output-> False
- not in :- Return true if the given value is not present in the sequence. Consider the below Example for better understanding.
a = ["rapid","coders"]
b = "rapid"
print(b not in a) # output-> True
print("akshay" in a) # output-> False
Bitwise Operators
Bitwise Operators are used to perform operations on bits. We will see Bitwise operators in detail in further articles.
Operators | Name | Example | Result |
---|---|---|---|
& | Bitwise AND | 1 & 0 | 0 |
| | Bitwise OR | 1 | 0 | 1 |
~ | Bitwise NOT | ~1 | -2 |
^ | XOR | 1^2 | 3 |
<< | Left shift | 11<<2 | 44 |
>> | Right shift | 11>>2 | 2 |