Your First Program
Every programmer starts by making the computer say something. On this page you will write a Python program in a file, save it and run it, and learn what the print() function does along the way.
Create a file
Open VS Code and open your kuza-python folder (see Installing Python & VS Code if you haven't set it up). Create a new file called hello.py. The .py ending tells your computer and your editor that the file contains Python code.
Type this single line into the file and save it with Ctrl+S (Cmd+S on a Mac):
print("Hello, world!")
Run the program
There are two easy ways to run it.
- In VS Code: click the Run Python File triangle in the top-right corner. A terminal panel opens at the bottom and shows the output.
- From a terminal: move into the folder with
cd, then ask Python to run the file.
cd kuza-python python hello.py # Windows python3 hello.py # macOS and Linux
Congratulations: you have written and run a real program. Every app you will ever build follows the same cycle: write → save → run → check the output.
Understanding print()
print is a built-in function: a named piece of code that does a job for you. You call it by writing its name followed by brackets, and you put the thing you want to show inside the brackets. Text must go inside quotation marks. Text in quotes is called a string.
print("Habari! My name is Zawadi.")
print('I am learning Python.')
print()
print("Today's goal:", "write my first program")
- You can use double quotes
"..."or single quotes'...', as long as both ends match. - An empty
print()prints a blank line. - When you give
print()several items separated by commas, it prints them on one line with a space between each. - Each
print()starts on a new line.
Text vs. numbers
Numbers don't need quotes. Python can work them out before printing them. Watch what changes when you add quotes:
print(2 + 3)
print("2 + 3")
print("Total:", 450 + 120)
Without quotes, 2 + 3 is a calculation. With quotes, it is just text, and Python prints it exactly as written. You will learn much more on the Numbers & Arithmetic and Strings pages.
A slightly bigger program
Programs run from the top line to the bottom line, one instruction at a time. Here is a short "profile card" program you can personalise:
print("==== PROFILE ====")
print("Name:", "Baraka Mwangi")
print("Town:", "Nakuru")
print("Age next year:", 19 + 1)
print("=================")
When things go wrong
Mistakes are normal, and Python's error messages are there to help. The last line of an error tells you what went wrong; the lines above it tell you where.
Python is case-sensitive. Print("Hi") with a capital P fails with a NameError, because Python only knows a function called print in lowercase.
Missing quotes or brackets cause a SyntaxError. For example, print("Hello) is missing its closing quote, and print("Hello" is missing its closing bracket. Python usually points to the exact spot with a ^ marker.
Forgetting to save. If your output doesn't change after you edit the code, you probably ran the old saved version. In VS Code, an unsaved file shows a white dot on its tab.
Key points
- Python programs are saved in files ending in
.py. - Run a file with the VS Code Run button, or with
python file.py/python3 file.pyin a terminal. print()displays values; separate several items with commas.- Text goes in quotes; numbers and calculations don't.
- Read error messages from the bottom line up. They point you to the problem.
Tip: use the ← and → keys to move between pages.
