Here, You can read basic concepts of machine learning and enhance your level manifolds.
Operators in Python

Operators

In Python, operators are symbols or special characters that perform certain operations on one or more values or variables.

For example, the plus (+) operator performs addition between two numbers, while the equal-to (==) operator compares whether two values are equal. Python has several types of operators as given beolow:

  1. Arithmetic Operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, modulus, and exponentiation on one or more operands.

    Example:

    a = 10
    b = 5
    print(a + b) # Output: 15
    print(a - b) # Output: 5
    print(a * b) # Output: 50
    print(a / b) # Output: 2.0 (floating-point division)
    print(a // b) # Output: 2 (integer division)
    print(a % b) # Output: 0 (remainder after division)
    print(a ** b) # Output: 100000 (a raised to the power of b)

  2. Comparison or Relational Operators: Comparison operators are used to compare the values of two operands and return a Boolean value (True or False).

    a = 10
    b = 5
    print(a > b) # Output: True
    print(a < b) # Output: False
    print(a >= b) # Output: True
    print(a <= b) # Output: False
    print(a == b) # Output: False
    print(a != b) # Output: True

  3. Logical Operators: Logical operators are used to combine multiple conditions and return a Boolean value (True or False).

    a = 10
    b = 5
    c = 7
    print((a > b) and (b < c)) # Output: True
    print((a < b) or (b < c)) # Output: True
    print(not(a > b)) # Output: False

  4. Assignment Operators: Assignment operators are used to assign values to variables.

    a = 10
    b = 5
    a += b # Equivalent to a = a + b
    print(a) # Output: 15
    a -= b # Equivalent to a = a - b
    print(a) # Output: 10
    a *= b # Equivalent to a = a * b
    print(a) # Output: 50
    a /= b # Equivalent to a = a / b
    print(a) # Output: 10.0
    a %= b # Equivalent to a = a % b
    print(a) # Output: 0.0
    a **= b # Equivalent to a = a ** b
    print(a) # Output: 0.0

  5. Bitwise Operators: Bitwise operators are used to perform operations on binary digits.

    a = 60 # binary value 111100
    b = 13 # binary value 1101
    print(a & b) # Output: 12 (binary value 1100)
    print(a | b) # Output: 61 (binary value 111101)
    print(a ^ b) # Output: 49 (binary value 110001)
    print(~a) # Output: -61 (complement of binary value 111100)
    print(a << 2) # Output: 240 (binary value 11110000)
    print(a >> 2) # Output: 15 (binary value 1111)

  6. Identity Operators: Identity operators are used to compare the memory addresses of two objects.

    a = [1, 2, 3]
    b = a
    c = [1, 2, 3]
    print(a is b) # Output: True
    print(a is c) # Output: False
    print(a is not c) # Output: True

    In the above example, a and b refer to the same list object in memory, so a is b is True. However, c is a new list object with the same values as a, so a is c is False.

  7. Membership Operators: Membership operators are used to test whether a value is a member of a sequence or a mapping.

    a = [1, 2, 3]
    b = 2
    c = 4
    print(b in a) # Output: True
    print(c in a) # Output: False
    print(b not in a) # Output: False
    print(c not in a) # Output: True

    In the above example, b is a member of the list a, so b in a is True. However, c is not a member of a, so c in a is False.

  8. Ternary Operator: Ternary operator is used to assign values based on a condition.

    a = 10
    b = 5
    c = "a is greater than b" if a > b else "a is less than or equal to b"
    print(c) # Output: "a is greater than b"

    In the above example, if a is greater than b, the value of c will be "a is greater than b", otherwise it will be "a is less than or equal to b".

  9. Null Coalescing Operator: Null coalescing operator is used to check for the existence of a value and provide a default value if it does not exist.

    a = None
    b = 10
    c = a or b
    print(c) # Output: 10

    In the above example, a is None, so c takes the value of b, which is 10.

  10. Slice Operator: Slice operator is used to extract a portion of a sequence such as a string or a list.

    a = "Hello, World!"
    b = [1, 2, 3, 4, 5]
    print(a[2:7]) # Output: "llo, "
    print(b[1:4]) # Output: [2, 3, 4]
    print(b[:2]) # Output: [1, 2]
    print(b[2:]) # Output: [3, 4, 5]
    print(b[:-1]) # Output: [1, 2, 3, 4]

    In the above example, a[2:7] extracts a slice of a from the third character up to (but not including) the eighth character. Similarly, b[1:4] extracts a slice of b from the second element up to (but not including) the fifth element. b[:2] extracts a slice of b from the beginning up to (but not including) the third element, and b[2:] extracts a slice of b from the third element to the end. b[:-1] extracts a slice of b from the beginning up to (but not including) the last element.

Advertisement

Advertisement