Untitled
Python Programming
****
Complete the code to return the output
d = {
'one': 1,
'two': 2,
'three': 3,
'four': 4
}
d[]
d['two']
Add 'Patchwork' to the end of the boardgames
list.
Complete the code to return the output
boardgames = [
'Rivals for Catan',
'7 Wonders Duel',
'Carcassonne',
'Hive',
]
('Patchwork')
print(boardgames)
Expected Output
['Rivals for Catan', '7 Wonders Duel', 'Carcassonne', 'Hive', 'Patchwork']
6s
Define a function named hours_to_seconds
that converts hours to seconds.
Complete the code to return the output
def hours_to_seconds(hours ):
return hours * 60 * 60
hours_to_seconds(8)
Expected Output
28800
Fill in the blanks
Change the rating of the book to 4.6
Complete the code to return the output
book = {
'title': 'The Giver',
'author': 'Lois Lowry',
'rating': 4.13
}
book['rating'] = 4.6
book['rating']
Expected Output
4.6
What would be the output of the code snippet?
ints = set([1,1,2,3,3,3,4])
print(len(ints))
7
6
4
1
Access the docstrings of the factorial()
function.
Complete the code to return the output
def factorial(n):
"""returns n!"""
return 1 if n < 2 else n * factorial(n-1)
factorial.__doc__
Expected Output
'returns n!'
Count the number of banks in the online_banks
tuple.
Select the code to return the output
online_banks = ('consensus systems',
'chain inc',
'abra',
'bitfury')
number_of_items = len(online_banks)
print(number_of_items)
online_banks.count()
online_banks.len()
len(online_banks)
max(online_banks)
Expected Output
4
Appropriately call the function below.
Complete the code to return the output
x = lambda : "I know how to call this function."
x()
Expected Output
'I know how to call this function.'
Complete the code to return the output
class AstroBody:
description = 'Natural entity in the observable universe.'
class Star(AstroBody):
pass
sun = Star()
sun.description
Expected Output
'Natural entity in the observable universe.'
Complete the type hints in the function below.
Select the code to return the output
def add_two(x: int)-> int:
return x + 2
add_two(1)
::
<>
:
->
=>
Expected Output
3
Your answer has been submittedNext Question
Complete the code to return the output
dict_gen = { num: num*10 for num in range(5) }
dict_gen
Expected Output
{0: 0, 1: 10, 2: 20, 3: 30, 4: 40}
Define a function that squares a single input argument. Complete the docstring for the function.
Complete the code to return the output
def square(x):
"""Returns the square of the argument x"""
return x*x
square.__doc__
Expected Output
'Returns the square of the argument x'
Complete the code to return the output
class Candy:
flavor = 'sweet'
def __init__(self, name):
self.name = name
c = Candy('Chocolate')
c.name
Expected Output
'Chocolate'
Complete the code to return the output
class Planet:
def __init__(self, name, diameter_km):
self.name = name
self.diameter_km = diameter_km
e = Planet('Earth', 12742)
e.name, e.diameter_km
Expected Output
('Earth', 12742)
Declare an empty class named BankAccount
Complete the code to return the output
class BankAccount:
pass
account = BankAccount()
print(type(account))
Expected Output
<class '__main__.BankAccount'>
Complete the code to return the output
class Person:
def __init__(self, name):
self.name = name
m = Person('Michael')
m.name
Expected Output
'Michael'
Get the value of the "three" key using a dictionary method.
Complete the code to return the output
d = {
'one': 1,
'two': 2,
'three': 3,
'four': 4
}
d.get('three')
Expected Output
3
Add 'Gloomhaven' to the boardgames
list in the first position
Complete the code to return the output
boardgames = [
'Pandemic Legacy: Season 1',
'Terraforming Mars',
'Brass: Birmingham'
]
boardgames.insert(0, 'Gloomhaven')
print(boardgames)
Expected Output
['Gloomhaven', 'Pandemic Legacy: Season 1', 'Terraforming Mars', 'Brass: Birmingham']
Complete the code to return the output
x = (1, 2, 3)
s = set(x)
s
Expected Output
{1, 2, 3}
Complete the code to return the output
w = 'python'
w_iterator = iter(w)
next(w_iterator)
Expected Output
'p'
Complete the code to return the output
letters = ['a', 'b', 'c']
for in enumerate(letters):
for ii, x in enumerate(letters):
print(ii, ": ", x)
Expected Output
0 : a
1 : b
2 : c
Complete the code to return the output
with open('hello.txt', 'w') as file:
file.write("hello!")
print(file.closed)
Expected Output
True
Complete the code to return the output
class Building:
def __init__(self, number):
self.number = number
b = Building(245)
b.number
Expected Output
245
Call the function defined below (with the appropriate input argument) to produce the desired output.
Complete the code to return the output
def return_random(value):
return value
return_random('two')
Expected Output
'two'
Add a key-value pair to the d
Python dictionary such that it matches the output.
Complete the code to return the output
d = {
'apple': 1,
'banana': 2,
'coconut': 3
}
d['durian'] = 4
d
Expected Output
{'apple': 1, 'banana': 2, 'coconut': 3, 'durian': 4}
Complete the code to return the output
class Dog:
def __init__(self):
pass
def bark(self):
return "bark bark bark bark bark bark..."
d = Dog()
d.bark()
Expected Output
'bark bark bark bark bark bark...'
Define a function named hours_to_seconds
that converts hours to seconds.
Complete the code to return the output
def hours_to_seconds(hours):
return hours * 60 * 60
hours_to_seconds(8)
Expected Output
28800
Complete the statement using a list comprehension with appropriate logic and operations such that it matches the output.
Complete the code to return the output
[i * 3 for i in range(5)]
Expected Output
[0, 3, 6, 9, 12]
Complete the code to return the output
packages = ["numpy", "pandas", "scipy"]
for i in packages:
print(i)
Expected Output
numpy
pandas
scipy
Add the following item to the book
dictionary:
format: paperback
Complete the code to return the output
book = {
'title': 'The Giver',
'author': 'Lois Lowry',
'rating': 4.13
}
book['format'] = 'paperback'
book['format']
Expected Output
'paperback'
Complete the code to return the output
class Dog:
def woof(self):
return 'woof!'
t = Dog()
t.woof()
Expected Output
'woof!'
Complete the code to return the output
class Building:
def __init__(self, number):
self.number = number
b = Building(245)
b.number
Expected Output
245
Complete the code to return the output
with open('hello.txt', 'w') as file:
file.write("hello!")
print(file.closed)
Expected Output
True
Two functions have been defined for you. Use the square_args()
function to square the arguments passed to multiply()
.
Complete the code to return the output
def square_args(func):
def inner(a, b):
return func(a ** 2, b ** 2)
return inner
square_args
def multiply(a, b):
return a * b
multiply(3, 9)
Expected Output
729
Decorate the adder()
and subtractor
functions so that the result of the adder
function is multiplied by 2
and the result of the subtract
function is multiplied by 3
.
Complete the code to return the output
def multiply(by = None):
def multiply_real_decorator(function):
def wrapper(*args,**kwargs):
return by * function(*args,**kwargs)
return wrapper
return multiply_real_decorator
def adder(a,b):
return a + b
def subtractor(a,b):
return a - b
print(adder(2,3))
print(subtractor(2,3))
Expected Output
10
-3
Complete the code to return the output
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a.intersection(b)
Expected Output
{3, 4}
Complete the code to return the output
class Cat:
return 'meow!'
s = Cat()
s.meow()
Expected Output
'meow!'
Complete the code to return the output
class Cat:
def meow(self):
return 'meow!'
s = Cat()
s.meow()
Expected Output
'meow!'
Complete the code to return the output
f = : x * y
f = lambda x, y: x * y
f(3,3)
Expected Output
9
The function sample
has been written using a for
loop, as shown below. Using a list comprehension, create a more efficient version of this function.
def sample(n):
x = []
for i in range(n):
x.append(random.random())
return x
Complete the code to return the output
import random
random.seed(2427)
def efficient_sample(n):
x = [random.random() for n]
x = [random.random() for i in range(n)]
return x
efficient_sample(20)
Expected Output
[0.6709313964859867,
0.9738115340563225,
0.6064264401595373,
0.4066259803173813,
0.241007454546324,
0.9570332484250537,
0.2349020347673353,
0.8876755137054037,
0.9720131163571095,
0.1492980772443857,
0.9414046155591562,
0.5597323750561738,
0.7608989127589141,
0.5249642801198838,
0.1344891272249087,
0.9796560039964438,
0.06863221669260322,
0.8766064411366202,
0.5504489926930571,
0.4880661379761838]
Complete the code to return the output
s = "a"
s.center(3)
Expected Output
' a '
s = "a"
Complete the code to return the output
lst = ['numpy', 'pandas', 'requests']
lst_gen = (pkg for pkg in lst)
(lst_gen)
next(lst_gen)
Expected Output
'numpy'
Last updated
Was this helpful?