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.
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))
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.
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.quotient = dividend / divisor
remainder = dividend % divisor
These lines perform the division and modulus operations.
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.print("Quotient is: " + repr(quotient))
print("Remainder is: " + repr(remainder))
These lines print the results of the arithmetic operations.
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.