QUIZ: Python's syntax and standard library

As a follow-up to the quiz I posted recently and using what has been learned from that one, I present a new quiz. This one is a bit less on the esoteric side, and the questions should be more relevant - either because they’re commonly used features, or because the question specifically aims to point out something important.

This isn’t “version two” of the other quiz, but I suck at coming up with titles for quizzes…

Enjoy!

9 Likes

Darn, 19/20 and the one I got wrong was because I didn’t fully read its code. Disappointed in myself now. Would’ve paid more attention had I known I’d be so close.

1 Like

What’s “slightly tricky” in the “Comprehensions are a very useful way to build up collections. Which of these is true?” question?

One of the responses is syntactically valid, but it’s not a comprehension - it’s a generator expression. Python does not have tuple comprehensions. It’s not particularly tricky to someone who has a lot of experience with Python, but given the close parallel in all four of the examples, knowing WHY that one of them isn’t valid is important.

16/20, did not read the decorator carefully enough, got my number types mixed up (too much C++ and Zig recently) and then got two wrong where I honestly had no idea (the warn and assert questions, I never really use those)

Great quiz!

Thanks, glad you liked it!

The warn one is really less about warnings and more about star imports, and the fact that it’s the one question with the lowest hit rate says a lot about how readable star imports are.

One thing I’d really like to do would be to add an annotation after each question that is revealed only when you’ve locked in your answer. That’d be where I would explain a quirk, or give a recommendation about actual code. Perhaps some of those will end up here.

1 Like

I never use star imports but and rarley the math module so I was very disadvantaged there as well, even if I hadn’t been duped by the warn part. I did remember that I have had trouble using the correct log function in the past but the question was well designed and deceived me.

1 Like

Maybe. That’s the one I got wrong. I did recognize that that overwrites log. But … I have zero interest in logging and after reading the options I was already so fed up with the question that I managed to lackadaisically pick the 2.014903 option anyway, the one option I could’ve and should’ve ruled out.

Then @ajoino , I would say you are disadvantaged at a for-fun quiz, but massively advantaged at writing clear and readable code :slight_smile:

1 Like

19/20.

The wrong one was (obviously?) that with star imports. :slight_smile:

1 Like

Fun quiz, thanks for making it!

Got 19/20 because I have no clue what weird combinations of string literal prefixes are actually allowed and/or sensible. But my biggest takeaway here is that I should start telling my students about the ever important class of smiley face operators.

1 Like

Nice :slight_smile: Presumably you’re talking about the bytes literal question as that’s the only one with multiple prefixes. ub would mean “Unicode Bytes” which makes no sense, so the only really notable point is that f-strings are text-only.

Yes! These are extremely important. Do please strive to make your code more friendly by the proper application of smiley operators. Replace <=2 with <3 :slight_smile:

I actually did choose the ub option! :smiley:
My mental model basically is that f is for formatting, r disables the backslash stuff, and u is redundant since Python 3. I think I would’ve expected that a ub string essentially just creates the unicode string and then converts it into a bytes object using some magically chosen encoding, probably utf-8. Guess this is showing my lack of low level programming experience, I never really think of string literals as representing a sequence of bytes directly. But yeah, when you think about it, of course the actual idea behind byte strings makes a ub string completely meaningless.

That’s fair. In Python, bytes and text are fundamentally different[1], which fortunately frees us from needing to think about that.


  1. is that really much of a spoiler? :slight_smile: ↩︎

For anyone who’s gotten tangled by the bytes question, here’s a quick rundown of Python’s string literal prefixes:

  • u"..." makes a Unicode string. This is the default though, so you don’t need to say it.
  • b"..." makes a byte string instead.
  • r"..." changes how backslashes are handled.
  • f"..." lets you format data into your string.

Hopefully that’ll help. Some of these prefixes can be combined, and some can be uppercased (but really, who ever does that??). Not all combinations make sense, but if they’re valid, they mean exactly what you’d expect.

(19/20, completely skimmed over the * import)


I think this one could be classified as IndentationError: unexpected indent :grinning_face_with_smiling_eyes:

TIL you can have leading 0’s in float literals.

Unless the order is randomized, throwing the decorator question first was evil.

It is randomized :slight_smile:

That function is all supposed to be on one line, as per the description above it. If it’s wrapped on your screen, ignore that, as there isn’t actually a newline in there (some editors will do the same).

I actually meant the leading spaces.

Ah, consequence of the quiz format. We can assume that there’s context around it that makes it legal.

18/20. Some nicely tricksy ones in there :slight_smile:

1 Like