🐍 Python for Everyone

← Back to exercises

Chapter 2 Practice Bank 35-45 min, hands-on

Practice Bank: Variables & Data Types

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Still uses only print(), variables, input(), string concatenation with + / str(), identifier naming, dynamic typing, type(), id(), and the conversion functions int(), float(), str(), bool(), complex().

How to run these Download starter.py below, then run it from a terminal with python3 starter.py.

Topic 1: Naming variables properly

1 Three properly named variables

Find # TODO 1.1. Create snake_case variables for a person's first name, last name, and age, and print each on its own line.

2 Rename a bad variable

Find # TODO 1.2. Someone named a shopping cart total x. Create a properly named replacement, assign it 49.99, and print it.

3 Case sensitivity

Find # TODO 1.3. Create total, Total, and TOTAL, each with a different value, and print all three.

4 Underscore-prefixed name

Find # TODO 1.4. Create a variable name starting with an underscore for an internal-use value, assign it, and print it.

5 Fixing bad identifiers

Find # TODO 1.5. Rewrite three badly chosen identifiers (2nd_place, user name, api-key) as valid snake_case variables with sample values.

💼 Scenario

Config keys

Find # TODO 1.A. Name three config variables — a max retry count, an API timeout in seconds, and a debug-mode flag — with proper snake_case identifiers.

💼 Interview Prep

Why three separate variables?

Find # TODO 1.B. Explain why total, Total, and TOTAL can all exist as separate variables in the same program.

Topic 2: Python decides the type for you

1 Four types, one print()

Find # TODO 2.1. Create a str, int, float, and bool variable, and print all four in a single print() call.

2 Reassigning to a new type

Find # TODO 2.2. Create signal = 10, print it, then reassign it to the string "ten" and print again.

3 Boolean to string

Find # TODO 2.3. Create ready = True, print it, then reassign it to the string "yes" and print again.

4 Complex numbers exist too

Find # TODO 2.4. Create a complex number variable like 3 + 4j and print it directly.

5 Float to int

Find # TODO 2.5. Create a float variable, print it, then reassign it to an int and print again.

💼 Scenario

Feature flag reload

Find # TODO 2.A. Reassign a boolean feature flag to a string to simulate a config reload, and explain why that's legal in Python but wouldn't compile in a statically typed language.

💼 Interview Prep

Debug: cart summary crash

Find # TODO 2.B. This code crashes with a TypeError because a cart's item count is joined directly with a string. Find the bug and fix it.

Topic 3: type() and id()

1 What type is this?

Find # TODO 3.1. Create a float variable and print its type with type().

2 Same value, same id?

Find # TODO 3.2. Create two variables holding the same small int, and print both id() values.

3 Type and id together

Find # TODO 3.3. Create a string variable and print both its type and its id.

4 Larger numbers, same id?

Find # TODO 3.4. Create two variables holding the same larger int (500) and compare their ids — do they still match?

5 type() reflects the current value

Find # TODO 3.5. Create a boolean variable, print its type, reassign it to an int, and print its type again.

💼 Scenario

QA type check

Find # TODO 3.A. Prove a config value is really an int before it's trusted by a billing system, using type().

💼 Interview Prep

Do id(x) and id(y) match?

Find # TODO 3.B. If x = 5 and y = 5, will id(x) and id(y) be the same? Prove it and explain why.

Topic 4: Converting between types

1 Text to int

Find # TODO 4.1. Ask for the user's age with input(), convert it to an int, and print the value and its type.

2 Text to float

Find # TODO 4.2. Convert a string like "3.14" to a float using float() and print it.

3 Int to string

Find # TODO 4.3. Convert an int to a str using str() and print the value and its type.

4 Zero and one as bool

Find # TODO 4.4. Convert 0 and 1 to bool and print both to compare.

5 Debug the Code

Find # TODO 4.5. This code crashes with a ValueError because int() can't parse the text "three". Find the bug and fix it.

💼 Scenario

Signup form age check

Find # TODO 4.A. Ask for the user's current age, convert it to an int, and print a sentence stating their age next year.

💼 Interview Prep

The bool() string trap

Find # TODO 4.B. What does bool("False") return, and why does that surprise people? Prove it and explain the empty-string rule.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks. Note that id() values will differ every time you run the program, so don't expect your exact numbers to match.
← Exercises Interview Questions →