Skip to content
PythonPython Foundationsbeginner25 min read

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

Your first Python program
python
print('Hello, Python!')
Output
Hello, Python!

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

You do not need to put quotation marks around a number when you want to print the numeric value. Quotation marks change a value such as 25 into the string "25".

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

Three statements in sequence
python
print('Start')
print('Learning Python')
print('Finish')
Output
Start Learning Python Finish

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

The \n notation in the dry run represents a newline. It is explanatory notation here, not extra text printed by the program.

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

Display an arithmetic result
python
print(4 + 3)
print(10 - 6)
print(3 * 4)
Output
7 4 12

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

A comma between arguments to print() separates values to display. A plus sign inside an arithmetic expression performs addition. Do not confuse these two uses.

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 default separator
python
print('Code', 'Nivo', 3)
Output
Code Nivo 3

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

sep must be a string or None. If you provide only one argument, there is no pair of arguments between which a separator could be inserted.

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

Default line breaks
python
print('Hello')
print('World')
Output
Hello World

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

If output appears joined together unexpectedly, check whether a previous print() call used a custom end value.

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

A comment does not print anything
python
# Display a welcome message
print('Welcome to CodeNivo')
Output
Welcome to CodeNivo

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

Comments help humans understand source code; print() produces program output. These serve different purposes.

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

Correct a missing quotation mark
python
print('Hello')
Output
Hello

The opening and closing single quotes match. The closing parenthesis completes the function call. Python can parse and execute the statement.

Caution

A SyntaxError means Python could not parse the code as written. Fix the syntax first; do not assume the program ran and produced a normal result.

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

A complete beginner program
python
print('Welcome', 'to', 'CodeNivo', sep=' ')
print('Your first result:', end=' ')
print(8 + 4)
print('Keep practicing!')
Output
Welcome to CodeNivo Your first result: 12 Keep practicing!

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

The output shown is the exact text produced by the program. The notation \n used in this lesson represents line breaks between displayed lines.

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.

Incorrect Approach
script.py
python
Expecting print() to display the quotation marks around a string.
Correct Solution
script.py
python
Quotation marks delimit a string in source code. print() displays the string contents, not those surrounding 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.

Incorrect Approach
script.py
python
Putting an arithmetic expression inside quotation marks and expecting it to calculate.
Correct Solution
script.py
python
print(4 + 3) displays 7. print("4 + 3") displays the literal text 4 + 3.

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.

Incorrect Approach
script.py
python
Assuming multiple print() arguments are joined without spaces.
Correct Solution
script.py
python
print() uses a single space as the default sep. Set sep explicitly when you need 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.

Incorrect Approach
script.py
python
Forgetting that print() adds a newline by default.
Correct Solution
script.py
python
Each call normally ends on a new line. Use end=" " or end="" when you intentionally want subsequent output on the same line.

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.

Incorrect Approach
script.py
python
Expecting comments to appear in the output.
Correct Solution
script.py
python
Python ignores comments during normal execution. Use print() when you want text to be displayed.

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.

Incorrect Approach
script.py
python
Ignoring the actual error message after a syntax mistake.
Correct Solution
script.py
python
Check matching quotation marks, parentheses, commas, and the line identified by Python before rerunning the program.

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.

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?
Answer:
print() writes the text representation of its arguments to a text stream, normally standard output. By default, it separates multiple arguments with a space and ends with a newline.
Q2:What is the difference between print(5 + 2) and print("5 + 2")?
Answer:
The first statement evaluates the arithmetic expression and displays 7. The second displays the literal string 5 + 2 because the expression is enclosed in quotation marks.
Q3:How are statements executed in a simple Python script?
Answer:
In ordinary sequential code, Python executes statements in source order. Branching, loops, function calls, exceptions, and other control-flow features can change which statements execute and when.
Q4:What is the purpose of the sep parameter in print()?
Answer:
sep specifies the string inserted between the arguments passed to print(). Its default is one space. For example, print("A", "B", sep="-") displays A-B.
Q5:What is the purpose of the end parameter in print()?
Answer:
end specifies what print() writes after its arguments. The default is a newline. Setting end=" " ends the output with a space, allowing a subsequent print() call to continue on the same line.
Q6:What is the difference between a comment and a print() statement?
Answer:
A comment provides information for people reading the code and is ignored during normal execution. A print() statement executes and writes output.
Q7:Why might print("Age:", 22) and print("Age:" + 22) behave differently?
Answer:
The first call accepts separate arguments and converts them to text for display. In the second expression, Python attempts to add a string and an integer, which raises a TypeError. Convert the integer to a string or use separate print() arguments.
Q8:How would you troubleshoot a SyntaxError in a simple print() statement?
Answer:
Check that quotation marks match, parentheses are balanced, and commas and arguments are correctly placed. Read the error message and inspect the indicated line, then rerun the corrected code.

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.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: