Selection in Python If statements.

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

Making Decisions and Working With Strings
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Control Structures Selections Repetitions/iterations
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
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.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Lecture no 3 Control statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Statements: Part1  if, if…else, switch 1.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
 Type Called bool  Bool has only two possible values: True and False.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Sequence, Selection, Iteration The IF Statement
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Boolean Expressions & the ‘if’ Statement
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Program Statements
Bools and simple if statements
Chapter 7 Conditional Statements
Visual Basic – Decision Statements
Expressions.
Logic of an if statement
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Selection in Python If statements

Control Structures Sequence Selection Repetition Module

Selection If or if / else statement choosing between mutually exclusive possibilities Two forms see next slide

Selection Two forms if logical expression: statement else:

Logical Expressions Relational Operators ==, !=, <=, >=, >, < They are binary operators – have two operands work on any type of data, be careful about matching types produce a bool result (True or False)

Syntax of if statement if logical expression: statement Indentation required if expression is true, then statement is executed if expression is false, statement is NOT executed

Syntax of if statement if x > 0: y = sqrt(x) if x > y: t = x

If-Then Statement TRUE expression FALSE statement Determine whether or not to execute a statement (which can be a single statement or an entire block) TRUE expression FALSE statement

Examples output larger of two numbers don't allow sqrt of negative number don't allow overflow determining even or odd give student another chance at a question

Visual Aid Decision tree “if person is 18 or over and state is KY, they can have regular license” “if person is 16 or over and under 18 and state is TN or VA, they can have learner’s permit” “if person is under 16, in any state, they can’t have license” “if person is over 65 in VA, they have modified license”

Logical Operators and or not and, or are binary operators, not is unary used to combine bool values produce a bool result truth tables operator precedence not then and then or not, and, or are very low, below other operators

Precedence of operators See http://docs.python.org/py3k/reference/expressions.html#evaluation-order Note that this table is “upside down”, lowest precedence is on top!

Converting English to logic "0 is less than x is less than 5" "x is 5 or 6" "x is bigger than 5 and less than 10“ 5 < x < 10 IS valid in Python! impossible situations "dead code" always true – “x < x + 1” “x > 5 or x < 8” always false – “x < 5 and x > 10”

Write an expression for each taxRate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22

Some Answers (taxRate > .25) and (income < 20000) (temperature <= 75) or (humidity < .70) (age > 21) and (age < 60) (age == 21) or (age == 22)

Nested if's the statement to be executed in an if statement can be another if an else branch goes with the if which matches its indentation

what’s the difference? if a > 5: if b < 10: print(“green”) else: print(“blue”) if a > 5: if b < 10: print(“green”) else: print(“blue”)

if .. else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE expression if clause else clause

Example Given x, y on Cartesian plane, which quadrant is it in? (I, II, III, IV) if x > 0: if y > 0: quadrant = 1 else: quadrant = 4 if y > 0: # NOT a redundant test! quadrant = 2 quadrant = 3

Another form of nested if's if condition: statement; elif condition: statement else: # good for mutually exclusive and exhaustive #conditions

Comparing Strings Two objects of type string (or a string object and a C string) can be compared using the relational operators A character-by-character comparison is made using the ASCII character set values If all the characters are equal, then the 2 strings are equal. Otherwise, the string with the character with smaller ASCII value is the “lesser” string space < digits < uppercase < lowercase “ “ < “0” < … < “9” < “A” < “B” < … < “Z” < “a” < … < “z”

myState > yourState true string myState; string yourState; myState = “Texas”; yourState = “Maryland”; Expression Value myState == yourState false myState > yourState true myState == “Texas” true myState < “texas” true

Comparing Real Values Do not compare floating point values for equality, compare them for near-equality. a = 0 for i in range(5000000): a = a + 0.001; if abs(a – 5000) < 0.00001: print( “They are close enough!”)