Having followed the setup instructions for your operating system, let's ensure Python is correctly installed and usable. We will also write and execute a basic Python program file, often called a script. This exercise verifies your environment and gives you hands-on experience running Python code.
The first step is to confirm that your system recognizes the Python installation.
Open your terminal or command prompt:
Check the Python version: Type the following command and press Enter:
python --version
Depending on your installation and operating system, you might need to use python3
instead:
python3 --version
You should see output similar to this (the specific version numbers may differ):
Python 3.11.4
If you see a version number starting with "3.", your installation is likely correct. If you get an error message like "command not found", revisit the installation steps for your operating system, paying close attention to instructions about adding Python to your system's PATH environment variable.
The Read-Eval-Print Loop (REPL) allows you to type Python commands one at a time and see immediate results. It's a great way to experiment.
Start the REPL: In the same terminal or command prompt, type python
(or python3
) and press Enter:
python
or
python3
Look for the prompt: You should see the Python prompt, which is usually three greater-than signs (>>>
). This indicates Python is waiting for your command.
Enter a simple command: Type 2 + 2
and press Enter:
>>> 2 + 2
Python will evaluate the expression and print the result:
4
>>>
Exit the REPL: Type exit()
and press Enter, or use the keyboard shortcut Ctrl+Z
then Enter (Windows) or Ctrl+D
(macOS/Linux).
>>> exit()
Successfully starting the REPL, executing a basic calculation, and exiting confirms that the Python interpreter is working correctly.
Now, let's move from interactive commands to writing a program in a file.
Open a text editor or IDE: Use any plain text editor (like Notepad on Windows, TextEdit on macOS, gedit on Linux) or a code editor/IDE you might have installed (like VS Code, PyCharm Community Edition, Sublime Text). Avoid word processors like Microsoft Word, as they add formatting that interferes with code.
Write the code: Type the following single line of Python code into your editor:
print("Hello, Python environment!")
The print()
function is a built-in Python command used to display output to the screen.
Save the file: Save the file with a meaningful name and the .py
extension. This extension tells the system (and developers) that it's a Python script.
python_practice
.hello.py
. The .py
extension is important.Navigate to the file's directory: Go back to your terminal or command prompt. Use the cd
(change directory) command to navigate to the folder where you saved hello.py
. For instance, if you saved it on your Desktop:
cd Desktop
cd ~/Desktop
(The ~
usually represents your home directory)python_practice
folder inside your home directory: cd python_practice
(or cd ~/python_practice
on macOS/Linux).Run the script: Execute the script using the Python interpreter. Type python
(or python3
) followed by the filename and press Enter:
python hello.py
or
python3 hello.py
Check the output: You should see the text from your print()
statement displayed in the terminal:
Hello, Python environment!
Troubleshooting Tips:
python: can't open file 'hello.py': [Errno 2] No such file or directory
: This usually means you are not in the same directory as your hello.py
file in the terminal. Use cd
to navigate to the correct directory, or double-check that you spelled the filename correctly.hello.py
. Ensure the parentheses and quotation marks match the example exactly. Even a small typo can prevent the code from running.python
or python3
command not found: If this happens now but worked when checking the version, ensure you haven't closed and reopened a terminal window that doesn't have the PATH configured correctly. Otherwise, revisit the installation/PATH setup steps.Congratulations! You have successfully verified your Python installation, interacted with the REPL, and created and executed your first Python script file. This setup provides the foundation for all the programming concepts we will cover next.
© 2025 ApX Machine Learning