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

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Python quick start guide
4. Python - Basic Operators
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
If statements while loop for loop
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
G. Pullaiah College of Engineering and Technology
Control Flow (Python) Dr. José M. Reyes Álamo.
If/Else Statements.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 6: Loops.
The switch Statement, and Introduction to Looping
Python: Control Structures
Control Structures.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements.
Outline Altering flow of control Boolean expressions
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Structured Program
CS190/295 Programming in Python for Life Sciences: Lecture 6
3 Control Statements:.
2.6 The if/else Selection Structure
LOOP Basics.
Presentation transcript:

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

Decision making statements  Python programming language assumes any non-zero and non-null values as true and if it is either zero or null then it is assumed as false value.  Python programming language provides following types of decision making statements.

Decision making statements  if statements if statements if statements An if statement consists of a boolean expression followed by one or more statements.  if...else statements if...else statements if...else statements An if statement can be followed by an optional else statement, which executes when the boolean expression is false.  nested if statements nested if statements nested if statements You can use one if or else if statement inside another if or else if statement(s).

Syntax of if statement if expression: statement(s)   Single Statement Suites: If the suite of an if clause consists only of a single line, it may go on the same line as the header statement.  Here is an example of a one-line if clause:

Indentation   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

Syntax of If-else statement if expression: statement(s)else: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

The elif Statement  The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s)  Python does not currently support switch or case statements as in other languages.

Nested if example Output

Decision making  while loop while loop while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.  for loop for loop for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.  nested loops nested loops nested loops You can use one or more loop inside any another while, for or do..while loop.

While loop example

The else Statement Used with Loops

Python For-loop statements  The for loop in Python has the ability to iterate over the items of any sequence, such as a list,string, tuple and other built-in iterables. If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assig ned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assig ned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

For 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”)

Nested loop (print prime numbers)

Loop Control Statements  break statement break statement break statement Terminates the loop statement and transfers execution to the statement immediately following the loop.  continue statement continue statement continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.  pass statement pass statement 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.

Break statement example output

Continue statement example

Pass statement example for pnum in [1,2, -1, 3]: if pnum < 0: pass else: print pnum Output 1 2 3

Exercises   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.

Steps to install IDLE IDE for python   For linux / Fedora users #yum install python-tools   For Ubuntu users #sudo apt-get install idle