Tutoriel
Partie 5 — Writing Your First Python Program (Step-by-Step Beginner Guide 2026)

Partie 5 — Writing Your First Python Program (Step-by-Step Beginner Guide 2026)

Learn how to write and run your first Python program step by step. This beginner-friendly guide explains print(), input(), variables, conditions, and how Python executes code.

Python 11 Mis à jour 42 minutes ago
Conseil : lisez d’abord les sections clés, puis essayez un QCM lié à la même notion pour valider votre compréhension.

5. Writing Your First Python Program (Step-by-Step)

Writing your first Python program is an important milestone. It proves that your environment works and that you understand the basic workflow: write code → run it → see the result → improve it. In this guide, we’ll build your very first Python program step by step.

Objective: By the end of this section, you will know how to create, save, and run a Python file correctly.

Step 1: Create a Python File

Open VS Code (or your editor), create a new file, and save it as: hello.py

Always make sure the file extension is .py. Without it, Python will not recognize the file.

Step 2: Write Your First Line of Code

In your hello.py file, type:

print("Hello, World!")
    

The print() function displays text in the terminal. This is traditionally the first program written in any language.


Step 3: Run the Program

Open the terminal inside VS Code and type:

python hello.py
    

If you are on macOS or Linux, you might need:

python3 hello.py
    
If everything is configured correctly, you will see: Hello, World!

Step 4: Make It Interactive

Let’s improve the program by asking the user for their name.

name = input("Enter your name: ")
print("Hello,", name)
    

The input() function waits for the user to type something. The value entered is stored in the variable name.


Step 5: Understanding How It Works

  • input() → waits for user input
  • Variable assignment (=) → stores data
  • print() → displays output
  • Execution order → top to bottom

Python reads your code line by line. If there is an error, it stops and shows you the line where the problem occurred.


Step 6: Add Simple Logic

Now let’s make it smarter by asking for the user's age.

age = int(input("Enter your age: "))

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")
    
Notice that we used int() to convert the input into a number. Without it, Python would treat the value as text.

Step 7: Common Beginner Mistakes

  • Forgetting the .py extension
  • Running the file from the wrong folder
  • Forgetting to convert input to int or float
  • Indentation errors
If you get an error, read it carefully. Python usually tells you exactly where the problem is.

Mini Challenge

Modify your program to:

  • Ask for the user’s name
  • Ask for the user’s birth year
  • Calculate their age automatically
  • Print a personalized message

This small exercise helps you practice variables, input, conversion, and simple calculations.


Conclusion

Writing your first Python program is simple but powerful. It introduces the essential building blocks: input, output, variables, and basic logic. Master these fundamentals and you’ll be ready to build more complex programs confidently.

The key to progress is repetition. Rewrite the program from memory and improve it step by step.