Skip to content

Hello, Python!

Write your very first Python program, then make a name badge out of text.

Start here: open pygwide. On the home page, choose Tutorials, then Python Basics, then Lesson 1: Hello, Python!

This lesson takes about an hour. The extensions take about 30 minutes more.

What you will learn

  • How to make Python show a message with print().
  • That text in Python goes inside quote marks.
  • How to add comments, which Python skips.
  • How to read an error message, and fix the mistake.

What you need from before

You don't need to know any Python yet. If you have made things in Scratch, you are ready.

Step 1: Your first program

The starter project has a few lines that start with #. Click at the end of the last one, press Enter to start a new line, and type this:

print("Hello, world!")

Now press ▶ Run. The console shows:

Hello, world!

You just wrote a program! print() tells Python to show whatever is inside its brackets.

As soon as you typed, pygwide made your own copy of the project in My Projects, so your work is kept.

In Scratch

print() is like the say block, but it writes in the console instead of a speech bubble.

Step 2: Text goes in quotes

The words inside the quote marks are called a string. Python shows a string exactly as you typed it, spaces and all.

Add some more lines, and run your program again:

print("Hello, world!")
print("I am learning Python.")
print("Python runs my lines in order, from the top.")

Each print() starts a new line. Python runs your lines from top to bottom, just like blocks stacked in Scratch.

Step 3: Mistakes are fine

Everyone makes mistakes when they code, even experts. Let's make one on purpose. Delete the last " from one of your lines, so it looks like this:

print("Hello, world!)

Press ▶ Run. Instead of your messages, the console shows an error in red. Read it carefully. It tells you:

  • which line the mistake is on, and
  • what kind of mistake it is. Here, the string was never finished.

Put the " back and run it again. Fixed!

Tip

When you see an error, look at the line it names, and the line just before it. Check for missing quote marks and brackets.

Step 4: Comments

Lines that start with # are comments. Python skips them. They are notes for people reading your code, including you.

Change the first comment to say what your program does:

# My name badge
# Lines that start with # are comments. Python skips them.

Step 5: Make a name badge

Now replace your print() lines with a name badge. Use your own name and the things you like:

print("+------------------------+")
print("|   HELLO, MY NAME IS    |")
print("|                        |")
print("|          Alex          |")
print("|                        |")
print("+------------------------+")
print()
print("I like football and drawing.")
print("My favourite food is pizza.")

print() with nothing inside the brackets prints an empty line.

Run it. Do the edges of your badge line up? If not, add or take away spaces until they do.

Check your code

Your program should look something like this, with your own name and likes:

# My name badge
# Lines that start with # are comments. Python skips them.

print("+------------------------+")
print("|   HELLO, MY NAME IS    |")
print("|                        |")
print("|          Alex          |")
print("|                        |")
print("+------------------------+")
print()
print("I like football and drawing.")
print("My favourite food is pizza.")

Extensions

Multiply text

In Python, you can multiply a string by a number. Try this:

print("*" * 26)

It prints 26 stars! You can also add strings together with +. Use these to make a banner for the top of your program.

One way to do it
# A banner made by multiplying text

print("*" * 26)
print("*  WELCOME TO MY CODE!   *")
print("*" * 26)
print()
print("=-" * 13)
print("Hip " * 2 + "hooray!")

Draw a picture

Use print() lines to draw a picture out of letters and symbols, such as a cat, a rocket or a house.

Extra bits

Fix the bugs

Each of these lines has one mistake. Can you spot them? Type each one into your program, run it, read the error, and fix it.

print("Hello!"
Print("Hello!")
print(Hello!)
print("Hello!')

Did you know?

Python is not named after a snake. Its inventor, Guido van Rossum, named it after a comedy show he liked, called Monty Python's Flying Circus.

Export your project

pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.

Next time, choose ⇪ Import a project on the home page to carry on with it.