<jupyter_start><jupyter_text>If statement============As always I believe I should start each chapter with a warm up typing exercise so here is a short program to compute the absolute value of a number:<jupyter_code>n = float(input("Number? "))
if n < 0:
    print("The absolute value of", n, "is", -n)
else:
    print("The absolute value of", n, "is", n)<jupyter_output><empty_output><jupyter_text>Here is the output from the two times that I ran this program:<jupyter_code>Number? -34
The absolute value of -34.0 is 34.0

Number? 1
The absolute value of 1.0 is 1.0<jupyter_output><empty_output><jupyter_text>So what does the computer do when when it sees this piece of code? First it prompts the user for a number with the statement `n = float(input("Number? "))`. Next it reads the line `if n < 0:` . If `n` is less than zero Python runs the line `print("The absolute value of", n, "is", -n)`. Otherwise python runs the line `print("The absolute value of", n, "is", n)`.More formally Python looks at whether the *expression* `n < 0` is true or false. A if statement is followed by a *block* of statements that are run when the expression is true. Optionally after the if statement is a else statement. The else statement is run if the expression is false.There are several different tests that a expression can have. Here is a table of all of them:|  |  |  |  || --- | --- | --- | --- || operator | function |  |  || `<` | less than |  |  || `<=` | less than or equal to |  |  || `>` | greater than |  |  || `>=` | greater than or equal to |  |  || `==` | equal |  |  || `!=` | not equal |  |  |Another feature of the if command is the `elif`  statement. It stands for else if and means if the original if statement is false and then the elif part is true do that part. Here's a example:<jupyter_code>a = 0
while a < 10:
    a = a + 1
    if a > 5:
        print(a, " > ", 5)
    elif a <= 7:
        print(a, " <= ", 7)
    else:
        print("Neither test was true")<jupyter_output><empty_output><jupyter_text>and the output:<jupyter_code>1  <=  7
2  <=  7
3  <=  7
4  <=  7
5  <=  7
6  >  5
7  >  5
8  >  5
9  >  5
10  >  5<jupyter_output><empty_output><jupyter_text>Notice how the `elif a <= 7` is only tested when the if statement fail to be true. elif allows multiple tests to be done in a single if statement.Examples========High\_low.py<jupyter_code>#Plays the guessing game higher or lower
# (originally written by <NAME>, improved by Quique)

#This should actually be something that is semi random like the
# last digits of the time or something else, but that will have to
# wait till a later chapter.  (Extra Credit, modify it to be random
# after the Modules chapter)
number = 78
guess = 0

while guess != number:
    guess = int(input("Guess a number: "))

    if guess > number:
        print("Too high")

    elif guess < number:
        print("Too low")

print("Just right")<jupyter_output><empty_output><jupyter_text>Sample run:<jupyter_code>Guess a number:100
Too high
Guess a number:50
Too low
Guess a number:75
Too low
Guess a number:87
Too high
Guess a number:81
Too high
Guess a number:78
Just right<jupyter_output><empty_output><jupyter_text>even.py<jupyter_code>#Asks for a number.
#Prints if it is even or odd

number = float(input("Tell me a number: "))
if number % 2 == 0:
    print(number, "is even.")
elif number % 2 == 1:
    print(number, "is odd.")
else:
    print(number, "is very strange.")<jupyter_output><empty_output><jupyter_text>Sample runs.<jupyter_code>Tell me a number: 3
3.0 is odd.

Tell me a number: 2
2.0 is even.

Tell me a number: 3.14159
3.14159 is very strange.<jupyter_output><empty_output><jupyter_text>average1.py<jupyter_code>#keeps asking for numbers until 0 is entered.
#Prints the average value.

count = 0
sum = 0.0
number = 1 #set this to something that will not exit
#           the while loop immediatly.

print("Enter 0 to exit the loop")

while number != 0:
    number = float(input("Enter a number:"))
    count = count + 1
    sum = sum + number

count = count - 1 #take off one for the last number
print("The average was:", sum/count)<jupyter_output><empty_output><jupyter_text>Sample runs<jupyter_code>Enter 0 to exit the loop
Enter a number:3
Enter a number:5
Enter a number:0
The average was: 4.0

Enter 0 to exit the loop
Enter a number:1
Enter a number:4
Enter a number:3
Enter a number:0
The average was: 2.66666666667<jupyter_output><empty_output><jupyter_text>average2.py<jupyter_code>#keeps asking for numbers until count have been entered.
#Prints the average value.

sum = 0.0

print("This program will take several numbers then average them")
count = int(input("How many numbers would you like to sum:"))
current_count = 0

while current_count < count:
    current_count = current_count + 1
    print("Number ", current_count)
    number = float(input("Enter a number:"))
    sum = sum + number

print("The average was:", sum/count)<jupyter_output><empty_output><jupyter_text>Sample runs<jupyter_code>This program will take several numbers then average them
How many numbers would you like to sum:2
Number  1
Enter a number:3
Number  2
Enter a number:5
The average was: 4.0

This program will take several numbers then average them
How many numbers would you like to sum:3
Number  1
Enter a number:1
Number  2
Enter a number:4
Number  3
Enter a number:3
The average was: 2.66666666667<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10470719051149, 'fraction_numerical': 0.02742772424017791, 'mean_word_length': 3.9377859103385178, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '502645', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1744, 'n_words': 723, 'path': 'tutorial4.ipynb', 'repo_name': 'dschwen/learn_python', 'size': '5403'}