Writing Your First Python Program
It is time to write your first Python program.
Do not worry if the code looks strange. We are going to break down every single part.
Our first program is:
print("Hello, World!")And when Python runs it, you get:
Hello, World!
That tiny program teaches us several important programming concepts.
Understanding print
The word print tells Python that we want to display something.
For example:
print("Hello")Output:
Hello
Another example:
print("I am learning Python")Output:
I am learning Python
You can think of print as asking Python: "Please show this on the screen."
What Is a Function?
A function is a reusable piece of code designed to perform a particular task.
Think about a washing machine.
You do not need to understand every motor, sensor, and electrical component inside it.
You simply press: Start
The machine performs many operations for you.
A function works similarly. You give it information, and it performs a task.
For example:
print("Hello")Here, print is a built-in Python function. You are telling Python: "Use the print function to display this text."
Function Calls
When you write:
print("Hello")you are calling the print function.
The parentheses are important: print()
They tell Python that you are using the function.
Later, you will learn that functions can accept information called arguments.
Here:
print("Hello")the text "Hello" is an argument passed to print.
What Is a String?
Now look at:
"Hello, World!"
This is a string. A string is a sequence of characters used to represent text.
Examples:
"Hello""Python""12345""I love programming"
Even this is a string: "12345"
Why? Because the value is written as text inside quotes.
There is an important difference between:
12345 (a number)
and
"12345" (text)
We will explore this more deeply when we study data types.
Why Do We Need Quotes?
Python needs a way to distinguish text from Python instructions.
Consider:
Hello
Python may interpret Hello as the name of something, such as a variable.
But:
"Hello"
clearly tells Python: "This is text."
Quotes act like a boundary around the text. Think of them like putting a label around a message:
"Hello"
↑ ↑
start end
Python knows everything between the quotes belongs to the string.
Single Quotes and Double Quotes
Python allows both:
print("Hello")print('Hello')Both produce:
Hello
Hello
As a beginner, choose one style and stay consistent.
Let's Break Down the Whole Statement
Look carefully:
print("Hello, World!")There are three important pieces.
Part 1: print
This is the name of the function.
Part 2: Parentheses ()
They indicate that we are calling the function.
Part 3: String "Hello, World!"
This is the text we want to display.
Together: print + parentheses + string becomes:
print("Hello, World!")How Does Execution Flow?
Imagine Python is following a recipe.
The recipe says: "Display the words Hello, World!"
Python processes your statement and eventually produces the output.
A simplified flow is:
graph TD
A[Start Program] --> B[Read print statement]
B --> C[Recognize print as a function]
C --> D[Read the string Hello, World!]
D --> E[Call print]
E --> F[Display text]
F --> G[Program continues or ends]
You Can Print Anything
Try:
print("My name is Aarav")print("I am learning Python")print("Python is fun")You can also print multiple values:
print("Age:", 20)Output:
Age: 20
Do not worry yet about how Python handles different types of values. We will learn that soon.
Printing Multiple Lines
You can write multiple print statements:
print("Hello")print("Welcome")print("I am learning Python")Output:
Hello
Welcome
I am learning Python
Each print normally moves the cursor to a new line after displaying its content.
A Common Beginner Mistake
This is correct:
print("Hello")This is incorrect:
print(Hello)Why? Because "Hello" is explicitly marked as text. Without quotes, Python looks for something called Hello. If you have not defined it, Python will produce an error.
Another Common Mistake
Make sure your quotes match.
Correct:
print("Hello")Incorrect:
print("Hello')You opened the string with one kind of quote and closed it with another. Python is very literal. It expects the syntax to be correct.
The Big Lesson
Your first Python program may look tiny:
print("Hello, World!")But it introduces three major programming ideas:
- Function — reusable code that performs a task.
- Function call — asking a function to perform its task.
- String — text represented inside quotes.
These ideas will appear everywhere in your Python journey.
Do not underestimate simple programs. A beginner who understands simple code deeply will eventually understand complex code much faster.
[!TIP] Key Takeaways
- Parentheses are used when calling a function.
- A string represents text.
- Quotes tell Python that something is text.
print("Hello, World!")displays text on the screen.- Python follows precise syntax rules.