avatarLiu Zuo Lin

Summary

This context is a Python quiz with seven questions and their answers, covering topics such as decorators, tuple unpacking, class instantiation, Global Interpreter Lock (GIL), reserved keywords, and context managers.

Abstract

The context provides a Python quiz consisting of seven questions that test the reader's understanding of various Python concepts. Each question is followed by a multiple-choice answer and an explanation of the correct answer. The topics covered in the quiz include decorators, tuple unpacking, class instantiation, Global Interpreter Lock (GIL), reserved keywords, and context managers. The quiz aims to help the reader assess their Python skills and learn new concepts.

Bullet points

  • The context is a Python quiz with seven questions and their answers.
  • The first question tests the reader's understanding of decorators in Python.
  • The second question covers tuple unpacking in Python.
  • The third question tests the reader's knowledge of class instantiation in Python.
  • The fourth question covers the Global Interpreter Lock (GIL) in Python.
  • The fifth question tests the reader's understanding of reserved keywords in Python.
  • The sixth question covers context managers in Python.
  • Each question is followed by a multiple-choice answer and an explanation of the correct answer.
  • The quiz aims to help the reader assess their Python skills and learn new concepts.

You’re Decent At Python If You Can Answer These 7 Questions Correctly

# No cheating pls!!

Try to answer these questions without googling or using Python to test.

If you can answer all of them correctly without cheating, I can safely say that you’re probably pretty decent at Python!

1) @print???

@print
def testing():
  print('hello!!')
  return 1000

What does this do?

  • A) Syntax error. There is no such thing as @ in Python
  • B) this causes testing() to automatically print 1000 when we call it
  • C) this prints <function testing at 0x1023e2340>
  • D) this prints 1000 automatically without us calling testing()
  • E) testing()'s metadata will automatically print whenever it is called

2) *_ ???

a, b, *_ = [1, 2, 3, 4, 5]
print(_)

What does this print?

  • A) Syntax error
  • B) [3, 4, 5]
  • C) [1, 2, 3, 4, 5]
  • D) <generator object <genexpr> at 0x1003847c0>
  • E) NameError: ‘_’ is not a valid variable name

3) more than 1 *_ ???

*__, a, b, *_ = [1, 2, 3, 4, 5, 6]
print(__, _)

What does this print?

  • A) Syntax error
  • B) [1] [4, 5, 6]
  • C) [1, 2] [5, 6]
  • D) [1, 2, 3] [6]
  • E) <generator object <genexpr> at 0x1003847c0>

4) class shenanigans

class Dog:
  def __init__(self, *args, **kwargs):
    args, kwargs = kwargs, args
    self.name = args['name']
    self.age = kwargs[0]

dog = Dog('rocky', 5)

What error does this cause?

  • A) No error
  • B) ZeroDivisionError
  • C) IndexError
  • D) KeyError
  • E) All of the above

5) GIL

What is the Python Global Interpreter Lock (GIL)?

  • A) a physical lock that secures Python servers from intruders
  • B) a thing that prevents the Python interpreter from leaking data to other processes on your computer
  • C) a programming paradigm that allows us to run multiple Python processes concurrently
  • D) a thing that allows one thread to run per interpreter at one time
  • E) a thing that enables our Python interpreter to run code faster

6) True = False

True = False
False = True

print(not True, not False)

what does this print?

  • A) SyntaxError
  • B) False True
  • C) True False
  • D) True True
  • E) False False

7) Context manager

What is a context manager used for?

  • A) to manage contexts
  • B) to properly handle resources eg file operations or databases
  • C) to ensure that type hints in Python are enforced
  • D) to make sure that any exceptions do not affect other contexts
  • E) to ensure that the Python interpreter does not taking up an excessive amount of RAM

Answers Below!!

Answers!!

1) @print???

@print
def testing():
  print('hello!!')
  return 1000

What does this do?

  • A) Syntax error. There is no such thing as @ in Python
  • B) this causes testing() to automatically print 1000 when we call it
  • C) this prints <function testing at 0x1023e2340>
  • D) this prints 1000 automatically without us calling testing()
  • E) testing()'s metadata will automatically print whenever it is called

Answer: C

^ here, we are decorating testing() with print(). This is the same as:

def testing():
  print('hello!!')
  return 1000

testing = print(testing)

since print(testing) is called, <function testing at 0x1023e2340> will be printed (the gibberish numbers at the end might differ tho)

2) *_ ???

a, b, *_ = [1, 2, 3, 4, 5]
print(_)

What does this print?

  • A) Syntax error
  • B) [3, 4, 5]
  • C) [1, 2, 3, 4, 5]
  • D) <generator object <genexpr> at 0x1003847c0>
  • E) NameError: ‘_’ is not a valid variable name

Answer: B

  • _ is a valid variable name
  • a is assigned to 1 and b is assigned to 2 (tuple unpacking)
  • * in front of _ allows _ to “catch” multiple values (0 to infinite)
  • _ will thus “catch” all unassigned numbers — 3, 4, 5
  • _ will thus be [3, 4, 5]

3) more than 1 *_ ???

*__, a, b, *_ = [1, 2, 3, 4, 5, 6]
print(__, _)

What does this print?

  • A) Syntax error
  • B) [1] [4, 5, 6]
  • C) [1, 2] [5, 6]
  • D) [1, 2, 3] [6]
  • E) <generator object <genexpr> at 0x1003847c0>

Answer: A

Only ONE * is allowed per expression. Having 2 causes a SyntaxError

4) class shenanigans

class Dog:
  def __init__(self, *args, **kwargs):
    args, kwargs = kwargs, args
    self.name = args['name']
    self.age = kwargs[0]

dog = Dog('rocky', 5)

What error does this cause?

  • A) No error
  • B) ZeroDivisionError
  • C) IndexError
  • D) KeyError
  • E) All of the above

Answer: D

*args allows our function to take in any number of positional arguments, while **kwargs allow our function to take in any number of keyword arguments.

Dog('rocky', 5) passes 2 positional arguments to __init__, and thus args=('rocky', 5) while kwargs={} (no keyword arguments here)

args, kwargs = kwargs, args switches args and kwargs. So now, args={} and kwargs=('rocky', 5)

When we attempt to call args['name'], we get a KeyError as at this point, args is an empty dictionary.

5) GIL

What is the Python Global Interpreter Lock (GIL)?

  • A) a physical lock that secures Python servers from intruders
  • B) a thing that prevents the Python interpreter from leaking data to other processes on your computer
  • C) a programming paradigm that allows us to run multiple Python processes concurrently
  • D) a thing that allows one thread to run per interpreter at one time
  • E) a thing that enables our Python interpreter to run code faster

Answer: D

The Python GIL makes is such that only 1 thread is able to run in the Python interpreter at one time. (tho we can use multiprocessing to circumvent this)

6) True = False

True = False
False = True
print(not True, not False)

what does this print?

  • A) SyntaxError
  • B) False True
  • C) True False
  • D) True True
  • E) False False

Answer: A

We cannot assign anything to True as True is a reserved Python keyword. I hope you answered this correctly.

7) Context manager

What is a context manager used for?

  • A) to manage contexts
  • B) to properly handle resources eg file operations or databases
  • C) to ensure that type hints in Python are enforced
  • D) to make sure that any exceptions do not affect other contexts
  • E) to ensure that the Python interpreter does not taking up an excessive amount of RAM

Answer: B

# let's say we need to read a file

file = open('hi.txt')

print(file.read())

file.close()

^ if we do this, there’s a chance that file.close() might not executed due to some error. Not closing the file might lead to the following problems:

  • the file might take up RAM and slow down your program
  • changes made to the file might not stick, as many files only save properly when they are closed
  • in Windows, a file is treated as locked when it is open, so other scanners or programs might not be able to read it
  • and other weird issues
# reading file using context manager

with open('hi.txt') as file:
  print(file.read())

The with keyword allows us to use open('hi.txt') as a context manager. This means that at the start of the context __enter__ will run, and when the context closes, __exit__ will run.

What you need to know: this will definitely close the file properly.

Conclusion

How many did you answer correctly without cheating?

If you didn’t answer all of them correctly, it doesn’t mean that you’re a loser. It could mean 1) you were careless somewhere 2) you’re not decent at Python YET

Cheers!

If You Wish To Support Me As A Creator

  1. Clap 50 times for this story
  2. Leave a comment telling me your thoughts
  3. Highlight your favourite part of the story

Thank you! These tiny actions go a long way, and I really appreciate it!

YouTube: https://www.youtube.com/@zlliu246

LinkedIn: https://www.linkedin.com/in/zlliu/

Python
Python3
Python Programming
Programming
Coding
Recommended from ReadMedium