Page 260 - CTS - CSA TP - Volume 2
P. 260
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 126 : Construct and analyze code segments
that perform iteration
Objectives
At the end of this exercise you shall be able to
• develop python program to Construct and analyze code segments that perform iteration.
Requirements
Tools/Materials
• PC/Laptop with Windows OS
• Latest Version of Python
Procedure
Python While loop
TASK 1: Python Program for printing numbers from 1 to n
Code:
limit=int(input(“Enter the Limit: “))
# Initialize the variable i with the value 1
i = 1
# The loop will continue as long as i is less than or equal to limit
whilei<= limit:
# Print the current value of i, followed by a space instead of a newline
print(i, end=’ ‘)
# Increment the value of i by 1 in each iteration
i += 1
Explanation:
• i is initialized to 1.
• The while loop continues as long as the condition i<= limit is true.
• Inside the loop, print(i, end=’ ‘) prints the current value of i followed by a space, without moving to the next line.
• i += 1 increments the value of i by 1 in each iteration of the loop.
Output:
245