Skip to content

First Python Program

Writing Your First Python Program

Let’s create and run your first Python program. This guide will help you write a simple script that prints “Hello, World!”—a classic way to start programming in any language.


Step 1: Write the Program

Option 1: Using a Text Editor

  1. Open any text editor, such as:
    1. Notepad (Windows)
    1. TextEdit (macOS, in plain text mode)
    1. nano or vim (Linux)
  2. Type the following code:

print(“Hello, World!”)

  • Save the file with a .py extension, such as hello.py.

Option 2: Using an IDE or Code Editor

  1. Open an IDE (e.g., PyCharm, VS Code) or Python’s built-in IDLE.
  2. Create a new file and enter:

print(“Hello, World!”)

  • Save the file as hello.py.

Step 2: Run the Program

Method 1: Command Line or Terminal

  1. Open a terminal (macOS/Linux) or Command Prompt (Windows).
  2. Navigate to the directory where you saved the hello.py file:

cd path/to/your/file

  • Run the script by typing:

python hello.py

or for Python 3:

python3 hello.py

Output:

Hello, World!


Method 2: Using Python IDLE

  1. Open IDLE.
  2. Go to File > Open, then select hello.py.
  3. In the editor window, press F5 or go to Run > Run Module.

Output in IDLE:

Hello, World!


Method 3: Using an Online Interpreter

  1. Visit an online Python compiler, such as:
    1. Replit
    1. Google Colab
  2. Paste the code:

print(“Hello, World!”)

  • Click the Run button.

Explaining the Code

  1. The print() Function:
    1. The print() function displays the text or data inside the parentheses on the screen.
    1. In this case, “Hello, World!” is the string that gets printed.
  2. Double or Single Quotes:
    1. Python allows both single (‘) and double (“) quotes for strings:

print(‘Hello, World!’)


Experiment with Modifications

Try changing the message or adding new lines to expand your program:

print(“Welcome to Python programming!”)

print(“Let’s learn and explore together.”)

Output:

Welcome to Python programming!

Let’s learn and explore together.