Python 4,5 Expt SBM.
Python 4,5 Expt SBM.
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 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
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 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
EXPERIMENT NO.05
AIM: WRITE A PYTHON PROGRAM TO UNDERSTAND DIFFERENT LOOPING STATEMENTS AND FUNCTIONS.
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 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.
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 code block that follows the for statement is executed repeatedly for each element in the sequence.
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.
while condition:
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.
Here is the example code that uses the while Loop 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.
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.