1 / 21

COMPSCI 101 Principles of Programming

COMPSCI 101 Principles of Programming. Lecture 20 – Boolean Operators Dr. Patricia J. Riddle. Learning outcomes. 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.

marvel
Download Presentation

COMPSCI 101 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101Principles of Programming Lecture 20 – Boolean Operators Dr. Patricia J. Riddle

  2. Learning outcomes • 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 COMPSCI 101 - Principles of Programming

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

  4. Boolean Operator Examples • 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") COMPSCI 101 - Principles of Programming

  5. Boolean Operator Truth Tables COMPSCI 101 - Principles of Programming

  6. Exercise • 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 100. COMPSCI 101 - Principles of Programming

  7. Original Leap Year Code • From lecture 12, slide 19 defis_leap_year(year): if year % 400 == 0: return True elifyear % 100 == 0: return False elifyear % 4 == 0: return True else: return False COMPSCI 101 - Principles of Programming

  8. Answer COMPSCI 101 - Principles of Programming

  9. Short Circuit Evaluation • 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: COMPSCI 101 - Principles of Programming

  10. Exercise • 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'] COMPSCI 101 - Principles of Programming

  11. Answer COMPSCI 101 - Principles of Programming

  12. Exercise • 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 Schwartzkopfvex'd by NJ IQ” “Blowzy night-frumps vex'd Jack Q” COMPSCI 101 - Principles of Programming

  13. Answer COMPSCI 101 - Principles of Programming

  14. Exercise • 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 Schwartzkopfvex'd by NJ IQ” “Blowzy night-frumps vex'd Jack Q” COMPSCI 101 - Principles of Programming

  15. Answer COMPSCI 101 - Principles of Programming

  16. The Game of Life http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life 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 8neighbours, 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 COMPSCI 101 - Principles of Programming

  17. Exercise • 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 COMPSCI 101 - Principles of Programming

  18. Answer • Nested if statements can be hard to read COMPSCI 101 - Principles of Programming

  19. Answer COMPSCI 101 - Principles of Programming

  20. Summary • 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 COMPSCI 101 - Principles of Programming

  21. Tomorrow • Complex String Processing COMPSCI 101 - Principles of Programming

More Related