2.4. Your First Python Program

Traditionally, the first program written in a new language is called Hello, World! because all it does is display the words, Hello, World! on the screen. In Python, the source code looks like this.

print("Hello, World!")

This is an example of using the print function, which doesn’t actually print anything on paper. It displays a value on the screen. In this case, the result is the phrase:

Hello, World!

2.4.1. Try It!

Type print("Hello, World!") into the code editor below, then click the green Run button.

Warning

Do NOT just copy/paste the code. You will learn best by typing, trying, changing, and fixing.

The quotation marks inside the parentheses () mark the beginning and end of the value to display. They don’t appear in the result.

Some people judge the quality of a programming language by the simplicity of the Hello, World! program. By this standard, Python does very well.

Note

If you typed correctly, you saw the output Hello, World!. If you left out or mistyped any characters, then you either saw a misspelled output or an error message. Do not worry if you make mistakes! These experiences still teach you something. Fix any errors and try again.

2.4.2. Now Play

Once you print Hello, World! successfully, go back and play around with the code. Make a change, click Run, and see what happens. Try to:

  1. Change the message printed.

  2. Figure out what the parentheses do. Will the code work without them?

  3. Remove one or both quotation marks. Do we need to include both opening and closing marks?

  4. Is there a difference between using a single or a double quote (' vs. ")?

  5. Print multiple messages one after the other.

  6. Print two messages on the same line.

  7. Print a number. (Bonus: Print two numbers added together).

  8. Print a message that contains quote marks, such as You're or I can print, "Hello, World!".

  9. Other. You choose!

Spend a few minutes trying these changes. Do not worry if you miss some of the targets. Learning comes through experience, and you WILL learn all the details behind print soon.

Once you finish practicing (and hopefully making some mistakes), you will have a pretty good idea of how the print function in Python works.

Try It

On paper (or in a document on your computer), write one or two sentences about print. You should provide more detail than, “It prints things.”

2.4.3. Check Your Understanding

Question

The print function:

  1. sends information to be printed on paper.
  2. displays a value on the screen.
  3. tells the computer to put the information in print, rather than cursive, font.
  4. tells the computer to speak the information.

Question

Which of the following correctly prints Coding Rocks? Select ALL that apply.

  1. print(Coding Rocks)
  2. print"Coding Rocks"
  3. print('Coding Rocks')
  4. print("Coding Rocks')
  5. print("Coding Rocks")