Python First Program


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.

How Python Executes Code?

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.

Creating Your First Python File

You can write Python code in any editor, but most beginners use IDLE or VS Code because they make the process smooth.

Creating a File in IDLE

  1. Open IDLE.

  2. Go to File > New File.

  3. A blank editor window appears.

  4. Write your code inside this window.

  5. Save the file with a .py extension.

Creating a File in VS Code

  1. Open VS Code.

  2. Create a new folder to store your programs.

  3. Open the folder in VS Code.

  4. Create a new file and name it something like first_program.py.

  5. Start writing your code inside the editor.

Both tools support saving, editing and running your file easily.

Writing the Classic “Hello World” Program

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.

Why “Hello World” Matters

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.

Running Your Program in IDLE

Once your code is saved, running it is simple.

  1. Press F5 or choose Run > Run Module.

  2. IDLE opens the Python Shell.

  3. You will see the output:

Hello World

If the output appears correctly, it means your setup and code are working.

Running Your Program in VS Code

VS Code provides multiple ways to run a Python file.

Using the Run Button

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.

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

Adding More Print Statements

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.

Understanding Basic Errors

When learning, mistakes are normal. Python helps by showing clear error messages. For example:

Missing Quotation Marks

print(Hello World)

This will cause an error because the text is not inside quotes.

Incorrect Indentation

    print("Hello")

If you add extra spaces without context, Python may show an indentation error.

Misspelling “print”

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.

Adding Comments to Your Program

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.

Using Variables in Your First Program

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.

Creating Basic User Input

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.

Running Your Program Several Times

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.

What You Have Learned So Far

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.

Summary of the Tutorial

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.


Practice Questions

  1. What role does the print function play in a beginner’s first Python program, and why is it often the starting point for learning?
  2. How does Python’s line-by-line execution help beginners understand their code more clearly?
  3. Why is it important to check your Python version before writing your first program?
  4. What is the difference between running Python code in IDLE’s Shell and running it from a saved file?
  5. How does writing your first program help you build confidence in understanding Python’s workflow?
  6. Write a program that prints your name and your city on two separate lines.
  7. Write a Python script that prints the result of 25 + 17 using the print function.
  8. Create a program that prints a short welcome message, then prints the current year as a separate line.
  9. Write a script that stores a message in a variable and prints that variable.
  10. Create a small program that prints three sentences, each on a new line, without using multiple print statements.

Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top