Lesson 2 of 52First steps
Variables and Types
Store values in names, tell the basic types apart with type(), and read input from the user.
Every program you will ever write stores values and gives them names: a user's email, a cart total, whether someone is logged in. Python's model for this is simple but different from many languages, and getting it right now prevents a whole family of confusing bugs later (and a classic interview question about "pass by reference").
A name is a label, not a box
In many languages you picture a variable as a box that holds a value. In Python, a better picture is a name tag tied to an object. The = sign is called assignment: it makes the name on the left point at the object on the right.
Press Run (⌘ Enter) to see what happens.An object is a value that lives in memory: the number 29, the text "Ada", a list. A name (or variable) is how your code refers to it. Watch what happens when two names get involved.
Python creates an int object 5 and ties the name x to it.
That is why changing x never changes y here. You moved a tag; you did not edit the number 5. (Numbers and strings cannot be edited at all, which you will see in the next lesson.)
What does this print?
a = 10 b = a a = a + 1 print(a, b)
The five basic types
Every object has a type, which decides what you can do with it. These five cover most of what you will write at first:
| Type | What it holds | Examples |
|---|---|---|
int | Whole numbers, any size | 42, -7, 0, 1_000_000 |
float | Numbers with a decimal point | 3.14, -0.5, 2.0 |
str | Text (a "string" of characters) | "hello", 'Ada', "" |
bool | True or false | True, False |
NoneType | "No value here" | None |
None deserves a word. It is Python's way of saying "nothing yet" or "no result", like an empty field on a form. There is exactly one None object, and functions that do not return anything give you None.
You can ask any value for its type with the built-in type() function:
Press Run (⌘ Enter) to see what happens.Notice that "42" (in quotes) is a str, not an int. To Python, the quotes are everything.
What does this print?
print(type(2.0))
"False" (with the quotes)?Dynamic typing: names do not have types
In languages like Java or C#, you declare int age = 29; and age can only ever hold an int. Python is dynamically typed: the object has a type, but the name does not. A name can point at an int now and a string later.
Press Run (⌘ Enter) to see what happens.This is flexible, but reusing one name for different kinds of value makes code hard to follow. In real projects you will often add type hints like age: int = 29. Python does not enforce them when the program runs, but editors and tools like mypy use them to catch mistakes. You will see hints throughout this track and learn them properly later.
Naming rules and PEP 8
Python has hard rules for names, and a style guide on top of them.
The rules (break these and you get a SyntaxError):
- Letters, digits and underscores only. No spaces or dashes.
- Cannot start with a digit:
2nd_placeis invalid,second_placeis fine. - Cannot be a keyword, a word Python reserves for itself, like
if,for,class,None,True. - Case-sensitive:
score,ScoreandSCOREare three different names.
The style comes from PEP 8, Python's official style guide. Tools like ruff check it automatically.
- Variables and functions:
snake_case(lowercase words joined by underscores), likeuser_count. - Constants:
UPPER_SNAKE_CASE, likeMAX_RETRIES = 3. - Classes:
CapWords, likeShoppingCart. - Pick names that say what the value means:
seconds_left, notsordata2.
Python has no real constants. MAX_RETRIES = 3 can be reassigned like anything else. The capital letters are a convention: a promise between programmers that says "do not change this".
None picked yet
Talking to the user: print and input
print() writes values to the screen. Give it several values and it separates them with spaces. Two optional settings change that: sep (what goes between values) and end (what goes at the end, a newline by default).
Press Run (⌘ Enter) to see what happens.input() shows a prompt, waits for the user to type, and always returns a string. Even if they type 29, you get "29", so you convert it with int() or float() before doing maths.
On this site, the browser cannot pause for typing, so input() returns an empty string "". The or trick below gives a fallback when the answer is empty (you will learn exactly how or works in the conditions lesson).
Press Run (⌘ Enter) to see what happens.What does this print?
print("a", "b", sep="")
print("c", end="!")
print("d")7 at n = input(). What does n * 2 give?Age next year
input() always hands you a string, even when the person types a number. Write next_year_age(age_text) that takes an age as text and returns their age next year as an int.
next_year_age("29") → 30
next_year_age(" 41 ") → 42The text always holds a whole number, possibly with spaces around it.
Name that type
Write type_of(value) that returns the name of the value's type as a string.
type_of(42) → "int"
type_of("42") → "str"
type_of(None) → "NoneType"- variable (name)
- A label that points at an object.
=ties the name to the object; it never copies. - object
- A value in memory with a type, like the int
5or the str"Ada". - None
- The single object meaning "no value". Its type is
NoneType. - dynamic typing
- Names have no fixed type; the same name can point at an int, then a str.
- PEP 8
- Python's style guide:
snake_casenames,UPPER_CASEconstants,CapWordsclasses. - input()
- Reads a line the user types and always returns a
str.