Skip to chapter content
navan✳THE BUILD LABPYTHON / GUIDE
0/26 read
CHAPTER 01 / 26 · FOUNDATIONS

Python basics.

Run a program, name a value, and understand what each line asks Python to do.

BY NAVAN SAMI5 MIN READTHEORY FIRST
01 / THE BASICS

Start here.

A computer follows instructions; a program is a written sequence of those instructions. Python is a language for expressing them. The text you write is source code, and the Python interpreter reads it and carries out its meaning. In a simple file, Python starts at the top and works downward. It does not know the goal you had in mind, so the order and spelling of each instruction matter. A line ending in .py is a Python source file, not a special kind of data that already contains an answer.

A value is a piece of data. In the example, 12 and 3 are numbers. A variable is a name that refers to a value. The line price = 12 gives the name price the integer value 12; the equals sign is assignment, not a permanent mathematical equality. When Python later reads price * quantity, it looks up the current values of both names, multiplies them, and produces a new number. Assigning that result to total makes it available to later lines. Names are case-sensitive, so Total and total would be different names.

A function is a named operation you can call. print(total) calls Python's print function and passes total as an argument inside parentheses. It displays a value but does not create it. Text goes inside quotes, as in 'Total: '. An f-string begins with f and inserts the value of an expression inside braces: f'Total: {total}' produces the text 'Total: 36'. The hash sign begins a comment that explains code to a reader; Python ignores the rest of that line. These small pieces recur throughout the course, so the first example traces each one explicitly.

02 / THE IDEA

Build the model.

A program is a sequence of instructions, but reading it as a sequence of changing values is more useful than memorising punctuation. Python evaluates an expression, gives its result to a name when you assign it, and carries on to the next statement. A small script can therefore turn input into a result without any special framework.

A file ending in .py contains Python source code. Running it with python or python3 asks the interpreter to execute its statements in order. The interpreter reports either output or an error with a line number. An error describes where Python could no longer carry out the requested instruction; its message helps identify the value or rule involved.

03 / UNDER THE SURFACE

How it works.

Values have types: 4 is an integer, 4.0 is a floating-point number, and '4' is text. Python chooses operations based on those types. Addition can add two numbers or join two strings, but adding a string to a number is an error until you convert intentionally. Use int() or float() when numeric input arrives as text, and check that conversion can fail.

Assignment binds a name to a value; it does not print anything. print() writes a display representation to the terminal. A comment beginning with # helps a reader but is ignored during execution. A simple name such as total or customer_name should explain the role of the value, so a later line remains readable without tracing every earlier calculation.

04 / A WORKED EXAMPLE

Read it line by line.

example.py
price = 12
quantity = 3
total = price * quantity
print(f'Total: {total}')

Python binds 12 to price and 3 to quantity. It reads both names, multiplies their integer values, and binds 36 to total. The f-string converts the value of total into text inside the label, and print writes 'Total: 36'. If quantity were 4, the multiplication would produce 48 and the displayed text would change accordingly.

05 / ANOTHER EXAMPLE

See the rule again.

example-2.py
price = 7
quantity = 4
subtotal = price * quantity
print(subtotal)

The output is 28. Each binding names a useful part of the calculation. Keeping the arithmetic in subtotal makes it easy to add tax or a discount later without hiding the original price.

06 / REMEMBER THIS

A mistake to avoid.

input() always returns text. If you read a quantity with input(), convert it before multiplying or adding, and handle an invalid entry rather than assuming every visitor types digits.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗Python · Understand the idea, then write the code.