Boolean Operators

Overview of Conditional Statements and Boolean Operators

  • Conditional Statements: Used in programming to perform different actions based on different conditions.

  • Boolean Operators: Used to combine two Boolean expressions. The primary Boolean operators are:

    • AND: Returns true only if both expressions are true.

    • OR: Returns true if at least one of the expressions is true.

Boolean Operators in Python

  • Operators Defined:

    • Plus for addition +

    • Minus for subtraction -

    • Multiplication *

    • Division /

    • Boolean operators used in Python: and, or, not.

    • Other languages may use symbols like && for AND or || for OR, but Python uses keywords.

Truth Tables for Boolean Operations

  • Truth Tables: A tabular representation of the truth values of a logical expression.

    • AND Operator:

    • If both expressions are true, the result is true.

      • Example: x > 1 is true and x < 10 is true → Result = true.

    • If one expression is false, the result is false.

      • Example: true and false → Result = false.

    • OR Operator:

    • If at least one expression is true, the result is true.

      • Example: true or false → Result = true.

    • Only false if both expressions are false.

      • Example: false or false → Result = false.

Evaluation of Boolean Expressions

  • Evaluation Mechanics:

    • In Python, expressions are evaluated from left to right.

    • Use parentheses () to change the order of evaluation, similar to mathematics.

    • Example: In the expression x > 1 and x < 10 or y < 50, the operators are evaluated in order and considering parentheses will change the outcome.

Short-circuit Evaluation

  • Short-circuit evaluation:

    • With the OR operator, if the first expression is true, the second is not evaluated.

    • Example: In x < 10 or y < 50, if x < 10 is true, Python skips evaluating y < 50.

    • This is efficient and can prevent errors if the second expression could cause an issue when evaluated.

Variable and Memory Management

  • Variable Storage in Python:

    • Simple variables typically store values directly in memory.

    • Complex objects (like lists or dictionaries) may store an address (reference) to the object's memory location instead of the object itself, often referred to as the heap in languages like Java.

  • Effect of Reference:

    • When two variables reference the same object, modifying one affects the other.

    • Example: If y references a list and z is set to y, both refer to the same list in memory.

Handling Null Values

  • Null and Undefined:

    • In Python, if a variable does not point to any value, it can be checked using None.

    • Attempting to access a non-existent or null variable can cause programs to crash, hence checking for None is common before accessing a variable.

Identity Comparison in Python

  • Identity Operators:

    • is: Checks if two variables refer to the same object in memory.

    • is not: Checks if two variables do not refer to the same object.

    • Example: Two lists can have the same content but are distinct objects if they reside at different memory addresses.

Conditionals and Lists

  • Using IF Statements with Lists:

    • To check for membership in lists, Python uses the in operator.

    • Example: if user_num in a checks if user_num exists in list a.

    • Reverse check with not in:

    • Example: if user_num not in a.

Reading Input from Users

  • User Input:

    • Obtaining user numbers via input:
      python user_num = int(input("Enter a number: "))

    • Check user input against conditions or lists.

Ternary Operator in Python

  • Ternary Conditional Operator:

    • A condensed format for conditional assignment in Python that allows you to execute an expression based on a condition without using multiple lines of code.

    • Syntax: value_if_true if condition else value_if_false.

    • Example:
      python full_name = "Frodo Baggins" if five in a else "Samwise Gamgee"

Practical Application of Operators

  • Programming involves using Boolean operations to control the flow of the program based on conditions. Example application includes user input validation, determining if a user’s guess is correct, or whether a variable is set and valid.

  • Final Practice Exercise:

    • Students are encouraged to implement a simple program to check if a year is a leap year using conditional logic and input from the user, exemplifying the use of conditional statements, Boolean logic, and the testing of multiple conditions.