Your First Python Program
Write and run your first Python program, understand statements and execution order, display values with print(), and control spaces and line breaks.
In this lesson, you will learn
- ✓Explain what a Python statement does and how statements execute in order.
- ✓Use print() to display strings, numbers, and the values of simple expressions.
- ✓Distinguish strings from numbers and understand the role of quotation marks.
- ✓Use multiple arguments, sep, and end to control printed output.
- ✓Trace a short program line by line and predict its exact output.
- ✓Recognize common syntax errors and correct simple print() statements.
1. Your First Python Statement
A **statement** is an instruction that Python executes. A Python program usually contains multiple statements that work together to complete a task.
The simplest way to begin is with the built-in **print() function**. It displays information on standard output, which is normally your terminal when you run a script from the command line.
The parentheses contain the information you want to print. For example, print("Hello, Python!") asks Python to display the text Hello, Python!.
Text enclosed in quotation marks is called a **string literal**. Python supports both single quotes and double quotes for ordinary strings, provided the opening and closing quotes match.
Try the following code in a file named first_program.py. Save the file and run it using the Python command appropriate for your system, as explained in [Lesson 2: Installing Python and Choosing a Tool](/python/installing-python).
Your first Python program
Python reads the print() statement. It evaluates the string literal 'Hello, Python!'. The print() function displays the text without displaying its surrounding quotation marks. By default, print() finishes by writing a newline.
Note
2. Statements Execute from Top to Bottom
In a straightforward Python script, statements execute in the order they appear. Python starts with the first statement and proceeds to the next after the current statement finishes, unless control-flow features or an error change that behavior.
This order matters because the output of one statement appears before the output of the next statement. If a program contains three print() calls, each call runs in sequence.
Python does not rearrange ordinary print() statements alphabetically or decide which message is more important. Their order in the source code determines their execution order.
When tracing a program, read the code from top to bottom. Record the output produced by each print() call, then combine those lines in the same order.
Three statements in sequence
The first statement displays Start. The second statement displays Learning Python. The third statement displays Finish. Each print() call ends with a newline by default, so each message appears on its own line.
Note
3. Printing Strings, Numbers, and Expressions
The print() function can display different kinds of values, including strings, integers, floating-point numbers, and the results of expressions.
An **expression** is code that Python evaluates to produce a value. For example, 4 + 3 is an arithmetic expression whose result is 7.
You can pass an expression directly to print(). Python evaluates the expression and displays its result. You do not have to calculate the answer yourself and type it into the program.
Be careful with quotation marks. print(4 + 3) evaluates arithmetic, while print("4 + 3") displays the characters 4 + 3. The second example contains a string, so Python does not perform the addition.
Display an arithmetic result
Python evaluates 4 + 3 and displays 7. It evaluates 10 - 6 and displays 4. It evaluates 3 * 4 and displays 12. The asterisk (*) is Python’s multiplication operator. Each result appears on a separate line because each print() call uses the default newline.
Note
4. Control Spaces with sep
When print() receives multiple arguments, its **sep parameter** determines the text placed between them. The default separator is a single space.
You can change sep by providing it as a keyword argument. For example, sep="-", places a hyphen between the printed arguments.
The separator applies between arguments in the same print() call. It does not automatically change the spaces inside a string literal.
Use sep when you want predictable formatting, such as displaying a date-like sequence, a path-like label, or values separated by commas.
The default separator
The print() call receives three arguments. Python places the default single-space separator between Code and Nivo. It also places a single space between Nivo and 3.
Note
5. Control Line Breaks with end
By default, print() ends its output with a newline. This is why consecutive print() calls normally display their values on separate lines.
The **end parameter** controls what print() writes after its arguments. Its default value is the newline character, written as "\n".
If you set end=" ", the next print() call continues after a space instead of moving to a new line. If you set end="", the next output begins immediately after the preceding output.
The end parameter affects the end of each print() call. It does not change the separator between multiple arguments; that is the job of sep.
Default line breaks
The first print() displays Hello and ends with a newline. The second print() starts on the next line and displays World. The second call also ends with a newline, although no additional visible text follows.
Note
6. Comments and Readable Programs
A **comment** is text in source code that Python ignores during normal execution. A comment begins with the # character and continues to the end of that physical line, provided the # is not inside a string literal.
Comments can explain the purpose of a statement or mark a step in a learning exercise. They do not produce output by themselves.
Good comments explain something useful that is not already obvious from the code. Avoid adding a comment to every simple print() call when the statement already explains itself.
Readable code also uses clear text, consistent indentation when indentation is needed, and one statement per line for beginner examples. These habits make it easier to compare the source code with its output.
A comment does not print anything
The first line is a comment and is ignored during normal execution. The second line calls print(). Only the welcome message appears in the output.
Note
7. Common Errors in Your First Program
Small syntax mistakes can prevent a program from running. When Python reports an error, read the message and inspect the indicated line before changing unrelated code.
**Missing quotation marks:** a string needs matching opening and closing quotation marks. For example, print("Hello) is invalid because the string is not closed.
**Missing parentheses:** a function call such as print("Hello") needs its opening and closing parentheses.
**Mismatched quotation marks:** opening with a single quote and closing with a double quote usually creates an unterminated string. Use matching quotes or deliberately escape quotes when necessary.
**Unexpected output spacing:** print() inserts a space between separate arguments by default. If you need a different format, check sep or use a single formatted string.
**Confusing source code with output:** print("Hello") is the code you write; Hello is the displayed output. The output does not include the function name or the surrounding string quotation marks.
Correct a missing quotation mark
The opening and closing single quotes match. The closing parenthesis completes the function call. Python can parse and execute the statement.
Caution
8. Put Everything Together
You now know how to print text, display calculated values, control separators, and choose whether consecutive print() calls start new lines.
The following example combines these ideas in a small welcome program. Read each statement and predict the output before checking the answer.
Notice how the first print() call uses sep to format the values, the second uses end to keep the next output on the same line, and the final call prints a calculation.
A complete beginner program
The first statement prints three arguments with a single-space separator. This is also the default separator, but it is specified explicitly for practice. The second statement displays 'Your first result:' and ends with a space instead of a newline. The third statement evaluates 8 + 4 to produce 12. Its output appears after the space left by the second statement. The final statement displays 'Keep practicing!' on the next line because the preceding print() call ended with its default newline.
Note
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Expecting print() to display the quotation marks around a string.
Why it happens: The quotation marks are syntax delimiters used in source code; print() displays the string value without those delimiters.
Key takeaway: Quotation marks delimit a string in source code. print() displays the string contents, not those surrounding delimiters.
#2 Putting an arithmetic expression inside quotation marks and expecting it to calculate.
Why it happens: Quotation marks turn the expression into ordinary text, so Python prints the characters instead of evaluating the arithmetic.
Key takeaway: print(4 + 3) displays 7. print("4 + 3") displays the literal text 4 + 3.
#3 Assuming multiple print() arguments are joined without spaces.
Why it happens: print() separates multiple arguments with a space by default, which differs from joining them with an empty string or another separator.
Key takeaway: print() uses a single space as the default sep. Set sep explicitly when you need another separator.
#4 Forgetting that print() adds a newline by default.
Why it happens: The default end value for print() is a newline, so consecutive calls normally start on separate lines.
Key takeaway: Each call normally ends on a new line. Use end=" " or end="" when you intentionally want subsequent output on the same line.
#5 Expecting comments to appear in the output.
Why it happens: Comments are for readers and are ignored during execution; they do not produce output.
Key takeaway: Python ignores comments during normal execution. Use print() when you want text to be displayed.
#6 Ignoring the actual error message after a syntax mistake.
Why it happens: Beginners may focus on the final error line or guess at the cause instead of reading the reported location and checking delimiters and parentheses.
Key takeaway: Check matching quotation marks, parentheses, commas, and the line identified by Python before rerunning the program.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What does the print() function do?
Q2:What is the difference between print(5 + 2) and print("5 + 2")?
Q3:How are statements executed in a simple Python script?
Q4:What is the purpose of the sep parameter in print()?
Q5:What is the purpose of the end parameter in print()?
Q6:What is the difference between a comment and a print() statement?
Q7:Why might print("Age:", 22) and print("Age:" + 22) behave differently?
Q8:How would you troubleshoot a SyntaxError in a simple print() statement?
Key takeaways
- A statement is an instruction Python executes.
- In ordinary sequential code, statements execute from top to bottom.
- print() displays strings, numbers, and evaluated expressions.
- Quotation marks make text a string; they do not appear around that string in normal print() output.
- The default sep value is a single space between arguments.
- The default end value is a newline after each print() call.
- Comments beginning with # are for readers and do not produce output.
- Exact output depends on argument values, separators, and ending characters.
- Reading error messages and tracing statements are essential debugging skills.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: