20
Python Control Python Control Flow statements Flow statements There are three control There are three control flow statements in Python flow statements in Python - if, for and while. - if, for and while.

Python Control Flow statements There are three control flow statements in Python - if, for and while

Embed Size (px)

Citation preview

Page 1: Python Control Flow statements There are three control flow statements in Python - if, for and while

Python Control Flow Python Control Flow statementsstatements

There are three control flow There are three control flow statements in Python - if, for and statements in Python - if, for and while.while.

Page 2: Python Control Flow statements There are three control flow statements in Python - if, for and while

Decision making statementsDecision making statements

Python programming language assumes Python programming language assumes any any non-zeronon-zero and  and non-nullnon-null values  values as as truetrue and if it is either  and if it is either zerozero or  or nullnull then  then it is assumed as it is assumed as falsefalse value. value.

Python programming language provides Python programming language provides following types of decision making following types of decision making statements. statements.

Page 3: Python Control Flow statements There are three control flow statements in Python - if, for and while

Decision making statementsDecision making statements if statements

An if statement consists of a boolean expression An if statement consists of a boolean expression followed by one or more statements.followed by one or more statements.

if...else statements

An if statement can be followed by an optional else An if statement can be followed by an optional else statement, which executes when the boolean statement, which executes when the boolean expression is false.expression is false.

nested if statements

You can use one if or else if statement inside You can use one if or else if statement inside another if or else if statement(s).another if or else if statement(s).

Page 4: Python Control Flow statements There are three control flow statements in Python - if, for and while

Syntax of if statementSyntax of if statement

if expression: statement(s)if expression: statement(s) Single Statement Suites:

If the suite of an If the suite of an ifif clause consists only of a  clause consists only of a single line, it may go on the same line as the single line, it may go on the same line as the header statement.header statement.

Here is an example of a Here is an example of a one-line ifone-line if clause: clause:

Page 5: Python Control Flow statements There are three control flow statements in Python - if, for and while

IndentationIndentation White space is important in Python White space at the beginning of the line is important, is called

indentation. Leading whitespace (spaces and tabs) at the beginning of the

logical line (which understand by python) is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.

This means that statements which go together must have the same indentation.

Do not use a mixture of tabs and spaces for the indentation as it does not work across different platforms properly. It is strongly recommend that use a single tab or four spaces

Page 6: Python Control Flow statements There are three control flow statements in Python - if, for and while

Syntax of If-else statementSyntax of If-else statementif expression: if expression:

statement(s) statement(s) else: else:

statement(s)statement(s)

#!/usr/bin/python var1 = 100 if var1:

print "1 - Got a true expression value" print var1

else: print "1 - Got a false expression value" print var1

Page 7: Python Control Flow statements There are three control flow statements in Python - if, for and while

The elif Statement The elif statement allows you to check The elif statement allows you to check

multiple expressions for truth value multiple expressions for truth value and execute a block of code as soon as and execute a block of code as soon as one of the conditions evaluates to true.one of the conditions evaluates to true.

if expression1: if expression1:

statement(s) statement(s)

elif expression2: elif expression2:

statement(s) statement(s)

elif expression3: elif expression3:

statement(s) statement(s)

else: statement(s)else: statement(s)

Python Python does not currently support switch or does not currently support switch or case statements as in other languages.case statements as in other languages.

Page 8: Python Control Flow statements There are three control flow statements in Python - if, for and while

Nested if exampleNested if example

Output

Page 9: Python Control Flow statements There are three control flow statements in Python - if, for and while

Decision makingDecision making while loopwhile loop

Repeats a statement or group of statements while Repeats a statement or group of statements while a given condition is true. It tests the condition a given condition is true. It tests the condition before executing the loop body.before executing the loop body.

for loopfor loop

Execute a sequence of statements multiple times Execute a sequence of statements multiple times and abbreviates the code that manages the loop and abbreviates the code that manages the loop variable.variable.

nested loopsnested loops

You can use one or more loop inside any another You can use one or more loop inside any another while, for or do..while loop.while, for or do..while loop.

Page 10: Python Control Flow statements There are three control flow statements in Python - if, for and while

While loop exampleWhile loop example

Page 11: Python Control Flow statements There are three control flow statements in Python - if, for and while

The else Statement Used with Loops

Page 12: Python Control Flow statements There are three control flow statements in Python - if, for and while

Python For-loop statementsPython For-loop statements

The The for loop in Python has the ability to iterate over for loop in Python has the ability to iterate over the items of any sequence, such as a list ,string, the items of any sequence, such as a list ,string,

tuple and other built-in iterablestuple and other built-in iterables..

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

Page 13: Python Control Flow statements There are three control flow statements in Python - if, for and while

For loop exampleFor loop example#!/usr/bin/python

for letter in 'Python': #example1

print 'Current Letter :', letter

fruits = ['banana', 'apple', 'mango']

for fruit in fruits: #example2

print 'Current fruit :', fruit

print "Good bye!“

for I in range(1,5): #example3

print (i)

else:

print (“The for loop is over”)

Page 14: Python Control Flow statements There are three control flow statements in Python - if, for and while

Nested loop (print prime numbers)Nested loop (print prime numbers)

Page 15: Python Control Flow statements There are three control flow statements in Python - if, for and while

Loop Control StatementsLoop Control Statements break statementbreak statement

Terminates the Terminates the looploop statement and transfers  statement and transfers execution to the statement immediately following execution to the statement immediately following the loop.the loop.

continue statementcontinue statement

Causes the loop to skip the remainder of its body Causes the loop to skip the remainder of its body and immediately retest its condition prior to and immediately retest its condition prior to reiterating.reiterating.

pass statementpass statement

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

Page 16: Python Control Flow statements There are three control flow statements in Python - if, for and while

Break statement exampleBreak statement example

output

Page 17: Python Control Flow statements There are three control flow statements in Python - if, for and while

Continue statement exampleContinue statement example

Page 18: Python Control Flow statements There are three control flow statements in Python - if, for and while

Pass statement examplePass statement examplefor pnum in [1,2, -1, 3]:

if pnum < 0:

pass

else:

print pnum

Output

1

2

3

Page 19: Python Control Flow statements There are three control flow statements in Python - if, for and while

ExercisesExercises Write a python program to check for validity of

password using if statement Write a python program to check the eligibility

for voting using if-else Write a python program to print student garde

using elif Write a python program to find the factorial of

number using while loop Write a python program to add 10 numbers by

inputting each from the keyboard using for loop Write a python program to print string or number

is a palindrome or not.

Page 20: Python Control Flow statements There are three control flow statements in Python - if, for and while

Steps to install IDLE IDE for Steps to install IDLE IDE for pythonpython

For linux / Fedora users

#yum install python-tools

For Ubuntu users

#sudo apt-get install idle