Relational and boolean operators
Download
1 / 12

Relational and Boolean Operators - PowerPoint PPT Presentation


  • 125 Views
  • Uploaded on

Relational and Boolean Operators. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Relational Operators. Conditional expressions of form operand1 relationalOperator operand2 Comparing operand1 and operand2 in some way Python relational operators: > greater than

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Relational and Boolean Operators' - andres


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Relational and boolean operators

Relational and Boolean Operators

CSIS 1595: Fundamentals of Programming and Problem Solving 1


Relational operators
Relational Operators

  • Conditional expressions of form operand1 relationalOperator operand2

    • Comparingoperand1 and operand2 in some way

  • Python relational operators:

    > greater than

    < less than

    <= less than or equal to

    >= greater than or equal to

    != is not equal to

    == is equal to

    (not same as =)


Relational operators example
Relational Operators Example


Relational and arithmetic operators
Relational and Arithmetic Operators

  • Can combine in same conditionx = 3y = 7if x * 2 < y + 1: …

  • Precedence: Arithmetic operators done first

    • Evaluate expressions on either side of relational operator to get 2 values

    • Evaluate relational operator in term of those 2 values to get either True or False


Relational operators and types
Relational Operators and Types

  • Some operators only make sense for certain types

    • Numbers: > < >= <= == !=

      • Be careful with floats and ==

    • Strings: == !=

      • password = input(“Enter password:”)if password == “xyzzy”: …

        • Can do > < >= <= but get strange results

  • Can only compare similar types

    “Fred” > 2  error


Equality and float type
Equality and Float Type

  • == checks whether operands are same number

    • Can be problem with floats due to lack of precision

      x = 5 * 2/3y = 2/3 * 5if x == y: …  False


Equality and float type1
Equality and Float Type

  • Goal: determine whether x and y are “close enough”

    • Within some “margin of error” of each other

  • Method:

    • Compute differencebetween the values

    • Take absolute value

      • Difference may be positive or negative

      • Have built-in abs function

    • Compare with “margin of error” using < instead of ==

      x = 5 * 2/3y = 2/3 * 5if abs(x – y) < 0.000001: …


Boolean logic
Boolean Logic

  • Some problems involve multiple conditions

    • “x must be between 1 and 10”

    • “either a or b must not be 0”

  • Can create using and, or, other boolean operators

    • x must be greater than or equal to 1 and less than or equal to 10

    • a must not be 0 orb must not be 0


Boolean expressions
Boolean Expressions

  • Syntax: conditionalExprbooleanOpconditionalExpr

  • Examples:

    • if x >= 1 and x <= 10: …

    • if a != 0 or b != 0: …


Boolean operators
Boolean Operators

  • and: true if both operands are true

    • true and true  true

    • true and false false

    • falseand true false

    • falseand false  false

  • or: true if eitheroperands are true

    • true and true  true

    • true and false  true

    • false and true  true

    • false and false  false


Not operator
not Operator

  • not: Reversesboolean value of unary operand

    • Syntax: not conditionalExpr

      • not true false

      • not false  true

    • Example: if not x > 0: …

  • Can often just use “opposite” relational operator

    • if x <= 0: …

  • Commonly used to reverse value of booleanfunction

    • if not math.isfinite(x): …


Precedence and boolean operators
Precedence and Boolean Operators

  • Precedence:

    • Evaluate arithmetic expressions in conditional expressions

    • Evaluate conditional expressions to True or False

    • Evaluate boolean operators using those values

      • notevaluated first

      • andevaluated next

      • orevaluated last

    • Can always use () if not sure!