Your First Python Script: "Hello, World!" (Part 1)
Following our exploration of Setting Up Your Development Environment (Part 2): Configuring Your IDE and Virtual Environments, it's time to write and run your very first Python script. In this article, we'll cover the classic "Hello, World!" program and how to run it from the command line.
📚 Prerequisites
A working Python installation and access to a command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ How to write a simple "Hello, World!" script in Python.
- ✅ How to save a Python script.
- ✅ How to run a Python script from the command line.
- ✅ The basics of the
print()function.
🧠 Section 1: Writing Your "Hello, World!" Script
The "Hello, World!" program is a tradition in programming. It's a simple program that prints "Hello, World!" to the screen. In Python, it's just one line of code.
- Open a text editor: You can use any plain text editor, like Notepad on Windows, TextEdit on macOS, or any code editor like VS Code or PyCharm.
- Write the code: Type the following line of code into your text editor:
print("Hello, World!")
💻 Section 2: Saving Your Python Script
Now, you need to save the file with a .py extension. This extension tells your computer that it's a Python file.
- Go to "File" > "Save As".
- Name your file
hello.py. - Make sure the file type is set to "All Files" or "Plain Text" to avoid any unwanted extensions.
- Save the file to a location you can easily access, like your Desktop or a dedicated "python_scripts" folder.
🛠️ Section 3: Running Your Script from the Command Line
Now for the exciting part! Let's run your script.
-
Open your command line:
- Windows: Open the Command Prompt or PowerShell.
- macOS/Linux: Open the Terminal.
-
Navigate to the directory where you saved your file: Use the
cd(change directory) command. For example, if you saved the file on your Desktop, you would type:cd Desktop -
Run the script: Type the following command and press Enter:
python3 hello.py(On Windows, you might need to use
python hello.py)
You should see the following output on your screen:
Hello, World!
🔬 Section 4: The print() Function
The print() function is one of the most basic and useful functions in Python. It simply prints the specified message to the screen.
- The text inside the parentheses is called an argument.
- The text is enclosed in double quotes (
") to indicate that it's a string (a sequence of characters).
We'll explore the print() function in more detail in a future article.
💡 Conclusion & Key Takeaways
Congratulations! You've written and run your first Python script. You're officially a Python programmer!
Let's summarize the key takeaways:
print(): The function used to display output..pyextension: The standard extension for Python files.python3command: The command used to run Python scripts from the command line.
Challenge Yourself:
Modify your hello.py script to print your name instead of "Hello, World!".
➡️ Next Steps
In the next article, we'll continue our exploration of the "Hello, World!" program in "Your First Python Script: "Hello, World!" (Part 2): Understanding basic script structure and the print() function."
Happy coding!
Glossary (Python Terms)
- String: A sequence of characters.
- Argument: A value passed to a function.