Page 297 - CTS - CSA TP - Volume 2
P. 297
COMPUTER SOFTWARE APPLICATION - CITS
TASK 10: Write a program to display Even Numbers using range() in Python
Code:
# Example 10: Using range with start, stop, and step
start=int(input(“Enter the Start Value: “))
stop = int(input(“Enter the Stop Value: “))
step = int(input(“Enter the Step Value: “))
for num in range(start,stop,step):
print(num, end=’ ‘)
Explanation:
1 User Input: The program uses input to prompt the user to enter the start, stop, and step values. The int(...) is
used to convert the input to integers.
2 Range Function: The range(start, stop, step) function generates a sequence of numbers starting from start, up
to (but not including) stop, with a specified step between numbers.
3 For Loop: The for loop iterates over the generated sequence of numbers. For each number, it executes the
indented block of code.
4 Print Statement: Inside the loop, each number (num) is printed using the print function. The end=’ ‘ argument
ensures that the numbers are printed on the same line, separated by a space.
5 User Interaction: The user interaction allows for a dynamic experience where users can input their desired
range and step, and the program displays the sequence accordingly.
Output:
TASK 11: Write a program that takes two numbers (base and exponent) as input and calculates and
prints the result of raising the base to the power of the exponent using the pow function
Code:
# 11. pow
base=int(input(“Enter the Base : “))
exponent = int(input(“Enter the Exponent : “))
power_result = pow(base, exponent)
print(f”{base} raised to the power of {exponent}: {power_result}”)
Explanation
1 User Input: The program prompts the user to enter the base and exponent values using input. The int(...)
converts the entered values to integers.
2 Pow Function: The pow(base, exponent) function is used to calculate the power of the base raised to the
specified exponent.
3 Result Display: The result of the power calculation is stored in the variable power_result and is then printed
using the print statement. The formatted string (f”...”) allows for a clear and concise display of the calculation.
282
CITS : IT & ITES - Computer Software Application - Exercise 132