Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions for Notebook 3

if 1:
    print('The expression is true')
if 103.51:
    print('The expression is true')
if -1:
    print('The expression is true')
a = True
if a:
    print('The expression is true')
b = -73.445
if b:
    print('The expression is true')
if 5 < 5.1:
    print('The expression is true')
if 5 <= 5:
    print('The expression is true')
if 5.1 > 5.1:
    print('The expression is true')
if 5.1 >= 5.1:
    print('The expression is true')
# "!=" is "not-equals"
if 5 != 5.1:
    print('The expression is true')
if 5 < 6 and 10 <= 9:
    print("i don't think this is true")


if not False or True:
    print("it is true?")
else:
    print("it wasn't true?")
it is true?
def check_number(a):
    if a <= 5:
        print(a, "is less than or equal to 5")
    elif a < 10: 
        # note we don't need to test if it's greater than 5 since the first if already took 
        # care of that for us
        print(a, "is between 5 and 10")
    else:
        print(a, "is greater than or equal to 10")
        
# Testing your function
check_number(1)  
check_number(5)  
check_number(7)
check_number(10)
check_number(15)
The first code below is the bad one, with nested ifs.
# our function
def connected(subscription,paid,computer,on):
    if subscription:
        if paid:
            if computer:
                if on:
                    return print('Hello world')
                else:
                    return print('computer not on')
            else:
                return print('no computer')
        else:
            return print('not paid')
    else:
        return print('no subscription')

#test
connected(1,0,1,1)
connected(1,1,1,1)
not paid
Hello world

Improved way of writing, without nesting:

# our function
def connected(subscription,paid,computer,on):
    if not subscription:
        return print('no subscription')
    if not paid:
        return print('not paid')
    if not computer:
        return print('no computer')
    if not on:    
        return print('computer not on')
    else:   
        return print('Hello world')

#test
connected(1,0,1,1)
connected(1,1,1,1)
not paid
Hello world
def factorial(a):
    # f = our factorial result
    f = 1
    i = 2
    while(i<=a):
        f *= i
        i += 1
    return f
    
print(factorial(4))
print(factorial(2))
# Your code here

s = 0
for i in range(11):
    s += np.sin(i)**2

print("Sum is", s)
s = 0

# Note the range should end at 101!
# It should also start at 1: range(101) will also execute 
# the loop with i=0 (which won't matter here...)
for i in range(1,101):
    s += i

print(s)
maxnum=1e6
i=0
while i <= maxnum:
    if i*(i-10)==257024:
        break
    i+=1
    
print(i)
def leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            return False
        return True
    return False

years = [1, 4, 100, 400, 2000, 2012, 2020, 2021, 2024, 2100]
for year in years:
    print(year, leap_year(year))
1 False
4 True
100 False
400 True
2000 True
2012 True
2020 True
2021 False
2024 True
2100 False
## Solution
import numpy as np
y = np.array([2, 2, 3, 4, 5, 4, 3, 8, 6, 4]) 

for i in range(1, len(y)-1):
    if y[i-1] < y[i] and y[i+1] < y[i]:
        print('index:', i, '\t value:', y[i])                                  
password = 'practicum123'
tries = 0 

from time import sleep

while True:
    input_pw = input('What is the password?')
    if input_pw == password:
        break
    if (tries+1)%3==0:
        sleep(60+tries**3)
    tries += 1