0
0
Pythonprogramming~10 mins

Use cases for each method type 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 define a static method inside a class.

Python
class Calculator:
    @staticmethod
    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 define a class method that returns the class name.

Python
class Person:
    @classmethod
    def get_class_name(cls):
        return cls.[1]
Drag options to blanks, or click blank then click option'
A__qualname__
Bname
C__class__
D__name__
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance attributes or incorrect class attributes.
3fill in blank
hard

Fix the error in the instance method that returns the full name.

Python
class User:
    def __init__(self, first, last):
        self.first = first
        self.last = last
    def full_name([1]):
        return f"{self.first} {self.last}"
Drag options to blanks, or click blank then click option'
Auser
Bcls
Cthis
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using cls or other names instead of self.
4fill in blank
hard

Fill both blanks to create a class method that creates an instance from a string.

Python
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author
    @classmethod
    def from_string(cls, book_str):
        title, author = book_str.split([1])
        return cls([2], author)
Drag options to blanks, or click blank then click option'
A", "
Btitle
Cauthor
D': '
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong split delimiter or passing wrong variable to constructor.
5fill in blank
hard

Fill all three blanks to create a static method that checks if a number is even.

Python
class NumberUtils:
    @staticmethod
    def is_even([1]):
        return [2] % [3] == 0
Drag options to blanks, or click blank then click option'
Anum
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name or wrong divisor.