📦 Variables & Types

Variables & Types: A variable is a labeled box — write name = "Ali" and you've put "Ali" in a box labeled "name".

A variable is a labeled box — write name = "Ali" and you've put "Ali" in a box labeled "name". But here's the real question: in Java, you must declare the box's TYPE before using it (String name = "Ali") — why doesn't Python ask for that? Because Python peeks INSIDE the box to figure out what's there, instead of trusting a label. That's faster to write, but it carries a risk: put a string in that box today and a number tomorrow, and Python won't complain — not at compile time, but mid-test, when it actually breaks. In QA automation, the root cause behind "AttributeError: 'int' object has no attribute 'strip'" is almost always exactly this: the box held a DIFFERENT type than you assumed.

Java requires explicit type: int x = 5. Python just needs x = 5 — the type is inferred automatically. This is called dynamic typing.

Micro Lab: Python coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Java vs Python — Variables

Which function checks the type of a variable in Python?

type(x) is Python's built-in. isinstance(x, int) also works and checks inheritance.

What is the most common method to determine the class (data type) of a Python variable?

The built-in type() function is used in Python to directly return the type of a variable.

Data types are like different drawer types in a kitchen: you don't put glasses in the cutlery drawer, you don't put pots on the spice rack — everything has its "right place." In Java this separation is a strict rule: the int drawer only accepts whole numbers, and the compiler simply won't allow otherwise. In Python, everything is technically an object — so the same drawer system exists, just without a lock. So which is it — freedom or risk? Both: it lets you prototype fast, but if you don't notice the difference between "200" (a string) coming back from an API and 200 (a number), your test can report a false "PASS". This is exactly where a QA engineer's real job begins: checking the data's TYPE instead of assuming it.

Java separates primitives (int, double, boolean) from Objects. In Python, everything is an object — int, str, list, dict are all class instances.

Which Python data type is immutable (cannot be changed after creation)?

Tuples are immutable — you cannot add or remove elements after creation. Like Java's List.of().

Which of the following Python data types is mutable (can be changed after creation)?