1 / 12

# Relational and Boolean Operators - PowerPoint PPT Presentation

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

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

## PowerPoint Slideshow about 'Relational and Boolean Operators' - andres

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

CSIS 1595: Fundamentals of Programming and Problem Solving 1

• 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

• 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

• Some operators only make sense for certain types

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

• Be careful with floats and ==

• Strings: == !=

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

• Can only compare similar types

“Fred” > 2  error

• == 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

• 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

• 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

• Syntax: conditionalExprbooleanOpconditionalExpr

• Examples:

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

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

• 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: 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:

• 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!