Variables & data types
Dynamic typing, naming rules, core built-in types, None, type conversion and how names reference objects.
Every program works with data: names, prices, dates, yes/no answers. In Python you give data a name with a variable, and every piece of data has a type that decides what you can do with it. This lesson builds the mental model you'll rely on for the rest of the course.
Creating variables#
A variable is created the first time you assign to it with =:
There is no let, var or type declaration. Read lessons = 38 as "make the name lessons refer to the value 38". You can reassign a name at any time, even to a different type:
That's what dynamically typed means: the type belongs to the value, not to the name. It's flexible, but it means you must keep track of what each name holds — good names help a lot.
Naming rules and conventions
- Names can contain letters, digits and underscores, but can't start with a digit:
total_2, not2total. - They are case-sensitive:
scoreandScoreare different. - You can't use keywords like
if,for,class,def,None. - Convention (PEP 8):
snake_casefor variables and functions,UPPER_CASEfor constants,PascalCasefor classes.
Prefer descriptive names (unit_price, retry_count) over cryptic ones (up, x2).
The core built-in types#
You'll get a full lesson on each collection type. For now, use type() to ask any value what it is:
In real code, prefer isinstance() for checks, because it also accepts subclasses and several types at once:
None: the absence of a value#
None is Python's way of saying "nothing yet" or "not applicable". Functions that don't return anything return None, and it's common as a placeholder:
Always compare with is None / is not None rather than == None.
Converting between types#
Values don't change type on their own — Python won't silently turn "5" into 5. You convert explicitly with the type's name:
int("3.7") and int("abc") raise ValueError. When converting user input you'll handle that with try/except (covered in the exceptions lesson).
The last line hints at truthiness: every value can act as a boolean. 0, 0.0, "", [], {}, set() and None are falsy; almost everything else is truthy. You'll use this constantly in if statements.
Multiple assignment and swapping#
Names are labels, not boxes#
This is the most important idea in the lesson. A variable is a label attached to an object, not a box that holds a copy. Assignment attaches another label; it never copies:
With immutable types (int, float, str, bool, tuple, None) this never surprises you, because those objects can't change — any "change" creates a new object:
With mutable types (list, dict, set, most custom objects), several names can see the same change. When you really want an independent copy, ask for one: b = a.copy() or b = list(a).
Common mistakes#
- Mixing text and numbers:
"Age: " + 30is aTypeError. Usestr(30)or, better, an f-string:f"Age: {30}". - Using
=when you mean==:=assigns,==compares.if x = 5:is a syntax error. - Shadowing built-ins: naming a variable
list,str,inputorsumhides the built-in function, and later calls likelist(...)break mysteriously. - Assuming assignment copies: it doesn't — see "Names are labels" above.
What's next#
You know how to name values and inspect their types. Next we'll dig into the first family of values properly: numbers and operators, including the division gotchas every beginner trips over.
Check your understanding
Quick quiz
1.What does
type(3.14)return?2.After
a = [1, 2],b = a,b.append(3), what isa?3.What is the result of
int("3.7")?
Finished reading?
Mark this lesson complete to track your progress.