Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

Similar documents
Text Input and Conditionals

Introduction to Bioinformatics

Introduction to Python (All the Basic Stuff)

Loops and Iteration. Chapter 5. Python for Informatics: Exploring Information

Flow Control: Branches and loops

ENGR 101 Engineering Design Workshop

Conditionals: Making Choices

Python: Short Overview and Recap

Visualize ComplexCities

Conditionals and Recursion. Python Part 4

Python - Loops and Iteration

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CMSC 201 Computer Science I for Majors

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Lab 5 - Repetition. September 26, 2018

Conditionals. C-START Python PD Workshop

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Control Structures 1 / 17

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS 1301 Exam 1 Fall 2009

Object-Oriented Programming

Intro. Scheme Basics. scm> 5 5. scm>

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Decision Structures CSC1310. Python Programming, 2/e 1

CS 112: Intro to Comp Prog

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Programming Fundamentals

CMSC201 Computer Science I for Majors

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Decisions, Decisions. Testing, testing C H A P T E R 7

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

Chapter 2 Writing Simple Programs

Introduction to Problem Solving and Programming in Python.

Python a modern scripting PL. Python

CSCE 110 Programming I

CS 1301 Exam 1 Answers Fall 2009

CS 115 Lecture 8. Selection: the if statement. Neil Moore

cs1114 REVIEW of details test closed laptop period

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Beyond Blocks: Python Session #1

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

Introduction to Computer Programming for Non-Majors

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Built-in functions. You ve used several functions already. >>> len("atggtca") 7 >>> abs(-6) 6 >>> float("3.1415") >>>

CS 1301 Exam 1 Fall 2010

Hello, World and Variables

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Control flow statements

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Python Tutorial. Day 2

DCS/100: Procedural Programming

Getting Started with Python

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

Lecture 04 More Iteration, Nested Loops. Meet UTA Jarrett s dog Greta, lying in her nest

Programming in Python

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Python! Lecture 2

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

GOLD Language Reference Manual

PROGRAMMING FUNDAMENTALS

Python Programming: An Introduction to Computer Science

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CPSC 217 L01 Midterm

JME Language Reference Manual

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

N-grams in Python. L445/L515 Autumn 2010

YOLOP Language Reference Manual

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

The Big Python Guide

Sequence Types FEB

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Decision Structures Zelle - Chapter 7

How Do Robots Find Their Way?

Python Evaluation Rules

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Fundamentals of Programming (Python) Getting Started with Programming

CS Summer 2013

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Flow Control. So Far: Writing simple statements that get executed one after another.

CS1 Lecture 3 Jan. 18, 2019

COMP 364: Conditional Statements Control Flow

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Chapter 4: Control Structures I (Selection)

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python Boot Camp. Day 3

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Module 08: Searching and Sorting Algorithms

CS1 Lecture 3 Jan. 22, 2018

Transcription:

Control of Flow There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests. If Statements While Loops Assert Statements 6

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

If Statements Another Ex. x = killer rabbit if x == roger : print how s jessica? elif x == bugs : print What s up, doc? else: print Run away! Run away! Run away! Run away! 8

While Loops x = 3 while x < 10: x = x + 1 print Still in the loop. print Outside of the loop. 9

While Loops, more examples While 1: print Type Ctrl-C to stop me! x = spam While x: print x x = x[1:] spam pam am m 10

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. 11

While Loop with Break while 1: name = raw_input( Enter name: ) if name == stop : break age = raw_input( Enter age: ) print Hello, name, =>,\ int(age)**2 12

While Loop with Break, cont. Enter name: mel Enter age: 20 Hello mel => 400 Enter name: bob Enter age: 30 Hello, bob => 900 Enter name: stop 13

While Loop with Continue x = 10 while x: x = x-1 if x % 2!= 0: continue print x #Odd? Skip print 14

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. assert(number_of_players < 5) if number_of_players >= 5: STOP 15

Logical Expressions 16

True and False True and False are constants in Python. Generally, True equals 1 and False equals 0. Other values equivalent to True and False: False: zero, None, empty container or object True: non-zero numbers, non-empty objects. Comparison operators: ==,!=, <, <=, etc. X and Y have same value: X == Y X and Y are two names that point to the X is Y 17 same memory reference:

Boolean Logic Expressions You can also combine Boolean expressions. True if a is true and b is true: True if a is true or b is true: True if a is false: a and b a or b not a Use parentheses as needed to disambiguate complex Boolean expressions. 18

Special Properties of And and Or Actually and and or don t return True or False. They return the value of one of their sub-expressions (which may be a non-boolean value). X and Y and Z If all are true, returns value of Z. Otherwise, returns value of first false subexpression. X or Y or Z If all are false, returns value of Z. Otherwise, returns value of first true subexpression. 19

The and-or Trick There is a common trick used by Python programmers to implement a simple conditional using and and or. result = test and expr1 or expr2 When test is True, result is assigned expr1. When test is False, result is assigned expr2. Works like (test? expr1 : expr2) expression of C++. But you must be certain that the value of expr1 is never False or else the trick won t work. I wouldn t use this trick yourself, but you 20 should be able to understand it if you see it

Regular expressions in Python >>> import re >>> p = re.compile( + ) >>> line = this is a test a test a test >>> line.split( ) >>>? >>> p.split(line) >>>? >>> line.replace(, = ) >>>? >>> p.sub( =, line) >>>? >>> p.subn( =, line) 21

More regular expression functions p.match(string) Matches p from the beginning of string p.search(string) Matches p anywhere in string, stops at first match p.findall(string) Return all matched strings in a list 22

An example >>> line = These are test1 test2 test3 >>> p2 = re.compile(r test\d ) >>> if p2.search(line): print p2.search(line).group() >>>? >>> p2.findall(line) >>>? 23

Escape! Escape! >>> string1 = \section >>> p3 = re.compile( \\section ) >>> p3.search(string1).group() >>>? >>> p3 = re.compile( \\\\section ) >>> p3.search(string1).group() >>>? >>> p3.search(string1).group() >>> p3 = re.compile(r \\section ) >>>? 24