Basic Operators in Python
In Python, operators are symbols or special characters that perform various operations on operands, such as variables or values.
Here are some commonly used operators in Python with examples:
1. Arithmetic Operators:
Addition: + Example: 3 + 4 equals 7
Subtraction: - Example: 7 - 3 equals 4
Multiplication: * Example: 2 * 5 equals 10
Division: / Example: 10 / 3 equals 3.3333
Floor Division: // (returns the quotient, discarding the remainder) Example: 10 // 3 equals 3
Modulus (Remainder): % Example: 10 % 3 equals 1
Exponentiation: ** Example: 2 ** 3 equals 8
2. Comparison Operators:
Equal to: == Example: 5 == 5 returns True
Not equal to: != Example: 5 != 3 returns True
Greater than: > Example: 7 > 4 returns True
Less than: < Example>= Example: 5 >= 5 returns True
Less than or equal to: <= Example: 3 <= 2 returns False
3. Assignment Operators:
Assign: = Example: x = 5
Add and assign: += Example: x += 3 (equivalent to x = x + 3)
Subtract and assign: -= Example: x -= 2 (equivalent to x = x - 2)
Multiply and assign: *= Example: x *= 4 (equivalent to x = x * 4)
Divide and assign: /= Example: x /= 2 (equivalent to x = x / 2)
Modulus and assign: %= Example: x %= 3 (equivalent to x = x % 3)
4. Logical Operators:
AND: and Example: True and False returns False
OR: or Example: True or False returns True
NOT: not Example: not True returns False
5. Membership Operators:
in: Checks if a value exists in a sequence Example: 2 in [1, 2, 3] returns True
not in: Checks if a value does not exist in a sequence Example: 5 not in [1, 2, 3] returns True
6. Identity Operators:
is: Checks if two variables refer to the same object Example: x is y returns True if x and y refer to the same object
is not: Checks if two variables refer to different objects Example: x is not y returns True if x and y refer to different objects
7. Bitwise operators: Bitwise operators manipulate individual bits within numbers, which can be useful in certain low-level operations and optimizations.
Bitwise AND (&):
Example: 5 & 3 equals 1
Explanation: Performs a bitwise AND operation on the binary representations of the numbers. In this case, 5 is 101 in binary, and 3 is 011. The result of 101 & 011 is 001, which is 1 in decimal.
2. Bitwise OR (|):
Example: 5 | 3 equals 7
Explanation: Performs a bitwise OR operation on the binary representations of the numbers. In this case, 5 is 101 in binary, and 3 is 011. The result of 101 | 011 is 111, which is 7 in decimal.
3. Bitwise XOR (^):
Example: 5 ^ 3 equals 6
Explanation: Performs a bitwise XOR (exclusive OR) operation on the binary representations of the numbers. In this case, 5 is 101 in binary, and 3 is 011. The result of 101 ^ 011 is 110, which is 6 in decimal.
4. Bitwise NOT (~):
Example: ~5 equals -6
Explanation: Performs a bitwise NOT operation, which flips the bits of the number. In this case, 5 is 00000101 in binary. The result of ~00000101 is 11111010, which is -6 in two's complement representation.
5. Left Shift (<<):
Example: 5 << 2>>):
Example: 15 >> 2 equals 3
Explanation: Performs a right shift operation, shifting the bits of the number to the right by the specified number of positions. In this case, 15 is 1111 in binary. Shifting it right by 2 positions gives 11, which is 3 in decimal.
These are some basic examples of operators in Python.
21 likes