length = 4
width = 2
length > width
True
length == width
False
0 == False
True
'' == False
False
bool('')
False
bool('asdf')
True
0 == False
True
Write a boolean expression that verifies if a number n is between the values a and b
a = 0
b = 5
n = 3
a <= n <= b
True
if condition:
block of code
a = 0
b = 5
n = int(input('n?'))
if a <= n <= b:
print('Yay')
n?10
def is_odd(x):
return x % 2 != 0
n = int(input('n?'))
# Alternative execution
if is_odd(n):
print('{} is odd!'.format(n))
else:
print('{} is even!'.format(n))
n?10 10 is even!
import random
x = random.randint(-10, 10)
if x < 0:
xtype = 'negative'
elif x > 0:
xtype = 'positive'
else:
xtype = 'zero'
print('{} is {}'.format(x, xtype))
0 is zero
TARGET = random.randint(0, 10)
def guess(number):
if not number:
return
number = int(number)
if number > TARGET:
print('too high')
elif number < TARGET:
print('too low')
else:
print('you win!')
interact(guess, number='')
interactive(children=(Text(value='', description='number'), Output()), _dom_classes=('widget-interact',))
<function __main__.guess(number)>
guess(7)
too high
guess(4)
too low
guess(5)
too low
guess(6)
you win!
from ipywidgets import interact
interact(guess, number='')
interactive(children=(Text(value='', description='number'), Output()), _dom_classes=('widget-interact',))
<function __main__.guess(number)>