Blog Post #5: The Classic Rite of Passage: Writing “Hello, World!” in Python

The moment has arrived. Over the last three posts (Post #2, Post #3, and Post #4), we have diligently set up our development environments on Windows, macOS, and Linux. All that preparation, all that setup, leads directly to this: writing your very first line of Python code.

In the world of programming, there is a time-honored tradition for anyone learning a new language. The first program you write is one that simply displays the words “Hello, World!” on the screen. It’s a simple task, but it’s a powerful first step that proves your environment is working and that you can make the computer do what you tell it to. Today, you join that tradition.

Meet Your First Python Command: The print() Function

Our tool for this task is the print() function. Think of a function in Python as a named command that tells the computer to perform a specific action. The print() function does exactly what its name suggests: it displays, or prints, whatever you put inside its parentheses to the screen.

The information you give to a function is called an “argument.” When we want to print simple text, that text is our argument. In programming, a piece of text is called a string, and you must always wrap strings in quotation marks. You can use either single quotes (') or double quotes (")—Python doesn’t mind, as long as you’re consistent.

For our rite of passage, our code will be: print("Hello, World!")

Method 1: Using the Python Interactive Interpreter

The quickest way to run a line of Python code is with the interactive interpreter. This is a command-line tool that lets you type Python code and see the results instantly. It’s the perfect place for quick experiments.

First, open your terminal application:

  • On Windows: Open the Start Menu, type cmd or powershell, and press Enter.
  • On macOS/Linux: Open the Terminal app.

Now, start the Python interpreter. The command differs slightly by OS:

  • On Windows: Type python and press Enter.
  • On macOS/Linux: Type python3 and press Enter (if you installed a specific version like in our guides, you could also use python3.12).

You’ll know you’re in the interpreter when you see a prompt that looks like three greater-than signs (>>>). This is Python waiting for your command.

At the prompt, type your code and press Enter:

print("Hello, World!")

The interpreter will immediately execute your command and display the result on the next line:

Hello, World!

Congratulations! You just wrote and executed your first piece of Python code. To exit the interpreter, you can type exit() and press Enter, or press Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows).

Method 2: Writing and Running a Python Script File

The interactive interpreter is great for testing, but real programs are saved in files. This allows you to write many lines of code and run them all at once, anytime you want. Let’s do that now.

Step 1: Create the File

Open a very basic text editor. For now, avoid word processors like Microsoft Word, which add extra formatting.

  • On Windows: Use Notepad.
  • On macOS: Use TextEdit (Important: Go to “Format” -> “Make Plain Text”).
  • If you have one: A code editor like VS Code or Sublime Text is even better.

In your empty text file, type the exact same line of code:

print("Hello, World!")

Step 2: Save the File

This is a critical step. Go to “File” -> “Save As…”. Name your file hello.py.

The .py extension is essential. It is the standard file extension for Python scripts, and it’s how your operating system and the Python interpreter recognize it as a file containing Python code. Save it somewhere easy to find, like your Desktop.

Step 3: Run the Script from the Terminal

Now, go back to your terminal (Command Prompt or Terminal). You need to tell it to look in the directory where you saved your file. Use the cd (change directory) command. For example, if you saved it to your Desktop, you would type:

cd Desktop

Once your terminal is in the correct directory, you can run your script.

  • On Windows: python hello.py
  • On macOS/Linux: python3 hello.py

Press Enter. The exact same output will appear on your screen:

Hello, World!

You’ve now written a proper Python program and successfully executed it!

What’s Next?

You’ve done it. You are now, officially, someone who has written a Python program. That simple “Hello, World!” is a huge milestone.

While our one-line program is simple, it’s built on fundamental rules that govern the language. In Post #6, we will dissect a Python script to understand its core syntax—the grammar of the language, including the critical role of indentation and other rules that make Python code unique.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment