AnyLearn
All lessons
Programmingbeginner

What a Program Is: Values, Variables, and Your First Script

Start from zero: what a program actually is, how to run Python in the shell or as a script, the four basic types, arithmetic expressions, variables as names, strings and f-strings, and why input() always hands you text. You finish by writing a working bill-splitting program.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 10

A program is a list of instructions

A program is text that tells a computer what to do, one instruction at a time. The computer does not guess what you meant, skip the boring parts, or fix your typos. It runs each line in order, from the top of the file to the bottom, and does exactly what each line says.

print("Hello")
print("Goodbye")

Run that and you get Hello on one line, then Goodbye. Swap the two lines and the output swaps too. That sounds trivial, but it is the rule everything else in this course builds on: order is meaning. Later lessons add ways to skip lines (decisions) and repeat lines (loops), but even those follow precise rules about which line runs next.

Definition: A program is a sequence of instructions a computer executes in order. A programming language is the set of rules for writing those instructions so the computer can read them without ambiguity.

Full lesson text

All 10 steps on one page, for reading, reference, and search.

Show

1. A program is a list of instructions

A program is text that tells a computer what to do, one instruction at a time. The computer does not guess what you meant, skip the boring parts, or fix your typos. It runs each line in order, from the top of the file to the bottom, and does exactly what each line says.

print("Hello")
print("Goodbye")

Run that and you get Hello on one line, then Goodbye. Swap the two lines and the output swaps too. That sounds trivial, but it is the rule everything else in this course builds on: order is meaning. Later lessons add ways to skip lines (decisions) and repeat lines (loops), but even those follow precise rules about which line runs next.

Definition: A program is a sequence of instructions a computer executes in order. A programming language is the set of rules for writing those instructions so the computer can read them without ambiguity.

2. Why start with Python

Any language can teach the fundamentals, because variables, decisions, loops and functions exist in nearly all of them. Python is a common first choice for practical reasons: the syntax is short, blocks are marked by indentation rather than braces, and you can type a line and see its result immediately.

It is also what most learners already reach for. In the Stack Overflow Developer Survey 2025, Python was the most used language among respondents who said they were learning to code:

Languages used by people learning to code
Share of learning-to-code respondents (%)02040608071.866.662.8484745.4PythonHTML/CSSJavaScriptCBash/ShellSQL
Source: Stack Overflow Developer Survey 2025, Technology section, learning-to-code respondents

Among all respondents the same survey put Python at 57.9%. The ideas transfer: once you understand a loop in Python, a loop in JavaScript is new spelling, not a new idea.

3. Two ways to run Python

Python comes with an interpreter: a program that reads your Python and carries it out. You can talk to it in two ways.

Interactive shell (REPL)Script file
How you start itType python in a terminalSave code in tip.py, run python tip.py
What you seeA >>> prompt, one line at a timeThe whole file runs top to bottom
Shows resultsAutomatically, after each expressionOnly what you print
Best forTrying one idea, checking a valueAnything you want to keep or rerun

REPL stands for read, evaluate, print, loop, which is exactly what it does. On some systems the command is python3 rather than python; the official Python tutorial covers both.

Gotcha: In the REPL, typing 2 + 3 shows 5. In a script, the same line computes 5 and throws it away silently. If a script prints nothing, check whether you forgot print.

4. Values and their types

Every piece of data in a program is a value, and every value has a type that decides what you can do with it. Four types cover most beginner programs:

TypeExample valuesWhat it is for
int42, -7, 0Whole numbers, counting
float3.5, 0.1, -2.0Numbers with a fractional part
str"hello", '42', ""Text, written inside quotes
boolTrue, FalseYes or no answers

The built-in type() function tells you what you are holding:

print(type(42))     # <class 'int'>
print(type(3.5))    # <class 'float'>
print(type("42"))   # <class 'str'>
print(type(True))   # <class 'bool'>

Notice "42" in quotes is text, not a number. It looks the same on screen, but Python treats it completely differently, which is the source of one of the most common beginner bugs later in this lesson.

5. Expressions: computing new values

An expression is anything Python can reduce to a single value. Arithmetic is the first kind you meet:

OperatorMeaningExampleResult
+ - *Add, subtract, multiply4 * 312
/Divide, always gives a float7 / 23.5
//Divide and round down7 // 23
%Remainder after division7 % 21
**Power2 ** 101024

