Your First Python Program
It's time to write real Python code.
In this lesson, you will create your first
Python program, learn how the print()
function works, understand strings and
comments, and practice writing small programs
of your own.
Learning objectives
By the end of this lesson, you should be able to:
- Create your first Python source file.
-
Use the
print()function. - Display text and numbers on the screen.
- Understand Python strings at a basic level.
- Add comments to Python programs.
- Recognize common beginner mistakes.
- Write and run small Python programs independently.
Getting Started
In the previous lesson, you installed Python and prepared your development environment. Now we can finally write some code.
Python programs are usually saved in files
ending with the .py extension.
Create a new file called
hello.py.
The name is not important, but the
.py extension tells your
computer that the file contains Python
source code.
Your first line of code
Type the following into your file:
print("Hello, Python!")
Save the file. You have just written your first Python program.
The print() Function
The print() function is one of
the first Python functions beginners learn.
It displays information in the program's
output.
Basic syntax
print(value)
The value inside the parentheses is what Python will send to the output.
Printing text
print("Welcome to PyLearn")
Welcome to PyLearn
Printing a number
print(25)
25
Notice that the number 25 does
not need quotation marks.
Quotation marks are used when we want Python
to treat something as text.
Building Your First Program
Let's create a slightly more meaningful program.
print("My name is Adnan")
print("I am learning Python")
print("Python is powerful")
My name is Adnan
I am learning Python
Python is powerful
Python executes these statements from top to
bottom.
The first print() runs first,
followed by the second and then the third.
For now, imagine Python reading your program one instruction at a time, from the first line toward the last.
Understanding What Your Code Means
Let's examine this simple statement:
print("Hello")
There are two important ideas here.
| Part | Meaning |
|---|---|
print
|
The function we are calling. |
("Hello")
|
The value passed to the function. |
The parentheses are used to provide information to the function. The value inside them is called an argument.
We will study functions and arguments in much greater depth later in the course.
Text and Strings
Text in Python is represented using a data type called a string.
Strings can be written using single or double quotation marks.
print("Hello")
print('Hello')
Both statements produce the same visible output.
Hello
Hello
Strings can contain spaces
print("Python is easy to learn")
Strings can contain numbers
print("I am 25 years old")
Because the entire value is inside quotation marks, Python treats it as text.
"25" is text, while
25 is a number.
They may look similar on the screen,
but Python treats them differently.
Printing Multiple Values
The print() function can accept
multiple values separated by commas.
print("Name:", "Adnan")
print("Age:", 25)
print("City:", "Multan")
Name: Adnan
Age: 25
City: Multan
Python automatically places a space between
values passed to print() in this
simple case.
Printing calculations
print() can also display the
result of an expression.
print(10 + 5)
print(20 * 3)
print(100 / 4)
15
60
25.0
This introduces an important idea: Python can calculate an expression before displaying its result.
Special Characters in Strings
Sometimes you need to place special characters inside a string. Python provides escape sequences for this.
New line
The \n sequence represents a
new line.
print("Hello\nPython")
Hello
Python
Tab space
The \t sequence inserts a tab
space.
print("Name:\tAdnan")
print("Course:\tPython")
Quotation marks inside strings
print("He said, \"Python is fun!\"")
Escape sequences become much more useful when you start working with text processing. We will revisit strings in depth later.
Common Beginner Mistakes
Making mistakes is a normal part of learning programming. Here are some common problems beginners encounter.
Mistake 1 — Missing quotation marks
print(Hello)
If Hello is intended to be text,
it should be written as a string:
print("Hello")
Mistake 2 — Missing a closing parenthesis
print("Hello"
The correct version is:
print("Hello")
Mistake 3 — Incorrect quotation marks
Use matching quotation marks around a string.
print("Hello')
Better:
print("Hello")
Mistake 4 — Saving the wrong file
Make sure your source file is saved with
the .py extension.
A file named hello.txt is not
the same as hello.py.
Practice Exercises
Now practice what you have learned. Try solving these exercises before looking at any solution.
Introduce Yourself
Write a Python program that prints:
- Your name
- Your city
- The fact that you are learning Python
Display Your Goals
Print three programming goals you want to achieve.
For example:
print("Learn Python")
print("Build projects")
print("Become a better programmer")
Simple Calculations
Use print() to calculate:
- 50 + 25
- 100 - 35
- 12 × 8
- 100 ÷ 4
Add Comments
Take one of your previous programs and add comments explaining what each section does.
Mini Challenge: Personal Profile
Let's combine everything you've learned into a small program.
Create a Python program that displays a simple personal profile.
Your program should display:
- Your name
- Your age
- Your city
- Your favorite programming language
- Your learning goal
Example solution
# My personal profile
print("===== My Profile =====")
print("Name: Adnan")
print("Age: 25")
print("City: Multan")
print("Favorite language: Python")
print("Goal: Become a skilled programmer")
===== My Profile =====
Name: Adnan
Age: 25
City: Multan
Favorite language: Python
Goal: Become a skilled programmer
Change the information and make the program your own. The goal is to practice writing code, not simply reading it.
Quick Knowledge Check
Test your understanding before continuing.
1. Which function is commonly used to display output in Python?
2. Which extension is normally used for Python source files?
3. Which of these is a Python string?
4. Which symbol starts a single-line Python comment?
5. What does Python generally do with statements in a simple program?
Lesson Summary
You have now written your first Python programs. More importantly, you have started learning how Python code is structured and executed.
.py extension.
print() function displays
information in the output.
print().
#.
\n
can control text formatting.
You can now create a Python file, write code, execute it and understand the basic output. In the next lesson, we will learn Python syntax — the rules that tell Python how code should be written.
Finished Lesson 03?
Mark this lesson complete after you have finished the reading, exercises and quiz.
Adding Comments
Comments are notes written inside source code for humans. Python does not execute comments as program instructions.
A single-line comment begins with the
#symbol.The comment helps explain the code but does not appear in the output.
Comments can explain individual lines
Use comments when they genuinely improve understanding. Avoid comments that simply repeat obvious code.