Lecture 20 – Boolean Operators Dr. Patricia J. Riddle.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

LEUCEMIA MIELOIDE AGUDA TIPO 0
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Advanced Piloting Cruise Plot.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions Numbers and number system Numbers and the number system, fractions, decimals, proportion & probability.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
Lecture 07 – Iterating through a list of numbers.
ABC Technology Project
TV Show Trivia Princeton Puzzle Hunt Do NOT flip over your answer sheet!
3 Logic The Study of What’s True or False or Somewhere in Between.
1 Computer Programming Boolean Logic Copyright © Texas Education Agency, 2013.
Evaluate the numerical expression 52 – 2 ∙ 4 + (7 – 2)
Software Testing and Quality Assurance
© S Haughton more than 3?
Twenty Questions Subject: Twenty Questions
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Squares and Square Root WALK. Solve each problem REVIEW:
Energy & Green Urbanism Markku Lappalainen Aalto University.
4 If-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Do you have the Maths Factor?. Maths Can you beat this term’s Maths Challenge?
CMPT 120 Control Structures in Python
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Slippery Slope
Test B, 100 Subtraction Facts
Week 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
We will resume in: 25 Minutes.
Figure Essential Cell Biology (© Garland Science 2010)
A SMALL TRUTH TO MAKE LIFE 100%
1 Unit 1 Kinematics Chapter 1 Day
How Cells Obtain Energy from Food
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
CS1022 Computer Programming & Principles
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
1 The Game of Life Supplement 2. 2 Background The Game of Life was devised by the British mathematician John Horton Conway in More sophisticated.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
AP Java Learning Objectives
Presentation transcript:

Lecture 20 – Boolean Operators Dr. Patricia J. Riddle

 At the end of this lecture, students should be able to:  Use and, or and not in conditional conditions  Understand basic truth tables  Use short circuit evaluation 2COMPSCI Principles of Programming

 Booleans represent truth values  True  False  Relational operators compare two different things  Evaluate to Boolean values  ==  <  <=  >  >=  != 3COMPSCI Principles of Programming

 Boolean Operators are and, or and not: if palindrome("word") if not palindrome("word") if palindrome("word") and heterogram("word") if palindrome("word") or heterogram("word") 4COMPSCI Principles of Programming

PQnot PP and QP or Q True FalseTrue False True FalseTrue FalseTrue False TrueFalse 5COMPSCI Principles of Programming

 Write a function named is_a_leap_year() that accepts a year as a parameter and returns True if the year is a leap year and False otherwise.  A year is a leap year if it is divisible by 400, or divisible by 4 but not by COMPSCI Principles of Programming

 From lecture 12, slide 19 def is_leap_year(year): if year % 400 == 0: return True elif year % 100 == 0: return False elif year % 4 == 0: return True else: return False 7COMPSCI Principles of Programming

def is_a_leap_year(year): if not isinstance(year,int): print("year is not an integer") return False if (year % 400) == 0 or ((year % 4) == 0 and (year % 100) != 0): return True return False 8COMPSCI Principles of Programming

 If A and B  B is not evaluated unless A is True  If A or B  B is not evaluated unless A is False  Examples If divisor != 0 and numerator/divisor > 4: If divisor = 0 or numerator/divisor > 4: 9COMPSCI Principles of Programming

 Write a function named find_names_with() that accepts a letter, a location and a list of names, and returns the list of names that have the letter in the specified location. find_names_with("r", 3, ["Sara","Fred","Al","Tar"]) ['Sara', 'Tar'] 10COMPSCI Principles of Programming

def find_names_with(letter, place, names_list): name_list = [] for name in names_list: if len(name) >= place and name[place-1] == letter: name_list += [name] return name_list 11COMPSCI Principles of Programming

 Write a function named pangram() that accepts a string parameter and returns True if the string is a pangram and False otherwise.  A pangram is a sentence which contains every letter in the alphabet. “The quick brown fox jumps over the lazy dog” Perfect panagrams “TV quiz jock, Mr. PhD, bags few lynx” “Glum Schwartzkopf vex'd by NJ IQ” “Blowzy night-frumps vex'd Jack Q” 12COMPSCI Principles of Programming

def pangram(sentence): if not isinstance(sentence,str): print("sentence is not a string") return False alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] for letter in alphabet: if letter not in sentence: return False return True 13COMPSCI Principles of Programming

 Write a function named pangram2() that accepts a string parameter and returns True if it is a Pangram and False otherwise, and works for both uppercase and lowercase letters Perfect panagrams “TV quiz jock, Mr. PhD, bags few lynx” “Glum Schwartzkopf vex'd by NJ IQ” “Blowzy night-frumps vex'd Jack Q” 14COMPSCI Principles of Programming

def pangram2(sentence): if not isinstance(sentence,str): print("sentence is not a string") return False alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] capital_alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y”, "Z"] alphabet_range = range(0,len(alphabet)) for index in alphabet_range: if alphabet[index] not in sentence and capital_alphabet[index] not in sentence: return False return True 15COMPSCI Principles of Programming

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its 8 neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:  Any live cell with fewer than two live neighbours dies, as if caused by under-population.  Any live cell with two or three live neighbours lives on to the next generation.  Any live cell with more than three live neighbours dies, as if by overcrowding.  Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.  Wikipedia 16COMPSCI Principles of Programming

 Write a function named is_alive() that accepts 2 parameters: a Boolean value (alive) and a number between 1 and 8 (live_neighbours) and returns True if the cell should turn on and False otherwise >>> is_alive(True,1) False >>> is_alive(True,3) True >>> is_alive(False,3) True >>> is_alive(False,4) False 17COMPSCI Principles of Programming

 Nested if statements can be hard to read def is_alive(alive,neighbours_alive): if alive: if neighbours_alive < 2: return False if neighbours_alive > 3: return False return True if neighbours_alive == 3: return True return False 18COMPSCI Principles of Programming

def is_alive2(alive,neighbours_alive): its_dead = False its_alive = True if alive and (neighbours_alive 3): return its_dead if not alive and neighbours_alive != 3: return its_dead return its_alive 19COMPSCI Principles of Programming

 Basic Truth tables  and  or  not  Use of Complex conditions:  Use of “and”, “not”, and “or”  Benefits of Short Circuit evaluation:  Allows you to write code that will not trigger error messages 20COMPSCI Principles of Programming

 Complex String Processing 21COMPSCI Principles of Programming