Practical 3: Program to Calculate and Display the Power of a Number Without Using Formatting Characters
Problem Statement
Write a Python program to calculate and display the power of a number without using formatting characters.
The program should:
- Accept a base number and an exponent from the user.
- Compute the result using the exponentiation operator (**).
- Display the result without using formatting techniques like format() or f-strings.
Solution Code
# Program to display the power of a number without using formatting characters
# Input: Take base and exponent from the user
base = int(input("Enter the base number: "))
exponent = int(input("Enter the exponent: "))
# Calculating power
result = base ** exponent
# Output the result
print("The result of", base, "raised to the power of", exponent, "is", result)
Output
- The program will take two integer inputs: a base and an exponent.
- It will calculate the power using the ** operator.
- The result will be displayed in a simple output format.
Example output:
Enter the exponent: 10
The result of 2 raised to the power of 10 is 1024
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 3 Practical 3 solution where we have written Python program to calculate and display the power of a number without using formatting characters.
- Taking Input from the User:
- The input() function is used to take two integer values from the user: base and exponent.
- The int() function converts the input from string to an integer.
- Calculating the Power:
- The ** operator is used to calculate the power (base ** exponent).
- The result is stored in the variable result.
- Displaying the Output:
- The print() function is used to display the result.
- String concatenation is avoided, and values are separated using commas in print().
Key Concepts Learned
- Exponentiation Using ** Operator:
- The ** operator is used to compute powers in Python.
- Basic Input and Output Handling:
- Taking user input, performing calculations, and displaying results.
- String Concatenation with print():
- Instead of using formatting methods, values are printed using commas in print(), which automatically adds spaces.