Basic operations

Basic operations

·

2 min read

Let's look at some basic operations that you may require while programming.

Arithmetic operations:

  1. Addition(+): adds two numbers

  2. Subtraction (-): Subtracts the right operand from the left.

  3. Multiplication (*): Multiplies two numbers.

  4. Division (/): Divides the left operand by the right.

  5. Floor Division (//): Returns the floor of the division result (discards the fractional part).

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

  7. Exponentiation (**): Raises the left operand to the power of the right.

    Here is some code to further your understanding:

     # Addition
     result_addition = 5 + 3
     print("Addition:", result_addition)
     #outputs 8
    
     # Subtraction
     result_subtraction = 10 - 4
     print("Subtraction:", result_subtraction)
     #outputs 6
    
     # Multiplication
     result_multiplication = 6 * 2
     print("Multiplication:", result_multiplication)
     #outputs 12
    
     # Division
     result_division = 15 / 3
     print("Division:", result_division)
     #outputs 5
    
     # Floor Division
     result_floor_division = 15 // 4
     print("Floor Division:", result_floor_division)
     #outputs 3
    
     # Modulus (Remainder)
     result_modulus = 17 % 5
     print("Modulus:", result_modulus)
     #outputs 2
    
     # Exponentiation
     result_exponentiation = 2 ** 3
     print("Exponentiation:", result_exponentiation)
     #outputs 8
    

Next, let's look at some Logical Operators and comparison operators.

Logical operators and Comparison operators

  • Comparison Operators (==, !=, >, <, >=, <=): Compare values and return boolean results i.e. true or false.

  •   x = 10
      y = 5
    
      is_equal = x == y
      #now is_equal is false
      is_not_equal = x != y
      #now is_equal is true
    
  • Logical Operators (and, or, not): Combine or negate boolean values.

    1. and Operator:

The and operator returns True if both operands are True, otherwise, it returns False.

    x = True
    y = False
    result = x and y

    # result is False because one of the operands (y) is False
  1. or Operator:
  • The or operator returns True if at least one of the operands is True. It returns False only if both operands are False.

  •   x = True
      y = False
      result = x or y
      # result is True because one of the operands (x) is True
    

    3. not Operator:

    The not operator returns the opposite boolean value of the operand. If the operand is True, not returns False. If the operand is False, not returns True.

x = True
result = not x
# result is False because not True is False

You can also combine these operators to form complex expressions which can be used for evaluation and other purposes.

That's it for today!