15

Python session3

Embed Size (px)

Citation preview

Page 1: Python session3
Page 2: Python session3

AGENDA

Decision Making

Loops

Page 3: Python session3

Python Decision Making

Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Page 4: Python session3

Types of decision making statements

IF statements

IF...ELSE statements

Nested IF statements

Page 5: Python session3

IF statements Flow Diagram:

Test Exp

Body of if

TRUE FALSE

EXIT

Page 6: Python session3

IF...ELSE statements Flow Diagram:

Test Exp

Body of Else

FALSE

EXIT

If Block TRUE

The else statement is an optional statement and there could be at most only one else statement following if .

Page 7: Python session3

LOOPS

A loop statement allows us to execute a statement or group of statements multiple times

Python programming language provides following types of loops to handle looping requirements

While loop

For loop

Page 8: Python session3

While Loop

Example:

count = 0

while (count < 9):

print 'The count is:', count

count = count + 1

print "Good bye!"

Page 9: Python session3

For Loop

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

Example:

for letter in 'Python':

print 'Current Letter :', letter

String

Page 10: Python session3

Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

break statement continue statement pass statement

Page 11: Python session3

Break Statement:

Example

for letter in 'Python':

if letter == 'h':

break

print 'Current Letter :', letter

Break Statement

Page 12: Python session3

continue statement

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

for letter in 'Python':

if letter == 'h':

continue

print 'Current Letter :', letter

continue statement

Page 13: Python session3

pass statement

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

The pass statement is a null operation; nothing happens when it executes.

for letter in 'Python':

if letter == 'h':

pass

print 'This is pass block'

print 'Current Letter :', letter

Page 14: Python session3

Reference

http://www.tutorialspoint.com/python/

Page 15: Python session3