0
0
Pythonprogramming~10 mins

Return values in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the sum of two numbers.

Python
def add(a, b):
    return a [1] b
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
2fill in blank
medium

Complete the code to return the first character of a string.

Python
def first_char(s):
    return s[1]
Drag options to blanks, or click blank then click option'
A[1]
B[-1]
C[:1]
D[0]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using index 1 which is the second character.
Using negative index which gives last character.
3fill in blank
hard

Fix the error in the function to return the square of a number.

Python
def square(n):
    result = n [1] n
    return result
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
4fill in blank
hard

Fill both blanks to return a dictionary with words as keys and their lengths as values for words longer than 3 letters.

Python
def word_lengths(words):
    return {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B<=
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself as value instead of its length.
Using wrong comparison operator.
5fill in blank
hard

Fill all three blanks to return a dictionary with uppercase keys and values only if the value is positive.

Python
def filter_positive(data):
    return { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using original keys instead of uppercase.
Using wrong comparison operator or filtering condition.