Your First Python Program
Table of Contents + −
You set up an editor in the last lesson. So now it all comes together. Python is installed, you have an editor, and you know how to run a file. Let’s write your first real program and see it work. It is a small step, but it is the one everybody remembers.
🤔 Why start with “Hello, World”?
Almost every programmer’s first program just prints the words “Hello, World”. It sounds too simple to matter. But there is a good reason behind it.
- It proves your whole setup works. Python, your editor, and the run command all talk to each other correctly.
- It is small, so if something breaks, the problem is easy to find.
- It gives you a quick win, which feels good and keeps you going.
So we are not really learning about the words “Hello, World”. We are checking that your tools work, with the smallest possible program.
There is also a little history here, see. A teacher named Brian Kernighan used “hello, world” in a programming book back in the 1970s. It caught on. So now it is a tradition. When your screen shows those words, you have joined a very long line of people who started the exact same way.
👋 Writing the program
Here is the entire program. One line.
print("Hello, World!")That is a complete, working Python program. Now before we run it, let’s slow down and read it one piece at a time. This single line has four parts, and each one is doing a job.
printis a built-in function. A function is a ready-made action that someone already wrote for you, so you just call it by name. Theprintfunction’s job is to show something on the screen.- The
( )round brackets right afterprintare how you call the function. You put the thing you want printed inside them. Calling means “run this function now”. - The
" "double quotes mark where your text starts and ends. They are like a fence around the words. - The
Hello, World!inside the quotes is the actual text you want to show.
Text wrapped in quotes is called a string. A string is just a piece of text, like a word or a sentence. The quotes are not printed themselves. They only tell Python “everything between us is text, treat it as words, do not try to understand it as code”.
So you read the whole line like this. “Call the print function, and hand it the string Hello, World!, so it shows up on the screen.”
Single quotes work too
Python is happy with single quotes as well. These two lines do the exact same thing.
print("Hello, World!")print('Hello, World!')So "..." and '...' are both fine. Pick one and try to stay with it. The only rule is they must match. If you open with a double quote, you must close with a double quote. Mixing them like print("Hello') will confuse Python.
🏃 Running it step by step
Let’s get it on your screen. Follow along in your editor.
-
Create a file
In your editor, create a new file and name it
hello.py. The.pyending tells everyone, and Python itself, that this is a Python file. -
Type the code
Type this one line into the file:
print("Hello, World!") -
Save the file
Save it. In most editors that is
Ctrl + S(orCmd + Son a Mac). Saving matters, because Python runs the saved version, not what is only on screen. -
Run it
Click the Run button in your editor, or open a terminal in the same folder and run:
Terminal window python hello.py
Output
Hello, World!If you see Hello, World! printed out, congratulations. You just wrote and ran your first Python program.
💻 The other way: the interactive shell
Saving a .py file is how you run a full program. But Python also has a quick scratchpad called the interactive shell. It is a place where you type one line, hit Enter, and see the result right away. No file, no saving. Great for quickly testing a small idea.
To open it, type python in your terminal with nothing after it, and press Enter. You will see three arrows, >>>. That is the shell waiting for you.
Now type your line right after the arrows:
>>> print("Hello, World!")Hello, World!So you typed the line, pressed Enter, and the answer showed up underneath. No run button needed.
- The
>>>is the shell’s prompt. It means “your turn, type something”. You do not type the arrows yourself, they are already there. - Each line runs the moment you press Enter. So it feels like a conversation with Python.
- To leave the shell, type
exit()and press Enter, or pressCtrl + Zthen Enter on Windows (Ctrl + Don Mac and Linux).
The thing is, the shell forgets everything once you close it. So use a saved .py file for real programs you want to keep, and use the shell for quick little tests.
🧱 A few more first programs
One line is great, but let’s write a few more so the print function really sinks in.
Print two lines
Add more lines so your file looks like this:
print("Hello, World!")print("My name is Alex.")print("I am learning Python.")Save and run it again.
Output
Hello, World!My name is Alex.I am learning Python.See how each print shows its text on its own line? Python ran the lines in order, from the top down. That top-to-bottom order is the basic rule of how every Python program runs.
Print your name
Try changing the text to your own name. The point is that whatever you put inside the quotes is what shows up.
print("My name is Riya.")print("Python is fun.")Whatever sits between the quotes, Python prints it exactly as is.
Do some math
Here is a fun one. Numbers do not need quotes. Try this:
print(2 + 3)Output
5So Python did the math and printed 5, not 2 + 3. Why? Because there are no quotes around it. Remember, quotes mean “this is plain text, do not think about it”. No quotes means “this is real code, work it out”. So:
print("2 + 3")prints the text2 + 3, because the quotes make it a string.print(2 + 3)prints5, because without quotes Python treats it as actual math and solves it first.
That one difference, quotes or no quotes, decides whether Python shows something or calculates something. Good to remember early.
💬 Adding a comment
You can also leave notes in your code for yourself. These are called comments. A comment is a note for humans to read, and Python ignores it completely when it runs. A comment starts with a #.
# This is my first programprint("Hello, World!") # this line prints a greetingLet’s see what Python does with each part.
- The first line starts with
#, so the whole line is a comment. Python skips it. - On the second line, the code runs normally, but anything after the
#is a comment. Python runs theprintand ignores the note after it.
So comments are free. They never change what your program does. Use them to explain why you did something, so future-you, or a teammate, understands the code later.
⚠️ Common Mistakes
When you run your first programs, you will hit small errors. That is completely normal. Let’s look at the usual ones, and at the actual error message Python shows, so they stop being scary.
Forgetting the quotes
Text for print must be inside quotes. Without them, Python thinks Hello is the name of something in your code, goes looking for it, and does not find it.
# ❌ Avoid: text with no quotesprint(Hello)
# ✅ Good: text inside quotesprint("Hello")The wrong version gives you this:
Error
NameError: name 'Hello' is not definedSee, Python is saying “you used the name Hello, but I do not know what that is”. Wrap it in quotes and the message goes away.
Forgetting a bracket
Every ( needs a matching ). If you leave the closing one out, like print("Hi", Python reaches the end of the line still waiting for the ) that never came.
# ❌ Avoid: missing the closing bracketprint("Hi"Error
SyntaxError: '(' was never closedThe fix is simple. Add the missing ) at the end: print("Hi").
A couple more to watch for
- Wrong capital letters. Python is case-sensitive. It is
print, notPrintorPRINT. The capital ones give aNameError. - Curly or fancy quotes. Copying from a document can give you “smart quotes”. Python only understands straight quotes,
"and'. If your code looks right but still breaks, check the quotes.
Errors are not failure
When Python shows an error, it is not punishing you. It is telling you exactly which line it could not understand. Read the last line of the error message. It usually points right at the problem.
✅ Best Practices
- Save your file every time before you run it. Python runs the saved version.
- Pick one quote style, single or double, and stay with it so your code looks tidy.
- Use the interactive shell for quick tests, and a saved
.pyfile for real programs you want to keep. - Use comments to explain the tricky parts, so future-you understands the code.
- When an error appears, read it slowly. It is a hint, not a wall.
🧩 What You’ve Learned
You wrote, saved, and ran real Python. Here is the short version.
- ✅
print()is a built-in function that shows text on the screen, and text inside quotes is called a string. - ✅ Single quotes and double quotes both work, as long as they match.
- ✅ You can run code two ways: save a
.pyfile and runpython file.py, or type lines into the interactive shell at the>>>prompt. - ✅ Numbers do not need quotes, so
print(2 + 3)does the math and prints5. - ✅ A comment starts with
#, and Python ignores it when it runs. - ✅ Small errors are normal. The error message points you to the line to fix.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does print("Hello, World!") do?
Why: `print()` displays whatever you put inside the brackets. The text in quotes, a string, gets shown on the screen.
- 2
What will print(2 + 3) show?
Why: There are no quotes, so Python treats it as real math, works out 2 + 3, and prints the result 5.
- 3
Which of these will cause an error?
Why: Text must be inside quotes. `print(Hello)` has no quotes, so Python looks for a name called Hello, does not find it, and raises a NameError.
- 4
What happens to a line that starts with #?
Why: A line starting with # is a comment. It is a note for humans, and Python skips it completely when it runs.
🚀 What’s Next?
You can write and run Python now. Next, let’s learn the basic rules of how Python code is written, like indentation and lines.