Lab_02_Loops_Conditional_statements_Functionsss
Lab_02_Loops_Conditional_statements_Functionsss
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.
for x in range(5):
print(x)
a = 'Jamshoro'
for i in a:
print(i, end= ' ')
Code:
n = 5
for i in range(n):
for j in range(n):
print('*', end='')
print('')
Code:
n = 5
for i in range(n):
for j in range(i+1):
print('*', end='')
print('')
Code:
n = 5
for i in range(n):
for j in range(i,n):
print('*', end='')
print('')
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 is a keyword. It is used to execute a block of statements repeatedly until a given condition is satisfied. And
when the condition becomes false, the line immediately after the loop in the program is executed.
increment is used to increase the value until the final condition is reached.
Output
Exercise
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.
Conditional Statements :
a = 10
if a%2 == 0:
print("It is a even number")
IF-Else
The if-else statement is used to code the same way you would use it in the English language. The syntax
for the if-else statement is:
Else:
In programming languages, an else statement is an alternative statement that is executed if the result of a
previous test condition evaluates to false.
if(condition):
Indented statement block for when condition is TRUE
else:
Indented statement block for when condition is FALSE
a = int(input("Enter a number"))
if a %2 ==0:
print(a, " is Even number")
else:
print(a, "is Odd number")
If ELIF Else
if(Condition1):
Indented statement block for Condition1
elif(Condition2):
Indented statement block for Condition2
else:
Alternate statement block if all condition check above fails
Example # 04 Use If Elif Else statement to calculate your grades.
A: 90 and above
B: between 80 and 89
C: between 70 and 79
D: between 60 and 69
F: below 60
Lab Tasks
1. Write a program that takes three inputs from the user and displays the largest number.
2. Write a program to make a calculator using conditional statements.
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
Prepared By: Engr. Saqib Hussain / Engr. Rohma Qadir
Mehran University of Engineering & Technology, Jamshoro
Department of Telecommunication Engineering
BS in Cyber Security (22BSCYS)
5th Semester, 3rd Year
Artificial Intelligence (CSC320)
Lab Experiment No # 02
Functions:
A function is a group of related statements that performs a specific task. Functions help break our program
into smaller and modular chunks. As our program grows larger and larger, functions make it more
organized and manageable. Function can be reusable throughout the program.
Function has two types:
1. Built-in function
2. User define function
Built-in function:
The Python built-in functions are defined as the functions whose functionality is pre-defined in Python.
The python interpreter has several functions that are always present for user.
Examples:
Help
Add
Insert
Remove
Extend
User defined function:
A function that you define yourself in a program is known as user defined function. You can give any
name to a user defined function, however you cannot use the Python keywords as function name. In
python, we define the user-defined function using def keyword, followed by the function name.
1. User-defined functions help to decompose a large program into small segments which makes
program easy to understand, maintain and debug.
2. If repeated code occurs in a program. Function can be used to include those codes and execute
when needed by calling that function.
3. Programmers working on large project can divide the workload by making different functions.
Output
def even_odd():
a = int(input("Enter a number to check "))
if a % 2 == 0:
print("Entered number is even ")
else:
print("Entered number is odd")
return a
even_odd()
Exercise