Sequential Statements
Python Basics: Introduction - Features –
Execution Environment – Indentation – Comments -
BasicElements: Data Types– Variables – Input/Output
Statements – format() –
Sequential – Basics of Conditionals – Selection
(Conditional): Simple if – if else – If elif else – Nested if –
Loops: for – for else – while - while else – Selection
(Unconditional): break – continue – pass – Nested Loops –
Functions: - Introduction to Functions, inbuilt functions,
user defined functions, passing parameters, return values,
recursion, Lambda functions
Decision Making or Conditional
Statements
• Decision making is about deciding the order of
execution of statements based on certain
conditions.
• if statements
• if-else statements
• if-elif ladder
• Nested if statements
Python if
statement
if statement is a conditional statement in Python used
to determine whether a block of code will be executed.
If the program finds the condition defined in the if statement
true, it will execute the code block inside the if statement.
Syntax:
if ( expression ):
Statement 1
Statement 2
.
Statement n
Example:
a = 20
b = 20
if a == b:
print("a and b are equal")
print("If block ended")
if-else
statements
if-else statement checks the expression and executes
the if block when the expression is True otherwise it
will execute the else block of code.
syntax
if( expression ):
Statement
else:
Statement
Example:
number1 = 20
number2 = 30
if number1 >=
number2:
print("number 1
is greater than
number 2")
else:
print("number 2
is greater than
number 1")
Python if-elif ladder
• elif keyword to chain multiple conditions one after another. With elif ladder,
we can make complex decision-making statements.
Syntax
if( expression1 ):
statement
elif (expression2 ) :
statement
elif(expression3 ):
statement
.
.
else:
statement
Example:
price = 100
if price > 100:
print("price is greater than
100") if price == 100:
print("price is
100") if price < 100:
print("price is
less than 100")
Nested if
statement
Nested if
statement
.
• Syntax:
statements is an if statement inside another
if
if (expression):
if(expression):
Statement of nested
if else:
Statement of nested if
else Statement of outer if
Statement outside if block
Example:
num1 = int(input())
num2 = int(input())
if num1 >= num2:
if num1 ==
num2:
print(f'{num1}
and {num2} are
equal')
else:
print(f'{num1}
is greater than
{num2}')
else:
print(f'{num1} is
smaller than
Looping
A loop statement allows us to execute a statement or
group of statements multiple times until the condition is
satisfied
Types:
• while loop
• While loop else
• for loop
• for loop else
• Nested loops
while
loop
while loop 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.
Syntax:
• while expression:
• statement(s)
Example:
count = 0
while count < 3:
count = count +
1
print("Hello")
while loop
else
• The statements in the else block will be executed
after all iterations are completed. The program
exits the loop only after the else block is executed.
Exampl
e
x = 0
while x < 5:
x += 1
print(f"iteration no
{x} in while loop")
else:
print("else block in
loop")
print("Out of loop")
iteration no 1 in for
loop iteration no 2 in
for loop iteration no 3
in for loop iteration
no 4 in for loop
iteration no 5 in for
loop else block in loop
Out of loop
For
loop
For loops are used for sequential traversal. For
example: traversing a list or string or array etc.
for iterator_var in
sequence: statements(s)
Example:
fruits = ["apple",
"banana", "cherry"]
for x in fruits:
print(x)
For loop
else
• The statements in the else block will be executed
after all iterations are completed. The program
exits the loop only after the else block is executed.
For
Loop
for x in range(5):
print("iteration no {} in for
loop".format(x+1))
else:
print("else block in loop")
print("Out of loop")
• iteration no 1 in for
loop iteration no 2 in
for loop iteration
no 3 in for loop
iteration no 4 in for
loop iteration no 5 in
for loop else
block in loop
Out of loop
Python nested
loop
A nested loop is a loop inside the body of the outer
loop. The inner or outer loop can be any type, such as
a while loop or for loop. For example, the outer for
loop can contain a while loop and vice versa.
Syntax:
for element in sequence :
for element in sequence:
body of inner for loop
body of outer for loop
Example:
for i in range(1, 11):
# nested loop
# to iterate from 1 to 10
for j in range(1, 11):
# print multiplication
print(i * j, end=' ')
print()
Loop control statements
• Loop control statements are used to change the
flow of execution
• These can be used if you wish to skip an iteration or
stop the execution.
The three types of loop control statements are:
• break statement
• continue statement
• pass statement
Break statement
Based on the given condition, the break statement
stops the execution and brings the control out of the
loop.
for num in range(1, 11):
if num == 5:
break
else:
pri
Continue statement
Continue statement is used to skip the current
iteration when the condition is met and allows the
loop to continue with the next iteration
for num in range(1, 11):
if num == 6:
continue
print(num)
pas
s
Pass statement is used when we want to do nothing
when the condition is met. It doesn’t skip or stop the
execution, it just passes to the next iteration
for num in range(1, 11):
if num == 6:
Pass
print(num)

pds first unit module 2 MODULE FOR ppt.pptx

  • 1.
  • 2.
    Python Basics: Introduction- Features – Execution Environment – Indentation – Comments - BasicElements: Data Types– Variables – Input/Output Statements – format() – Sequential – Basics of Conditionals – Selection (Conditional): Simple if – if else – If elif else – Nested if – Loops: for – for else – while - while else – Selection (Unconditional): break – continue – pass – Nested Loops – Functions: - Introduction to Functions, inbuilt functions, user defined functions, passing parameters, return values, recursion, Lambda functions
  • 3.
    Decision Making orConditional Statements • Decision making is about deciding the order of execution of statements based on certain conditions. • if statements • if-else statements • if-elif ladder • Nested if statements
  • 4.
    Python if statement if statementis a conditional statement in Python used to determine whether a block of code will be executed. If the program finds the condition defined in the if statement true, it will execute the code block inside the if statement. Syntax: if ( expression ): Statement 1 Statement 2 . Statement n
  • 5.
    Example: a = 20 b= 20 if a == b: print("a and b are equal") print("If block ended")
  • 6.
    if-else statements if-else statement checksthe expression and executes the if block when the expression is True otherwise it will execute the else block of code. syntax if( expression ): Statement else: Statement
  • 7.
    Example: number1 = 20 number2= 30 if number1 >= number2: print("number 1 is greater than number 2") else: print("number 2 is greater than number 1")
  • 8.
    Python if-elif ladder •elif keyword to chain multiple conditions one after another. With elif ladder, we can make complex decision-making statements. Syntax if( expression1 ): statement elif (expression2 ) : statement elif(expression3 ): statement . . else: statement
  • 9.
    Example: price = 100 ifprice > 100: print("price is greater than 100") if price == 100: print("price is 100") if price < 100: print("price is less than 100")
  • 10.
    Nested if statement Nested if statement . •Syntax: statements is an if statement inside another if if (expression): if(expression): Statement of nested if else: Statement of nested if else Statement of outer if Statement outside if block
  • 11.
    Example: num1 = int(input()) num2= int(input()) if num1 >= num2: if num1 == num2: print(f'{num1} and {num2} are equal') else: print(f'{num1} is greater than {num2}') else: print(f'{num1} is smaller than
  • 12.
    Looping A loop statementallows us to execute a statement or group of statements multiple times until the condition is satisfied Types: • while loop • While loop else • for loop • for loop else • Nested loops
  • 13.
    while loop while loop isused 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. Syntax: • while expression: • statement(s)
  • 14.
    Example: count = 0 whilecount < 3: count = count + 1 print("Hello")
  • 15.
    while loop else • Thestatements in the else block will be executed after all iterations are completed. The program exits the loop only after the else block is executed.
  • 16.
    Exampl e x = 0 whilex < 5: x += 1 print(f"iteration no {x} in while loop") else: print("else block in loop") print("Out of loop") iteration no 1 in for loop iteration no 2 in for loop iteration no 3 in for loop iteration no 4 in for loop iteration no 5 in for loop else block in loop Out of loop
  • 17.
    For loop For loops areused for sequential traversal. For example: traversing a list or string or array etc. for iterator_var in sequence: statements(s)
  • 18.
    Example: fruits = ["apple", "banana","cherry"] for x in fruits: print(x)
  • 19.
    For loop else • Thestatements in the else block will be executed after all iterations are completed. The program exits the loop only after the else block is executed.
  • 20.
    For Loop for x inrange(5): print("iteration no {} in for loop".format(x+1)) else: print("else block in loop") print("Out of loop") • iteration no 1 in for loop iteration no 2 in for loop iteration no 3 in for loop iteration no 4 in for loop iteration no 5 in for loop else block in loop Out of loop
  • 21.
    Python nested loop A nestedloop is a loop inside the body of the outer loop. The inner or outer loop can be any type, such as a while loop or for loop. For example, the outer for loop can contain a while loop and vice versa. Syntax: for element in sequence : for element in sequence: body of inner for loop body of outer for loop
  • 22.
    Example: for i inrange(1, 11): # nested loop # to iterate from 1 to 10 for j in range(1, 11): # print multiplication print(i * j, end=' ') print()
  • 23.
    Loop control statements •Loop control statements are used to change the flow of execution • These can be used if you wish to skip an iteration or stop the execution. The three types of loop control statements are: • break statement • continue statement • pass statement
  • 24.
    Break statement Based onthe given condition, the break statement stops the execution and brings the control out of the loop. for num in range(1, 11): if num == 5: break else: pri
  • 25.
    Continue statement Continue statementis used to skip the current iteration when the condition is met and allows the loop to continue with the next iteration for num in range(1, 11): if num == 6: continue print(num)
  • 26.
    pas s Pass statement isused when we want to do nothing when the condition is met. It doesn’t skip or stop the execution, it just passes to the next iteration for num in range(1, 11): if num == 6: Pass print(num)