Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Unit 2

Uploaded by

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

Python Unit 2

Uploaded by

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

Conditional statement

A program’s control flow is the order in which the program’s code executes.

The control flow of a Python program is regulated by conditional statements, loops, and function calls.

Python has three types of control structures:

• Sequential
• Selection
• Repetition

Sequential

Sequential statements are a set of statements whose execution process happens in a sequence. The problem with
sequential statements is that if the logic has broken in any one of the lines, then the complete source code execution
will break.
Example
a=10
b=20
c=a-b
Print(c)

Selection/Decision control statements

The selection statement allows a program to test several conditions and execute instructions based on which condition is
true.Some decision control statements are:

• if
• if-else
• nested if
• if-elif-else

if Statement:

• An if statement is a selection control statement which is based on the valueof a given Boolean Expression.

Syntax:

if test_expression:

statement 1

.....

statement n

statement x

➢ if structure may include 1 or n statements enclosed within if block.


➢ First, test expression is evaluated. If the test expression is true, thestatement of if block (statement 1 to n) are
executed, otherwise thesestatements will be skipped and the execution will jump to statement x.
Flowchart:

Example

X=10

If(x>0):

x=x+1

Print(x)

if-else –

The if-else statement evaluates the condition and will execute the statement block1 if if the test condition is True, but
if the condition is False, then the statement block2 of else block is executed.

Syntax:

if (test expression):

statement_block 1

else:

statement_block 2

statement x
Flowchart:

Example

n=5

If(n%2==0):

Print(“Even number”)

else:

Print(“Odd number”)

In the following code example, the else body will execute as 5 modulo 2 is not equal to 0.

Nested if Statements :

Nested if statement in python are the nesting of on if statement inside another if ststement with or without else statement

Syntax

If(test expression1):

Statement

If(test experssion 2):

Statement

Example

rno=int(input(“Enter the roll number”))

name=int(input(“Enter the name”))

m1= int(input(“Enter the mark1”))

m2= int(input(“Enter the mark2”))

m3= int(input(“Enter the mark3”))

print(“rno=”,rno)

print(“name=”,name)

print(“mark1 =”,m1)

print(“mark3=”,m3)

if(m1>=40)and(m2>=40)and(m3>=40) :
total=m1+m2+m3

avg=total/3

print(“total=”,total)

print(“avg=”,avg)

if(avg>90):

print(“Grade A”)

elif(avg>80)and(avg>=90):

print(“Grade B”)

elif(avg>60)and(avg>=80):

print(“Grade C”)

else:

print(“Grade D”)

else:

print (“Result=fail”)

print(“Grade=Nil”)

if-elif-else Statement :

➢ The if-elif-else construct works in the same way as a usual if-else statement.

➢ If-elif-else construct is also known as nested-if construct.

➢ A series of if and elif statements have a final else block, which is executed if none of the if or elif expressions is True.

Syntax:

if (test expression 1):

statement block1

elif (test expression 2):

statement block2

. . . . . . . . . . . . . . ..

elif( test expression N):

statement block N

else:
statement block X

Flowchart:

Example

a=10

b=15

c=20

If (a>b) and (a>c) :

Print(“a is the Biggest Value”)

Elif (b>a)and (b>c)

Print(“b is the Biggest Value”)

Else

Print(“c is the Biggest Value”)

Looping Statements/Iterative Structure:

Iterative statements are decision control statements that are used to repeat the execution of a list of statements.

Python supports 2 types of iterative statements-

⚫ while loop

⚫ for loop.

while Loop :The While loop provides a mechanism to repeat one or more statements while a particular condition is TRUE.
Statement while (condition):

Statement block

Statement y

⚫ If the condition is TRUE, only then the statements will be executed otherwise if the condition is False, the control will
jump to statement y, that is the immediate statement outside the while loop block.

Flowchart:

Example

n=int(input(“Enter the number”))

S=0

While(n>0):

r=n%10

s=s*10+r

n=n//10

Print(“reverse the given number “,s)

for Loop:

For loop provides a mechanism to repeat a task until a particular condition is True. It is usually known as a determinate or
definite loop because the programmer knows exactly how many times the loop will repeat.

The for...in statement is a looping statement used in Python to iterate over a sequence of objects.

Syntax:
for loop_control_var in sequence:

statement block

FLOWCHART

for Loop and range() Function :

o The range( ) function is a built-in function in Python that is used to iterate over a sequence of numbers.

Syntax

range(beg,end,[step])

⚫ The range( ) produces a sequence of numbers starting with beg (inclusive) and ending with one less than the number end.

⚫ The step argument is optional . By default, every number in the range is incremented by 1 but we can specify a different
increment using step. It can be both negative and positive, but not zero.

EXAMPLE

for i in range(1,5):

Print(i,end= “ “)

Output

1 2 3 4

Example

for I in range(1,10,2)

Print(I,end=” “)
Output

1 3 5 7 9

⚫ If range( ) function is given a single argument, it produces an object with values from 0 to argument-1. For example:
range(10) is equal to writing range(0, 10).

⚫ If range( ) is called with two arguments, it produces values from the first to the second. For example, range(0, 10) gives
0-9.

⚫ If range( ) has three arguments then the third argument specifies the interval of the sequence produced. In this case, the
third argument must be an integer.

For example, range(1, 20, 3) gives 1, 4, 7, 10, 13, 16, 19.

Nested Loops :

Nested loop means inside a loop for example while loop inside for loop and for loop inside another for loop.

Example:

Program to print the following pattern

1 2

1 2 3

1 2 3 4

1 2 3 4 5

For i in range (1,6)

Print( )

For j in range(1,i+1)

Print(j,end=” “)

Jump statement

Jump ststement are used in looping statement for and while.

➢ Break
➢ Continue
➢ pass

TheBreak Statement:
⚫ The break statement is used to terminate a loop execution and skip the next code
after the loop
syntax

Break

Example:Programtodemonstratethebreakstatement

When i is equal to 5 the break statement terminates the loop.

The Continue Statement:

It is used to terminate the current iteration of the loop and the control flow
of the program goes to the next iteration.

Syntax
Continue
Example
For I in range(1,10) :
If(I==5):
Continue
Print(I)
Print(“done”)

Output
12346789
Done

The Pass Statement:

Pass is a keyword that does nothing executed


It is a NULL ststement
This statement is not ignored by the interpreter but the ststement result in no operation

Example:

1. Program to demonstrate pass statement


Else suite in python

Python allows else keyword to be used with looping ststement.

If the else statement is used with for and while in the else statement is executed when the
loop has completed iterating.

If the condition becomes false the else statement will be executed.

For with else syntax

for var in sequence:

Statement

else:

statement

while with else syntax

while(condition):

Statement

else:

statement

You might also like