Operators in python
Operators are essential components in programming languages that allow us to perform various operations on data types like integers, floats, strings, and more. Python offers a rich set of operators that enables developers to manipulate data efficiently. In this blog, we'll explore the various operators available in Python and their usage.
Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and more.
Example
+ Addition 5 + 2 = 7
- Subtraction 5 - 2 = 3
* Multiplication 5 * 2 = 10
/ Division 5 / 2 = 2.5
% Modulus 5 % 2 = 1 (remainder)
** Exponentiation 5 ** 2 = 25
// Floor division 5 // 2 = 2 (quotient)
Comparison Operators:
Comparison operators are used to compare two values and return a Boolean value (True or False) based on the comparison.
Example
== Equal 5 == 5 (True), 5 == 6 (False)
!= Not Equal 5 != 6 (True), 5 != 5 (False)
> Greater Than 5 > 2 (True), 5 > 7 (False)
< Less Than 5 < 7 (True), 5 < 2 (False)
>= Greater or Equal 5 >= 5 (True), 5 >= 6 (False)
<= Less or Equal 5 <= 5 (True), 5 <= 2 (False)
Logical Operators:
Logical operators are used to combine two or more conditions and return a Boolean value.
Example
and Logical AND (5 > 2) and (7 > 5) (True)
or Logical OR (5 < 2) or (7 > 5) (True)
not Logical NOT not (5 < 2) (True)
Assignment Operators:
Assignment operators are used to assign values to variables.
Example
= Assign x = 5
+= Add and Assign x += 5 (same as x = x + 5)
-= Subtract and Assign x -= 5 (same as x = x - 5)
*= Multiply and Assign x *= 5 (same as x = x * 5)
/= Divide and Assign x /= 5 (same as x = x / 5)
%= Modulus and Assign x %= 5 (same as x = x % 5)
**= Exponentiate and Assign x **= 5 (same as x = x ** 5)
//= Floor divide and Assign x //= 5 (same as x = x // 5)
Identity Operators:
Identity operators are used to compare the memory locations of two objects.
Example
is True if both variables are the same object x is y
is not True if both variables are not the same object x is not y
Membership Operators:
Membership operators are used to check if a value is present in a sequence.
Example
in True if value is found in the sequence x in y
not in True if value is not found in the sequence x not in y
Bitwise Operators:
Bitwise operators are used to perform operations on bits of binary numbers
Comments
Post a Comment