{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-10T02:18:35.628043+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1023,"instructions":"## What is Truthiness\n\n- **Truthiness:** The ability to use any type of value as a conditional, not just Booleans.\n\nUnlike some languages, Python does not require that `if` statements have Boolean expressions in their conditional.\nThis may seem surprising, but it is a convenient feature called _truthiness_.\nThe idea of truthiness is that any expression \u2014 whether it is an integer, string, Boolean, or otherwise \u2014 can be evaluated in a conditional.\n\n## Type Truthiness\n\n![A table of the Truthiness values for each type.](bakery_if_truthiness_table.png)\n\nAny expression can be evaluated as a conditional according to the rules of truthiness.\nHow it is evaluated depends on its type.\nFor integers and floats, any non-zero value is considered true.\nFor strings, any non-empty string is considered true.\nThe None type has no true values.\n\n## Testing Truthiness\n\n```python testing-truthiness\nif 5:\n    print(\"This will be printed because 5 is Truthy\")\nelse:\n    print(\"This will not be printed!\")\n```\n\nIt is important to remember that most non-Boolean values are not strictly equal (`==`) to `True` or `False`.\nTheir truthiness is whether or not they will be considered as `True` or `False` _when evaluated in the context of an `if` statement_.\n\n## The bool Function\n\n```python bool-conversion\nprint(\"The integer 5 is\", bool(5))\nprint(\"The integer 0 is\", bool(0))\nprint(\"The string 'Cat' is\", bool('Cat'))\nprint(\"The string '' (the empty string) is\", bool(''))\n```\n\nYou can also test for truthiness by converting the expression to a Boolean value using the `bool` conversion function.\nThe result will then be either `True` or `False`, indicating what the original expression's truthiness was.\nHere, the zero and empty string will evaluate to `False` when passed into the `bool` function, but the `5` and `\"Cat\"` string will evaluate to `True`.\n\n## Truthy and Falsy\n\n```python truthy-falsy-test\n# This is False because True is not equal to 5\nprint(True == 5)\n\n# This is True because 5 is Truthy\nprint(True == bool(5))\n```\n\nBecause the expressions themselves are not `True` or `False`, we sometimes say that the values are \"truthy\" and \"falsy\" instead of \"true\" and \"false.\" This distinguishes a \"truthy\" value (like `5`) from the actual `True` Boolean value.\n\n## String Example\n\n```python truthy-string\nname = input(\"What is your name?\")\n\n# `name` is a string variable\nif name:\n    # This path if `name` is NOT the empty string\n    print(\"Your name is\", name)\nelse:\n    # This path if `name` is empty string\n    print(\"No name given.\")\n```\n\nLet's explore a simple example where we take some input from a user.\nIf the user enters an empty string, we'll print a different message.\n\n## Unnecessary Comparisons\n\n```python unnecessary-comparison\nif a_string != \"\":\n    ...\n# Simplifies to\nif a_string:\n    ...\n#-----------------------\nif a_number != 0:\n    ...\n# Simplifies to\nif a_number:\n    ...\n```\n\nWhen you need to check if a string is not empty, or a number is not 0, truthiness is the way to go.\nNotice how each of these tests can become much more concise. In some cases, this change can also clarify the expression.\n\n## Empty Check\n\n```python empty-check\ndollar_amount = input(\"Enter a dollar amount\")\nif dollar_amount:\n    # Need the string to be non-empty before we can index\n    dollar_symbol = dollar_amount[0]\n#-----------------------\npeople = input(\"How many people are there?\")\nif people:\n    # Need the string to be non-empty before we can convert\n    number_of_people = int(people)\n    if number_of_people:\n        # need the integer to be non-zero before we can divide\n        print(\"The price per person is:\", 5/number_of_people)\n```\n\nTesting with truthiness is often useful when you want to check if a string is not empty (for example, before you index the first element or convert the string to an integer) or check if a number is not zero before you divide.\n\n## Accidental Truthiness\n\n```python accidental-truthiness\nalpha = 5\n\n# incorrect!\nif alpha == 3 or 4:\n    print(\"This WILL be printed!\")\n\n# correct!\nif alpha == 3 or alpha == 4:\n    print(\"This will NOT be printed!\")\n```\n\nOne of the reasons why understanding truthiness is so important is that it opens an entire new set of mistakes.\nThis example program demonstrates the mistake.\nIf you read the code out loud, you would probably expect the condition to evaluate to `False` and the `print` statement to not be executed.\nThe variable `alpha` holds the value `5`, which is not equal to `3` or `4`.\nHowever, you may recall that the `or` operator has a lower priority than the `==` operator.\nPython will first evaluate the equality test between the variable `alpha` and the value `3`, which will result in `False`.\nSince the operator is `or`, we next evaluate its right-hand operand, so Python will evaluate the value `4` on its own and use that value as the result of the conditional expression.\nSince `4` is a non-zero value, the entire expression evaluates to true thanks to the rules of truthiness.\nThe proper version of the code is also provided; you just repeat the comparison operator a second time.\nWhen evaluating expressions, you must keep track of whether truthiness was accidentally involved.\n\n## Summary\n\n- All expressions in Python can be evaluated for their Truthiness.\n  - Non-empty strings and non-zero numbers are Truthy.\n  - Empty strings and zero numbers (`0` and `0.0`) are Falsey.\n- Truthiness is used in `if` statements' conditionals and when you convert values using the `bool` function.\n- Truthiness is not the same thing as being equal to the values `True` or `False`\n- Truthiness can make conditional expressions slightly more readable by removing a comparison operation.\n- Truthiness can also be the source of mistakes, such as when you try to use `or` and `and` to chain together comparisons.","ip_ranges":"","name":"3A2) Truthiness 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\": \"Truthiness\",\n  \"slides\": \"bakery_if_truthiness.pdf\",\n  \"youtube\": {\n    \"Bart\": \"fxVqHpsKY0o\",\n    \"Amy\": \"2jUOideXVKc\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_if_truthiness-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_if_truthiness-Amy.mp4\"\n  },\n  \"summary\": \"In this lesson, you will learn about how any expression can be used in an If statement's conditional expression, in an idea called Truthiness.\",\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_if_truthiness_read","version":7},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1023,"assignment_version":7,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:39.469554+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:39.469554+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036897,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-fa6028ee-873d-46af-8458-57cd78adf909","user_id":2044668,"user_id__email":"","version":0},"success":true}
