Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 3 · What code is

Your first line of code

Since the 1970s, many programming books have started with a program that just says hello. In Python it's one line, print("Hello, world!"): print shows it on the screen, the brackets hold what you give it, and the quotes mark the exact text. Running it proves your editor, Python and the screen all work.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

A tradition since the 1970s

Ask almost any coder what their first program did, and the answer is the same: it said hello. Since the 1970s, many programming books have opened with a tiny program that prints a greeting, usually "hello, world". Learning a new language today, people still start the same way.

One line of Python

In Python, the whole program is one line:

``python print("Hello, world!") ``

It has three parts:

  • print means "show it on the screen". It is built into Python, ready to use.
  • The brackets ( ) hold what you're giving to print, the thing you want shown.
  • The quotes " " mark where your text starts and stops. Everything between them is text, and Python shows it exactly as written, spaces, comma and exclamation mark included.

Run it

Press run, and the output panel shows:

`` Hello, world! ``

That's it. You've written a real program: an instruction, saved in a file, that the computer follows.

Why start so tiny?

A one-line program is a quick check that everything works: your editor can save and run code, Python is installed and reading it, and the screen shows the output. If "Hello, world!" appears, the whole chain is working, so you can build something bigger. If it doesn't, you know the problem is in the setup, not in a big program you wrote.

Try it yourself

Open any free online Python editor (search "online Python") and run:

``python print("Hello, world!") print("Hi, I'm learning Python!") ``

You should see:

`` Hello, world! Hi, I'm learning Python! ``

Now change the words inside the quotes to your own name and run it again. Then try deleting one of the quotes and running it: Python stops with an error, because it can't tell where your text ends.

Good to know

  • Every snippet on this page was run with Python 3: print("Hello, world!") printed Hello, world!.
  • The "hello, world" example is usually traced to Brian Kernighan at Bell Labs: it appears in his 1972 tutorial for the B language and was made famous by the 1978 book The C Programming Language by Kernighan and Ritchie. "Many programming books" is fair; not every book starts this way.
  • The exact words vary: the originals used lowercase "hello, world"; this lesson uses "Hello, world!".
  • In Python 3, print is a built-in function and the brackets hold its argument (a word saved for a later lesson). Very old tutorials for Python 2 wrote print without brackets, which is why they look different.
  • Single quotes and double quotes both make text in Python; a later lesson covers that.

Answer to the comment question

What does print("2 + 2") show on the screen? It shows 2 + 2, the text itself, not 4. The quotes make it text, so Python shows it exactly as written and does no adding. Without the quotes, print(2 + 2) would work out the sum and show 4.

Test your knowledge

Quick quiz: Your first line of code

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What does print do in Python?
2In print("Hello, world!"), what do the brackets do?
3Why are there quotes around Hello, world!?
4Why do coders start with such a tiny program?

Sources

Video transcript

Why does every coder's first program say Hello, world?

It's a tradition. Since the 1970s, many programming books have started with a program that just says hello.

In Python, it's one line. We type print, and in brackets, the words Hello, world, in quotes.

Print means show it on the screen. The brackets hold what you're giving it. The quotes mark where your text starts and stops, and Python shows it exactly as written.

Run it, and the output panel says Hello, world. That's it. You've written a real program.

Why start so tiny? Because it proves everything works, your editor, Python and the screen, before you build anything bigger.

What does this print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons