Chapter 02 of 11

Your First Python Program

Now that we know what Python is, it's time to write our first Python program. Don't worry—we'll start with something very simple.

Our first goal is just to make Python display a message on the screen.

Writing Your First Python Program

The simplest Python program is:

print("Hello, World!")

When you run it, you'll get:

Hello, World!

And congratulations! 🎉 You just wrote your first Python program.

Understanding the Code

Let's break it down:

print("Hello, World!")

Here, print() is a built-in Python function used to display something on the screen.

The text "Hello, World!" is the message we want to display.

So you can think of it like this:

print() → Display something
"Hello, World!" → What we want to display

Printing Different Messages

We can print any text we want:

print("Welcome to Python")
print("I am learning programming")
print("Python is fun!")

Output:

Welcome to Python
I am learning programming
Python is fun!

Each print() statement displays its content on a new line.

Printing Numbers

The print() function isn't limited to text. We can also print numbers:

print(10)
print(25)
print(100)

Output:

10
25
100

We can even perform calculations:

print(10 + 5)
print(20 * 2)

Output:

15
40

Python calculates the expression and then displays the result.

Using Variables

We can also store information in variables and print it:

name = "John"
age = 21

print(name)
print(age)

Output:

John
21

We can also combine text and variables:

name = "John"

print("Hello", name)

Output:

Hello John

Adding Comments

Sometimes we want to write notes in our code that Python should ignore. These are called comments.

A single-line comment starts with #:

# This is my first Python program
print("Hello, World!")

Python ignores the comment and only executes the print() statement.

Comments are useful for explaining what your code is doing, especially when your programs become larger.

A Small Example

Let's put a few things together:

# Student information
name = "John"
age = 20

print("Student Name:", name)
print("Age:", age)
print("Welcome to Python!")

Output:

Student Name: John
Age: 20
Welcome to Python!

This is still a very small program, but you're already using variables, strings, numbers, comments, and the print() function.

And that's how your Python journey begins! 🐍