Read this as a study guide instead

Python basics

Variables, types, loops, conditionals: the moving parts of any program.

Code with its results

A notebook-style walk through the idea — every output shown is the real result of the code above it.

Variables, types, and the values a program moves around

Name a few values, check their types, combine them with operators, drop them into text, and read the yes-or-no each one carries.

Real output Every Out block below was produced by running the code above it. You can copy these cells into your own python3 and run them top to bottom to see the same numbers.

In the pipeline, values are counts and settings for a request. Each has a type, and a name refers to a value; type() reports the type.

In [1]
tokens = 80
cost = 0.02
print(tokens, cost)
print(type(tokens), type(cost))
Out [1]
80 0.02
<class 'int'> <class 'float'>

An operator combines values into a new value. What + does depends on the types: it adds numbers but joins strings, and / always gives a float.

In [2]
print(20 + 60)
print(80 / 1000)
print(4 * 20)
print("tool:" + "search")
Out [2]
80
0.08
80
tool:search

An f-string, written with an f before the opening quote, drops each value straight into the text at its placeholder.

In [3]
tool = "search"
tokens = 80
print(f"{tool} used {tokens} tokens")
Out [3]
search used 80 tokens

Every value is truthy or falsy. bool() shows which: an empty string and zero are falsy, other values truthy.

In [4]
print(bool("search"))
print(bool(""))
print(bool(0))
print(bool(80))
Out [4]
True
False
False
True

The same ideas, as prose

These are the exact fragments the model serves — also available as an ordered study guide.

The moving parts of a program

A program is not magic. A running Python program holds values — a number, a piece of text, a plain yes-or-no — and gives them names so it can refer to them again. Everything larger is built by combining those values with a small set of operators and looking at the result. Master the values and the combining, and the rest of the language has something to hold onto.

Two habits make all of this approachable. First, every value has a type that decides what you can do with it, so knowing which type you hold is the first thing to check when a line misbehaves. Second, you can try a single line at a time and see its result immediately, before committing it to a saved program. That tight loop — write a little, see what it does — is how you build trust in the pieces one at a time.

This concept is the vocabulary the whole language rests on: naming values, knowing their types, combining them with operators, dropping them into readable text, and reading the yes-or-no every value quietly carries. None of it is throwaway. The concepts that follow assume you can already do these small things without thinking about them.

A name for a value

price a name refers to 19 a value (int)
The name price refers to the value 19: an assignment binds a name to a value it can stand in for.

A variable is a name that refers to a value. You create one by writing the name, an equals sign, and a value: price = 19 makes the name price refer to the number 19. From then on, writing price anywhere means the value the name currently refers to, and Python substitutes 19 in its place.

The equals sign here is not the equals of arithmetic; it does not claim two things are already equal. It is an instruction — make this name refer to this value like a name tag you can peel off one object and stick on another: the tag is the same, but which thing it points to is up to you. Because it is an instruction and not a fact, you can point the same name at a new value later: price = 24 simply moves the name to a different number, and the old one is forgotten if nothing else refers to it.

Names are how a program stays readable. A value worked out once can be stored under a clear name and used in ten later lines, and a reader sees the name rather than the raw number, which says what the value is for. Choose names that describe the value — price, attempts, user_name — and the code begins to explain itself.

int, float, str, and bool

Every value in Python has a type, and the type decides what the value can do. Four types cover almost everything a first program touches: int, a whole number like 19; float, a number with a decimal point like 1.5; str, a piece of text written in quotes like "hello"; and bool, one of the two truth values True and False.

The type belongs to the value, not to the name that refers to it. 19 is an int wherever it appears, and the difference between 19 and 19.0 is exactly that one is an int and the other a float, even though they sit at the same spot on the number line. You can ask Python a value's type directly with type(19), which reports int — a useful move when a result surprises you and you want to know what you are actually holding.

Types matter because they change what an operation means and whether it is allowed at all. Two int values added give their sum; wrapping a number in quotes makes it a str, and text and numbers do not mix without a deliberate conversion such as str(19) or int("19"). Checking which type you hold is the first thing to do when a line does not behave the way you expected.

Combining values

5 + 2 two ints: add 7 "5" + "2" two strings: join "52" the same operator, read against different types
The plus operator adds two numbers but joins two strings: an operator's meaning comes from the types of its operands.

