Writing First Python Program

Time to write some code. This is the foundation. Don't mess it up.

The "Hello, World!" Tradition

Every programming journey starts with "Hello, World!". It's a simple program that just prints that phrase to the screen. It's a test to make sure your setup works and to show you the basic syntax of the language. In Python, it's one line.

Open your editor and type this:

print("Hello, World!")

Run it. You should see Hello, World! in the output. That's it. You've written a Python program.

Mastering print()

The print() function is how you display output in Python. You'll use it constantly, especially for debugging to see what your code is doing.

What it does: It takes whatever you put inside the parentheses and prints it to the console.

  • Strings: To print text, you must put it inside quotes (" or '). This tells Python it's a string of characters.
  • Multiple Items: You can print more than one thing by separating them with commas. Python will add a space between them automatically.
print("There are", 24, "hours in a day.")

Output:

There are 24 hours in a day.

Common Mistakes to Avoid

Newcomers always make the same errors. Learn them now so you can avoid them.

SyntaxError: This is the most common error. It means you broke a grammar rule in the language.

  • Missing Parentheses: Forgetting the () after print.
  • Missing Quotes: Forgetting the quotes around your text (string). This leads to a NameError because Python thinks you're trying to use a variable that doesn't exist.

Wrong:

print "Hello"  # This is Python 2 syntax, it will fail in Python 3
print(Hello)   # This will cause a NameError

Right:

print("Hello")

Understanding the Basics

Let's break down that one line of code: print("Hello, World!").

  • print: This is a function. A function is a block of code that performs a specific task. Python has many built-in functions, and print is one of the most common. You call a function by writing its name followed by parentheses ().
  • (): These are parentheses. You put the things the function needs to do its job inside them. These "things" are called arguments.
  • "Hello, World!": This is the argument we gave to the print function. Specifically, it's a string, which is just a sequence of characters. You can tell it's a string because of the quotes.

That's all there is to it. You've learned how to output information, what the most common initial errors are, and the basic components of a Python command.

We will learn in the next lesson:

Scroll to Top