Loops
Containers
100

What is the difference between break and continue?

  • break → immediately exits the entire loop.

  • continue → skips the rest of the current iteration and moves to the next one.

100

{1,2}.intersection({2,3})

{2}

200

What does "while (False):" do?

It never runs - the loop body is skipped entirely.

200

Is the following a valid container:

{[1,2]: 3, [4,5]: 6}

No — it’s not valid.

Dictionary keys must be immutable and hashable.

300

Which loop should we use for the game guess_the_number if we do not limit the number of attempts?

Use a while loop, since it can run indefinitely until the correct number is guessed.

300

Write 1 line to print the number of unique characters in mystr.

print(len(set(mystr)))


400

This built-in function gets both index and value when iterating a sequence.

enumerate()

400

Classify the following as mutable or immutable:

Tuple, list, set, dictionary

  • Tuple: Immutable

  • List: Mutable

  • Set: Mutable

  • Dictionary: Mutable

500

When iterating a list of (a, b) pairs, you can write "for a, b in pairs:" — this feature is called what? Example: pairs = [(1,2), (3,4)]

tuple unpacking

500

sorted({"a": 1, "b": 0}, reverse=True)

['b', 'a']