In this chapter, we run our first Python program, Hello World. Before that, we need to make sure our system is ready.
Set Up Your Python Programming Environment
First, we check if Python is already installed. If not, we install the latest version. After that, we install a text editor to write and manage our Python programs.
A text editor is very important. It highlights the Python code you write and makes it easier to understand the structure of your program. This helps you avoid mistakes and makes your code more readable and clean.
Python on Different Systems
Python works slightly differently on Windows, macOS, and Linux. So, we need to keep some points in mind for each operating system. In the next steps, I will guide you to check if Python is installed and working correctly on your system.
Python Versions
Programming languages keep improving over time, and Python is no different. Developers release new versions to make it more powerful and easy to use.
Right now, the latest version is Python 3.11. But do not worry. Everything in this course will run perfectly fine on Python 3.9 or later.
We first check if Python is already installed. If your version is old, you can update it to the latest one.
Running Small Python Snippets
Python has an interpreter. This allows you to write and test small pieces of code directly in the terminal. You do not need to save a file for this.
For example, open your terminal and type:
>>> print("Hello World!")
Hello World!
The >>>
symbol is called the Python prompt. It tells you that Python is ready to accept code. You type your code, press ENTER, and see the output right away.
In this course, I will sometimes use these snippets to explain new ideas. This is quick and helps you understand concepts without writing a full program.
But most of the time, you will write your code in a text editor, save it, and then run the program. That is how you will create your first Hello World! program.
Why Hello World?
There is an old tradition in programming. The first program in any language usually prints Hello World! on the screen. It looks simple, but it is very important.
If you can run Hello World successfully, it means your Python setup is correct. After that, you can write any Python program without issues.
About VS Code Editor
Now let’s talk about the tool you will use to write your Python programs. One of the best options is Visual Studio Code (VS Code).
VS Code is:
- Free to use.
- Beginner-friendly.
- Works on Windows, macOS, and Linux.
Supports many programming languages, including Python.
It is powerful enough for big projects but still easy for beginners. If you get comfortable with it now, you can continue using it when you work on larger projects in the future. In this chapter, I will guide you step by step to install VS Code on your computer.
Python on Different Operating Systems
Python is a cross-platform language. This means it works on almost every modern operating system, such as Windows, macOS, and Linux. If Python is installed, any program you write will run on that system without problems.
But here’s the key point: the steps to set up Python are a little different for each operating system.
In this section, we make sure Python is ready on your computer. First, we check if you already have a recent version. If not, we install it. Then, we install Visual Studio Code (VS Code) as the text editor. These are the only two steps that change depending on your operating system.
Once this setup is done, we run our first program, Hello World. If something goes wrong, we troubleshoot it together. I will walk you through the process step by step for each operating system. This way, you get a Python environment that works reliably every time.
Python on Windows
Most Windows computers do not come with Python pre-installed. So, in most cases, you will need to install Python and then install VS Code as well.
Installing Python
First, let’s check if Python is already installed on your Windows system.
- Open the Start menu.
- Type command into the search bar.
- Click the Command Prompt app.
A black terminal window opens. In that window, type the word:
python
- If Python is installed, you will see the Python prompt:
>>>
This means your system is ready to run Python commands.
- If you see an error message like:
'python' is not recognized as an internal or external command
or if the Microsoft Store opens automatically, then Python is not installed on your computer.
Note: If the Microsoft Store opens, close it immediately. It is better to install Python directly from the official website instead of using Microsoft’s version.
Downloading and Installing Python
If Python is not installed, or if you have a version earlier than Python 3.9, you must install a newer version.
- Open your web browser and go to the official Python website: https://python.org
- A big button will appear with the latest version of Python. Click that button.
- The website will automatically detect that you are using Windows and give you the correct installer for your system.
- When the installer finishes downloading, run the file.
- On the first installation screen, make sure you check the box “Add Python to PATH.”
- This step is very important. It allows you to run Python easily from the command line.
- Continue with the installation until it finishes.
Running Python in a Terminal Session
Now that Python is installed, let’s test it in the terminal.
- Open a new Command Prompt window.
- Type python in lowercase and press ENTER.
If everything is set up correctly, you will see the Python prompt:
If everything is set up correctly, you will see the Python prompt:
C:\> python
Python 3.x.x (main, Jun ... , 13:29:14) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
symbol tells you that Python is running and ready to accept commands.
Running Your First Line of Code
At the Python prompt, type this line:
>>> print("Hello Python interpreter!")
Hello Python interpreter!
>>>
You should see the message Hello Python interpreter! printed on the screen.
This confirms that Python is working on your system.
Closing the Python Session
When you are done, you need to exit the Python prompt. You can do this in two ways:
- Press CTRL + Z and then press ENTER
- Or type:
exit()
After this, you return to the normal command window.
Now you know how to open Python, run a simple command, and close the session. This skill will help you test code quickly as you continue learning.
Python on macOS
The latest versions of macOS do not include Python by default. So, if you have a new Mac, you need to install Python yourself. In this section, we install the latest version of Python, then set up Visual Studio Code (VS Code), and make sure it works correctly.
Note: Older versions of macOS used to include Python 2, but that version is outdated and should not be used. Always use Python 3 for your projects.
Checking Whether Python 3 Is Installed
- Open the Terminal.
- Go to Applications > Utilities > Terminal, or
- Press ⌘ + Spacebar, type terminal, and press ENTER.
- In the terminal window, type:
python3
- In many cases, you will see a message asking you to install the command line developer tools.
- Do not install them yet. It is better to install Python first. If this message appears, close the pop-up window.
- If your terminal shows Python 3.9 or later, then you already have the right version installed. You can skip the installation step and move to Running Python in a Terminal Session.
- If your version is older than Python 3.9, or if no version is found, then you need to install the latest Python release.
Important Note About macOS Commands
On macOS, you should always use the command python3 instead of python.
- The command python may point to an outdated version that is only used by the system itself.
- On some Macs, typing python shows nothing and only gives an error.
So, whenever you see python in this book, replace it with python3 on your Mac.
Installing the Latest Version of Python
- Open your web browser and go to python.org.
- Hover over the Downloads link.
- You will see a button for the latest version of Python. Click it.
- The website will automatically give you the correct installer for macOS.
- After the file downloads, open it and run the installer.
When the installer finishes, a Finder window will open. In that window, double-click on the file named:
Install Certificates.command
Running this file is very important. It makes it easier to install extra Python libraries later. You will need these libraries for real-world projects, especially in the second half of this course.
At this stage, Python 3 is installed on your Mac, and you are ready to set up VS Code next.
Running a Hello World Program
Now that you have Python and VS Code installed, you are ready to write your first program. But before you do that, there is one more important step. You need to install the Python extension for VS Code.
Installing the Python Extension for VS Code
VS Code supports many programming languages. To use it properly for Python, you must install the Python extension. This extension gives you extra features like syntax highlighting, code completion, and the ability to run your programs inside VS Code.
Here’s how to install it:
- Open VS Code.
- Click the Manage icon in the bottom-left corner. It looks like a gear.
- In the menu that appears, click Extensions.
- In the search box, type python.
- Find the extension named Python that is published by Microsoft.
- Click Install.
During installation, VS Code might ask you to install extra tools. Let it complete the setup.
- If you see a message saying you need to install Python, but you have already installed it, you can safely ignore this warning.
- On macOS, you might also get a pop-up asking to install the command-line developer tools. Click Install. Sometimes, macOS shows that it will take a very long time, but usually it finishes within 10–20 minutes on a normal internet connection.
After this step, VS Code is ready for Python programming.
Writing and Running hello_world.py
Before writing the first program, let’s create a folder to store your Python projects.
- On your desktop, make a new folder called python_work.
- Use all lowercase letters.
- If you need a space, use an underscore (
_
). - Example:
python_work
This naming style is important because Python uses it in many places. It also makes your work look professional.
- Open VS Code.
- Close the Get Started tab if it is still open.
- Create a new file:
- Click File > New File, or
- Press CTRL + N (on Windows) or ⌘ + N (on macOS).
- Save the file inside the
python_work
folder with the name:
hello_world.py
The .py
extension tells VS Code that this is a Python file. VS Code will then highlight your code and know how to run it.
- Inside the file, type this line:
print("Hello Python world!")
Running the Program
To run your program:
- In VS Code, go to the menu and select Run > Run Without Debugging, or
- Press CTRL + F5 (Windows) or ⌘ + F5 (macOS).
At the bottom of the VS Code window, a terminal screen will open. You should see:
Hello Python world!
You might also see some extra output showing which Python interpreter VS Code used. That is normal.
Troubleshooting
Sometimes your program may not run as expected. Don’t worry, this happens to every beginner (and even experts!). If your hello_world.py file is not working, here are some ways you can fix the problem:
1. Read the Error Message (Traceback)
When Python finds an error in your program, it shows a traceback. A traceback is basically a report that tells you where the problem is. Read it carefully. It may point to the exact line and error, such as missing parentheses or a misspelled word.
2. Take a Short Break
If you can’t figure out the problem right away, step away from the computer for a few minutes. Sometimes a small break helps you notice mistakes you didn’t see before. Remember, in programming even a tiny issue like a missing quotation mark or bracket can stop your program from running.
3. Start Fresh
If things still don’t work, delete your hello_world.py file and create it again from scratch. Often, rewriting the code helps you catch mistakes. Don’t worry, you don’t need to uninstall Python or VS Code for this step.
4. Don’t Feel Shy Asking for Help
Never think that you are bothering others. Every programmer even professionals has been stuck before. The Python community is known for being friendly and welcoming to beginners. Just explain clearly:
- What you are trying to do
- What steps you already tried
- What results or errors you are seeing
People will be able to help you quickly with this information.
Check out our Python Cheat Sheet for quick revision.
Also Read: