0
0
Pythonprogramming~10 mins

Difference between method types in Python - Interactive Practice

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

Complete the code to define an instance method that prints the name.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def show_name(self):
        print(self.[1])
Drag options to blanks, or click blank then click option'
Aself
Bname
CPerson
Dcls
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'name' inside print statement.
Using 'cls' which is for class methods.
Using 'Person' which is the class name, not an instance variable.
2fill in blank
medium

Complete the code to define a class method that prints the class name.

Python
class Person:
    @classmethod
    def show_class(cls):
        print(cls.[1])
Drag options to blanks, or click blank then click option'
Aname
B__name__
C__class__
D__qualname__
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance variable names like 'name'.
Using __class__ which is not an attribute of cls.
Using __name__ which is a module attribute.
3fill in blank
hard

Fix the error in the static method that returns a greeting string.

Python
class Person:
    @staticmethod
    def greet():
        return 'Hello, ' + [1]
Drag options to blanks, or click blank then click option'
A'World'
Bcls.name
Cself.name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access instance or class variables inside static method.
Using undefined variable 'name'.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dword.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word > 3' which compares string to number.
Using 'word.length' which is not valid in Python.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than 6.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = [1]: [2] for word in words if [3]
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) < 6
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Using wrong length condition like greater than 6.