KEMBAR78
FLOW OF CONTROL-INTRO PYTHON | PPTX
Python Flow Control
Class: XI
Computer Science-083
Introduction
Part-1
Python Flow Of Control
A program’s control flow is the order in which the program’s code executes. A Flow
Control Statement defines the flow of the execution of the Program. Control flow
statements are required to alter the flow of program execution based on the
conditions.
SEQUENTIAL :
SELECTION CONDITIONAL:
ITERATIVE / LOOPING:
It is classified into three categories:
Python Flow Of Control
SEQUENTIAL :
It to provide the order in which we execute our instructions.
Example: Program to display sum of two numbers.
A=10
B=12
sum=A+B
print(sum)
All the lines are in a
sequence
Python Flow Of Control
Decision-making statements : if, if..else, if..elif..else
SELECTION CONDITIONAL: Selection to allow decisions to be made.
Python Flow Of Control
Looping statements : for, while
Branching statements : break, continue
ITERATIVE / LOOPING:
Iteration to loop or repeat our instructions as many times as we
need.
Decision-making Statements or Conditional Statements
if Statement
if - else Statement
elif statements
The if statement is used to test a specific condition. If the condition is true, a block of
code or block of statements will be executed.
The if-else statement is similar to if statement except that, it also provides the block of
the code for the false case of the condition to be checked. If the condition provided in
the if statement is false, then the else statement will be executed.
In python, we have one more conditional statement called elif statements. elif
statement is used to check multiple conditions only if the given if condition false. It’s
similar to an if-else statement and the only difference is that in else we will not check
the condition but in elif we will do check the condition.
Nested if-else statements
Nested if-else statements mean that an if statement or if-else statement is present inside
another if or if-else block. Python provides this feature as well, this in turn will help us to
check multiple conditions in a given program.
elif Ladder
It means a program which contains ladder of elif statements or elif statements which are
structured in the form of a ladder.
This statement is used to test multiple expressions
What is for loop in Python?
Python Loops or Iteration
for loops
There are two types of loops in Python
Loops can execute a block of code number of times until a certain condition is met.
while loops While Loop is used to repeat a block of code. Instead of running the
code block once, It executes the code block multiple times until a
certain condition is met
In Python, "for loops" are called iterators.ust like while loop, "For Loop"
is also used to repeat the program. But unlike while loop which
depends on condition true or false. "For Loop" depends on the
elements it has to iterate
So first we discuss Decision-making Statements
if Statement
if - else Statement
elif statements
Nested if-else statements
elif Ladder
if Statement
The if statement is used to test a specific condition. If the condition is true only, a block
of code or block of statements will be executed.
Syntax:
if condition or expression:
-----Statements-------
-----Statements-------
Example: Program to check number is less than 10 , if yes
then display message "Number is smaller than 10"
no=5
If no<10:
print(“Number is smaller than 10”)
---Output----
Number is smaller than 10
This is indentation. Python relies on indentation (whitespace
at the beginning of a line) to define scope in the code
Let us understand the if condition indentation and scope with the help of another example:
Num = 5
if(Num < 10):
print(“Num is smaller than 10”)
print(“This statements will always be executed”)
---Output----
Number is smaller than 10
This statements will always be executed
Num = 15
if(Num < 10):
print(“Num is smaller than 10”)
print(“This statements will always be executed”)
---Output----
This statements will always be executed
Now in this case if we change the value of Num to 15, then if() condition not satisfied and
it not run the statement inside its scope , but last message will display it is because it not a
part of if condition , its out of its scope.
So in python indentation is very important to get a correct result.
If statement, without indentation (will raise an error)
a = 33
b = 200
if b > a:
print("b is greater than a")
For example , if we need to check that variable A is greater than B.
It display the error message , that
indentation missing.
a = 33
b = 200
if b > a:
print("b is greater than a")
Correct code is when we use proper indentation:
---Output----
B is greater than a
So now we know python indentation
Let take one more example: Write a program to accept the age of a person and check the
age is more than equal to 18, if yes than display ‘You are valid to vote’
age=int(input(“Enter the age:”))
if age>=18:
print(‘You are valid to vote’)
---Output----
Enter the age: 19
You are valid to vote
Now we use the logical operator AND , OR with if condition
OR with if condition:
Now if you want to display message “You select write number” if a user enter any one
number from the given list and the numbers are 1, 3, 5
no=int(input(“Enter the value[1 , 3 , 5]:”))
if (no==1 or no==3 or no == 5):
print(“You selected write number”)
---Output----
Enter the value[1,3,5]: 5
You selected write number
Now we use the logical operator AND , OR with if condition
AND with if condition:
Write a program to check the number is between 10 to 40 , if yes then print message that
“Your value in range of 10-40”
no=int(input(“Enter the value:”))
if (no>=10 and no<=40):
print(“You value in range of 10-40”)
---Output----
Enter the value: 15
Your value in range of 10-40
One important point the And is used for checking the range values. For Example:
How to use if’s without else part in a program
If’s condition:
Write a program to accept a number and print the following message:
If no is 1 “you selected 1” , if no is 2 “you selected 2” , if no is 3 “you selected 3”
no=int(input(“Enter the value:”))
if (no == 1):
print(“You selected 1”) ---Output----
Enter the value: 2
Your selected 2
if (no == 2):
print(“You selected 2”)
if (no == 3):
print(“You selected 3”)

