-
Hajipur, Bihar, 844101
Writing your first Python program is an important moment because it shows how the language works and how simple it is to get started. After installing Python and setting up your editor, the next step is understanding how to create a file, write code inside it and run it successfully. This chapter walks you through every step in a clear and practical way so you understand not just what to do, but why each step matters. By the end, you’ll be comfortable writing and running basic Python programs on your own.
Before writing a program, it helps to know how Python reads your file. Python executes your code from top to bottom, one line at a time. If Python finds an error, it stops immediately at that line and shows a message. This behaviour makes the language easier to learn because you always know where the mistake is.
Python programs are saved with the .py extension. When you run a .py file, the interpreter reads and processes each line exactly as written. Even small things like indentation matter because Python uses spacing to define blocks of code.
You can write Python code in any editor, but most beginners use IDLE or VS Code because they make the process smooth.
Open IDLE.
Go to File > New File.
A blank editor window appears.
Write your code inside this window.
Save the file with a .py extension.
Open VS Code.
Create a new folder to store your programs.
Open the folder in VS Code.
Create a new file and name it something like first_program.py.
Start writing your code inside the editor.
Both tools support saving, editing and running your file easily.
The traditional first program in any language prints a simple message. It helps you understand how output works. In Python, this is done with the print function.
Type this inside your file:
print("Hello World")
This line tells Python to display the text inside the quotation marks. The print function is built into Python, so you don’t need to import anything.
Even though it looks simple, this line teaches you several things:
Python uses parentheses for functions.
Text must be enclosed in quotes.
Python follows a straightforward structure.
Running the file triggers the interpreter to evaluate the code.
This small step gives you confidence because you see the program responding to your command.
Once your code is saved, running it is simple.
Press F5 or choose Run > Run Module.
IDLE opens the Python Shell.
You will see the output:
Hello World
If the output appears correctly, it means your setup and code are working.
VS Code provides multiple ways to run a Python file.
If the Python extension is installed, you’ll see a Run button at the top of the editor. Clicking it runs the file in the terminal.
You can also open the terminal in VS Code and type:
python file_name.py
or on macOS/Linux:
python3 file_name.py
The terminal then displays:
Hello World
Seeing this output confirms that Python is reading your file properly.
After printing a simple message, you can experiment by adding more lines:
print("Learning Python")
print("My first program is running")
print(10 + 5)
Python prints each line in order. The third line shows how Python handles numbers. Instead of printing them as text, it performs the calculation.
These small experiments help you understand the flow of a program.
When learning, mistakes are normal. Python helps by showing clear error messages. For example:
print(Hello World)
This will cause an error because the text is not inside quotes.
print("Hello")
If you add extra spaces without context, Python may show an indentation error.
prit("Hello")
Python will say the function is not defined.
Learning to read these messages is an important part of programming. They guide you to the exact line where something went wrong.
Comments help explain your code. Python ignores them when running the program. To add a comment, use the # symbol:
# This is my first Python program
print("Hello World")
Comments are useful because they help you remember what each part of your program does.
Once you understand printing, you can introduce variables. Variables store values that you can use later.
Example:
name = "Aarushi"
age = 20
print(name)
print(age)
Python reads the variable and prints its value. Variables help you build more meaningful programs.
You can also ask the user for information using input():
name = input("Enter your name: ")
print("Welcome", name)
When the program runs, it waits for the user to type something. This interaction makes programs feel dynamic.
Each time you change your code, save the file and run it again. This habit helps you understand how small changes affect the output. Repetition is a big part of learning programming.
By writing and running your first Python program, you’ve covered several essential concepts:
Using the print function
Understanding how Python executes code
Running files in both IDLE and VS Code
Reading basic error messages
Adding comments
Using variables
Accepting input from a user
These basics form the foundation for everything you’ll learn next.
Writing your first Python program marks the beginning of your programming journey. You start by creating a .py file, writing a simple print statement and running it through IDLE or VS Code. Along the way, you learn how Python reads code line by line, how to handle basic errors, how to use comments and how to experiment with variables and user input. These early steps prepare you for more advanced topics and help you gain confidence as you continue learning Python.