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

Python 4,5 Expt SBM.

The document provides an overview of conditional statements and functions in Python, detailing various types such as if, if-else, nested if, and if-elif statements. It also explains the concept of loops, including for loops, while loops, and nested loops, along with their syntax and examples. Additionally, it highlights the benefits of using functions for code readability and reusability.

Uploaded by

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

Python 4,5 Expt SBM.

The document provides an overview of conditional statements and functions in Python, detailing various types such as if, if-else, nested if, and if-elif statements. It also explains the concept of loops, including for loops, while loops, and nested loops, along with their syntax and examples. Additionally, it highlights the benefits of using functions for code readability and reusability.

Uploaded by

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

Jawahar Education Society's

A.C. Patil College of Engineering Kharghar


Department: Electrical Engineering

EXPERIMENT NO.04

AIM: WRITE A PYTHON PROGRAM TO UNDERSTAND DIFFERENT CONDITION STATEMENTS AND CREATING

FUNCTION IN PYTHON.

Conditional statements are an essential part of programming in Python. They allow you to make decisions based on the values of variables or the
result of comparisons.

Like every other programming language, Python also has some predefined conditional statements. A conditional statement as the name suggests
itself, is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions
encountered by the program.

if , if..else, Nested if, if-elif statements


There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do
next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases,
conditional statements can be used. The following are the conditional statements provided by Python.
1. if
2. if..else
3. Nested if
4. if-elif statements.

if Statement in Python
If the simple code of block is to be performed if the condition holds true then the if statement is used. Here the condition mentioned holds
then the code of the block runs otherwise not.
Python if Statement Syntax
Syntax: if condition:
# Statements to execute if
# condition is true

Flowchart of if Statement in Python


Below is the flowchart by which we can understand how to use if statement in Python:

Example: Basic Conditional Check with if Statement


In this example, an if statement checks if 10 is greater than 5. If true, it prints “10 greater than 5”; regardless, it then prints “Program ended”
as the next statement, indicating the program flow.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

if else Statement in Python


In conditional if Statement the additional block of code is merged as else statement which is performed when if condition is false.
Python if-else Statement Syntax
Syntax: if (condition): # Executes this block if # condition is trueelse: # Executes this block if # condition is false

Flow Chart of if-else Statement in Python


Below is the flowchart by which we can understand how to use if-else statement in Python:

Example 1: Handling Conditional Scenarios with if-else


In this example, the code assigns the value 3 to variable x and uses an if..else statement to check if x is equal to 4. If true, it prints “Yes”;
otherwise, it prints “No,” demonstrating a conditional branching structure.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Example 2: Nested if..else Chain for Multiple Conditions


You can also chain if..else statement with more than one condition. In this example, the code uses a nested if..else chain to check the value of
the variable letter. It prints a corresponding message based on whether letter is “B,” “C,” “A,” or none of the specified values, illustrating a
hierarchical conditional structure.

Nested if Statement
if statement can also be checked inside other if statement. This conditional statement is called a nested if statement. This means that inner if
condition will be checked only if outer if condition is true and by this, we can see multiple conditions to be satisfied.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Python Nested If Statement Syntax


Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here# if Block
is end here

Flow chart of Nested If Statement In Python


Below is the flowchart by which we can understand how to use nested if statement in Python:

Example: Managing Nested Conditions for Refined Control


In this example, the code uses a nested if statement to check if the variable num is greater than 5. If true, it further checks if num is less than
or equal to 15, printing “Bigger than 5” and “Between 5 and 15” accordingly, showcasing a hierarchical condition for refined control flow.

if-elif Statement in Python


The if-elif statement is shortcut of if..else chain. While using if-elif statement at the end else block is added which is performed if none of the
above if-elif statement is true
Python if-elif Statement Syntax:-
Syntax: if (condition): statement elif (condition): statement else: statement

Flow Chart of Python if-elif Statement


Below is the flowchart by which we can understand how to use elif in Python:
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Example: Sequential Evaluation with if-elif-else Structure


In this example, the code uses an if-elif-else statement to evaluate the value of the variable letter. It prints a corresponding message based on
whether letter is “B,” “C,” “A,” or none of the specified values, demonstrating a sequential evaluation of conditions for controlled branching.


Python Functions

Python Functions is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and
make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code
contained in it over and over again.
Some Benefits of Using Functions
 Increase Code Readability
 Increase Code Reusability

Python Function Declaration
The syntax to declare a function is:
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Types of Functions in Python


There are mainly two types of functions in Python.
 Built-in library function: These are Standard functions in Python that are available to use.
 User-defined function: We can create our own functions based on our requirements.

Creating a Function in Python


We can define a function in Python, using the def keyword. We can add any type of functionalities and properties to it as we require.

What are the parameters of a function in Python?


Parameters in Python are the variables that take the values passed as arguments when calling the functions. A function can have any number
of parameters. You can also set default value to a parameter in Python.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

EXPERIMENT NO.05

AIM: WRITE A PYTHON PROGRAM TO UNDERSTAND DIFFERENT LOOPING STATEMENTS AND FUNCTIONS.

What is a loop in Python?

A loop in Python is a control structure that allows a set of instructions to be executed repeatedly until a specific condition is met.

Looping Statements in Python

Looping is a powerful technique that simplifies complex problems and allows programmers to repeat a set of instructions a finite number of
times, thus avoiding repetitive code. Python offers three types of looping Statements in Python:

 for loop,

 while loop

 nested loop

For Loop

The for loop is one of the looping statements in Python that is used to iterate over a sequence of elements. This sequence can be a list, tuple,
string, or any other object that can be iterated.

Syntax of for Loop in Python

The for loop syntax in Python is as follows.

for variable in sequence:

# Code block to be executed

In this syntax,

 The variable is a temporary variable that holds the value of each element in the sequence during each iteration of the loop.

 The sequence is the data structure that is being traversed.

 The code block that follows the for statement is executed repeatedly for each element in the sequence.

Example of for Loop in Python

The given code shows the working of the for Loop in Python
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

While Loop

The while loop is another looping statements in Python that are used to repeat a block of code until a certain condition is met.

Syntax of while Loop in Python

The syntax of the while loop in Python is given below.

while condition:

# Code block to be executed

In this syntax,

 condition is a boolean expression that is evaluated at the start of each loop iteration.

 The code block that follows the while statement is executed repeatedly until the condition evaluates to False.

Example of while Loop in Python

Here is the example code that uses the while Loop in Python.

Nested Loops in Python

Python programming language allows to use one loop inside another loop which is called nested loop. Following section shows few examples to
illustrate the concept.

Nested Loops Syntax:

for iterator_var in sequence:


for iterator_var in sequence:
statements(s)
statements(s)
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

The syntax for a nested while loop statement in the Python programming language is as follows:

while expression:
while expression:
statement(s)
statement(s)

A final note on loop nesting is that we can put any type of loop inside of any other type of loops in Python. For example, a for loop can be inside
a while loop or vice versa.

You might also like