FLOW OF CONTROL-INTRO PYTHON

  • 1.
    Python Flow Control Class:XI Computer Science-083 Introduction Part-1
  • 2.
    Python Flow OfControl A program’s control flow is the order in which the program’s code executes. A Flow Control Statement defines the flow of the execution of the Program. Control flow statements are required to alter the flow of program execution based on the conditions. SEQUENTIAL : SELECTION CONDITIONAL: ITERATIVE / LOOPING: It is classified into three categories:
  • 3.
    Python Flow OfControl SEQUENTIAL : It to provide the order in which we execute our instructions. Example: Program to display sum of two numbers. A=10 B=12 sum=A+B print(sum) All the lines are in a sequence
  • 4.
    Python Flow OfControl Decision-making statements : if, if..else, if..elif..else SELECTION CONDITIONAL: Selection to allow decisions to be made.
  • 5.
    Python Flow OfControl Looping statements : for, while Branching statements : break, continue ITERATIVE / LOOPING: Iteration to loop or repeat our instructions as many times as we need.
  • 6.
    Decision-making Statements orConditional Statements if Statement if - else Statement elif statements The if statement is used to test a specific condition. If the condition is true, a block of code or block of statements will be executed. The if-else statement is similar to if statement except that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed. In python, we have one more conditional statement called elif statements. elif statement is used to check multiple conditions only if the given if condition false. It’s similar to an if-else statement and the only difference is that in else we will not check the condition but in elif we will do check the condition.
  • 7.
    Nested if-else statements Nestedif-else statements mean that an if statement or if-else statement is present inside another if or if-else block. Python provides this feature as well, this in turn will help us to check multiple conditions in a given program. elif Ladder It means a program which contains ladder of elif statements or elif statements which are structured in the form of a ladder. This statement is used to test multiple expressions
  • 8.
    What is forloop in Python? Python Loops or Iteration for loops There are two types of loops in Python Loops can execute a block of code number of times until a certain condition is met. while loops While Loop is used to repeat a block of code. Instead of running the code block once, It executes the code block multiple times until a certain condition is met In Python, "for loops" are called iterators.ust like while loop, "For Loop" is also used to repeat the program. But unlike while loop which depends on condition true or false. "For Loop" depends on the elements it has to iterate
  • 9.
    So first wediscuss Decision-making Statements if Statement if - else Statement elif statements Nested if-else statements elif Ladder
  • 10.
    if Statement The ifstatement is used to test a specific condition. If the condition is true only, a block of code or block of statements will be executed. Syntax: if condition or expression: -----Statements------- -----Statements------- Example: Program to check number is less than 10 , if yes then display message "Number is smaller than 10" no=5 If no<10: print(“Number is smaller than 10”) ---Output---- Number is smaller than 10 This is indentation. Python relies on indentation (whitespace at the beginning of a line) to define scope in the code
  • 11.
    Let us understandthe if condition indentation and scope with the help of another example: Num = 5 if(Num < 10): print(“Num is smaller than 10”) print(“This statements will always be executed”) ---Output---- Number is smaller than 10 This statements will always be executed Num = 15 if(Num < 10): print(“Num is smaller than 10”) print(“This statements will always be executed”) ---Output---- This statements will always be executed Now in this case if we change the value of Num to 15, then if() condition not satisfied and it not run the statement inside its scope , but last message will display it is because it not a part of if condition , its out of its scope.
  • 12.
    So in pythonindentation is very important to get a correct result. If statement, without indentation (will raise an error) a = 33 b = 200 if b > a: print("b is greater than a") For example , if we need to check that variable A is greater than B. It display the error message , that indentation missing. a = 33 b = 200 if b > a: print("b is greater than a") Correct code is when we use proper indentation: ---Output---- B is greater than a
  • 13.
    So now weknow python indentation Let take one more example: Write a program to accept the age of a person and check the age is more than equal to 18, if yes than display ‘You are valid to vote’ age=int(input(“Enter the age:”)) if age>=18: print(‘You are valid to vote’) ---Output---- Enter the age: 19 You are valid to vote
  • 14.
    Now we usethe logical operator AND , OR with if condition OR with if condition: Now if you want to display message “You select write number” if a user enter any one number from the given list and the numbers are 1, 3, 5 no=int(input(“Enter the value[1 , 3 , 5]:”)) if (no==1 or no==3 or no == 5): print(“You selected write number”) ---Output---- Enter the value[1,3,5]: 5 You selected write number
  • 15.
    Now we usethe logical operator AND , OR with if condition AND with if condition: Write a program to check the number is between 10 to 40 , if yes then print message that “Your value in range of 10-40” no=int(input(“Enter the value:”)) if (no>=10 and no<=40): print(“You value in range of 10-40”) ---Output---- Enter the value: 15 Your value in range of 10-40 One important point the And is used for checking the range values. For Example:
  • 16.
    How to useif’s without else part in a program If’s condition: Write a program to accept a number and print the following message: If no is 1 “you selected 1” , if no is 2 “you selected 2” , if no is 3 “you selected 3” no=int(input(“Enter the value:”)) if (no == 1): print(“You selected 1”) ---Output---- Enter the value: 2 Your selected 2 if (no == 2): print(“You selected 2”) if (no == 3): print(“You selected 3”)