Back to Browse

Python | Operators, and Expressions in Python basic arithmetic operations | SNS INSTITUTIONS

4 views
May 5, 2026
5:41

This program demonstrates the use of variables, operators, and expressions in Python to perform basic arithmetic operations. It begins by prompting the user to input two numbers using the input() function. Since input() returns values as strings, the inputs are converted into floating-point numbers using the float() function. This allows the program to handle both integer and decimal values effectively. The two values entered by the user are stored in variables a and b. These variables act as containers that hold the numeric data for further processing. The program then performs four fundamental arithmetic operations: addition, subtraction, multiplication, and division. Each result is stored in separate variables named add, sub, mul, and div. For division, a conditional expression is used to handle the case where the second number (b) is zero. Division by zero is mathematically undefined and would normally cause a runtime error. To prevent this, the program checks whether b is not equal to zero before performing the division. If b is zero, the result is set to the string "Undefined" instead of attempting the operation. Finally, the program displays the results using formatted string literals (f-strings), which allow variables to be embedded directly within strings. This makes the output more readable and user-friendly. Overall, this program is a simple yet effective example of how variables, operators, and expressions work together to perform computations in Python while also handling potential errors gracefully. #snsinstitutions #snsdesignthinkers #designthinking

Download

0 formats

No download links available.

Python | Operators, and Expressions in Python basic arithmetic operations | SNS INSTITUTIONS | NatokHD