Skip to content
Home » Blog » Python Coding Quiz You Can’t Ignore – Learn by Solving These 12 Code Puzzles

Python Coding Quiz You Can’t Ignore – Learn by Solving These 12 Code Puzzles

Python Coding Quiz You Can’t Ignore – Learn by Solving These 12 Code Puzzles

Table of Contents

Ready to Flex Your Python Skills?

Want to test your Python knowledge with quick and practical examples? These 12 Python Coding Quiz are designed to boost your confidence, sharpen your skills, and help you avoid the sneaky mistakes that trip up even experienced developers.

This post is inspired by my recent YouTube quiz – packed with tricky scenarios and gotchas you won’t want to miss.
🎥 👉 [Watch the full video here!]

12 Python coding quiz questions to test and improve your Python skills
Sharpen your Python skills with these 12 coding quiz questions—perfect for spotting tricky mistakes and leveling up your coding confidence!

Take your time with each question and try solving it on your own first—then check the answer and explanation below.
Let’s see how many you can crack without peeking!

📌 SUBSCRIBE to stay sharp: 🎥 @UncoveredAIwithEmi(https://www.youtube.com/@UncoveredAIwithEmi) Hit 🔔 to never miss a practical Python tip again!

Python Coding Quiz 1: What will be the output of the following code?

x = [1, 2, 3]
y = x
y.append(4)
print(x)

Options:

a) [1, 2, 3]
b) [1, 2, 3, 4]
c) None
d) Error


Answer: b) [1, 2, 3, 4]


Explanation:

When you create a list with x = [1, 2, 3], Python makes a list in memory and gives the name x to it.

Then you write y = x. But this doesn’t create a new list. It just says:

“Hey y, point to the same list that x is using.”

Now both x and y are sharing the same list.

Then you do y.append(4). This means you’re changing the shared list by adding the number 4 to it.

So now that list becomes [1, 2, 3, 4].

And finally, when you print x, you’re printing that same list — which now includes the number 4.

So the result is:

[1, 2, 3, 4]

Tiny tip:

If you really want a separate copy of the list, you can do this:

y = x.copy()

Now y has its own list, and changing y won’t mess with x.

2: Which of the following is the correct way to get the first character of a string s?

s = "Python"

Options:

a) s(0)
b) s[0]
c) s[1]
d) s(1)


Answer: b) s[0]


Explanation:

In Python, strings are like boxes of letters, and each letter sits in its own spot.
These spots are numbered — starting from 0, not 1.

So:

s = "Python"

Here’s how Python sees it:

IndexLetter
0P
1y
2t
3h
4o
5n

So when you do s[0], you’re asking:

“Hey Python, give me the letter at position 0.”

That’s "P" — the first character.


What about the wrong options?

  • a) s(0) – This tries to call the string like a function. Python goes, “Uhh, strings don’t work that way,” and throws an error. ❌
  • c) s[1] – This gives you "y", which is actually the second letter. ❌
  • d) s(1) – Same as (a), it tries to call a string like a function. ❌

Fun tip:

This same logic works for lists, tuples, and even strings inside strings:

"hello"[0]  →  'h'
[10, 20, 30][0]  →  10

3: What will be the output of the following code?

x = "Python"
y = x[2:]
print(y)

Options:

a) P
b) Py
c) tho
d) on


Answer: c) tho


Explanation (in simple English):

You’re slicing a string here with x[2:].

Let’s first see what’s inside x:

x = "Python"

Python gives each letter a number (called an index), starting from 0:

IndexLetter
0P
1y
2t
3h
4o
5n

Now look at the slice:

x[2:]

This means:

“Start from index 2 (which is 't') and go all the way to the end of the string.”

So we get:

't' + 'h' + 'o' = "tho"

And when you print y, you’ll see:

tho

Why the other options are wrong:

  • a) P → That’s x[0], not x[2:].
  • b) Py → That would be x[:2].
  • d) on → That would be x[4:].

Quick tip:

Slicing in Python is super handy. You can do:

  • x[:3] → From the start to index 2
  • x[1:4] → From index 1 to just before index 4
  • x[-2:] → The last two characters

4: Which of the following is used to define a block of code in Python?

Options:

a) Curly braces {}
b) Parentheses ()
c) Indentation (spaces or tabs) ✅
d) Square brackets []

Answer: c) Indentation (spaces or tabs)


Explanation (made super clear):

In many other programming languages like C, Java, or JavaScript, you write:

if (condition) {
    // block of code
}

The curly braces {} show where the block begins and ends.

But Python is like:

“Nope, I like things neat and clean. Use indentation instead!”

So in Python, you write:

if 5 > 2:
    print("Yup, 5 is greater than 2")

That space before print()? That’s indentation, and it’s not optional. If you mess it up, Python throws a tantrum (a.k.a. an IndentationError).

