AI and ML with
Python
Chapter 5 : Control Structures
Chapter 5:
Control Structures
What are Control Structures?
● Control Structures are used to control the execution of
some code based on whether it satisfies the condition or
event.
● It can be used to alter the flow of control of the program.
● It is defined using a block of code, which analyses the
condition and chooses a direction in which to proceed
based on the parameters.
Types of Control Structures in Python:
● if
● if else
● if elif else
● Nested if
● while loop
● for loop
● break
● continue
Python if Statement
● It is used to execute a statement if a particular condition is
true.
● It is used to check the condition is true OR false between
two or more variables.
● Conditions can be:
○ Equals to(==), Less than(<), Greater than(>), Not equals
to(!=).
○ Less than or Equals to(<=), Greater than or Equal to.(>=)
Example: if Statement in Python
● Indentation is mandatory in Control Structure Blocks.
● The Following example checks if X is less than Y.
Python if else Statement
● It is used to check if a particular condition is satisfied or
not.
● If the condition is true, if block will be executed.
● If the condition is false, else block will be executed.
Example: if else Statement in Python
● If X is less than Y, if block will execute.
● If X is greater than Y, else block will execute.
Python if elif else Statement
● It is used to check if a particular condition is satisfied or
not.
● If the first condition is true, if block will be executed.
● If the elif condition is true, the elif block will execute.
● Finally if no condition is true, else block will execute.
Example: if elif else Statement in Python
Python Nested if Statements
● We have can if Statements inside another if Block.
● This is called Nesting in Computer Programming where
control structures are created inside another control
structures.
Example: Nested if Statements Python
Taking User Input in Python
● In order to take User Input, we will call a method called
input().
● We will prompt what the input should be by passing an
Argument inside it.
Python while Loop
● while Loop is executed when a particular condition is true.
● It will keep executing the while block, till the condition is
true.
● Once the condition turns false, the while Loop will stop
executing.
Example: while Loop in Python
Python for Loop
● for Loops are used to iterate over lists,dictionaries,tuples,sets
or a string.
● We can iterate inside to search for specific data with specific
index or to print all the data inside them.
Example: Printing List Data using for Loop
in Python
Example: Searching for Data inside List
using for Loop in Python
break Statement in Python
● break Statement will stop the execution of the block and
exit the code block.
● It is used to stop the control structure once a condition is
satisfied.
Example: break Statement in Python
continue Statement in Python
● continue statement is generally used with a conditional
statement that omits the current iteration and continues to
the next iteration when the condition is met.
Example: continue Statement in Python
Thank You