Conditional Execution Chapter 3 Python for Informatics: Exploring Information www.pythonlearn.com.

Slides:



Advertisements
Similar presentations
Conditional Execution
Advertisements

Introduction to PHP Dr. Charles Severance
Python Programming Language
CS0007: Introduction to Computer Programming
PHP Functions / Modularity
Conditional Statements Introduction to Computing Science and Programming I.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Expressions and Control Flow in PHP Dr. Charles Severance
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python Control of Flow.
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.
Computing with Numbers Zelle - Chapter 3 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science, John.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
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.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Variables, Expressions, and Statements
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
PHP Functions Dr. Charles Severance
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
PHP Functions / Modularity Dr. Charles Severance
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Control Flow (Python) Dr. José M. Reyes Álamo.
If/Else Statements.
Reading Files Chapter 7 Python for Everybody
Conditional Execution
Introduction to Python
Selection and Python Syntax
Loops and Iteration Chapter 5 Python for Everybody
Functions Chapter 4 Python for Everybody
Computer Programming If Statements Trade & Industrial Education
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in PHP
Conditional Execution
Selection CIS 40 – Introduction to Programming in Python
Arithmetic operations, decisions and looping
Python - Conditional Execution
Conditions and Ifs BIS1523 – Lecture 8.
Conditional Execution
Conditional Execution
Selection Statements.
Boolean Expressions to Make Comparisons
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
Decision Structures Zelle - Chapter 7
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Conditional Execution Chapter 3 Python for Informatics: Exploring Information

Conditional Steps x = 5 X < 10 ? print 'Smaller' X > 20 ? print 'Bigger' print 'Finis' Yes No

Comparison Operators Boolean expressions ask a question and produce a Yes or No result which we use to control program flow Boolean expressions using comparison operators evaluate to - True / False - Yes / No Comparison operators look at variables but do not change the variables Remember: “ = ” is used for assignment. PythonMeaning <Less than <=Less than or Equal ==Equal to >=Greater than or Equal >Greater than !=Not equal

One-Way Decisions X == 5 ? Yes print 'Still 5' print 'Third 5' No print 'Is 5'

Indentation Increase indent indent after an if statement or for statement (after : ) Maintain indent to indicate the scope of the block (which lines are affected by the if/for) Reduce indent back to the level of the if statement or for statement to indicate the end of the block Blank lines are ignored - they do not affect indentation Comments on a line by themselves are ignored with regard to indentation

x = 5 if x > 2 : print ('Bigger than 2') print ('Still bigger') print ('Done with 2') for i in range(5) : print (i) if i > 2 : print ('Bigger than 2') print ('Done with i', i) print ('All Done') Think about begin/end blocks

Warning : Turn Off Tabs Most text editors can turn tabs into spaces - make sure to enable this feature NotePad++: Settings -> Preferences -> Language Menu/Tab Settings TextWrangler: TextWrangler -> Preferences -> Editor Defaults Python cares a *lot* about how far a line is indented. If you mix tabs and spaces, you may get “ indentation errors ” even if everything looks fine Please do this now while you are thinking about it so we can all stay sane...

x > 1 print 'More than one' x < 100 print 'Less than 100' print 'All Done' yes no x = 42 if x > 1 : print ('More than one') if x < 100 : print ('Less than 100') print 'All done' Nested Decisions

Two-way Decisions Sometimes we want to do one thing if a logical expression is true and something else if the expression is false It is like a fork in the road - we must choose one or the other path but not both x > 2 print 'Bigger' yesno X = 4 print 'Not bigger' print 'All Done'

Two-way using else : x = 4 if x > 2 : print 'Bigger' else : print 'Smaller' print 'All done' x > 2 print 'Bigger' yesno x = 4 print 'Smaller' print 'All Done'

Multi-way if x < 2 : print ('small') elif x < 10 : print ('Medium') else : print 'LARGE' print ('All done') x < 2 print 'small' yes no print 'All Done' x < 10 print 'Medium' yes print 'LARGE' no

Multi-way Puzzles if x < 2 : print 'Below 2' elif x < 20 : print 'Below 20' elif x < 10 : print 'Below 10' else : print 'Something else' if x < 2 : print ('Below 2') elif x >= 2 : print ('Two or more') else : print ('Something else') Which will never print?

The try / except Structure You surround a dangerous section of code with try and except If the code in the try works - the except is skipped If the code in the try fails - it jumps to the except section

$ cat notry.py astr = 'Hello Bob' istr = int(astr) print ('First', istr) astr = '123' istr = int(astr) print ('Second', istr) $ python notry.py Traceback (most recent call last): File "notry.py", line 2, in istr = int(astr)ValueError: invalid literal for int() with base 10: 'Hello Bob' All Done

try / except astr = 'Bob' try: print 'Hello' istr = int(astr) print ('There') except: istr = -1 print ('Done', istr) print 'Hello' print 'There' istr = int(astr) print 'Done', istr istr = -1 Safety net

Acknowledgements / Contributions These slides are Copyright Charles R. Severance ( of the University of Michigan School of Information and open.umich.edu and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information … Insert new Contributors and Translators here...