Important:

  • You must use consistent indentation.
  • You can use spaces or tabs, but don’t mix them.
  • Most Python developers just use 4 spaces.

What about the wrong answers?

  • a) Curly braces {} → Python doesn’t use them for code blocks.
  • b) Parentheses () → Used for function calls or grouping expressions, not for blocks.
  • d) Square brackets [] → Used for lists and indexing, not for structure.

Bonus Tip:

Here’s a full example using indentation:

for i in range(3):
    print("Hi!")
    print("Still inside the loop")
print("Now we’re outside the loop")

The two print() lines are indented, so they belong to the for loop block.
The last print() is not indented, so it runs after the loop ends.

5: What is the output of this code?

x = 10
y = 20
print(x == y)

Options:

a) True
b) False ✅
c) None
d) Error


Answer: b) False


What’s going on here?

  • You’ve got two variables:
    • x is 10
    • y is 20

Then you’re asking Python:
“Hey, is x equal to y?”
Using the == operator (double equals), which means comparison.

So Python checks:

10 == 20

That’s clearly not true, so Python answers:

False

What’s the difference between = and ==?

  • = → Assignment (you’re giving a value to a variable)
    Example: x = 10 means “x is now 10”
  • == → Comparison (you’re checking if two values are the same)
    Example: x == y asks “Are x and y equal?”

Why are the other answers wrong?

  • a) True → Only if x and y had the same value (they don’t)
  • c) None → That’s used when a function returns nothing, not here
  • d) Error → There’s no syntax or logic error in this code

Real-Life Analogy:

It’s like comparing two apples 🍎🍏. One is green (y = 20), and one is red (x = 10).
You’re just asking: “Are these the same apple?” Python looks and says:
“Nope, totally different apples. FALSE.”

6: What will this code print?

x = "Hello"
y = "Hello"
print(x is y)

Options:

a) True ✅
b) False
c) None
d) Error


Answer: a) True


Why does this happen?

This is not just a value comparison — it’s an identity check.

  • x == y → checks if the values are the same
  • x is y → checks if they are the same object in memory

In this case, both x and y contain the exact same short string: "Hello".

And here’s the cool part:

Python automatically reuses certain small, immutable objects — like short strings and integers — to save memory.

So instead of creating two separate "Hello" strings in memory, Python just reuses the same object for both x and y.


What’s “immutable”?

  • Immutable means “can’t be changed.”
    Strings, numbers, and tuples are immutable in Python.

Let’s test the difference!

x = "Hello"
y = "Hello"
print(x == y)  # True → values are the same
print(x is y)  # True → both point to the *same* memory location

Analogy time:

It’s like you and your friend both point at the same picture on the wall and say,
“This is our favorite.”
You’re not holding different copies — you’re pointing to the same exact thing.
So Python goes:
“Yeah, that’s the same thing. TRUE.”

7: Which of the following is the correct syntax for a while loop?

Options:

a) while x = 5:
b) while x == 5:
c) while (x == 5):
d) while x:

Answer: b) while x == 5:


What’s going on here?

Let’s understand each option one by one:


a) while x = 5:

This is wrong because = is the assignment operator, not a comparison.
You can’t assign a value inside the condition. Python will throw a SyntaxError.


b) while x == 5:

✅ This is correct.
It checks: “Is x equal to 5?”
If yes, then the loop runs.


c) while (x == 5): ✅ but unnecessary parentheses

Also correct — just wrapped in parentheses. Python doesn’t need them, but they’re allowed.
It’s like wearing a helmet to eat cereal — safe, but… why?


d) while x: valid but different

This is also technically valid.
It means: “Keep looping as long as x is truthy.”
So if x is not 0, False, None, or an empty value — the loop continues.

But it doesn’t check if x == 5, so it’s not answering this question.


Real example:

x = 5
while x == 5:
    print("Still five!")
    break

Output:

Still five!

Here, the loop checks x == 5, runs once, then breaks out.


Tiny tip:

  • Use == when comparing
  • Use = when assigning

Mixing them up is one of the most common beginner mistakes!

8: What will the following code print?

a = "Python"
b = a * 2
print(b)

Options:

a) PythonPython ✅
b) Python2
c) P
d) Error


Answer: a) PythonPython


Explanation (Simple and Fun):

In Python, using * with a string and a number means:

“Repeat this string that many times.”

So:

a = "Python"
b = a * 2  # "Python" is repeated twice

It’s like telling Python:

“Hey, give me two scoops of Python! 🍦🍦”

And Python cheerfully hands you:

PythonPython

Why other options are wrong:

  • b) Python2 → That’s what you’d get with:
b = a + "2"
  • c) P → That would be from:
a[0]

d) Error → Nah, Python is happy to multiply strings with integers. No error here.

