Your First Python Program: Write a Simple "Hello, World!" Program
Writing a "Hello, World!" program is one of the first steps in learning any new programming language, and Python is no exception. This simple exercise will help you understand how to write and run your first Python script. Let’s go step by step.
Step 1: Setting Up Your Python Environment
Before you begin writing your Python code, ensure that Python is correctly installed on your system. Follow the installation guide from the previous post to set up Python and an IDE (like VS Code or PyCharm).
Step 2: Writing the "Hello, World!" Program
Once you have your Python environment set up, follow these steps:
- Open Your IDE (VS Code, PyCharm, or IDLE).
- Create a New Python File:
- In your IDE, open a new file and save it as
hello_world.py
. The.py
extension indicates that this is a Python script.
- In your IDE, open a new file and save it as
- Write the Code: Type the following code in the file:
Step 3: Running the Program
To run your program, follow these steps depending on your IDE:
In VS Code:
- Open the terminal (
Ctrl + ~
orCmd + ~
). - Run the script by typing:
- You should see the output:
- Open the terminal (
In PyCharm:
- Right-click on the
hello_world.py
file. - Select Run 'hello_world'.
- The output will appear in the built-in terminal at the bottom:
- Right-click on the
In IDLE:
- Open the
hello_world.py
script in IDLE. - Press
F5
or select Run > Run Module. - The output will appear in the IDLE shell:
- Open the
Step 4: Understanding the Code
The Python code print("Hello, World!")
does the following:
print()
is a built-in Python function that outputs the text inside the parentheses to the screen.- The
"Hello, World!"
is a string—text surrounded by quotes—and is passed as an argument to theprint()
function.
This simple program is the foundation of understanding how to write Python code and see immediate results. It helps familiarize you with the Python syntax and the basic structure of a Python program.
Step 5: Experimenting with Your Code
Now that you’ve written your first Python program, try modifying the string inside the print()
function. For example:
When you run it again, the output will be:
Feel free to explore other variations like:
Conclusion: You’re on Your Way!
Congratulations! You’ve just written and run your first Python program. This is a fundamental step that will help you as you learn more complex programming concepts. From here, you can start experimenting with more Python functions, variables, loops, and more.
No comments: