CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

If Statements & Relational Operators Programming.
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,
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Recitation 1 Programming for Engineers in Python.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
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.
Week 8: Decisions Bryan Burlingame 21 October 2015.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Decision Making CMSC 201 Chang (rev ).
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CMSC201 Computer Science I for Majors Lecture 04 – Expressions Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides by Shawn Lupoli and.
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson Prof. Jeremy Dixon.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
CMSC201 Computer Science I for Majors Lecture 03 – Variables
Discussion 4 eecs 183 Hannah Westra.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
5.04 Apply Decision Making Structures
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3 Control Statements
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Selection (also known as Branching) Jumail Bin Taliba by
Making Choices with if Statements
Python: Control Structures
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
IF statements.
Intro to C Tutorial 4: Arithmetic and Logical expressions
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
CMSC201 Computer Science I for Majors Lecture 04 – Decision Structures
Conditionals & Boolean Expressions
Conditional Statements
Topics The if Statement The if-else Statement Comparing Strings
Conditionals & Boolean Expressions
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Conditions and Ifs BIS1523 – Lecture 8.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Arithmetic Expressions & Data Conversions
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CMSC201 Computer Science I for Majors Final Exam Information
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Summary of what we learned yesterday
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Life is Full of Alternatives
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Computer Programming Basics
CSC 1051 – Data Structures and Algorithms I
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
Intro to Programming (in JavaScript)
Control Structures.
Arithmetic Expressions & Data Conversions
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides by Shawn Lupoli and Max Morawski at UMBC

Last Class We Covered Expressions Python’s operators – Including mod and integer division The order of operations Different variables types – How to cast to a type Constants (and why using them is important) 2

Any Questions from Last Time?

Today’s Objectives To learn a bit about main() To learn more of Python’s operators – Comparison operators – Logical operators To practice using these new operators To become more familiar with using Boolean variables 4

Quick Note about main()

main() In Lab 1, we introduced the code def main(): as the first line of code in our file main() is an example of a function We can use functions to organize our code 6

Functions We’ll cover functions in more detail later For now, think of them as something similar to a variable – Variables hold data – Functions hold code 7

Calling main() With variables, we use the variable name to access the data they store We must do the same with functions like main(), using the function name to execute the code they store 8

