Lab_02_Loops_Conditional_statements_Functions
Lab_02_Loops_Conditional_statements_Functions
Learning Outcomes:
After this experiment, the understudy will be able to
• Basic Syntax of For and While Loops in python • Basic syntax of Conditional Statements in
Python.
• Understand the basic syntax of Functions in Python
Required Software:
• Python 3.13.1, Visual Studio Code (VS Code)
Loops :
Loop is a programming structure. It is used to execute a block of statement repeatedly until the particular
condition is satisfied. loop uses iterations which is repeating execution of same block again and again until the
final condition is reached. Iterations is a repeating steps of loops. Typically, a certain process is done, such as
getting an item of data and changing it, and then some condition is checked such as whether a counter has
reached a prescribed number.
1. For loop
2. While loop
For loop:
For is a keyword. For loop is used for sequential data types like list, tuple, string, range, set. For loop is used
to control flow statement that is used to repeatedly execute a group of statements as long as the condition is
satisfied. for loop is used for iterating over a sequence. In for loop, we can use different methods like to insert,
remove, delete, sort, copy etc.
Exercise
1. Write a program to print n natural numbers in descending order using for loop.
2. Write a python code that prints out a multiplication table based on the number provided as input.
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
…
5x10=50
3. Write a program to display all the multiples of 3 within the range 10 to 50.
(a)
1
22
333
4444
55555
(b)
1
12
123
1234
12345
(c)
*****
****
***
**
*
While loop:
1. Write a program that prints all even numbers from 0 to 20 using a while loop.
2. Write a program that calculates the factorial of a number provided by the user using a while loop.
3. Guess the Number: Create a simple guessing game where the program randomly selects a number
between 1 and 10, and the user has to guess it. Use a while loop to keep asking the user until they
guess correctly.
Lab Tasks
1. Write a program that takes three inputs from the user and displays the largest number.
3. Write a program that takes values of length and breadth of a rectangle from user and check if it is
square or not.
4. Write a program to calculate the percentage of the students. Student will enter the marks of 5
subjects out of 100 and then it will display the result like:
Percentage > 85 – A+
Percentage 80 to 75 → A
Percentage 70 to 65 → B
Percentage 60 to 55 → C
Percentage 50 → D
Percentage < 50 → Fail
Functions:
2. Write a function in python program to find the even and odd numbers.
4. Write a function of calculator in python. Like it can be used to add, subtract, division and
multiplication.