Python Control of Flow.

Slides:



Advertisements
Similar presentations
Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Advertisements

I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
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,
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 2 Writing Simple Programs
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
Geography 465 Assignments, Conditionals, and Loops.
Introduction to Python II CIS 391: Artificial Intelligence Fall, 2008.
Introduction to Python II CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Decision Structures and Boolean Logic
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
If statements while loop for loop
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Built-in Data Structures in Python An Introduction.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
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,
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
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.
Imperative Programming Statements and invariants.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
More about Iteration Victor Norman CS104. Reading Quiz.
Chapter 2 Writing Simple Programs
Control Flow (Python) Dr. José M. Reyes Álamo.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Basic operators - strings
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
Programming Fundamentals
Expressions and Control Flow in JavaScript
Topics The if Statement The if-else Statement Comparing Strings
Arithmetic operations, decisions and looping
Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Logical Operations In Matlab.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics The if Statement The if-else Statement Comparing Strings
(more) Python.
Class 13 function example unstring if if else if elif else
Chap 7. Advanced Control Statements in Java
Loops CGS3416 Spring 2019 Lecture 7.
Class code for pythonroom.com cchsp2cs
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
LOOP Basics.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Python Control of Flow

if Statements if x == 3: print “X equals 3.” elif x == 2: print “X equals 2.” else: print “X equals something else.” print “This is outside the ‘if’.” Be careful! The keyword if is also used in the syntax of filtered list comprehensions. Note: Use of indentation for blocks Colon (:) after boolean expression

Another if form An alternative if form returns a value This can simplify your code Example: return x+1 if x < 0 else x -1 return ‘hold’ if delta==0 else sell if delta < 0 else ‘buy’ Added in Python v 2.6 (?)

while Loops >>> x = 3 >>> while x < 5: print x, "still in the loop" x = x + 1 3 still in the loop 4 still in the loop >>> x = 6 >>>

break and continue You can use the keyword break inside a loop to leave the while loop entirely. You can use the keyword continue inside a loop to stop processing the current iteration of the loop and to immediately go on to the next one.

assert An assert statement will check to make sure that something is true during the course of a program. If the condition if false, the program stops (more accurately: the program throws an exception) assert(number_of_players < 5)

For Loops

For Loops / List Comprehensions Python’s list comprehensions provide a natural idiom that usually requires a for-loop in other programming languages. As a result, Python code uses many fewer for-loops Nevertheless, it’s important to learn about for-loops. Take care! The keywords for and in are also used in the syntax of list comprehensions, but this is a totally different construction.

For Loops 1 A for-loop steps through each of the items in a collection type, or any other type of object which is “iterable” for <item> in <collection>: <statements> If <collection> is a list or a tuple, then the loop steps through each element of the sequence If <collection> is a string, then the loop steps through each character of the string for someChar in “Hello World”: print someChar

For Loops 2 for <item> in <collection>: <statements> <item> can be more than a single variable name When the <collection> elements are themselves sequences, then <item> can match the structure of the elements. This multiple assignment can make it easier to access the individual parts of each element for (x,y) in [(a,1),(b,2),(c,3),(d,4)]: print x

For loops & the range() function Since a variable often ranges over some sequence of numbers, the range() function returns a list of numbers from 0 up to but not including the number we pass to it. range(5) returns [0,1,2,3,4] So we could say: for x in range(5): print x (There are more complex forms of range() that provide richer functionality…)

For Loops and Dictionaries >>> ages = { "Sam" : 4, "Mary" : 3, "Bill" : 2 } >>> ages {'Bill': 2, 'Mary': 3, 'Sam': 4} >>> for name in ages.keys(): print name, ages[name] Bill 2 Mary 3 Sam 4 >>>