<jupyter_start><jupyter_text>Chapter 4. Choose with if  Comment with   A comment is a piece of text in your program that is ignored by the Python interpreter.  Continue Lines with \Programs are more readable when lines are reasonably short. The recommended (not required) maximum line length is 80 characters. If you can’t say everything you want to say in that length, you can use the continuation character: `\` (backslash). Just put `\` at the end of a line, and Python will suddenly act as though you’re still on the same line. For example, if I wanted to add the first five digits, I could do it a line at a time:```pysum = 0sum += 1sum += 2sum += 3sum += 4sum  10```Or, I could do it in one step, using the continuation character:```pysum = 1 + \         2 + \          3 + \         + 4sum  10```Here’s a little trick—if you’re in the middle of paired parentheses (or square or curly brackets), Python doesn’t squawk about line endings:```pysum = (        1 +        2 +        3 +        4)sum  10```  Compare with if, elif, and else<jupyter_code>disaster = True
if disaster:
    print("Woe!")
else:
    print("Whee!")<jupyter_output>Woe!<jupyter_text>The and lines are Python statements that check whether a condition (here, the value of `disaster` ) is a boolean value, or can be evaluated as True. Remember, `print()` is Python’s built-in function to print things, normally to your screen 🧸**NOTE**If you’ve programmed in other languages, note that you don’t need parentheses for the test. For example, don’t say something such as `if` (diaster == True) (the equality operator `==` is described in a few paragraphs). You do need the colon (`:`) at the end. If, like me, you forget to type the colon at times, Python will display an error message.<jupyter_code>color = "mauve"
if color == "red":
    print("It's a tomato.")
elif color == "green":
    print("It's green pepper.")
elif color == "bee purple":
    print("I don't know what it is, but only bees can see it.")
else:
    print("I have never heard of the color", color)<jupyter_output>I have never heard of the color mauve<jupyter_text>Python’s comparison operators:                         equality            ==                             inequality            !=                             less than                                           less than or equal                                         greater than            >                              greater than or equal            >=               These return the boolean values `True` or `False`.<jupyter_code>x = 7
x == 5<jupyter_output><empty_output><jupyter_text>Note that two equals signs (`==`) are used to test equality; remember, a single equals sign (`=`) is what you use to assign a value to a variable.If you need to make multiple comparisons at the same time, you use the logical (or boolean) operators `and`, `or`, and `not` to determine the final boolean result. Logical operators have lower precedence than the chunks of code that they’re comparing. This means that the chunks are calculated first, and then compared.<jupyter_code>5 < x < 10<jupyter_output><empty_output><jupyter_text>What Is True? What if the element we’re checking isn’t a boolean? What does Python consider `True` and `False`?A `False` value doesn’t necessarily need to explicitly be a boolean `False`. For example, these are all considered `False`:                         boolean            False                             null            None                             zero integer             0                              zero float             0.0                              empty string            ' '                              empty list            [ ]                             empty tuple            ( )                             empty dict            { }                             empty set             set( )                Anything else is considered `True`. Python programs use these definitions of “truthiness” and “falsiness” to check for empty data structures as well as `False` conditions:  Do Multiple Comparisons with in Suppose that you have a letter and want to know whether it’s a vowel. One way would be to write a long statement:<jupyter_code>letter = 'o'
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u' :
    print(letter, 'is in vowel')
else:
    print(letter, 'is not a vowel')<jupyter_output>o is in vowel<jupyter_text>Whenever you need to make a lot of comparisons like that, separated by `or`, use Python’s membership operator `in`, instead. Here’s how to check vowelness more Pythonically, using `in` with a string made of vowel characters:<jupyter_code>vowels = 'aeiou'
letter = 'o'
if letter in vowels:
    print(letter, 'is in vowel')<jupyter_output>o is in vowel<jupyter_text>Here’s a preview of how to use with some data types that you’ll read about in detail in the next few chapters:```pyletter = 'o'vowel_set = {'a', 'e', 'i', 'o', 'u'}letter in vowel_set  Truevowel_list = ['a', 'e', 'i', 'o', 'u']letter in vowel_list  Truevowel_tuple = ('a', 'e', 'i', 'o', 'u')letter in vowel_tuple  Truevowel_dict = ('a': 'apple', 'e' : 'elephant', 'i': 'impala', 'o': 'ocelot', 'u'; 'unicorn')letter in vowel_dict  Truevowels_string = 'aeiou'letter in vowels_string  True```  New: I Am the Walrus Arriving in Python 3.8 is the walrus operator, which looks like this:$$name := expression$$<jupyter_code>tweet_limit  = 280
tweet_string = "Blash" * 50
if diff := tweet_limit - len(tweet_string) >= 0:
        print("A fitting tweet")
else:
    print("Went over by", abs(diff))<jupyter_output>A fitting tweet
{'fraction_non_alphanumeric': 0.09261241970021414, 'fraction_numerical': 0.006780870806566738, 'mean_word_length': 2.6419753086419755, 'pattern_counts': {'":': 3, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '522905', 'license': 'MIT', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1511, 'n_words': 701, 'path': 'Introducing Python Modern Computing in Simple Packages/Chapter 4 - Choose with if.ipynb', 'repo_name': 'Halston1031/Python', 'size': '5604'}