DEV Community

Praneeth
Praneeth

Posted on

Day -2 : Mastering basics of Python

Operators in python

1. Arithmetic Operators

Used for basic mathematical calculations.

  • + (Addition): Adds two numbers.

    Example: 5 + 3 → 8

  • - (Subtraction): Subtracts the second number from the first.

    Example: 5 - 3 → 2

  • * (Multiplication): Multiplies two numbers.

    Example: 5 * 3 → 15

  • / (Division): Divides the first number by the second, returning a float.

    Example: 5 / 2 → 2.5

  • // (Floor Division): Divides and rounds down to the nearest integer.

    Example: 5 // 2 → 2

  • % (Modulus): Returns the remainder of the division.

    Example: 5 % 2 → 1

  • ` (Exponentiation):** Raises the first number to the power of the second.
    Example:
    5 ** 2 → 25`

# Arithmetic Operators
a = 10
b = 3

print("Addition:", a + b)        # 13
print("Subtraction:", a - b)     # 7
print("Multiplication:", a * b)  # 30
print("Division:", a / b)        # 3.333...
print("Floor Division:", a // b) # 3
print("Modulus:", a % b)         # 1
print("Exponentiation:", a ** b) # 1000
Enter fullscreen mode Exit fullscreen mode

2. Assignment Operators

Used to assign values to variables.

  • = (Assign): Assigns a value to a variable.

    Example: x = 5

  • += (Add and Assign): Adds a value and reassigns.

    Example: x += 3 → x = x + 3

  • -= (Subtract and Assign): Subtracts a value and reassigns.

    Example: x -= 3 → x = x - 3

  • *= (Multiply and Assign): Multiplies a value and reassigns.

    Example: x *= 3 → x = x * 3

  • /= (Divide and Assign): Divides a value and reassigns.

    Example: x /= 3 → x = x / 3

  • //= (Floor Divide and Assign): Floor divides and reassigns.

    Example: x //= 3 → x = x // 3

  • %= (Modulus and Assign): Applies modulus and reassigns.

    Example: x %= 3 → x = x % 3

  • `= (Exponentiate and Assign):** Raises to a power and reassigns.
    Example:
    x *= 2 → x = x * 2`

# Assignment Operators
x = 5
print("Initial value:", x)       # 5

x += 3
print("After += 3:", x)          # 8

x -= 2
print("After -= 2:", x)          # 6

x *= 4
print("After *= 4:", x)          # 24

x /= 3
print("After /= 3:", x)          # 8.0

x //= 2
print("After //= 2:", x)         # 4.0

x %= 3
print("After %= 3:", x)          # 1.0

x **= 2
print("After **= 2:", x)         # 1.0
Enter fullscreen mode Exit fullscreen mode

3. Comparison Operators

Used to compare two values.

  • == (Equal to): Checks if two values are equal.

    Example: 5 == 5 → True

  • != (Not Equal to): Checks if two values are not equal.

    Example: 5 != 3 → True

  • > (Greater than): Checks if the first value is greater than the second.

    Example: 5 > 3 → True

  • < (Less than): Checks if the first value is less than the second.

    Example: 3 < 5 → True

  • >= (Greater than or Equal to): Checks if the first value is greater than or equal to the second.

    Example: 5 >= 5 → True

  • <= (Less than or Equal to): Checks if the first value is less than or equal to the second.

    Example: 3 <= 5 → True

# Comparison Operators
a = 10
b = 5

print("Equal:", a == b)          # False
print("Not Equal:", a != b)      # True
print("Greater than:", a > b)    # True
print("Less than:", a < b)       # False
print("Greater or Equal:", a >= b) # True
print("Less or Equal:", a <= b)  # False
Enter fullscreen mode Exit fullscreen mode

4. Logical Operators

Used for combining conditions.

  • and: Returns True if both conditions are true.

    Example: (5 > 3) and (4 > 2) → True

  • or: Returns True if at least one condition is true.

    Example: (5 > 3) or (2 > 4) → True

  • not: Reverses the truth value.

    Example: not(5 > 3) → False

# Logical Operators
a = True
b = False

print("AND:", a and b)           # False
print("OR:", a or b)             # True
print("NOT a:", not a)           # False
print("NOT b:", not b)           # True
Enter fullscreen mode Exit fullscreen mode

5. Bitwise Operators

Operate on binary representations of numbers.

  • &: Performs bitwise AND.

    Example: 5 & 3 → 1

  • |: Performs bitwise OR.

    Example: 5 | 3 → 7

  • ^: Performs bitwise XOR.

    Example: 5 ^ 3 → 6

  • ~: Performs bitwise NOT.

    Example: ~5 → -6

  • <<: Shifts bits to the left.

    Example: 5 << 1 → 10

  • >>: Shifts bits to the right.

    Example: 5 >> 1 → 2

# Bitwise Operators
a = 5  # Binary: 0101
b = 3  # Binary: 0011

print("Bitwise AND:", a & b)     # 1 (Binary: 0001)
print("Bitwise OR:", a | b)      # 7 (Binary: 0111)
print("Bitwise XOR:", a ^ b)     # 6 (Binary: 0110)
print("Bitwise NOT (~a):", ~a)   # -6
print("Left Shift:", a << 1)     # 10 (Binary: 1010)
print("Right Shift:", a >> 1)    # 2 (Binary: 0010)
Enter fullscreen mode Exit fullscreen mode

6. Membership Operators

Check if a value is present in a sequence.

  • in: Returns True if the value is in the sequence.

    Example: 'a' in 'apple' → True

  • not in: Returns True if the value is not in the sequence.

    Example: 'z' not in 'apple' → True

# Membership Operators
sequence = [1, 2, 3, 4, 5]

print("3 in sequence:", 3 in sequence)       # True
print("6 in sequence:", 6 in sequence)       # False
print("6 not in sequence:", 6 not in sequence) # True
Enter fullscreen mode Exit fullscreen mode

7. Identity Operators

Check if two variables reference the same object.

  • is: Returns True if two variables point to the same object.

    Example: x is y

  • is not: Returns True if two variables point to different objects.

    Example: x is not y

# Identity Operators
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print("a is b:", a is b)         # True (same object)
print("a is c:", a is c)         # False (different objects)
print("a is not c:", a is not c) # True
Enter fullscreen mode Exit fullscreen mode

8. Ternary Operator

The ternary operator allows a shorthand for if-else statements.

# Ternary Operator
a, b = 10, 20

max_value = a if a > b else b
print("Maximum value:", max_value)  # 20
Enter fullscreen mode Exit fullscreen mode

9. Walrus Operator (:=)

Introduced in Python 3.8, the walrus operator allows assignment inside expressions.

# Walrus Operator (Python 3.8+)
data = [1, 2, 3, 4, 5]

if (n := len(data)) > 3:
    print("Length is greater than 3:", n)  # 5
Enter fullscreen mode Exit fullscreen mode

10. Enumerate with Loops

The enumerate() function returns both the index and the value of each item in an iterable.

# Enumerate with Loops
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
    print(f"{index}: {color}")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)