DEV Community

Cover image for Getting Started with Python: Writing Your First Program
≀Paulo Portela
≀Paulo Portela

Posted on • Updated on

Getting Started with Python: Writing Your First Program

Introduction

Welcome to the exciting world of Python programming! This article will guide you through creating your very first program, the classic "Hello, world!". Through this simple example, you'll gain fundamental knowledge of Python's syntax and execution, paving the way for further exploration in the language.

Index

  • Setting Up
  • Writing the "Hello, World" Program
  • Running the Program
  • Conclusion

Setting Up

Before diving into code, ensure you have Python installed on your system. You can download it from the official website here.

Once installed, you can use either the command line or an Integrated Development Environment (IDE) to write and run your Python code. This article covers both methods:

Writing the "Hello, World" Program

Here's the code for your first Python program:

print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, world!
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The print() function is used to display output on the console.
  • In this case, the string "Hello, world!" is passed as an argument to the print() function, instructing it to print that message.

Running the Program

Executing the script from the command line

  1. Save the code in a file with a .py extension (e.g., hello_world.py).
  2. Open a terminal or command prompt and navigate to the directory where you saved the file.
  3. Use the python (or python3 depending on the Operating System) command followed by the filename to execute the script:
python hello_world.py
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, world!
Enter fullscreen mode Exit fullscreen mode

Executing the script using IDLE

  1. Open IDLE, which is usually included with Python installations.
  2. In the IDLE window, either:
    • Click on File > New Window to create a new file.
    • Click on File > Open to open the hello_world.py file you created earlier.
  3. Paste or type the code into the editor window.
  4. To run the program, you have two options:
    • Click on Run > Run Module (or press F5).
    • Click on the Run button in the toolbar (a green arrow icon).

Output:

The "Hello, world!" message will appear in the shell window at the bottom of the IDLE window.

Conclusion

This article provided a gentle introduction to Python programming through the "Hello, world!" program. You learned the basics of writing and running Python code using both the command line and IDLE, equipping you with the foundation for your future programming endeavors. As you progress, you'll encounter more complex concepts and functionalities, but remember that every journey begins with a single step. So, keep practicing, exploring, and creating with Python!

Top comments (0)