{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-08-26T13:57:26.719471+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":921,"instructions":"## Purpose\n\n- **Comparison operator:** Asks a question about two numbers' relationship.\n- **Boolean operator:** Asks a question about other comparisons.\n\nOne of the major reasons that programs are useful is because they can do different things depending on their input.\nTo be able to make these decisions, we need to be able to write \"logical expressions\", sometimes also known as \"conditional expressions\".\nThink of these as a question that the computer can ask about the data.\nWe will learn about two groups of operators: comparison operators and Boolean operators.\nComparison operators ask questions about two numbers' relationship with each other.\nThen Boolean operators can ask questions that combine those comparisons and other questions.\n\n## Comparison Operators\n\n* Equaliy `==`\n* Inequality `!=`\n* Less than `<`\n* Greater than `>`\n* Less than or equal to `<=`\n* Greater than or equal to `>=`\n\nOften, we want to know about the relationship between two numbers.\nFor this, we use the comparison operators.\nEach of these operators takes two numbers and returns either `True` or `False`.\n\n## == (Equal Operator)\n\n```python example-equality\nprint(5 == 5)\nprint(10 == 7)\nprint(5.0 == 5)\n```\n\nThe most basic test is whether two numbers are equal.\nThis may sound weird, but in Python we use two equal signs (`==`) to test for equality.\nDo not use one equal sign; that means something COMPLETELY different.\n\n## != (Not equal operator)\n\n```python example-inequality\nprint(5 != 5)\nprint(10 != 7)\n\nprint(5.0 != 5)\n```\n\nThe second most basic test is whether two numbers are NOT equal.\nThe operator for this is even weirder.\nIn Python, to test if two things are NOT equal, we use an exclamation mark and an equal sign (`!=`).\n\n## <, <=, >, >= (Greater than and Less than)\n\n```python example-order\nprint(5 < 10)\nprint(5 <= 5)\nprint(10 > 5)\nprint(10 >= 10)\n```\n\nThere are four other operators: The less than operator (`<`), the greater than operator (`>`), the less than or equal to operator (`<=`), and the greater than or equal to operator (`>=`).\nThese four operators ask about the order of two numbers instead of their equality.\nThis means that they are different from the two equality operators although they have a lot in common.\n\n## Returning True or False\n\n```python true-false\n# prints True\nprint(10 == 10)\n# prints False\nprint(10 != 10)\n```\n\nI said before that the operators evaluate to either `True` or `False`.\nImagine the result of these operations being replaced by `True` or `False`.\nIf you print the value, it will literally be the text `True` or the text `False`.\n\n## Boolean Operators\n\n- `and`: test if two expressions are BOTH true\n- `or`: test if two expressions are EITHER or BOTH true\n- `not`: test if a single expression is false\n\nIn the same way we can add and subtract numeric expressions together, we can also combine Boolean expressions.\nThere are three operators for this: `and`, `or`, and `not`.\n\n## And\n\n```python example-and\n# Literal boolean values\nprint(True and True)    # True\nprint(True and False)   # False\nprint(False and True)   # False\nprint(False and False)  # False\n\n# Or with math\nprint(4 < 5 and 5 < 6)\nprint(5 < 4 and 5 < 6)\nprint(5 < 4 and 6 < 5)\n```\n\nThe `and` operator returns `True` if both the left and right operands are `True`.\nIf either operand is `False`, then the result is also `False`.\nNotice in the second chunk of code, we are combining the `and` operator with mathematical comparisons.\n\n## Or\n\n```python example-or\n# Literal boolean values\nprint(True or True)    # True\nprint(True or False)   # True\nprint(False or True)   # True\nprint(False or False)  # False\n\n# Or with math\nprint(4 < 5 or 5 < 6)\nprint(5 < 4 or 5 < 6)\nprint(5 < 4 or 6 < 5)\n```\n\nThe `or` operator returns `True` if either the left or right expressions are `True`.\nIf either operand is `True`, then the result is also `True`.\nThis is not the opposite of `and`, but it is different.\n\n## Not\n\n```python example-not\n# Literal boolean values\nprint(not True)    # False\nprint(not False)   # True\n\n# Or with math\nprint(not 4 < 5)\nprint(not 5 < 4)\n```\n\nThe `not` operator returns `True` if the expression is `False` and `False` if the expression is `True`.\nThis is sometimes called the \"negation\" or \"logical opposite.\" Unlike the `and` and `or` operators, the `not` operator only takes in a single value for its operand.\n\n## Operator Priority\n\n* Highest: **Math** (Parentheses, Exponent, Multiplication, Modulo, Division, Addition, Subtraction)\n* Middle: **Comparison** (Greater than, Less than, Greater than or equal, Less than or equal, Equals, Eoes not equal)\n* Lowest: **Boolean** (Not, And, Or)\n\nYou can _nest_ logical expressions just like you can in math.\nFollowing the logic can be tricky, but just remember that the _Boolean operators have the lowest priority_.\nThey will always be the last operation applied unless parentheses interrupt the order.\nThe comparison operators have the second lowest priority.\nThey will be applied after any math operations but before the Boolean operators.\n\n## Nesting Logic\n\n```python example-nesting\nprint((5 > 7 + 3) or (not False and 3 < 2))\nprint(not (not (not (not True))))\n```\n\nKeeping this order in mind is very important when you are evaluating long, complex chains of math, comparison, and Boolean operators.\nThe first expression shown here begins with the addition operation, then the comparisons and boolean operations.\n\n## Logic Expressions Are Not Distributive\n\n```python example-not-distributive\n# WRONG\nprint(5 < 1 or 2)\n\n# Correct!\nprint(5 < 1 or 5 < 2)\n```\n\nA common beginner mistake is to think that you can distribute logical expressions.\nYou might think that the first expression asks if the number 5 is less than 1 or less than 2.\nBut the `or` operator makes the 2 evaluated separately from the rest of the comparison.\nTo properly ask this question, you need to write the first statement with the \"less than\" operator twice.\nIf you run the following code as shown, then the printed value will be `2` instead of `False`, which you may find surprising.\nIn a future lesson, we will learn why this happens.\nFor now, just remember that you must be explicit when trying to use `or` and `and` with comparison operators.\n\n## Summary\n\n- There are two kinds of logic operators: comparison operators and Boolean operators.\n- Comparison operators test the relationship between two values: their equality (`==` and `!=`) or their order (`<`, `>`, `<=`, `>=`).\n- Boolean operators can be used to combine and modify logical expressions.\n  - The `and` operator evaluates to `True` if both operands are `True`.\n  - The `or` operator evaluates to `True` if either or both operands are `True`.\n- The `not` operator evaluates to `True` if it's one operand is `False`.\n- All logic and math operators can be combined, but once again the order of evaluation is very precise: Math operators are evaluated first, followed by comparison operators, and then Boolean operators. Parentheses can be used to override the order.\n\n","ip_ranges":"","name":"1A7) Logic Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"Logic\",\n  \"slides\": \"bakery_intro_logic.pdf\",\n  \"youtube\": {\n    \"Bart\": \"UmEY8LKMRcQ\",\n    \"Amy\": \"3gtfkhuH40w\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_intro_logic-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_intro_logic-Amy.mp4\"\n  },\n  \"summary\": \"In this lesson, you will learn about how to create logical expressions. In other words, how you use logic when programming.\",\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_intro_logic_read","version":6},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":921,"assignment_version":6,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T23:50:33.492184+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T23:50:33.492184+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2037090,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-c7b044aa-05a2-4e6f-921a-8f6f1d9335e4","user_id":2044772,"user_id__email":"","version":0},"success":true}
