type
to query the type of a value or a variable¶a = 5
print( type(a) )
b = 5.3
print(type(b))
print(type("Cat"))
g = b > a
print(g)
print(type(g))
b = True
def myF(a):
return a
print(type(myF))
<class 'int'> <class 'float'> <class 'str'> True <class 'bool'> <class 'function'>
### Arithmetic operators
# addition
a = 5 + 4
print(a)
9
# subtraction
b = 6 - 2
print(b)
4
# multiplication
c = 4 * 6
print(c)
24
# power
p = 2 ** 10
print(p)
1024
# floating-point division (note the decimal point)
d = 7 / 2
print(d)
3.5
# integer division
e = 7 //2
print(e)
3
# modulo (remainder)
f = 7 % 2
print(f)
g = 10 % 4
print(g)
1 2
Note that:
q = x // y
, and r = x % y
then: x = y * q + r
Fahrenheit temperatures can be calculated in terms of Celcius temperatures using the following formula:
$F = C × \dfrac{9}{5} + 32$
Implement the functions celciusToFahrenheit(c)
and fahrenheitToCelcius(f)
that converts
the temperatures.
def celciusToFahrenheit(c):
return c * (9 /5) + 32
def fahrenheitToCelcius(f):
return (f - 32) * 5 /9
Online tool can be found here: https://meyerweb.com/eric/tools/color-blend/
Colors in a computer are sometimes represented using integer RGB values, corresponding to the
amount of red, green, and blue it is composed of. These values are integers between 0 and 255.
For example, (0,0,0)
is black, and (255,255,255)
is white.
Given two colors, we can combine them to form a palette (the colors in between the two). We can
decide how many colors we want in this palette (the midpoints).
For example, we can combine the colors crimson (RGB = (220, 20, 60)
) and mint (RGB = (189, 252,
201)
) using 3 midpoints to obtain:
color0: (220, 20, 60) # crimson
color1: (212, 78, 95)
color2: (205, 136, 131)
color3: (197, 194, 166)
color4: (189, 252, 201) # mint
This pallete has 5 colors. We could then ask ourselves: what is color 2?
Implement the function getColor(r1, g1, b1, r2, g2, b2, midpoints, n)
that takes as input
the two initial colors, the number of midpoints, and the number of the color in the palette, and
returns the RGB value of the n-th color.
For example, following the case above: getColor(220, 20, 60, 189, 252, 201, 3, 1)
returns
(212, 78, 95)
.
**Hint:** for the sake of this exercise, it is ok if your result differs by one unit from the example, since integer division is not exact. We will learn how to fix this later.
**Hint:** try thinking about the problem with one component at a time (red, green, or blue), and with small midpoints (e.g. 1, 2, 3).
def getColor(r1, g1, b1, r2, g2, b2, midpoints, n):
r = round((r2-r1)/(midpoints+1)*n) + r1
g = round((g2-g1)/(midpoints+1)*n) + g1
b = round((b2-b1)/(midpoints+1)*n) + b1
return (r, g, b)
# We can use assert to write test cases
assert(getColor(220,20,60, 189,252,201, 3, 0) == (220,20,60))
assert(getColor(220,20,60, 189,252,201, 3, 1) == (212,78,95))
assert(getColor(220,20,60, 189,252,201, 3, 2) == (204,136,130))
assert(getColor(220,20,60, 189,252,201, 3, 3) == (197,194,166))
assert(getColor(220,20,60, 189,252,201, 3, 4) == (189,252,201))
# If our code runs without errors and the following line is executed
# it means that it's passed the test cases
print("Passed...")
Passed...