Class 13 function example unstring if if else if elif else

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
CSC 110 Decision structures [Reading: chapter 7] CSC 110 H1.
1 Decision Structures Chapter 7 (Skip 7.1.3, 7.4) Adapted from the online slides provided by John Zelle (
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.
C++ for Engineers and Scientists Third Edition
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Geography 465 Assignments, Conditionals, and Loops.
Chapter 7 Decision Structures
Python Control of Flow.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
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.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Decision Structures, String Comparison, Nested Structures
Xiaojuan Cai Computational Thinking 1 Lecture 7 Decision Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Decision Making CMSC 201 Chang (rev ).
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
 Type Called bool  Bool has only two possible values: True and False.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures.
Python Boot Camp Booleans While loops If statements Else clauses
Control Flow (Python) Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Python: Control Structures
Python Programming: An Introduction to Computer Science
IF statements.
Topics The if Statement The if-else Statement Comparing Strings
Midterm Exam 答題分布 Python Programming, 2/e.
CMSC201 Computer Science I for Majors Lecture 04 – Decision Structures
Conditional Execution
Logical Operators, Boolean Data Types
Worksheet Key 9 11/14/2018 8:58 PM Quadratic Formula.
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Bools and simple if statements
Types, Truth, and Expressions (Part 2)
Class 12.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Conditional Execution
3. Decision Structures Rocky K. C. Chang 19 September 2018
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
Types, Truth, and Expressions (Part 2)
Chapter 3: Selection Structures: Making Decisions
Relational Operators.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Jurassic Park Python Programming, 2/e.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Class 13 function example unstring if if else if elif else forming simple conditions relational operators comparing strings Boolean expressions nesting conditions multiway conditions multiway conditions(if elif else) vs independent ifs

What is the output? def unstring(listOfnums): n=len(listOfnums) for i in range(n): listOfnums[i]=float(listOfnums[i].strip()) return listOfnums aList=['23', ' 45', '27\n'] bList=unstring(aList) print(bList)

if example: Temperature Warnings if fahrenheit >= 90: print("It's really hot out there, be careful!") if fahrenheit <= 30: print("Brrrrr. Be sure to dress warmly") Python Programming, 3/e

if if <condition>: <body> The body is a sequence of one or more statements indented under the if heading. Python Programming, 3/e

Forming Simple Conditions Simple comparison: <expr> <relop> <expr> <relop> is short for relational operator Python Programming, 3/e

Forming Simple Conditions Python Mathematics Meaning < Less than <= ≤ Less than or equal to == = Equal to >= ≥ Greater than or equal to > Greater than != ≠ Not equal to Python Programming, 3/e

Forming Simple Conditions Notice the use of == for equality. Since Python uses = to indicate assignment, a different symbol is required for the concept of equality. A common mistake is using = in conditions! Python Programming, 3/e

Forming Simple Conditions Conditions may compare either numbers or strings. When comparing strings, the ordering is lexigraphic, meaning that the strings are sorted based on the underlying Unicode. All upper-case Latin letters come before lower-case letters: (“Bbbb” comes before “aaaa”) Python Programming, 3/e

Boolean Expression When a Boolean expression is evaluated, it produces either a value of True (meaning the condition holds), or it produces False (it does not hold). (Some computer languages use 1 and 0 to represent True and False.) Python Programming, 3/e

Examples: Simple Conditions Boolean conditions are of type bool and the Boolean values of true and false are represented by the literals True and False. >>> 3 < 4 True >>> 3 * 4 < 3 + 4 False >>> "hello" == "hello" >>> "goodbye" < "hello" Python Programming, 3/e

Two-Way Decisions if-else statement: if <condition>: <statements> else: <statements> Python Programming, 3/e

Aside: Quadratic equation ax2 + bx + c roots=(-b±sqrt(b2-4ac)) / 2a if the sqrt exists. b2-4ac is called the discriminant. Python Programming, 3/e

Example: Two-Way Decisions discrim = b * b - 4 * a * c if discrim < 0: print("\nThe equation has no real roots!") else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print ("\nThe solutions are:", root1, root2 ) Python Programming, 3/e

Multi-Way Decisions Check the value of discrim when < 0: handle the case of no roots when = 0: handle the case of a double root when > 0: handle the case of two distinct roots We can do this with two if-else statements, one inside the other. Putting one compound statement inside of another is called nesting. Python Programming, 3/e

Multi-Way Decisions if discrim < 0: print("Equation has no real roots") else: if discrim == 0: root = -b / (2 * a) print("There is a double root at", root) # Do stuff for two roots Python Programming, 3/e

Multi-Way Decisions Imagine if we needed to make a five-way decision using nesting. The if-else statements would be nested four levels deep! There is a construct in Python that achieves this, combining an else followed immediately by an if into a single elif. Python Programming, 3/e

Multi-Way Decisions if <condition1>: <case1 statements> elif <condition2>: <case2 statements> elif <condition3>: <case3 statements> … else: <default statements> Python Programming, 3/e

Multi-Way Decisions This form sets off any number of mutually exclusive code blocks. Python evaluates each condition in turn looking for the first one that is true. If a true condition is found, the statements indented under that condition are executed, and control passes to the next statement after the entire if-elif-else. If none are true, the statements under else are performed. Python Programming, 3/e

Multi-Way Decisions The else is optional. If there is no else, it’s possible no indented block would be executed. Python Programming, 3/e

Example: Multi-Way Decisions discrim = b * b - 4 * a * c if discrim < 0: print("\nThe equation has no real roots!") elif discrim == 0: root = -b / (2 * a) print("\nThere is a double root at", root) else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print("\nThe solutions are:", root1, root2 ) Python Programming, 3/e