An operator combines values into a new value. The arithmetic ones read as you would expect: + adds, - subtracts, * multiplies, and ** raises to a power, so 2 ** 3 is 8. Division has two forms — / always produces a float, so 3 / 4 is 0.75, while // divides and drops the fraction and % gives the remainder left over.

Comparison operators ask a question and answer it with a bool. 3 < 4 evaluates to True, 3 == 3 to True, and 3 == 4 to False. The double equals == tests whether two values are equal, and it is a completely different thing from the single = that binds a name to a value — confusing the two is one of the first mistakes everyone makes, and worth catching early.

What an operator does depends on the types it is given. + between two numbers adds them, but + between two strings joins them end to end, so "py" + "thon" is "python". The same symbol, read against different types, means different things — which is why keeping track of your types pays off the moment you start combining values.

Expressions and the REPL

read you type a line eval compute its value print show the value expression value next line
The REPL loops: it reads an expression you type, evaluates it to a value, prints that value, then waits for the next line.

An expression is any piece of code that evaluates to a value. 19, price, 3 + 4, and "py" + "thon" are all expressions, because Python can reduce each one to a single value. Most lines you write are expressions or contain them, and running code is, at bottom, Python evaluating expressions down to the values they stand for.

Python ships with an interactive prompt — the REPL, short for read-eval-print loop. It reads a line you type, evaluates the expression, prints the resulting value, and loops back for the next one. Type 3 + 4, press enter, and it shows 7 at once, with no ceremony. That instant feedback makes the REPL the fastest place to check what a single line actually does before it goes into a program.

A saved program does not print on its own. When Python runs a script from top to bottom, it evaluates each expression but shows you nothing unless you ask, and asking is what print is for: print(3 + 4) evaluates the expression and writes 7 to the screen. The REPL echoing a value and print displaying one look alike at the prompt, but only print produces output in a script you run later — a distinction that saves confusion the first time a program runs silently.

Values inside text

Often you want a line of text with a value dropped into it. An f-string does this directly: write the letter f immediately before the opening quote, and inside the string a name wrapped in curly braces is replaced by the value it refers to. With name referring to "Ada" and score to 91, the f-string f"{name} scored {score}" becomes the finished text Ada scored 91.

The f prefix is what switches on the substitution; without it, "{name}" is just those literal characters, braces and all. Anything not inside braces is copied through unchanged, so the surrounding words, spaces, and punctuation appear exactly as written. You can drop in as many values as you like, each in its own pair of braces.

f-strings are the ordinary way to build readable output and messages. Rather than gluing text and numbers together with + and converting types by hand, you write the sentence the way you want it read and let each pair of braces pull in its value. The intent of the line stays visible instead of being buried in concatenation.

Every value carries a yes or no

Beyond its own value, every Python value also counts as either truthy or falsy — a built-in yes-or-no that Python reads whenever a value is used as a condition. Most values are truthy. The falsy ones are a short, learnable list: False itself, the number 0 and its float 0.0, an empty string "", and the special value None. Everything else — any non-zero number, any string with characters in it — is truthy.

You can see the verdict directly by asking bool for it: bool("Ada") is True, bool("") is False, and bool(0) is False. Reading a value as truthy or falsy is like flipping a switch and seeing which way it points like reading which way a light switch points: a single glance gives a clean on-or-off without changing the switch itself — the value itself is untouched, but Python now has a clean yes or no to act on.

This yes-or-no is the raw material decisions are built from later, so it is worth knowing which values are falsy before you lean on them. The usual surprise is the empty string and the number zero: both are perfectly good values, yet both count as no. Knowing that an empty piece of text reads as falsy quietly explains a great deal of the behavior you are about to meet.

Where this leads

These few moving parts — values with types, names that refer to them, operators that combine them, text that displays them, and the truth value each one carries — are the entire raw material of programming. Everything more advanced is a way of organizing these parts, not a replacement for them. A large program is still, at the bottom, values being named, combined, and shown.

The concepts that come next each take one of these parts and give it more reach. Grouping many values under one name, wrapping a computation so it can be reused, checking that code does what it claims — every one of them assumes you can already name a value, know its type, and read a result. Time spent here is not throwaway beginner material; it is the ground the rest of the language stands on.

It is also enough to build something with right away. A page that greets a visitor by name, a tool that totals a team's hours, a car that reacts to a distance reading, a script that counts an agent's steps — each is, underneath, variables holding values, operators combining them, and f-strings turning the result into something a person can read. Learn to see programs as those pieces and the larger builds stop looking like magic.