0% found this document useful (0 votes)
2 views

Lab_02_Loops_Conditional_statements_Functions

Conditional statements

Uploaded by

abdullahamin7877
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab_02_Loops_Conditional_statements_Functions

Conditional statements

Uploaded by

abdullahamin7877
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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
Name: __________________________ Roll No. Score:

Submission Date: Signature of the Lab Tutor:

LAB Subject Data analysis Ability to


PERFORMANCE knowledge and conduct
INDICATOR interpretation experiment
OBTAINED

TO GET FAMILIAR WITH LOOPS, CONDITIONAL STATEMENTS AND


FUNCTIONS IN PYTHON

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.

There are different types of loops:

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 loop flow chart:

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

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.

4. Write a program to print the square of first 5 numbers.


Sample output: 1 4 9 16 25

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

5. Write a program to print the pattern using loops.

(a)
1
22
333
4444
55555

(b)

1
12
123
1234
12345

(c)
*****
****
***
**
*

While loop:

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
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.

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

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.

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

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:

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
Exercise

1. Write a function in python program to find the maximum of three numbers.

2. Write a function in python program to find the even and odd numbers.

3. Write a function to find the cube of a given number.

4. Write a function of calculator in python. Like it can be used to add, subtract, division and
multiplication.

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
5. Write a python function to check whether a given number is positive or negative.

Prepared By: Engr. Saqib Hussain / Engr. Rohma Qadir

You might also like