0
0
Pythonprogramming~10 mins

sorted() function 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 sort the list in ascending order.

Python
numbers = [5, 2, 9, 1]
sorted_numbers = sorted([1])
print(sorted_numbers)
Drag options to blanks, or click blank then click option'
Asorted
Bnumbers
Clist
Drange
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing the function name 'sorted' instead of the list variable.
Passing 'list' or 'range' which are not the list to sort.
2fill in blank
medium

Complete the code to sort the list in descending order.

Python
fruits = ['banana', 'apple', 'cherry']
sorted_fruits = sorted(fruits, [1]=True)
print(sorted_fruits)
Drag options to blanks, or click blank then click option'
Aorder
Bkey
Creverse
Ddescending
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'key=True' which is incorrect.
Using 'order=True' or 'descending=True' which are not valid parameters.
3fill in blank
hard

Fix the error in the code to sort words by their length.

Python
words = ['cat', 'elephant', 'dog']
sorted_words = sorted(words, key=[1])
print(sorted_words)
Drag options to blanks, or click blank then click option'
Alen
Blength
Csize
Dcount
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'length', 'size', or 'count' which are not built-in functions.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths, including only words longer than 3 characters.

Python
word_lengths = {word: [1] for word in words if [2] > 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself instead of its length for the dictionary value.
Checking the word string directly instead of its length in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their original words as values, including only words longer than 4 characters.

Python
result = [1]: [2] for word in words if [3] > 4
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
Clen(word)
Dword.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word.lower() instead of uppercase for keys.
Using the length of the word as the value instead of the word itself.
Checking the word string directly instead of its length in the condition.