Your First Program!
This is it. You have installed Python and set up your smart notebook (VS Code). Now it is time to write your first line of code and give your computer its first instruction.
1. Create a Python File
- In VS Code, look at your
Python_Learningfolder on the left side. - Click the New File icon (it looks like a piece of paper with a
+symbol). - Name your file
hello.pyand press Enter.
Why the
.py? The.pyat the end is very important! Just like a photo ends in.jpgand music ends in.mp3, putting.pytells your computer: "Hey, this is a Python file!"
2. Write Your Code
Click inside your empty hello.py file and type exactly this:
print("Hello, World!")
What does this line mean?
The print() instruction tells the computer to take whatever is inside the quotation marks "" and show it on the screen. It is your way of making the computer talk to you!
3. Run Your Code
Now we need to tell Python to read our file and run our instruction.
- At the very top of VS Code, click Terminal -> New Terminal. A small black box will appear at the bottom of your screen.
- Click inside that black box, type this command exactly, and press Enter:
python hello.py
🎉 CONGRATULATIONS! 🎉
Look at the black box (terminal) at the bottom. Did it print Hello, World!?
If yes, you just successfully wrote and executed your first piece of software. You are officially a programmer!
- Case Sensitivity:
Print("Hello")with a capitalPwill fail. Python commands are always lowercaseprint(). - Missing Quotes:
print(Hello)will fail. Text must always be wrapped in quotes (""or''). - Unsaved File: If you see a white dot next to
hello.pyin VS Code, pressCtrl + Sto save before running.
4. Notes for Yourself (Comments)
As you start writing more code, you might want to leave notes for yourself or other programmers explaining what the code does. You can do this using Comments.
In Python, anything that comes after a hash symbol # is completely ignored by the computer. It is just text for humans to read!
# This is a comment. The computer will ignore this line.
print("Hello!") # You can also put comments at the end of a line
Quick Summary
- File Extension: Python files must end with the
.pyextension (e.g.,hello.py). print()Function: The built-in command used to output text, numbers, and variables to the screen.- Terminal Execution: Run Python files using
python filename.pyinside the integrated terminal. - Comments (
#): Explanatory notes for humans that Python ignores during execution. - Case Sensitivity: Python commands are strictly lowercase (
print, notPrint).
What's Next?
Now that you know how to give the computer basic instructions, let's learn how code actually runs behind the scenes in the next lesson!