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.
Step 1: Create a Python File
Open VS Code (or your editor), create a new file, and save it as:
hello.py
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
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.")
int() to convert the input into a number.
Without it, Python would treat the value as text.
Step 7: Common Beginner Mistakes
- Forgetting the
.pyextension - Running the file from the wrong folder
- Forgetting to convert input to
intorfloat - Indentation errors
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.