Skip to content

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.

13 min read0/6 questions

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.

Python
age = 29print(age) age = 30 # move the tag to a new objectprint(age)
Output
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.

Names point at objects
x = 5
NamesObjects in memoryxint5

Python creates an int object 5 and ties the name x to it.

1/4

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.)

Predict the outputPython

What does this print?

a = 10
b = a
a = a + 1
print(a, b)
Need a nudge? Hints walk you through it.

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:

TypeWhat it holdsExamples
intWhole numbers, any size42, -7, 0, 1_000_000
floatNumbers with a decimal point3.14, -0.5, 2.0
strText (a "string" of characters)"hello", 'Ada', ""
boolTrue or falseTrue, 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:

Python
print(type(42))print(type(3.14))print(type("42"))print(type(True))print(type(None))
Output
Press Run (⌘ Enter) to see what happens.

Notice that "42" (in quotes) is a str, not an int. To Python, the quotes are everything.

Predict the outputPython

What does this print?

print(type(2.0))
Need a nudge? Hints walk you through it.
Quick check
What is the type of "False" (with the quotes)?
Need a nudge? A hint rules out a wrong answer.

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.

Python
thing = 42print(thing, type(thing)) thing = "forty-two"print(thing, type(thing))
Output
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_place is invalid, second_place is fine.
  • Cannot be a keyword, a word Python reserves for itself, like if, for, class, None, True.
  • Case-sensitive: score, Score and SCORE are 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), like user_count.
  • Constants: UPPER_SNAKE_CASE, like MAX_RETRIES = 3.
  • Classes: CapWords, like ShoppingCart.
  • Pick names that say what the value means: seconds_left, not s or data2.

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".

Quick checkPick all that apply
Which of these are valid Python variable names? Pick all that apply.

None picked yet

Need a nudge? A hint rules out a wrong answer.

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).

Python
print("Score:", 42)print("2026", "10", "20", sep="-")print("Loading", end="...")print("done")
Output
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).

Python
name = input("What is your name? ") or "friend"age_text = input("How old are you? ") or "29"age = int(age_text)print("Hi", name)print("Next year you will be", age + 1)
Output
Press Run (⌘ Enter) to see what happens.
Predict the outputPython

What does this print?

print("a", "b", sep="")
print("c", end="!")
print("d")
Need a nudge? Hints walk you through it.
Quick check
A user types 7 at n = input(). What does n * 2 give?
Need a nudge? A hint rules out a wrong answer.
ChallengeEasy

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 ")  →  42

The text always holds a whole number, possibly with spaces around it.

Python
def next_year_age(age_text: str) -> int: # Turn the text into a number first, then add one. pass
ChallengeEasy

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"
Python
def type_of(value) -> str: # type(value) gives you the class. Classes have a __name__. pass
Key terms
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 5 or 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_case names, UPPER_CASE constants, CapWords classes.
input()
Reads a line the user types and always returns a str.