Lilian Blot PART II: BOOLEAN EXPRESSION Core Elements Autumn 2013 TPOP 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
We Can Read About Mixing Colors
Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1.
Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Types and Arithmetic Operators
© red ©
If Statements & Relational Operators Programming.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
JavaScript, Fourth Edition
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Variables and Expressions CMSC 201 Chang (rev )
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.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Primitive Variables.
Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Operators.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
What are Operators? Some useful operators to get you started.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
 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.
CompSci 230 S Programming Techniques
Introduction to Programming
Introduction to Python
Topic: Conditional Statements – Part 2
Overview: Programming Concepts
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Operators and Expressions
Decision Structures, String Comparison, Nested Structures
Introduction to Programming
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Like.
Averages and range: Mode from a frequency table
Relational Operators Operator Meaning < Less than > Greater than
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
What Color is it?.
Boolean Expressions to Make Comparisons
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
JavaScript conditional
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Introduction to Programming
Data Types and Maths Programming Guides.
Introduction to Python
Presentation transcript:

Lilian Blot PART II: BOOLEAN EXPRESSION Core Elements Autumn 2013 TPOP 1

Lilian Blot Overview Boolean Type (bool) Boolean Expression Autumn 2013 TPOP 2

Lilian Blot BOOLEAN & CONDITIONS More on Data Type and Operators Autumn 2013 TPOP 3

Lilian Blot Comparing Numbers We can compare numbers using comparators such as: >, =, <=, ==, != Autumn 2013 TPOP 4 >>> 10 < 7 False >>> 10 > 7 True Python shell The boolean expression returns True or False (note the capital letter). The returned value is a Boolean, a data type that can take only two values, True or False.

Lilian Blot Comparing Variables Values in variables can be compared, and the result can be stored in a variable. Autumn 2013 TPOP 5 >>> my_condition = 10 < 7 >>> type(my_condition) >>> small = 6 >>> big = 2000 >>> is_greater = (big >= small) >>> print is_greater True Python shell

Lilian Blot Pitfalls Make sure to compare comparable objects Autumn 2013 TPOP 6 >>> 7 <= 6 True >>> 7 >= 6 False >>> 7 >= 6 True Python shell Note: strings are compared alphabetically Exact equality between floating-point numbers is a dangerous concept. see video: Confusing Assignment = and equality operator == a common mistake

Lilian Blot Boolean Expression composition Boolean expressions can be combined to form complex expressions composition can be done using three operators and, or, not Autumn 2013 TPOP 7 >>> discount = ((age < 26) or (is_student == True)) >>> resit = not((average >= 40) and (compensable < 40)) Python shell

Lilian Blot Truth Tables andTrueFalse True False Autumn 2013 TPOP 8 orTrueFalse True FalseTrueFalse not TrueFalse True

Lilian Blot Operator Precedence OperatorName +, - Unary plus and minus (e.g. -10) ** Exponentiation not *, /, //, % Multiplication, division, integer division, and remainder +, - Binary plus and minus (e.g. 3-10) = Comparison ==, != Equality and or =, +=, -=, *=, /=, //=, %= Assignment operators Autumn 2013 TPOP 9

Lilian Blot Additional Material Videos: Boolean: Autumn 2013 TPOP 10

Lilian Blot Exercises Give a truth table that shows the (Boolean) value of each of the following Boolean expressions, for every possible combination of input values. a) not (P and Q) b) (not P) and Q c) (not P) or (not Q) d) (P and Q) or R e) (P or R) and (Q or R) Hint: including columns for intermediate expression is helpful. Autumn 2013 TPOP 11

Lilian Blot Exercises Here is some practice on writing complex Boolean expressions. Your task is to convert the English description into a Python Boolean expression. The variables to be used in the expressions are: dogSize with string values "small", "medium", and "large" dogHasSpots with Boolean values True or False dogAge with positive integer values dogColor with string values "white", "black", "red", and "brown" catSize with string values "small", "medium", and "large" catHasSpots with Boolean values True or False catColor with string values "white", "black", "orange", and "tabby" catEyeColor with string values "green", "brown", "blue" catAge with positive integer values An old dog is one with an age greater than seven years while an old cat has an age greater than or equal to nine years. A cat and a dog are young if they are younger than three years old. Autumn 2013 TPOP 12

Lilian Blot Exercises Write a Boolean expression that captures "Dogs that are large and have spots or cats that are white but do not have blue eyes." Young cats and dogs of small size and with brown eyes Old cats and dogs, where cats are white and dogs are not black. Autumn 2013 TPOP 13