Introduction:
Python operators are symbols or special keywords used to perform various operations on data. They allow you to manipulate variables and perform arithmetic, comparison, logical, and bitwise operations.
1. Arithmetic Operators:
Arithmetic operators are used for mathematical calculations.
- Addition (+): Adds two operands.python
num1 = 10
num2 = 5
result = num1 + num2 - Subtraction (-): Subtracts the second operand from the first.python
num1 = 10
num2 = 5
result = num1 - num2 - Multiplication (*): Multiplies two operands.python
num1 = 10
num2 = 5
result = num1 * num2 - Division (/): Divides the first operand by the second (float division).python
num1 = 10
num2 = 5
result = num1 / num2 - Floor Division (//): Divides the first operand by the second and returns an integer result.python
num1 = 10
num2 = 5
result = num1 // num2 - Modulus (%): Returns the remainder of the division of the first operand by the second.python
num1 = 10
num2 = 5
result = num1 % num2 - Exponentiation ():** Raises the first operand to the power of the second.python
num1 = 2
num2 = 3
result = num1 ** num2
2. Comparison Operators:
Comparison operators are used to compare two values and return a boolean value (True or False).
- Equal to (==): Checks if two operands are equal.python
num1 = 10
num2 = 5
result = num1 == num2 - Not Equal to (!=): Checks if two operands are not equal.python
num1 = 10
num2 = 5
result = num1 != num2 - Greater than (>): Checks if the first operand is greater than the second.python
num1 = 10
num2 = 5
result = num1 > num2 - Less than (<): Checks if the first operand is less than the second.python
num1 = 10
num2 = 5
result = num1 < num2 - Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second.python
num1 = 10
num2 = 5
result = num1 >= num2 - Less than or equal to (<=): Checks if the first operand is less than or equal to the second.python
num1 = 10
num2 = 5
result = num1 <= num2
3. Logical Operators:
Logical operators are used to combine multiple conditions and return a boolean value (True or False).
- AND (and): Returns True if both operands are True.python
x = True
y = False
result = x and y - OR (or): Returns True if at least one operand is True.python
x = True
y = False
result = x or y - NOT (not): Returns the opposite of the operand's value (True becomes False and vice versa).python
x = True
result = not x
4. Bitwise Operators:
Bitwise operators perform operations on binary representations of integers.
- Bitwise AND (&): Performs a bitwise AND operation.python
num1 = 10 # 1010 in binary
num2 = 5 # 0101 in binary
result = num1 & num2 # Result: 0000 (0 in decimal) - Bitwise OR (|): Performs a bitwise OR operation.python
num1 = 10 # 1010 in binary
num2 = 5 # 0101 in binary
result = num1 | num2 # Result: 1111 (15 in decimal) - Bitwise XOR (^): Performs a bitwise XOR operation.python
num1 = 10 # 1010 in binary
num2 = 5 # 0101 in binary
result = num1 ^ num2 # Result: 1111 (15 in decimal) - Bitwise NOT (~): Performs a bitwise NOT operation (inverts the bits).python
num = 10 # 1010 in binary
result = ~num # Result: -11 (in decimal)
These are the primary Python operators along with coding examples and explanations. They are essential tools for performing various operations in Python programming.
0 Comments