WEEK 2 :: PYTHON CRASH COURSE

Complete the function by filling in the missing parts. The color_translator function receives the name of a color, then prints its hexadecimal value. Currently, it only supports the three additive primary colors (red, green, blue), so it returns "unknown

def color_translator(color):
if color == "red":
hex_color = "#ff0000"
elif color == "green":
hex_color = "#00ff00"
elif color == "blue":
hex_color = "#0000ff"
else:
hex_color = "unknown"
return hex_color

What's the value of this Python expression: "big" > "small

False (The conditional operator > checks if two values are equal. The result of that operation is a boolean: either True or False. Alphabetically, "big" is less than "small".)

What is the elif keyword used for?

to handle more than 2 comparison cases. (The elif keyword is used in place of multiple embedded if clauses, when a single if/else structure is not enough.)

Students in a class receive their grades as Pass/Fail. Scores of 60 or more (out of 100) mean that the grade is "Pass". For lower scores, the grade is "Fail". In addition, scores above 95 (not included) are graded as "Top Score". Fill in this function so

def exam_grade(score):
if score == 100:
grade = "Top Score"
elif 95 >= score >= 60:
grade = "Pass"
else:
grade = "Fail"
return grade

What's the value of this Python expression: 11 % 5?

1 ("%" is the modulo operator, which returns the remainder of the integer division between two numbers. 11 divided by 5 equals 2 with remainder of 1.)

Complete the body of the format_name function. This function receives the first_name and last_name parameters and then returns a properly formatted string.
Specifically:
If both the last_name and the first_name parameters are supplied, the function should

def format_name(first_name, last_name):
# code goes here
if first_name != '' and last_name != '':
return ("Name: " + last_name + ", " + first_name)
elif first_name != '' or last_name != '':
return ("Name: " + last_name + first_name)
else:
return ''
return

The longest_word function is used to compare 3 words. It should return the word with the most number of characters (and the first in the list when they have the same length). Fill in the blank to make this happen.

def longest_word(word1, word2, word3):
if len(word1) >= len(word2) and len(word1) >= len(word3):
word = word1
elif len(word2) >= len(word1) and len(word2) >= len(word3):
word = word2
else:
word = word3
return(word)

What's the output of this code?
def sum(x, y):
return(x+y)
print(sum(sum(1,2), sum(3,4)))

10. (We're calling the sum function 3 times: returning 3, then 7, then adding up 3 plus 7 for the total of 10.)

What's the value of this Python expression?
((10 >= 5
2) and (10 <= 5
2))

TRUE (When using the "and" operator, a statement is True if both parts of the conditional are True.)

The fractional_part function divides the numerator by the denominator, and returns just the fractional part (a number between 0 and 1). Complete the body of the function so that it returns the right number. Note: Since division by 0 produces an error, if

def fractional_part(numerator, denominator):
if denominator == 0:
# Operate with numerator and denominator to
# keep just the fractional part of the quotient
return 0
return (numerator/denominator)%1