Quick Rule:

  • "text" * 3Repeats the string 3 times
  • But 3 * "text" → Also works!

Python is chill like that

9: What is the purpose of the break statement in a loop?

a) It skips the current iteration and moves to the next one.  
b) It pauses the loop.  
c) It exits the loop entirely. ✅  
d) It continues the loop forever.

Answer: c) It exits the loop entirely


Explanation (in plain English):

The break statement is like yelling:

“I’m outta here!”

As soon as Python hits a break, it jumps out of the loop — no questions asked, no looking back. It doesn’t wait for the loop condition to fail. It just leaves.


Example:

for i in range(10):
    if i == 5:
        break
    print(i)

Output:

0  
1  
2  
3  
4

As soon as i becomes 5, Python hits break and runs out of the loop.

Why the others are wrong:

  • a) Skipping the current iteration? That’s what continue does.
  • b) Pauses the loop? Python loops don’t take naps — unless you use time.sleep().
  • d) Continues forever? That’s what happens when you forget a break 😅.

In short:

break = “I’m done with this loop. Peace out!”

10: Which of the following is the correct way to create a dictionary in Python?

a) dict = {1: "apple", 2: "banana"} ✅  
b) dict = (1: "apple", 2: "banana")  
c) dict = [1: "apple", 2: "banana"]  
d) dict = "apple": 1, "banana": 2

Answer: a) dict = {1: “apple”, 2: “banana”}


Explanation (in human-friendly English):

A dictionary in Python is like a mini contact book — you look up a value (like “banana”) using a key (like 2).

Python dictionaries are always created using curly braces {}, and the format inside is:

key: value

So this:

{1: "apple", 2: "banana"}

…means:

  • Key 1 maps to "apple"
  • Key 2 maps to "banana"

Why the other options are wrong:

  • b) (1: “apple”, 2: “banana”)
    Parentheses are for tuples — they don’t work for dictionaries.
  • c) [1: “apple”, 2: “banana”]
    Square brackets are for lists, and they don’t like key-value pairs like that.
  • d) “apple”: 1, “banana”: 2
    This is just floating in space — without {}, Python has no clue it’s supposed to be a dictionary.

Quick Tip:

Avoid naming your dictionary dict — that’s the name of the built-in Python function!
Use a name like fruits or my_dict instead.


In short:

Correct dictionary = Curly braces + Key: Value pairs
Like this:

fruits = {"apple": 1, "banana": 2}

11: What will be the result of the following operation?

x = [1, 2, 3]
y = x
y[0] = 99
print(x)

Options:

a) [1, 2, 3]
b) [99, 2, 3] ✅
c) [1, 99, 3]
d) [99, 99, 99]


Answer: b) [99, 2, 3]


Explanation:

When you write:

y = x

You’re not copying the list. You’re just creating another name for the same list. So both x and y are pointing to the same list in memory — like two friends looking at the same pizza 🍕.

So when you do:

y[0] = 99

You’re not just changing y. You’re changing the actual list, and since x points to that same list, x sees the change too!


Visual Breakdown:

x ——▶ [1, 2, 3]

y —————

Then after:

y[0] = 99

It becomes:

x ——▶ [99, 2, 3]

y —————

Common Mistake:

People often think y = x makes a copy. It doesn’t. If you really want a copy, use:

y = x.copy()  # or y = list(x), or y = x[:]

Then y and x won’t affect each other. But in this question, they do!

12: What will the following code output?

x = 5
y = 2
z = 3
print(x // y * z)

Options:

a) 4
b) 6 ✅
c) 7
d) 1


Answer: b) 6


Explanation:

Let’s walk through it step by step:

Step 1: x // y

5 // 2

This is floor division, meaning it gives the whole number part of the result — without the remainder.

So:

5 // 2 = 2

Step 2: Multiply the result by z

2 * 3 = 6

Final Output:

print(2 * 3)  ➝  6

Correction from Earlier:

You may have seen the explanation saying the answer was 4 — but that’s incorrect. The right answer is 6.

The operations go left to right respecting operator precedence, and both // and * have the same precedence — so it evaluates in left-to-right order.

Conclusion

You made it to the end — high five! 🙌
These 12 quick questions weren’t just for fun — they tackled real Python concepts that trip up even experienced developers.

Whether you aced them or learned something new, the goal here was simple:

  • Boost your confidence
  • Clear your doubts
  • Make Python a little less mysterious

If you enjoyed this, check out the full quiz on YouTube and subscribe for more Python goodies!

🔗 Blog post link: https://emitechlogic.com

📌 SUBSCRIBE to stay sharp: 🎥 @UncoveredAIwithEmi(https://www.youtube.com/@UncoveredAIwithEmi) Hit 🔔 to never miss a practical Python tip again!

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *