Complete the function get_dict_keys(data) that takes a dictionary and returns a list of all its keys.
For example, get_dict_keys({ " name " : " John " , " age " : 25}) should return [ " name " , " age " ].
def get_dict_keys(data):
# TODO: Return a list of all dictionary keys
pass
Explanation:
Step 1: A dictionary stores data as key-value pairs.
Step 2: The .keys() method returns the dictionary's keys.
Step 3: To return the keys as a list, use list(data.keys()).
Correct code:
def get_dict_keys(data):
return list(data.keys())
Example:
print(get_dict_keys({ " name " : " John " , " age " : 25}))
Output:
[ ' name ' , ' age ' ]
In the code for item in my_list:, what does item represent?
Write a complete function word_count(text) that counts and returns the number of words in the given text.
Words are separated by spaces.
For example, word_count( " Hello world Python " ) should return 3.
def word_count(text):
# TODO: Count and return the number of words in text
pass
Explanation:
Step 1: The function receives one string parameter named text.
Step 2: Since words are separated by spaces, use the .split() method to split the string into a list of words.
Step 3: Use len() to count how many items are in the list.
Step 4: Return that count.
Correct code:
def word_count(text):
return len(text.split())
Example:
print(word_count( " Hello world Python " ))
Output:
3
Which components are required in every Python for loop?
Fix the indentation error in this function that should return a greeting message.
def greet(name):
return " Hello " + name
Explanation:
Step 1: In Python, the code inside a function must be indented.
Step 2: The return statement belongs inside the function body.
Step 3: Add indentation before the return statement.
Correct code:
def greet(name):
return " Hello " + name
Example:
print(greet( " Alice " ))
Output:
Hello Alice
Fix the missing return statement in this function that should return whether a number is even.
def is_even(number):
if number % 2 == 0:
True
else:
False
Explanation:
Step 1: The expression number % 2 == 0 checks whether the number is even.
Step 2: The original code writes True and False, but it does not return them.
Step 3: A function must use the return statement to send a value back to the caller.
Correct code:
def is_even(number):
if number % 2 == 0:
return True
else:
return False
Simplified correct code:
def is_even(number):
return number % 2 == 0
Example:
print(is_even(4))
print(is_even(7))
Output:
True
False
Complete the function format_name(first, last) that takes a first name and last name and returns them combined as " last, first " .
For example, format_name( " John " , " Smith " ) should return " Smith, John " .
def format_name(first, last):
# TODO: Return the name in " last, first " format
pass
Explanation:
Step 1: The function receives two string parameters: first and last.
Step 2: The required format is the last name first, followed by a comma, a space, and then the first name.
Step 3: Use string concatenation to combine the values correctly.
Correct code:
def format_name(first, last):
return last + " , " + first
Example:
print(format_name( " John " , " Smith " ))
Output:
Smith, John