Precedence follows school maths: ** first, then * / // %, then + -. Use parentheses whenever you are not sure; (2 + 3) * 4 is 20, while 2 + 3 * 4 is 14.

Gotcha: 0.1 + 0.2 prints 0.30000000000000004. Floats are stored in binary, and most decimal fractions cannot be represented exactly. The Python tutorial's appendix on floating point explains why. For display, format the number: round(0.1 + 0.2, 2) gives 0.3.

6. Variables are names, not boxes

A variable is a name that refers to a value. You create one with =, the assignment operator:

score = 0
score = score + 1
score += 1          # shorthand for score = score + 1
print(score)        # 2

Read = as "now refers to", never as "equals". score = score + 1 is nonsense as algebra but perfectly clear as an instruction: compute the right side using the current value, then point the name at the result.

Predict first

What does this print?

a = 5
b = a
a = 10
print(b)

Names can contain letters, digits and underscores, cannot start with a digit, and are case sensitive, so Total and total are different variables.

7. Strings: working with text

Text values are strings, and Python gives you a lot to do with them:

word = "Python"
print(len(word))        # 6, the number of characters
print(word[0])          # P, positions start at 0
print(word[-1])         # n, negative counts from the end
print(word[0:3])        # Pyt, from position 0 up to (not including) 3
print(word + "!" * 3)   # Python!!!
print(f"{word} has {len(word)} letters")   # Python has 6 letters

Two ideas here recur everywhere in programming. First, counting starts at 0: the first character is at position 0, so the last one of a six-letter word is at position 5. Second, ranges exclude their end: word[0:3] gives positions 0, 1 and 2.

The last line is an f-string. Put f before the opening quote and anything inside curly braces is evaluated and inserted. It is the clearest way to build a message out of values, and you will use it constantly.

8. Input is always text

input() shows a prompt, waits for the user to type something and press Enter, and gives back what they typed. It always gives back a string, even if the user typed digits.

print("3" + "4")             # 34, joining two strings
print(int("3") + int("4"))   # 7, adding two numbers
print(str(7) + " days")      # 7 days

So a program that asks for a number must convert it: int(...) for whole numbers, float(...) for decimals. Going the other way, str(...) turns a number into text.

Gotcha: age = input("Age? ") followed by age + 1 crashes with TypeError: can only concatenate str (not "int") to str. Python refuses to guess whether you meant text or arithmetic. The fix is age = int(input("Age? ")), converting at the moment the value enters the program.

If the user types three where you call int(), Python stops with a ValueError. Handling bad input gracefully is a later skill; for now, know where the error comes from.

9. The shape of almost every small program

Input, conversion, computation, output: the tip calculator in the next step follows this path exactly.

flowchart LR
  A["Ask the user: input()"] --> B["Convert text: float() / int()"]
  B --> C["Compute with expressions"]
  C --> D["Store results in variables"]
  D --> E["Show results: print() with f-strings"]

10. Your first real program

Save this as tip.py and run it with python tip.py:

# tip.py: split a restaurant bill
bill = float(input("Bill total: "))
tip_percent = float(input("Tip percent: "))
people = int(input("Number of people: "))

tip = bill * tip_percent / 100
total = bill + tip
each = total / people

print(f"Tip: {tip:.2f}")
print(f"Total: {total:.2f}")
print(f"Each person pays: {each:.2f}")

Typing 84, 15 and 3 gives:

Tip: 12.60
Total: 96.60
Each person pays: 32.20

Every idea from this lesson is here: instructions in order, input converted to numbers, arithmetic expressions, variables naming intermediate results, and f-strings for output. The :.2f inside the braces formats a float with two decimal places. The line starting with # is a comment, ignored by Python and written for humans.

Try it: Add a fourth question asking for a rounding amount, or print how much the tip adds per person. Change one thing, run it, check the output by hand.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. What does `print(7 // 2)` output?
    • 3.5
    • 4
    • 3
    • 1
  2. After `x = 4`, `y = x`, `x = 9`, what is the value of `y`?
    • 4
    • 9
    • 13
    • It raises an error
  3. A script contains only the line `2 + 3`. What appears when you run it with `python script.py`?
    • 5
    • 2 + 3
    • An error
    • Nothing
  4. `n = input("How many? ")` and the user types 5. What is `type(n)`?
    • int
    • str
    • float
    • bool
  5. With `word = "Python"`, what is `word[1:4]`?
    • "Pyth"
    • "yth"
    • "ytho"
    • "Pyt"

Related lessons