Code Bytes · "Intro to Programming" · Episode 1 · What code is
What a program is
A program is a set of instructions saved in a file, ready to run. Like a recipe card, nothing happens until someone follows it. When you run it, the computer follows the file from top to bottom, exactly as written, and gives the same result every time.
Instructions, saved in a file
A program is a list of instructions for a computer, saved in a file so it can be run whenever you like. Small programs can be a single file; big apps are many files working together.
It's like a recipe card in a drawer: the card holds every step, but nothing happens until someone takes it out and follows it. A program sits in its file doing nothing until you run it.
A tiny program in Python
Here is a whole program, two lines long, saved in a file called program.py:
``python print("Total:") print(3 + 4) ``
- Line 1 has quotes, so Python prints the text exactly as written:
Total: - Line 2 has no quotes, so Python works out
3 + 4first and prints the result.
Press run, and Python follows the file from top to bottom:
`` Total: 7 ``
It follows it exactly
The computer doesn't guess what you meant. Change line 1 to a typo:
``python print("Totl:") print(3 + 4) ``
and it prints the typo, just as written:
`` Totl: 7 ``
Same file, same result
Run that file again tomorrow, or a million times, and you get the same output. The computer never gets bored or sloppy. That's what makes programs so useful: you write the steps once, and they are carried out the same way every time.
Try it yourself
Open any free online Python editor (search "online Python"), make a new file and run:
``python print("Score:") print(2 + 5) print("2 + 5") ``
You should see:
`` Score: 7 2 + 5 ``
The last line has quotes, so Python prints the text 2 + 5 instead of working it out. Now swap the order of the lines and run it again: the output follows the new order, top to bottom.
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- "Top to bottom" is true for simple code like this. Loops, if statements and functions change the order, and later lessons cover them.
- "It never fixes what you meant" means the computer won't correct spelling inside text or mistakes in your logic. If Python can't understand a line at all, it stops with an error message instead (a later lesson covers errors).
- "Same result every time" is true for this program. Programs that use the clock, random numbers or what a user types can give different results on different runs.
Answer to the comment question
What does print(10 - 4) show on the screen? 6. There are no quotes, so Python works out 10 - 4 first and prints the result, 6. With quotes, print("10 - 4") would print the text 10 - 4 instead.
Test your knowledge
Quick quiz: What a program is
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "An Informal Introduction to Python": first steps towards programming, statements run in order
- Python Software Foundation, "Using the Python Interpreter": running a script file
- Python Software Foundation, "Execution model": how a program's code is executed
- Python Software Foundation, "print()" built-in function
Video transcript
What actually is a program?
A program is a set of instructions saved in a file, ready to run. Like a recipe card: nothing happens until someone follows it.
Here's a tiny one in Python. The first line prints the word Total. The second line has no quotes, so Python works out three plus four.
Press run, and Python follows the file from top to bottom. Total, then seven.
And it follows it exactly. Spell a word wrong, and it prints it wrong. It never fixes what you meant.
Run it again tomorrow, or a million times, and you get the same result. The computer never gets bored or sloppy.
What does this program print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP3Your first line of code
- "Intro to Programming" EP4Variables
- "Intro to Programming" EP5Data types
- "Intro to Programming" EP6Strings