Your First Python Program
Welcome to your very first step into the world of programming! If you’re here, you’re ready to learn Python—a language loved by beginners and professionals…
Welcome to your very first step into the world of programming! If you’re here, you’re ready to learn Python—a language loved by beginners and professionals alike. Don’t worry if you’ve never written a single line of code before. This guide will walk you through writing, saving, and running your first Python program. Let’s get started!
What Is Python?
Python is one of the most popular programming languages in the world. It’s known for being beginner-friendly, which makes it a great place to start your programming journey.
You’ll find Python everywhere: building websites, analyzing data, automating everyday tasks, creating games, and even powering artificial intelligence. One of the reasons people love Python is its simple, readable code. You can do a lot with just a few lines!
As you start learning Python basics, you’re opening the door to many exciting opportunities.
Setting Up Python
Before you write your first Python program, let’s make sure Python is ready to use on your computer.
Step 1: Check if Python Is Installed
Open your command line or terminal:
- Windows: Search for “Command Prompt” or “cmd” in the Start menu.
- Mac: Open “Terminal” from Applications > Utilities.
- Linux: Open your terminal application.
Type this command and press Enter:
python --version
If that doesn’t work, try:
python3 --version
If you see a version number (like Python 3.10.6), you’re all set! If you get an error, you’ll need to install Python.
Quiz Question 1
Question: What should you do if typing python --version doesn't work?
- A) Give up and try another language.
- B) Try typing
python3 --version. - C) Restart your computer.
- D) Skip installing Python.
Step 2: Install Python (If Needed)
Go to python.org/downloads and download the latest version for your operating system. Follow the instructions to install it.
Tip: During installation on Windows, make sure to check the box that says “Add Python to PATH.” This makes it easier to run Python code from the command line.
Quiz Question 2
Question: Do you need to install Python if your computer already shows a version number when you check?
- A) Yes, always install it again.
- B) No, you’re ready to go.
- C) Only if you want to.
- D) It doesn’t matter.
Step 3: Choose a Text Editor
You can write Python code in any text editor. Here are some beginner-friendly options:
- Notepad (Windows) or TextEdit (Mac, in plain text mode)
- VS Code (Download here)
- Sublime Text (Download here)
You can also use the Python interactive shell by typing python or python3 in your terminal, but for your first Python program, let’s use a text editor.
Quiz Question 3
Question: Can you use any text editor to write Python code, or do you need a special program?
- A) Only special Python editors work.
- B) Any text editor is fine.
- C) You must use Notepad.
- D) You can’t use a text editor.
Writing Your First Python Program
Let’s talk about what a program is. In simple terms, a program is a set of instructions you give the computer to do something.
There’s a fun tradition in programming: your first program prints the words “Hello, World!” on the screen. This is called the hello world python example. It’s simple, but it means you’ve written real code!
Open your text editor and type this line:
print("Hello, World!")
Let’s break that down:
printis a command that tells Python to show something on the screen.- The words inside the quotes (
"Hello, World!") are what will be displayed.
Quiz Question 4
Question: What is the purpose of the print command in your first Python program?
- A) It saves your code to a file.
- B) It displays a message on the screen.
- C) It installs Python on your computer.
- D) It checks your Python version.
Saving and Running Your Program
Now it’s time to see your code in action.
Step 1: Save Your File
- In your text editor, click “Save As.”
- Name your file
hello.py(the.pyextension tells your computer this is a Python script). - Choose a folder you can easily find, like your Desktop or Documents.
Quiz Question 5
Question: Which file extension should you use when saving a Python script?
- A) .txt
- B) .py
- C) .exe
- D) .doc
Step 2: Run Python Code
Open your command line or terminal and navigate to the folder where you saved your file. For example, if you saved it on your Desktop:
- Windows:
cd Desktop - Mac/Linux:
cd ~/Desktop
Now, run your program by typing:
python hello.py
Or, if you installed Python as python3:
python3 hello.py
Press Enter. You should see:
Hello, World!
Congratulations! You’ve just run your first Python program.
Quiz Question 6
Question: If you saved your program as hello.py on your Desktop, which command would you use to run it in the terminal?
- A) python hello.py
- B) run hello.py
- C) open hello.py
- D) python install hello.py
Common Mistakes and How to Fix Them
Everyone makes mistakes when learning to code. Here are some common ones and how to fix them:
- Typos: Make sure you typed
printcorrectly, and the quotes are straight ("), not curly (“ ”). - Indentation errors: For now, your code shouldn’t have any spaces before
print. Python is picky about spaces at the start of a line. - File not found: Double-check you’re in the right folder in your terminal.
- Error messages: If you see an error, read it slowly. Often, it will tell you exactly what went wrong. Don’t worry—mistakes are part of learning!
If you get stuck, try retyping your code or searching for the error message. You’re not alone—every programmer has been there.
Quiz Question 7
Question: What should you do if you see an error message after running your Python program?
- A) Ignore it and try again.
- B) Read the message, check for typos, and fix any mistakes.
- C) Restart your computer immediately.
- D) Delete your code and start over.
Next Steps in Your Python Journey
You’ve just completed your first Python program. That’s a big achievement! Take a moment to celebrate.
Next, you’ll learn more Python basics, like how to use variables (to store information) and get input from users. You’ll also find quizzes and practice exercises here on LearnPyFast.com to help you build your skills.
Keep exploring, keep practicing, and remember: every programmer started with “Hello, World!”
Quiz Question 8
Question: Why is “Hello, World!” traditionally used as the first Python program?
- A) It tests if your computer is fast enough.
- B) It’s a simple way to show you can write and run code successfully.
- C) It installs important Python packages.
- D) It’s required before you can write other programs.
Quick Practice
Try changing the message inside the print statement to something else, like your name or a favorite quote. Run your program again and see what happens!
Quiz Answer Key
Question 1
Correct answer: B) Try typing python3 --version.
Explanation: If python --version doesn’t work, try python3 --version—some systems use python3 as the command.
Question 2
Correct answer: B) No, you’re ready to go.
Explanation: If you see a version number, Python is already installed and you don’t need to install it again.
Question 3
Correct answer: B) Any text editor is fine.
Explanation: You can use any text editor to write Python code; you don’t need a special program.
Question 4
Correct answer: B) It displays a message on the screen.
Explanation: The print command tells Python to show text on the screen.
Question 5
Correct answer: B) .py
Explanation: Python scripts must be saved with a .py extension so the computer knows it's a Python file.
Question 6
Correct answer: A) python hello.py
Explanation: You run a Python script by typing python hello.py (or python3 hello.py) in the terminal.
Question 7
Correct answer: B) Read the message, check for typos, and fix any mistakes.
Explanation: Error messages often tell you what went wrong. Checking for typos and fixing mistakes is the best approach.
Question 8
Correct answer: B) It’s a simple way to show you can write and run code successfully.
Explanation: “Hello, World!” is a tradition that helps beginners confirm their setup works and they've written real code.
Congratulations again on running your first Python program! Keep practicing, try changing the message in your script, and explore more tutorials on LearnPyFast.com. You’re on your way to mastering Python basics and building real-world skills. Happy coding!