Basic Operators in Python

Python Methods and Functions

  1. Arithmetic operators: arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
  2. Operator Description Syntax
    + Addition: adds two operands x + y
    - Subtraction: subtracts two operands x - y
    * Multiplication: multiplies two operands x * y
    / Division (float): divides the first operand by the second x / y
    // Division (floor): divides the first operand by the second x // y
    % Modulus: returns the remainder when first operand is divided by the second x% y

    # Arithmetic operator examples

    a = 9

    b = 4

     
    # Adding numbers

    add = a + b

    # Subtract numbers

    sub = a - b

    # Number multiplication

    mul = a * b

    # Division (number) of the number

    div1 = a / b

    # Department (floor) rooms

    div2 = a / / b

    # Modulo both numbers

    mod = a % b

      
    # print results

    print (add)

    print (sub)

    print (mul)

    print (div1)

    print (div2)

    print (mod)

    Output:

     13 5 36 2.25 2 1 
  3. Relational Operators: Relational operators compare values. It returns either True, or False depending on the condition.
  4. Operator Description Syntax
    & gt; Greater than: True if left operand is greater than the right x & gt; y
    Less than: True if left operand is less than the right x
    == Equal to: True if both operands are equal x == y
    ! = Not equal to - True if operands are not equal x! = y
    & gt; = Greater than or equal to: True if left operand is greater than or equal to the right x & gt; = y
    Less than or equal to: True if left operand is less than or equal to the right x

    # Examples of relational operators

    a = 13

    b = 33

     
    # a & gt; b incorrect

    print (a & gt; b)

     
    # a & lt; b correct

    print (a & lt; b)

     
    # a == b incorrect

    print (a = = b)

     
    # a! = b true

    print (a! = b)

     
    # a & gt; = b incorrect

    print (a & gt; = b)

     
    # a & lt; = b true

    print (a & lt; = b)

    Exit:

     False True False True False True 
  5. Boolean operators: Boolean operators perform boolean operations AND , logical OR and logical NOT .
  6. Operator Description Syntax
    and Logical AND: True if both the operands are true x and y
    or Logical OR: True if either of the operands is true x or y
    not Logical NOT: True if operand is false not x

    # Boolean operator examples

    a = True

    b = False

     
    # Print a and b - False

    print (a and b)

     
    # Print a or b - True

    print ( a or b)

     
    # Printing is not false

    print ( not a)

    Output:

     False True False 
  7. Bitwise operators: Bitwise operators act on bits and perform a bitwise operation.
  8. Operator Description Syntax
    & amp; Bitwise AND x & amp; y
    | Bitwise OR x | y
    ~ Bitwise NOT ~ x
    ^ Bitwise XOR x ^ y
    & gt; & gt; Bitwise right shift x & gt; & gt;
    Bitwise left shift x

    # Examples of bitwise operators

    a = 10

    b = 4

     
    # Print bitwise AND

    print (a & amp; b )

     
    # Print a bitwise OR operation

    print (a | b) 

     
    # Printing is NOT bitwise operation

    print (~ a)

      
    # print bitwise XOR operation

    print (a ^ b)

     
    # print bitwise shift right

    print (a & gt; & gt;  2 )

     
    # print bitwise shift left

    print (a & lt; & lt; 2 )

    Output:

     0 14 -11 14 2 40 
  9. Assignment operators: operators assignments are used to assign values ​​to variables.
  10. Operator DescriptionSyntax
    = Assign value of right side of expression to left side operand x = y + z
    + = Add AND: Add right side operand with left side operand and then assign to left operand a + = b a = a + b
    - = Subtract AND: Subtract right operand from left operand and then assi gn to left operand a- = b a = ab
    * = Multiply AND: Multiply right operand with left operand and then assign to left operand a * = b a = a * b
    / = Divide AND: Divide left operand with right operand and then assign to left operand a / = b a = a / b
    % = Modulus AND: Takes modulus using left and right operands and assign result to left operand a% = b a = a% b
    // = Divide (floor) AND: Divide left operand with right operand and then assign the value (floor) to left operand a // = b a = a // b
    **= Exponent AND: Calculate exponent (raise power) value using operands and assign value to left operand a ** = b a = a ** b
    & amp; = Performs Bitwise AND on operands and assign value to left operand a & amp; = b a = a & amp; b
    | = Performs Bitwise OR on operands and assign value to left operand a | = b a = a | b
     ^ = Performs Bitwise xOR on operands and assign value to left operand a ^ = b a = a ^ b
    & gt; & gt; = Performs Bitwise right shift on operands and assign value to left operand a & gt; & gt; = b a = a & gt; & gt; b
    Performs Bitwise left shift on operands and assign value to left operand a

  11. Special Operators: There are several special operator types such as
    • Identity Operators
      is and not — identity operators, which are used to check if two values ​​are in the same part of memory. Two equal variables do not mean that they are identical.
        is  True if the operands are identical  is not  True if the operands are not identical 

    # Examples of identification statements

    a1 = 3

    b1 = 3

    a2 = 'GeeksforGeeks'

    b2 = 'GeeksforGeeks'

    a3 = [ 1 , 2 , 3 ]

    b3 = [ 1 , 2 , 3 ]

     

     

    print (a1 is not b1)

      

      

    print (a2 is b2)

     
    # Output False because lists are volatile.

    print (a3   is b3)

    Output:

     False True False 
    • Operator Membership — 
      in, and not in are membership operators; used to check if a value or variable exists in a sequence.
        in  True if value is found in the sequence  not in  True if value is not found in the sequence 

    # Operator membership examples

    x = 'Geeks for Geeks'

    y = { 3 : 'a' , 4 : ' b' }

      

     

    print ( 'G' in x)

     

    print ( 'geeks' not in x)

     

    print ( 'Geeks' not in x)

     

    print ( 3 in y)

     

    print ( 'b' in y)

    Output:

     True True False True False 




Tutorials