x = 10
y = 3
y == x
False
4 == 4.0
True
3.0 == 3.5
False
4.0 != 4
False
3 != 7
True
4 < 6
True
4.0 < 3.9
False
5 < 6 < 7
True
32 <= 32
True
32 <= 32.8
True
7.5 <= 4
False
5 > 6
False
8 > 4
True
4.0 >= 4
True
4.5 >= 3
True
4.5 >= 9
False
True and False
False
x = 5
print(0 < x and x != 5)
print(x <= 10 and x > 6)
print(x < 5 and x <= 4)
print(x <= 5 and x >= 3)
False False False True
a | b | a or b |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
True or False
True
x = 5
print(0 < x or x != 5)
print(x <= 10 or x > 6)
print(x < 5 or x <= 4)
print(x <= 5 or x >= 3)
True True False True
4 < 0 or 3 < 0
False
a | not a |
---|---|
True | False |
False | True |
x = 5
not x < 0
True
not x >= 4
False
if-else
def myAbs(x):
if x < 0:
x = -x
# ... other commands may appear here, for example, another assignment or another if!
else:
x = x
# else case cannot be empty
return x
myAbs(-3)
3
if (without else)
If an else
case "does nothing", we can get rid of it.
def myAbs(x):
if x < 0:
x = -x
return x
myAbs(-32)
32
Write the function that implements the signum
function:
$$ \text{sgn }x := \begin{cases} -1 & \text{if } x < 0 \\
0 & \text{if } x = 0 \\
1 & \text{if } x > 0
\end{cases}$$
When we need multiple conditional expressions after the if condition or between the if and else conditions, we can use the if-elif statements:
if-elif
def sign(n):
s = 0
if n < 0:
s = -1
elif n > 0:
s = 1
#else: # we can omit the else here. why?
# s = 0
return s
sign(-43)
-1
What's the output of the following function for x = 3?
def myFunction(x):
if x > 4:
x = x + 1
elif x < 3.5 and x >= 2:
x = x * 2
elif x > 0:
x = 1
else:
x = -1
x = x + 4
return x
The answer is 10
print(myFunction(3))
10
Implement the function middle(a, b, c)
that takes three different numbers as input, and returns the middle among them.
Let's enumerate all possible cases
$ b < a < c$
$ c < a < b$
$ a < b < c$
$ c < b < a$
$ a < c < b$
$ b < c < a$
def middle(a, b, c):
if b < a < c:
return a
if c < a < b:
return a
if a < b < c:
return b
if c < b < a:
return b
if a < c < b:
return c
if b < c < a:
return c
def testMiddle():
assert(middle(1,2,3) == 2)
assert(middle(3,2,1) == 2)
assert(middle(1,0,3) == 1)
assert(middle(1,200,100) == 100)
assert(middle(7, 100, 1)==7)
print("Passed...")
testMiddle()
Passed...
# elif instead of if
def middle(a, b, c):
if b < a < c:
return a
elif c < a < b:
return a
elif a < b < c:
return b
elif c < b < a:
return b
elif a < c < b:
return c
elif b < c < a:
return c
def testMiddle():
assert(middle(1,2,3) == 2)
assert(middle(3,2,1) == 2)
assert(middle(1,0,3) == 1)
assert(middle(1,200,100) == 100)
assert(middle(7, 100, 1)==7)
print("Passed...")
testMiddle()
Passed...
# else at the end
def middle(a, b, c):
if b < a < c:
return a
elif c < a < b:
return a
elif a < b < c:
return b
elif c < b < a:
return b
elif a < c < b:
return c
else:
return c
def testMiddle():
assert(middle(1,2,3) == 2)
assert(middle(3,2,1) == 2)
assert(middle(1,0,3) == 1)
assert(middle(1,200,100) == 100)
assert(middle(7, 100, 1)==7)
print("Passed...")
testMiddle()
Passed...
# more compact
def middle(a, b, c):
if b < a < c:
return a
elif c < a < b:
return a
elif a < b < c:
return b
elif c < b < a:
return b
else:
return c
def testMiddle():
assert(middle(1,2,3) == 2)
assert(middle(3,2,1) == 2)
assert(middle(1,0,3) == 1)
assert(middle(1,200,100) == 100)
assert(middle(7, 100, 1)==7)
print("Passed...")
testMiddle()
Passed...
# even more compact
def middle(a, b, c):
if b < a < c or c < a < b:
return a
elif a < b < c or c < b < a:
return b
else:
return c
def testMiddle():
assert(middle(1,2,3) == 2)
assert(middle(3,2,1) == 2)
assert(middle(1,0,3) == 1)
assert(middle(1,200,100) == 100)
assert(middle(7, 100, 1)==7)
print("Passed...")
testMiddle()
Passed...