Learn
01.2 | YOUR FIRST PROGRAM: PRINT AND COMMENTS
01 | MAKE YOUR PROGRAM SAY SOMETHING
Open a new Python file called first_program.py in the editor you set up. Type the line below, save it and run the whole file.
print() is a built-in function: Python already provides it. A function performs a named task. Here its task is to display information. The text inside quotation marks is a string, a value containing characters.
The parentheses contain the information passed to print. The quotation marks tell Python where the text begins and ends; they are not printed as part of the message.
print("Hello, Python!")EXPECTED OUTPUT
Hello, Python!
02 | READ THE LINE PIECE BY PIECE
The name print must be lowercase. Print is a different name and is not the built-in function. Opening and closing parentheses surround the argument. An argument is a value supplied to a function.
The paired straight quotation marks delimit the text. You may use single quotes or double quotes for an ordinary string, but the opening and closing type must match. Curly typographic quotes copied from a word processor are not the same Python delimiters.
Spaces inside the string belong to the message. Spaces between sensible parts of the code can make it readable, but they do not repair missing punctuation.
print("Hello")
print('Hello')EXPECTED OUTPUT
Hello Hello
03 | SEQUENCE: TOP TO BOTTOM
A simple script executes statements in order. A statement is an instruction such as a print call. Three calls produce three output lines because print normally ends with a newline.
Change the order of the statements and the output order changes. Python does not decide which sentence is most important; it follows the source order.
A blank line between statements helps people see separate parts of the program. It does not itself print an empty line. Use print() with no arguments when you deliberately want a blank output line.
print("Welcome to the coding club")
print()
print("Today we are learning Python")EXPECTED OUTPUT
Welcome to the coding club Today we are learning Python
04 | TEXT AND CALCULATIONS ARE DIFFERENT
Python can print a value produced by a calculation as well as literal text. Without quotes, 2 + 3 is evaluated. With quotes, the characters 2, space, +, space and 3 are displayed unchanged.
An expression is code that produces a value. The arithmetic expression 2 + 3 produces 5, then print displays that result. The string "2 + 3" is already a text value.
This distinction matters whenever output seems to show a calculation instead of an answer. Ask whether the code passed a text literal or an expression.
print(2 + 3)
print("2 + 3")EXPECTED OUTPUT
5 2 + 3
05 | GIVE THE READER SOME CONTEXT
A result such as 5 is more useful when a reader knows what it measures. print can receive several arguments separated by commas. By default, it puts a space between their displayed forms.
The comma is part of the function-call structure, not a printed comma. The number is evaluated before it is displayed. Later you will use variables and formatted strings to produce more flexible reports.
print("Points earned:", 2 + 3)
print("Name:", "Maya")EXPECTED OUTPUT
Points earned: 5 Name: Maya
06 | COMMENTS EXPLAIN YOUR THINKING
A comment begins with # outside a string and continues to the end of that source line. Python ignores that comment while executing the program. It helps the human reader understand a purpose, decision or assumption.
Use a comment to explain something useful, such as why you are separating a welcome message from a result. Repeating “prints hello” above an obvious print line rarely adds much.
A # inside a quoted string is ordinary text. Also, triple-quoted strings are strings, not a general comment syntax. For these lessons use # for comments.
# Welcome the user before showing a result
print("Welcome!")
print("Badge #1") # The # inside the string is printedEXPECTED OUTPUT
Welcome! Badge #1
07 | CHECK QUOTES, BRACKETS AND SPELLING
Try deliberately removing the closing quote, run the file and read the message. A SyntaxError means Python cannot parse the source as valid code. The indicated location helps, but the missing character may be just before it.
If you write Print instead of print, valid-looking syntax can still fail with NameError because the name is not defined. Different messages point to different causes.
A saved script with a syntax error is checked before its statements execute, so do not assume the first line printed before Python noticed a later missing bracket. Correct one issue, save and rerun.
08 | USE QUOTES THAT SUIT YOUR MESSAGE
To include an apostrophe, you can put the whole string in double quotes. To include a quoted word, use single quotes around the whole string. The delimiter you choose tells Python which quote ends the string.
You can also use a backslash escape such as " inside a double-quoted string, but choosing the other delimiter is often easier when beginning. A backslash-n escape represents a newline within the string.
print("I'm learning Python")
print('The robot says "Hello"')
print("Line one\nLine two")EXPECTED OUTPUT
I'm learning Python The robot says "Hello" Line one Line two
09 | PREDICT THE OUTPUT BEFORE YOU RUN
The activity below displays output for a small, fixed set of examples. It is an output-prediction tool, not a general Python interpreter. Read the code, write your prediction, then reveal and compare.
10 | WRITE A DIGITAL INTRODUCTION
Create a program with a welcome line, an invented first name, a favourite activity and one labelled calculation. Add one useful comment and one blank output line. Use several print statements rather than trying to fit everything into one long line.
Run it before showing anyone. Check punctuation in the output as well as whether Python accepts the source. A program can execute successfully but still communicate badly.
# A short introduction for the coding club
print("Welcome to my introduction")
print()
print("Name:", "Maya")
print("Favourite activity:", "Robotics")
print("Minutes practised:", 15 + 20)11 | IMPROVE ONE THING AT A TIME
Change one message, predict the effect and run again. Next change the order. Then add a comment. Small edits make it easier to connect a source change to an output change.
Keep the first working version before experimenting. If an error appears, compare the new version with the last successful one. You are learning a debugging habit: gather evidence, locate the changed cause and test the correction.
Challenge: make a three-line ticket for a fictional event. Include a title, a labelled ticket count computed from 4 + 6, and a closing message. Explain why the calculation has no quotes.