Your First Steps in Python

Table of contents
Time to write your first Python program! We'll start with the classic "Hello World" example, learn to use the print() function to display output, and explore different ways to run Python code. Your programming journey begins here.

Now that we understand what programming is—giving clear, step-by-step instructions to computers—let's put this knowledge into practice. It's time to write your first Python program and see how those instructions actually work.

Hello World: The Traditional Beginning

Every journey has its first step, and in our case, that first step is running what might very well be your first Python program.

Let's begin with the classic line:

python
print("Hello, World!")

This single line instructs the computer to display the message Hello, World! on your screen.

print() is something we call a function. We will explore functions in detail later, so do not worry about understanding everything right now.

For now, simply know that this particular function's responsibility is to display output onto your terminal, which we shall often refer to as standard output.

If your screen shows:

bash
Hello, World!

then congratulations — you have officially written and executed your first Python program!

Running Your First Program

There are several ways to run this simple program:

Using a Text Editor and Command Line

  1. Open any text editor (like Notepad, TextEdit, or one from your IDE)
  2. Type the line: print("Hello, World!")
  3. Save the file as hello.py (the .py extension tells the computer this is a Python file)
  4. Open your command line or terminal
  5. Navigate to the folder where you saved the file
  6. Type python hello.py and press Enter

Using Python's Interactive Mode

Python has a special interactive mode where you can type commands and see results immediately:

  1. Open your command line or terminal
  2. Type python or python3 and press Enter
  3. At the >>> prompt, type print("Hello, World!") and press Enter
  4. You should see the message displayed immediately
Important

You'll exit interactive mode by typing exit() or pressing Ctrl+D (on Unix/Mac) or Ctrl+Z followed by Enter (on Windows).

Using an IDE

If you're using an IDE like IDLE (which comes with Python), PyCharm, or VS Code:

  1. Open the IDE
  2. Create a new Python file
  3. Type print("Hello, World!")
  4. Run the program using the IDE's run command (often F5 or a play button)

Let's Experiment a Little

Now that we've written our first program, let's try a few variations to see what happens:

python
print("Hello, Python Beginner!")

This will display a different message.

python
print("Hello from Python!")
print("This is my first program.")

This program has two lines and will display two separate messages.

Discovery Moment

Each print() function typically creates a new line of output.

Let's try something a bit more interesting:

python
print("Hello!")
print("My favorite number is 7.")

You'll see:

bash
Hello!
My favorite number is 7.

You've taken your first step into the world of programming! In the next sections, we'll gradually build on this foundation to explore more of what Python can do.

Discussion (0)

Be the first to comment

Join the discussion

Share your thoughts and engage with the community

Your comment will appear immediately

No comments yet

Be the first to share your thoughts on this article!