PyLearn
PyLearn / Python Foundations / Your First Python Program
Lesson 03 · Foundations

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.

25–35 minutes Beginner Hands-on
Reading progress 0%

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.
01

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.

Your first Python file

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.

03

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")
Output
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.

Think sequentially

For now, imagine Python reading your program one instruction at a time, from the first line toward the last.

04

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.

05

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.

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.

Text vs number

"25" is text, while 25 is a number. They may look similar on the screen, but Python treats them differently.

06

Printing Multiple Values

The print() function can accept multiple values separated by commas.

print("Name:", "Adnan")
print("Age:", 25)
print("City:", "Multan")
Output
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)
Output
15
60
25.0

This introduces an important idea: Python can calculate an expression before displaying its result.

07

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.

# This is my first Python program

print("Hello, Python!")

The comment helps explain the code but does not appear in the output.

Comments can explain individual lines

print("Welcome")  # Display a welcome message
Good programming habit

Use comments when they genuinely improve understanding. Avoid comments that simply repeat obvious code.

08

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")
Output
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.

09

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.

10

Practice Exercises

Now practice what you have learned. Try solving these exercises before looking at any solution.

Exercise 1

Introduce Yourself

Write a Python program that prints:

  • Your name
  • Your city
  • The fact that you are learning Python
Exercise 2

Display Your Goals

Print three programming goals you want to achieve.

For example:

print("Learn Python")
print("Build projects")
print("Become a better programmer")
Exercise 3

Simple Calculations

Use print() to calculate:

  • 50 + 25
  • 100 - 35
  • 12 × 8
  • 100 ÷ 4
Exercise 4

Add Comments

Take one of your previous programs and add comments explaining what each section does.

11

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")
Example Output
===== My Profile =====
Name: Adnan
Age: 25
City: Multan
Favorite language: Python
Goal: Become a skilled programmer
Don't just copy the solution.

Change the information and make the program your own. The goal is to practice writing code, not simply reading it.

12

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?

13

Lesson Summary

You have now written your first Python programs. More importantly, you have started learning how Python code is structured and executed.

Python programs are commonly saved with the .py extension.
The print() function displays information in the output.
Text is represented using strings.
Strings can use single or double quotes.
Multiple values can be passed to print().
Python can evaluate expressions and display their results.
Comments begin with #.
Escape sequences such as \n can control text formatting.
Your Python journey has officially begun.

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.