Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Quotient and Remainder

In this example, we will explore a simple Python script that performs basic arithmetic operations: division and modulus. The script prompts the user to input two integers: a dividend and a divisor. It then calculates and prints the quotient and remainder of the division operation. This example demonstrates the use of basic input/output functions, arithmetic operators, and string formatting in Python.

Code Example

dividend = int(input("Please enter dividend: "))
divisor = int(input("Please enter divisor: "))

quotient = dividend / divisor
remainder = dividend % divisor

print("Quotient is: " + repr(quotient))
print("Remainder is: " + repr(remainder))

Detailed Code Explanation

Input Statements

dividend = int(input("Please enter dividend: "))
divisor = int(input("Please enter divisor: "))

These lines prompt the user to enter two numbers: the dividend and the divisor. The input() function reads a string from the user, and the int() function converts that string to an integer.

Step-by-Step Explanation

  • input("Please enter dividend: "): Displays the message “Please enter dividend:” and waits for the user to input a value.
  • int(...): Converts the user input (a string) to an integer.
  • The same process is repeated for the divisor.

Arithmetic Operations

quotient = dividend / divisor
remainder = dividend % divisor

These lines perform the division and modulus operations.

Step-by-Step Explanation

  • dividend / divisor: Divides the dividend by the divisor and stores the result in the quotient variable. The result is a floating-point number.
  • dividend % divisor: Calculates the remainder of the division of the dividend by the divisor and stores it in the remainder variable.

Output Statements

print("Quotient is: " + repr(quotient))
print("Remainder is: " + repr(remainder))

These lines print the results of the arithmetic operations.

Step-by-Step Explanation

  • repr(quotient): Converts the quotient value to a string in a way that is suitable for debugging (more precise than str()).
  • "Quotient is: " + repr(quotient): Concatenates the string “Quotient is: ” with the string representation of quotient.
  • print(...): Outputs the concatenated string to the console.
  • The same process is repeated for the remainder.