PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Conditional Statements Introduction to Computing Science and Programming I.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Structured programming
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Python Control of Flow.
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 Control Flow statements There are three control flow statements in Python - if, for and while.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
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,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
For loops in programming Assumes you have seen assignment statements and print statements.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
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!
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Learning Javascript From Mr Saem
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
G. Pullaiah College of Engineering and Technology
Control Flow (Python) Dr. José M. Reyes Álamo.
If/Else Statements.
Topic: Iterative Statements – Part 1 -> for loop
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Repetition Structures Chapter 9
Python: Control Structures
Chapter 4 MATLAB Programming
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.
ITM 352 Flow-Control: Loops
PH2150 Scientific Computing Skills
Arrays, For loop While loop Do while loop
An Introduction to Python
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Loops CIS 40 – Introduction to Programming in Python
More Looping Structures
Logical Operations In Matlab.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming Using Python PART 2
Selection Statements.
Fundamentals of visual basic
Statement-Level Control Structures
ICT Programming Lesson 3:
CHAPTER 5: Control Flow Tools (if statement)
More Looping Structures
Loops CGS3416 Spring 2019 Lecture 7.
REPETITION Why Repetition?
LOOP Basics.
Introduction to Python
Presentation transcript:

PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances when you want to break away from this e.g, Branch point: choose to go in one direction or another based on meeting some condition. Repeat: You may want to execute a block of code several times. Programming languages provide various control structures that allow for more complicated execution paths. Here we will focus on two such examples conditionals and loops: Will see the if, while and for statements and associated else, elif, range, break and continue commands

PH2150 Scientific Computing Skills if guess==number: #if followed by condition we are testing, colon indicates begin the if-block # indent shows that you are in the if-block, when the block of statements is completed you will drop back into the main program. The if statement is used to check a condition: if the condition is TRUE, we run a block of statements (the if-block), else (FALSE) we process another block of statements (else-block) or continue if no optional else clause. if guess==number: # if guess==number: #if TRUE execute statements # skips else else:#if FALSE executes the second block # returns to main program after these statements.

PH2150 Scientific Computing Skills if guess==number: #1 st condition # elif condition2: # checks for a 2 nd condition which if TRUE runs second block of statements. # elif condition3: # checks for a 3 rd condition which if TRUE runs 3 rd block of statements. # elif condition4: # checks for a 4 th condition which if TRUE runs 4 th block of statements. # else: # optional else if all above statements are FALSE # The el if statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE..

PH2150 Scientific Computing Skills if condition1: # # runs statements then checks next if. if condition2: #checks for a 2 nd condition which if TRUE runs second block of statements. # elif condition3: # checks for a 3 rd condition which if TRUE runs 3 rd block of statements. # else: # if condition 1 is TRUE, but 2 and 3 are FALSE. # else: # optional else if condition 1 is FALSE # You may want to check for another condition after a condition resolves to TRUE. Here you can use the nested if construct. In a nested if construct, you can have an if...elif...else construct inside another if...elif...else

PH2150 Scientific Computing Skills for letter in 'Python': # loops for each character in string print 'Current Letter :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # loops for each element in list print 'Current fruit :', fruit for index in range(len(fruits)): # loops for each element in list print 'Current fruit :', fruits[index] FruitsA=[x for x in fruits if x[0]==‘a’] # returns [‘apple’] Loops: for loops are traditionally used when you have a piece of code which you want to repeat number of times. As an alternative, there is the while Loop, the while loop is used when a condition is to be met, or if you want a piece of code to repeat forever.

PH2150 Scientific Computing Skills Control Structures in Python: range() The range() function creates a list of numbers determined by the input parameters. range([start,] stop[, step]) -> list of integers When step is given, it specifies the increment (or decrement). >>> range(5) [0, 1, 2, 3, 4] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 2) [0, 2, 4, 6, 8] e.g. How to get every second element in a list? for i in range(0, len(data), 2): print data[i]

PH2150 Scientific Computing Skills while expression: #while TRUE execute block of statements var = 1 while var == 1 : # This constructs an infinite loop num = raw_input("Enter a number :") print "You entered: ", num if num=='Q': # This gives a route out of loop print “Goodbye” break # break will terminate the loop Loops: the while loop is used when a condition is to be met, or if you want a piece of code to repeat forever.