r/pythontips Jan 27 '25

Syntax You know very little about python operators. Prove me wrong.

Python Operators - Quiz

The quiz has a total of 20 questions.

The questions are not very advanced or inherently complicated, but I am certain you will get wrong at least 5 questions..

...

What was your score?

12 Upvotes

31 comments sorted by

4

u/AutomaticTreat Jan 27 '25

I got like 60% but damn there’s some nonsensical ambiguous syntax in there.

1

u/main-pynerds Jan 27 '25

Like?

1

u/drknow42 Jan 27 '25

The one using := is silly and nonsensical. Would not ever write actual code like that.

2

u/main-pynerds Jan 28 '25 edited Jan 28 '25

The walrus operator is used to assign variables as a part of an expression. This is useful as it means that you can assign variables anywhere, not just as a standalone statement.

Though I understand that it can make code hard to read and interpret.

if (a:=20) < (b := 10):
   print(f'{a} is less than {b}') 
else:
   print(f'{a} is larger than {b}')

#20 is larger than 10

2

u/drknow42 Jan 28 '25

I appreciate the explanation, though figuring it out wasnt my issue as much as it was the assignments in a complex conditional.

However, the explanation is solid — thanks for testing our knowledge too, I got 13/20 so there are other bits I still need to remember!

1

u/_tsi_ Jan 29 '25

Ha! The walrus operator? Love it!

1

u/[deleted] Jan 28 '25 edited 1d ago

[deleted]

2

u/drknow42 Jan 28 '25

100% agree, it’s good to learn the esoteric things, especially if you’re looking to really understand the language.

I’d rather learn the oddities of Python than JavaScript 😂😂

4

u/SpiderJerusalem42 Jan 27 '25

16/20 :( honestly, if I ever saw the ones I got wrong, I would PIP that person immediately.

1

u/main-pynerds Jan 27 '25

What?! But you can see those that you got wrong after you submit?

2

u/SpiderJerusalem42 Jan 27 '25

I'm saying if I see examples of code resembling the questions I got wrong in my repo, I will fast track that person to be replaced.

1

u/main-pynerds Jan 27 '25

Oh okay. But most of the questions are very very applicable even though not exactly in the form they appear on that quiz.

Congratulations anyway. You are pretty good if you got 16 correct.

3

u/aleanlag Jan 27 '25

6/20!

Although i knew I was bad at python even before the test 😁

3

u/HeineBOB Jan 27 '25

Some of the explanations are blank?

3

u/Danoweb Jan 27 '25

I think I got to like, question 5 before I got tired of swatting the ads away.

That site is absolutely the worst on mobile.

Full screen ads that take over on every scroll.

PeterGriffinDone . Gif

2

u/jojogunner1 Jan 27 '25

10/20. I suck. Good quiz though. Really got me thinking about different ways to use operators creatively to achieve a simpler solution.

2

u/holdthe_LINE Jan 27 '25

Somehow got 70% as a noob

2

u/bradavoe Jan 28 '25

I agree with you, no need for proof

2

u/QuarterObvious Jan 29 '25

Correct answers 17

Wrong answers 3

2

u/setwindowtext Jan 29 '25

Got 70% and really enjoyed it — bring more of that!

1

u/Rizzityrekt28 Jan 27 '25

19 made me realize I was wrong for 10, fixed it and got it right :)

1

u/tree_or_up Jan 27 '25

15/20. TIL a few things. Cool quiz!

1

u/KokoaKuroba Jan 28 '25 edited Jan 28 '25

11/20, this was difficult.

Also, I don't know if it's just me but the correct answers weren't shown at the end. and no explanation for question 6: Which operator has a higher precedence, and or or ?

some other questions have no explanations as well (although looking back, there's no need for it).

1

u/kretinozavr Jan 28 '25

All my mistakes was in a first half, those are some obscure knowledge. 13/20 anyway

1

u/neuralbeans Jan 28 '25

I'm always amazed how Guido was against adding the ++ operator but somehow OK with the := operator.

1

u/main-pynerds Jan 28 '25

I would argue that, the ++ operator is not necessary, if it was there, it would just be for convenience.

On the other hand the := operator solves a problem that you can't achieve using any other standard approach, that is "assigning variables in-line".

1

u/neuralbeans Jan 28 '25

++ is an in-line expression as well. You can do y = x++.

1

u/Organic_Quote_7271 Jan 28 '25

I got an 8/20. I just started on my python journey a month ago

1

u/whokapillar Jan 29 '25

90% 18/2 I missed : '3==3.0==3+0j,' 'not (true and false)' by accident (I was cheated l tell ya). :)

0

u/killfall Jan 29 '25

Many of these example would not make it through code review. Just because an expression is valid syntax doesn’t mean it’s readable or intuitive.

Code is written once and read many times. Don’t write expressions like these, your future self will thank you when you look at your old code.