Alright, you've successfully set up your Python environment and perhaps experimented a bit with the interactive interpreter (REPL). The REPL is great for quick tests and exploring commands, but for anything more substantial, you'll want to write your code in a file, often called a script. This allows you to save your work, run it multiple times, and build more complex applications.
Let's create and run your very first Python script.
A Python script is simply a plain text file containing Python code. By convention, these files have a .py
extension (for example, my_program.py
or data_processor.py
). The Python interpreter reads this file and executes the commands written inside it, from top to bottom.
You can use any plain text editor to write a Python script. Simple editors like Notepad (Windows), TextEdit (macOS), or gedit (Linux) will work. However, as mentioned in the section on IDEs and Code Editors, using a dedicated code editor (like VS Code, Sublime Text, Atom) or an IDE (like PyCharm, Spyder) provides helpful features like syntax highlighting and code completion, which become very useful as your programs grow.
For this first example, let's keep it straightforward.
Open your chosen text editor or IDE.
Create a new file.
Type the following single line of code into the file:
print("Hello, Python!")
Let's break down this line:
print
is a built-in Python function. Functions are reusable blocks of code designed to perform a specific task. The print()
function's task is to display output to the console or terminal window."Hello, Python!"
is called a string. Strings are sequences of characters enclosed in quotes (either single '
or double "
). We are passing this string as an argument to the print()
function, telling it what we want to display.()
after print
are used to call the function and enclose any arguments being passed to it.Now, save the file.
File -> Save
or use the save shortcut (like Ctrl+S or Cmd+S).python_scripts
) is a good choice.hello.py
. Make sure the extension is .py
. Some simple text editors might try to add .txt
automatically; ensure it's saved exactly as hello.py
.You don't run the script by double-clicking the .py
file. Instead, you use the Python interpreter via your command line or terminal.
Open your terminal or command prompt.
cmd
or PowerShell
.Terminal
(usually found in Applications -> Utilities).gnome-terminal
, konsole
).Navigate to the directory where you saved hello.py
. Use the cd
(change directory) command. For example:
cd Desktop
python_scripts
inside your user's home directory: cd python_scripts
(on macOS/Linux) or cd Documents\python_scripts
(adjust path as needed on Windows).ls
(macOS/Linux) or dir
(Windows) to list files in the current directory to confirm hello.py
is there.Run the script. Type the following command and press Enter:
python hello.py
Note: Depending on your installation (especially if you have multiple Python versions), you might need to use python3
instead:
python3 hello.py
Observe the output. You should see the text "Hello, Python!" printed directly below your command in the terminal:
Hello, Python!
When you executed python hello.py
, you instructed the Python interpreter (the python
or python3
program) to:
hello.py
.print()
function, which displayed the specified string to your terminal.Congratulations! You've written and executed your first Python script. This simple "Hello, Python!" program is a traditional first step in learning many programming languages. It confirms your setup is working correctly and introduces the fundamental process of creating and running code from files, which is how most Python applications are developed. You're now ready to learn more about the building blocks of Python programming.
© 2025 ApX Machine Learning