Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:

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

Python Programming Language
CS0007: Introduction to Computer Programming
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
CIS Computer Programming Logic
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
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.
CPS120: Introduction to Computer Science Operations Lecture 9.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Control Flow (Python) Dr. José M. Reyes Álamo.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Introduction to Python
Lecture 3- Decision Structures
Factoring if/else code
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Relational Operators Operator Meaning < Less than > Greater than
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Introduction to Programming Using Python PART 2
CS 1111 Introduction to Programming Spring 2019
Computer Science Core Concepts
Types, Truth, and Expressions (Part 2)
Python Programming Language
Chapter 2 Programming Basics.
SSEA Computer Science: Track A
Using Decision Structures
CHAPTER 5: Control Flow Tools (if statement)
EECE.2160 ECE Application Programming
Python While Loops.
Types, Truth, and Expressions
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits: a significant part of this material has been created by Dr. Darin Brezeale and Dr. Gian Luca Mariottini 1

The Boolean Type Expressions of type boolean can only have two values: True, or False. – True and False are reserved keywords in Python. They are also literals. 2 >>> 3 > 2 True >>> type(True) >>> a = 3 == 2 >>> a False >>> a = (3 == 2) >>> a False Preferred style (parenthesize) >>> b = 4; >>> a = (b != 5) >>> a True

Relational operators The following operators generate boolean results: == equality != not equal < less than > greater than <= less than or equal to >= greater than or equal to 3

Boolean Operators The following Boolean operators can be used to produce Boolean expressions and results: not and or Make the truth tables for these operators 4

Using BooleanOperators 5 >>> a = 3 >>> b = 4 >>> (a == b) or (a+b == 7) True >>> (a == b) and (a+b == 7) False >>> not(a == b) True

Combining operators What does this line do? >>> (3 == 5) and (2 = 0) 6

Combining operators What does this line do? >>> (3 == 5) and (2 = 0) I don't know, and I don't need to know. – Use parentheses to make the meaning of these statements clear. >>> ((3 == 5) and (2 = 0)  True >>> (3 == 5) and ((2 = 0))  False 7

The if statement if boolean_expression: statement #only one statement Example: x = 2 if (x % 2) == 0: print(x, 'is even') -Indentation groups the statements together. Typically 4 white spaces are used for indenting. -Copy/paste problem: try to copy/paste this code and run it. What happens? … This is another reason to type it yourself. 8

The if statement if condition: statement …. statement Draw the flow of control diagram. 9 Block of instructions or suite

The if-else statement if condition: statement …. statement else: statement …. statement Draw the flow of control diagram. 10

The if-elif-else statement if condition1: # suite elif condition2: # suite elif condition3: # suite … else: # suite If condition1 evaluates to True the other ones will not be evaluated. Compare this with multiple if statements (at the same level) Draw the flow of control diagram. 11

What does it mean to be equal? Floats are approximations of real numbers. >>> u = >>> v = >>> w = >>> (u + v) + w >>> u+ (v + w)

What does it mean to be equal? Solution: >>> u = >>> v = >>> w = >>> x = (u + v) + w >>> y = u+ (v + w) >>> abs(x-y)< # abs computes the absolute value 13

What does it mean to be equal? Two different variables point to objects that have the same value. Two different variables point to the same object (same ID). – Use the is function to see if two variables point to the same object. Use the id function to get the object ID. 14

What does it mean to be equal? Book example (I used int instead of float): >>> a_int = 1 >>> b_int = 1 >>> c_int = b_int >>> a_int == b_int >>> b_int == c_int >>> a_int is b_int >>> b_int is c_int >>> id(a_int) >>> id(b_int) >>> id(a_int) 15

More on object creation Book says: “When you assign the same object to different variables using separate assignments, you create separate objects that happen to have the same value” – Example above I say: The return value of an expression becomes a new object: >>> id(‘a’+’b’) >>> id(‘c’+’d’) >>> id(‘a’+’b’) * notice that the above expressions were not mapped to variables. We said that in such cases we ‘lose’ the computed value. However, the newly computed value still has an id. How can these all happen at the same time? You should ask yourself these questions. 16

Chained relational operators >>> 0 <= x <= 5 Is : (0<=x) and (x <= 5) -Be careful in using them!: >>> 4 < 5 == True # You may think it should be True Is: (4<5) and (5 ==True) # It evaluates to False 17

Practice Construct boolean expressions (compute the tables) Write examples that use conditional statements. 18