digits look like? (in other words, what does digits do?)while vs for loopsA while loop has a condition that needs to evaluate to False to get out of the loop:
A for loop uses a variable:
Definite loop: the number of iterations is known before the loop begins execution
Which type of loop is always definite? while or for loops?
while and for loopsrangerange(start, stop, step)
You have 10 minutes to complete the quiz
if __name__HINT: use the keyword def to define a function
Write a python function called factorial that uses a loop (for or while, up to you) to calculate factorial of a non-negative integer n (n!)
A factorial is the product of a given positive integer and all the positive integers smaller than it down to 1:
\(5! = 5 × 4 × 3 × 2 × 1 = 120\)
The factorial of 0 (0!) is defined as 1.
Submit your solution (name your file loop_factorial.py) to gradescope
for loopfor loopwhile loopwhile loopSequence: an ordered collection of items
len() functionTuple can hold repeated values
my_tuple = ("apple", "banana", "pear", "banana")
my_tuple[1] # item at index 1
my_tuple[1:3] # slice from second to third item('banana', 'pear')
… can hold any type (including tuples, for nested tuples)
There are several edge cases for tuple literals
An empty tuple is just to parentheses containing nothing ()
A tuple with only one item must have a trailing comma tup_of_1 = (50,)
You can always have a trailing comma tup_t = (8, "6", 7, 5, "3", "09", )
If you have a bunch of comma separated values without parentheses they may also be interpreted as a tuple.
vals = 4, 5, 6, 3.3, "Tuple"
(2, 3) + (1, 2, 3)(2,) * 3Same operators as tuples
Looping over a sequence with an index:
Looping over a sequence without an index:
Write a python function called hex_to_rgb that gets as argument a hex code in the string format "#F54927" and returns the equivalent RGB code in a tuple of integers format (245, 73, 39)
Hint: The base of a hexadecimal system is 16, we want to convert a two digit hex code to decimal integer. For example, you can convert "CF" to python using the int() built-in function like so int("CF", 16)
Submit your solution colors.py to the in-class exercise in gradescope.
def hex_to_rgb(hex_string):
red = hex_string[1:3]
blue = hex_string[3:5]
green = hex_string[5:7]
return int(red, 16), int(blue, 16), int(green, 16)
if __name__ == "__main__":
assert hex_to_rgb("#F54927") == (245, 73, 39)
assert hex_to_rgb("#000000") == (0, 0, 0)
assert hex_to_rgb("#FFFFFF") == (255, 255, 255)
assert hex_to_rgb("#70B578") == (112, 181, 120)
print("Passed all tests")Passed all tests
Write a python function that determines whether a whole number greater than one is prime. A prime is divisible only by itself and 1 (not a product of two smaller natural numbers). Submit your python script prime.py to gradescope.
Test the remainder of the division with all primes up to the square root of the argument n.
Test cases:
Add to your previous solution a python function called get_primes that takes a whole number greater than one as argument and returns a tuple with all integers that are prime up (and including) n.
Test cases:
if __name__ == "__main__":
assert get_primes(2) == (2,)
assert get_primes(4) == (2, 3)
assert get_primes(29) == (2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
assert get_primes(53) == (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53)
assert get_primes(54) == (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53)
print("Passed all tests")