1 / 20

Logical Operators

Logical Operators. Operators that can be used to create complex Boolean expressions and or not. The and Operator. Takes two Boolean expressions as operands Creates compound Boolean expression that is true only when both sub expressions are true

Download Presentation

Logical Operators

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. Logical Operators • Operators that can be used to create complex Boolean expressions • and • or • not

  2. The and Operator • Takes two Boolean expressions as operands • Creates compound Boolean expression that is true only when both sub expressions are true • Can be used to simplify nested decision structures • Truth table for the and operator

  3. The or Operator • Takes two Boolean expressions as operands • Creates compound Boolean expression that is true when either of the sub expressions is true • Can be used to simplify nested decision structures • Truth table for the or operator

  4. The notOperator • Takes one Boolean expressions as operand and reverses its logical value • Sometimes it may be necessary to place parentheses around an expression to clarify what you are applying the not operator to • Truth table for the not operator

  5. Modified Loan Qualifier Program (and) # Notice the use of andinstead of multiple ifs. # This program determines whether a bank customer qualifies for a loan. MIN_SALARY = 30000.0 # The minimum annual salary MIN_YEARS = 2 # The minimum years on the job salary = float(input('Enter your annual salary: ')) years_on_job = int(input('Enter the number of ' + 'years employed: ')) # Determine whether the customer qualifies. if salary >= MIN_SALARY and years_on_job >= MIN_YEARS: print('You qualify for the loan.') else: print('You do not qualify for the loan’)

  6. Modified Loan Qualifier Program (or) • Qualifications for the loan has changed • Someone qualifies for a loan if they earn at least $30,000 or they have worked at their job for at least 2 years

  7. Modified Loan Qualifier Program (or) # Notice the use of or # This program determines whether a bank customer qualifies for a loan. MIN_SALARY = 30000.0 # The minimum annual salary MIN_YEARS = 2 # The minimum years on the job salary = float(input('Enter your annual salary: ')) years_on_job = int(input('Enter the number of ' + 'years employed: ')) # Determine whether the customer qualifies. if salary >= MIN_SALARY or years_on_job >= MIN_YEARS: print('You qualify for the loan.') else: print('You do not qualify for the loan’)

  8. Checking Numeric Ranges with Logical Operators • To determine whether a numeric value is within a specific range of values, use and • Example: x >= 10 and x <= 20 • To determine whether a numeric value is outside of a specific range of values, use or • Example: x < 10 or x > 20

  9. Boolean Variables • Can be assigned to only one of two values, True or False • Represented by bool data type • Commonly used as flags • Signals when some condition exists in a program • Flag set to False condition does not exist • Flag set to True  condition exists

  10. Boolean Variable Examples hungry = True door_open = False if sales >= 50000.0: sales_quota_met =True else: sales quota_met = False if sales_quota_met: print(‘you have met your sales quota!’) salary = base_salary + BONUS • Could have also used ‘if sales_quota = True:’

  11. Determining the State of the Turtle if turtle.ycor() < 0: turtle.goto(0, 0) if turtle.xcor() > 100 and turtle.xcor() < 200: turtle.goto(0, 0) The turtle.xcor() and turtle.ycor() functions return the turtle's X and Y coordinates Examples of calling these functions in an if statement:

  12. Determining Turtle’s Heading if turtle.heading() >= 90 and turtle.heading() <= 270: turtle.setheading(180) The turtle.heading() function returns the turtle's heading. (By default, the heading is returned in degrees.) Example of calling the function in an if statement:

  13. Determining If Pen is Down if turtle.isdown(): turtle.penup() if not(turtle.isdown()): turtle.pendown() The turtle.isdown() function returns True if the pen is down, or False otherwise. Example of calling the function in an if statement: Notice in example that there is no == since value returned is Boolean

  14. Determining If Turtle Is Visible if turtle.isvisible(): turtle.hideturtle() The turtle.isvisible() function returns True if the turtle is visible, or False otherwise. Example of calling the function in an if statement:

  15. Determining Pen and Fill Color if turtle.pencolor() == 'red': turtle.pencolor('blue') if turtle.fillcolor() == 'blue': turtle.fillcolor('white') When you call turtle.pencolor() without passing an argument, the function returns the pen's current color as a string. Example of calling the function in an if statement: When you call turtle.fillcolor() without passing an argument, the function returns the current fill color as a string. Example of calling the function in an if statement:

  16. Determining the State of the Turtle if turtle.bgcolor() == 'white': turtle.bgcolor('gray') When you call turtle.bgcolor() without passing an argument, the function returns the current background color as a string. Example of calling the function in an if statement:

  17. Determining Pen Size if turtle.pensize() < 3: turtle.pensize(3) When you call turtle.pensize() without passing an argument, the function returns the pen's current size as a string. Example of calling the function in an if statement:

  18. Determining Speed of Turtle if turtle.speed() > 0: turtle.speed(0) When you call turtle.speed() without passing an argument, the function returns the current animation speed. Example of calling the function in an if statement:

  19. Hit The Target Example Type in an angle to point the turtle to hit the square in the upper right Type in a force from 1 to 10 to reach the square Target is hit when inside the square

More Related