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

Lab_02_Loops_Conditional_statements_Functionsss

function

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)
1 views

Lab_02_Loops_Conditional_statements_Functionsss

function

Uploaded by

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

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 Data analysis Ability to


Subject
PERFORMANCE and conduct
INDICATOR knowledge 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.

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

Example #01 Print 5 numbers through for loop

for x in range(5):
print(x)

Example # 02 Print a string horizontally.

a = 'Jamshoro'
for i in a:
print(i, end= ' ')

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

Example # 03: Print a list through for loop.

Example # 4 Square Pattern

Code:

n = 5
for i in range(n):
for j in range(n):
print('*', end='')
print('')

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

Example # 5 Right angle Triangle

Code:
n = 5
for i in range(n):
for j in range(i+1):
print('*', end='')
print('')

Example # 6 Decreasing Triangle

Code:
n = 5
for i in range(n):
for j in range(i,n):
print('*', end='')
print('')

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

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

(a)
1
22
333
4444
55555

(b)

1
12
123
1234
12345

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

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
While loop:

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.

While loop has three terms ,

 one is initial value,

 2nd is final condition

 increment is used to increase the value until the final condition is reached.

While 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
Example:

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 :

Conditional Statement in Python perform different computations or actions depending on whether a


specific Boolean constraint evaluates to true or false.
Types of conditions:
 If
 If-Else
 If ELIF Else
If:
Python if Statement is used for decision-making operations. It contains a body of code which runs only
when the condition given in the if statement is true. If the condition is false, then the optional else statement
runs which contains some code for the else condition.

Python supports the usual logical conditions from mathematics:

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
 Equals: a == b
 Not Equals: a != b
 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b

Example # 01 Use of if condition in a program

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

Example # 02 Use of If-else statement

a = int(input("Enter a number"))

if a %2 ==0:
print(a, " is Even number")
else:
print(a, "is Odd number")

Example # 03 Use of If Else Statement

age = int(input("Enter your age"))

if age > 18:


print("Congrats! You can apply for CNIC")
else:
print("You can't apply for cnic")

If ELIF Else

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
The syntax followed by the if-else-if statement is:

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

marks = int(input("Please enter the marks (0-100): "))


if 0 <= marks <= 100:
if marks >= 90:
grade = 'A'
elif marks >= 80:
grade = 'B'
elif marks >= 70:
grade = 'C'
elif marks >= 60:
grade = 'D'
else:
grade = 'F'
print("Grade:", grade)
else:
print("Invalid marks. Please enter marks between 0 and 100.")

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.

Advantages of User Defined Function

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.

Example # 01 Simple function to say greet message.

def greet(): # Function Definition


a = input("Enter your name")
print("Nice to meet you!", a)

greet() # Function Call

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
Output

Example # 02 Function to find the sum of two numbers.

def sum (a,b): #(a,b) are called as function parameters


c = a + b
print("The sum is ", c)

sum(5,5) # (5,5) are called as arguments

Output

Example # 03 Function to check even and odd numbers

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

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