Using main() for Your Code From now on, use main() in your code: def main(): class = int(input("What class is this? ") print(class, "is awesome!") main() 9 declaring our main() function calling our main() function

Review: Control Structures & Operators 10

Control Structures What are the three control structures? – Sequential – Decision Making Also known as “Selection” – Looping Also known as “Repetition” (We can also call a function) 11

Control Structures: Flowcharts 12

Types of Operators in Python Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators 13 focus of today’s lecture

Comparison Operators

Vocabulary Comparison operators Relational operators Equality operators – Are all the same thing Include things like >, >=, <, <=, ==, != 15

Vocabulary Logical operators Boolean operators – Are the same thing Include and, or, and not 16

Comparison Operators Always return a Boolean result – True or False – Indicates whether a relationship holds between their operands 17 operands a >= b comparison operator

Comparison Examples What is the following comparison asking? a >= b – Is a greater than or equal to b ? a == b – Is a equivalent to b ? 18

List of Operators 19

Comparison Examples (Continued) What do these evaluate to if a = 10 and b = 20 ? a >= b – Is a greater than or equal to b ? – Is 10 greater than or equal to 20 ? – FALSE 20

Comparison Examples (Continued) What do these evaluate to if a = 10 and b = 20 ? a == b – Is a equivalent to b ? – Is 10 equivalent to 20 ? – FALSE 21

Comparison vs Assignment A common mistake is to use the assignment operator ( = ) in place of the relational ( == ) – This is a very common mistake to make! This type of mistake does trigger an error in Python, but you may still make it on paper! 22

Equals vs Equivalence What does a = b do? – Sets a equal to b – Replaces a ’s value with the value of b What does a == b do? – Checks if a is equivalent to b 23

Comparison Operator Examples

Comparison Operators and Simple Data Types Examples: 8 < 15 evaluates to True 6 != 6 evaluates to False 2.5 > 5.8 evaluates to False 5.9 <= 7.5 evaluates to True 25

“Value” of Boolean Variables When we discuss Boolean outputs, we think True and False We can also think of it in terms of 1 and 0 True = 1 False = 0 26

“Value” of Boolean Variables Other data types can also be seen as “ True ” or “ False ” in Python Anything empty or zero is False – "" (empty string), 0, 0.0 Everything else is True – 81.3, 77, -5, "zero", 0.01 – Even "0" evaluates to True 27

Comparison Operation Examples a = 10 b = 20 c = 30 bool1 = a == b bool2 = c < b bool3 = c != a print(bool1, bool2, bool3) 28 Prints: False False True

More Comparison Operation Examples a = 10 b = 20 c = 30 bool1 = int(a == a) bool2 = a == a >= 10 bool3 = (a == a) + (b == b) + (c == c) print(bool1, bool2, bool3) 29 Prints: 1 True 3

Logical Operators

Logical Operators There are three logical operators: – and – or – not They allow us to build more complex Boolean expressions – By combining simpler Boolean expressions 31

Logical Operators – and Let’s evaluate this expression bool1 = a and b 32 Value of a Value of b Value of bool1

Logical Operators – and Let’s evaluate this expression bool1 = a and b 33 Value of a Value of b Value of bool1 True False TrueFalse

Logical Operators – and Let’s evaluate this expression bool1 = a and b For a and b to be True, both a and b must be true 34 Value of a Value of b Value of bool1 True False TrueFalse

Logical Operators – and Two ways to write and expressions 1.Explicitly use the keyword: 3 > 2 and 2 > 1 2.String them together, like in math: x > y > z – Evaluates to x > y and y > z 35

Examples of and a = 10 b = 20 c = 30 ex1 = a < b < c ex2 = a < b and b < c ex3 = a + b == c and b – 10 == a and c / 3 == a print (ex1, ex2, ex3) 36 Prints: True True True

More Examples of and a = 10 b = 20 c = 30 bool1 = a > b > c bool2 = a == b > c bool3 = a < b < c print(bool1, bool2, bool3) 37 Prints: False False True

Logical Operators – or Let’s evaluate this expression bool2 = a or b 38 Value of a Value of b Value of bool2

Logical Operators – or Let’s evaluate this expression bool2 = a or b 39 Value of a Value of b Value of bool2 True FalseTrue FalseTrue False

Logical Operators – or Let’s evaluate this expression bool2 = a or b For a or b to be True, either a or b must be true 40 Value of a Value of b Value of bool2 True FalseTrue FalseTrue False

Examples of or a = 10 b = 20 c = 30 ex1 = a > b or c < b ex2 = a + b c ex3 = a == c or b + 10 <= a or c/3 == a print (ex1, ex2, ex3) 41 Prints: False True True

Usage Example Here’s an easy way to remember how the and and or logical operators work In order to pass the class, you must have: (grade >= 70) and (cheating == False) For the grade to count for CMSC majors: ltrGrade == "A" or ltrGrade == "B" 42

Logical Operators – not Let’s evaluate this expression bool3 = not a not a calculates the Boolean value of a and returns the opposite of that 43 Value of a Value of bool3 TrueFalse True

Complex Expressions We can put multiple operators together! bool4 = a and (b or c) What does Python do first? – Computes (b or c) – Computes a and the result 44

Complex Expression Example bool4 = a and (b or c) 45 Value of a Value of b Value of c Value of bool4 True FalseTrue FalseTrue False True False TrueFalse TrueFalse

Complex Expression Example bool4 = a and (b or c) 46 Value of a Value of b Value of c Value of bool4 True FalseTrue FalseTrue False True False TrueFalse TrueFalse

Truth Table Layout Truth tables follow a pattern for their values 47 Value 1Value 2Value 3Answer True False TrueFalseTrue False True FalseTrueFalse True False

“Short Circuit” Evaluation

Short Circuit Evaluation “ and ” statements short circuit as soon as an expression evaluates to False “ or ” statements short circuit as soon as an expression evaluates to True 49

Short Circuiting – and Notice that in the expression: bool1 = a and (b or c) If a is False The rest of the expression doesn’t matter Python will realize this, and if a is false won’t bother with the rest of the expression 50

Short Circuiting – or Notice that in the expression: bool1 = a or (b or c) If a is True The rest of the expression doesn’t matter Python will realize this, and if a is true won’t bother with the rest of the expression 51

More Practice Given: a = 4 b = 5 c = 6 d = True e = False 52 False bool1 = d and (a > b) bool2 = (not d) or (b != c) bool3 = (d and (not e)) or (a > b) bool4 = (a%b==2) and ((not d) or e) False True

More More Practice Given: a = 4 b = 5 c = 6 d = True e = False 53 True bool1 = (d + d) >= 2 and (not e) bool2 = (not e) and (6*d == 12/2) bool3 = (d or (e)) and (a > b) True False

Decision Making So, why do we care about comparison operators and logical operators so much? We can use them to control how our program works and what code it runs – We’ll discuss this next time 54

Announcements Your Lab 2 is meeting this week! – Make sure you attend your correct section Homework 2 is out – Due by Monday (Feb 15th) at 8:59:59 PM Homework 2 is on Blackboard – Complete Academic Integrity Quiz to see HW2 55

Practice Problems Evaluate these expressions – do them yourself before testing them in Python! False and False 1 == 1 or 2 == 1 True and 1 == 1 False and 0 == 0 True or 1 == 1 "test" == "testing" not ("testing" == "testing" and "Zed" == "Cool Guy") 1 == 1 and (not ("testing" == 1 or 1 == 0)) 56 "test" == 1 not (True and False) not (1 == 1 and 0 >= 1) not (10 == 1 or 1000 == 1000) not (1 <= 10 or 3 == 4) 1 >= 0 and 2 == 1 From:

Practice Problems Create and fill out truth tables for the following Boolean expressions – Try it with and without using short circuiting! a or b or c not a and not b a or (b and not c) a and (b or c) and not d 57