Kahibaro
Discord Login Register

Your first Python program

Writing and Running a Tiny Python Program

You’re going to create a real Python program, step by step. It will be very small, but it’s a complete program that the computer can run and that you can change and experiment with.

In this chapter you will:

What a “first program” usually does

The classic first program in many languages is one that prints a short message on the screen. In Python that message is often:

Hello, world!

Your program will do exactly that: make the computer show a message you choose.

The `print()` function

To show text on the screen in Python, you use the built-in print() function.

Basic pattern:

Example:

print("Hello, world!")

Key points specific to print() in this chapter:

Running Python without worrying about setup (for now)

This course will later show you how to install Python on your computer and use different tools. For your very first program, you can:

The important thing right now is seeing Python run, not how it was installed.

Your first program: step by step (interactive way)

  1. Open a Python interpreter (for example, an online one).
  2. Find the place where you can type code (often labeled “Console” or “Shell”).
  3. Type this exactly:
   print("Hello, world!")
  1. Press Enter.

You should see something like:

Hello, world!

You have now written and run a Python program.

Common first-time mistakes

When typing your first program, small typing mistakes are very common. Here are a few and how to fix them:

  1. Missing quotes:
   print(Hello, world!)

This will cause an error. Fix by adding quotes:

   print("Hello, world!")
  1. Only one quote:
   print("Hello, world!)

Fix by closing the quote:

   print("Hello, world!")
  1. Missing parenthesis:
   print "Hello, world!"

In modern Python, this is incorrect. Fix:

   print("Hello, world!")

Changing the message

Now make the program your own. Change the text inside the quotes.

For example:

print("Hello, Python!")
print("My name is Alex.")
print("I am learning to code.")

Each print() call shows one line. When you run this, you’ll see:

Hello, Python!
My name is Alex.
I am learning to code.

Try different variations:

First tiny “experiments” to build intuition

Try these small experiments, one at a time, and see what happens.

  1. No space after the comma:
   print("Hello,world!")
  1. Extra spaces:
   print("   Hello, world!   ")
  1. Empty message:
   print("")
  1. Numbers inside quotes:
   print("12345")

Observe:

A tiny “program” with more than one line

A program can have more than one instruction. For example:

print("Welcome to my first program!")
print("This is line 2.")
print("This is line 3.")

All three lines together are already a program. When run, Python executes them from top to bottom.

Saving your first script (concept only)

Later chapters will show you in detail how to:

For now, just understand that:

Example content of hello.py:

print("Hello from a file!")

You’ll learn how to actually create and run this file in the “Setting Up Python” chapter.

Small practice ideas

Try these in your Python environment:

  1. Write a program that prints your name on one line, and your favorite color on the next.
  2. Write a program that prints a short two-line poem or quote.
  3. Write a program that prints:
   I am learning Python.
   This is fun!

using two print() statements.

These are all real Python programs, just very small ones. From here, you’ll start learning how to make them do more.

Views: 19

Comments

Please login to add a comment.

Don't have an account? Register now!