Your First Python Program — Step-by-Step Guide for Absolute Beginners
In this tutorial, you'll learn about Your First Python Program. We cover key concepts, practical examples, and best practices.
Write your first Python program step by step. Install Python, use the interactive shell, write a script, and build a simple project. No experience needed.
What You'll Learn
By the end of this tutorial, you will have Python installed on your computer, written your first program, learned basic syntax, and built a simple interactive project.
Why It Matters
Python is the most popular language for beginners. It is used in web development, data science, automation, and security tools. Writing your first program is the biggest step in your coding journey.
Real-World Use
Durga Antivirus Pro uses Python for its scanning engine. DodaZIP uses Python for its automation scripts. The language you are about to learn powers real products used by millions.
Your Learning Path
flowchart LR
A[Basic Math for Coding] --> B[First Python Program]
B --> C[Coding for Absolute Beginners]
C --> D[Choosing Your First Language]
D --> E[Setting Up Dev Environment]
B --> F{You Are Here}
style F fill:#f90,color:#fff
Step 1: Install Python
Check if Python Is Already Installed
Open your terminal (or command prompt on Windows) and type:
python --version
If you see something like Python 3.10+, you are ready. If not, download Python from python.org.
Install on Windows
# Download the installer from python.org
# Run it and CHECK "Add Python to PATH"
# Then verify:
python --version
Install on macOS
# If you have Homebrew installed:
brew install python3
python3 --version
Install on Linux
sudo apt update
sudo apt install python3
python3 --version
Step 2: The Python Interactive Shell
The interactive shell lets you run Python code one line at a time. Think of it as a conversation with Python.
# Start the interactive shell
python3
You will see >>> prompt. Type code and press Enter to see results immediately.
>>> 2 + 2
4
>>> print("Hello, World!")
Hello, World!
>>> 10 * 5
50
Exit the shell with exit() or Ctrl + D.
Step 3: Your First Python Script
Create a file called hello.py and open it in a text editor.
# This is a comment. Python ignores it.
# Comments explain what the code does.
# Print a greeting
print("Hello, World!")
# Do some math
print("2 + 2 =", 2 + 2)
print("10 * 5 =", 10 * 5)
# Use a variable
name = "Python"
print("I am learning", name)
Run the Script
python3 hello.py
Expected output:
Hello, World!
2 + 2 = 4
10 * 5 = 50
I am learning Python
Step 4: Working With Variables
Variables store data so you can use it later.
# String (text)
greeting = "Hello"
name = "Alice"
print(greeting, name)
# Integer (whole number)
age = 25
print("Age:", age)
# Float (decimal number)
price = 19.99
tax = 1.60
total = price + tax
print("Total:", total)
Expected output:
Hello Alice
Age: 25
Total: 21.59
Step 5: Getting User Input
Make your program interactive by asking the user for input.
# Ask the user for their name
user_name = input("What is your name? ")
# Ask for their favorite number
favorite_number = input("What is your favorite number? ")
# Convert the number from text to integer
favorite_number = int(favorite_number)
# Calculate and display
double = favorite_number * 2
print(f"Nice to meet you, {user_name}!")
print(f"Double your favorite number is {double}.")
Expected output (if user enters "Bob" and "7"):
What is your name? Bob
What is your favorite number? 7
Nice to meet you, Bob!
Double your favorite number is 14.
Step 6: Making Decisions With Conditionals
# Check if a number is positive, negative, or zero
number = int(input("Enter a number: "))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
Expected output (if user enters -5):
Enter a number: -5
Negative
Step 7: Build a Mini Project — Number Guessing Game
Combine everything you learned into a small game.
import random
# Generate a random number between 1 and 10
secret = random.randint(1, 10)
guess = 0
print("I am thinking of a number between 1 and 10.")
print("Can you guess it?")
while guess != secret:
guess_text = input("Your guess: ")
guess = int(guess_text)
if guess < secret:
print("Too low. Try again.")
elif guess > secret:
print("Too high. Try again.")
else:
print("Correct! You got it!")
Expected output (example playthrough):
I am thinking of a number between 1 and 10.
Can you guess it?
Your guess: 5
Too low. Try again.
Your guess: 8
Too high. Try again.
Your guess: 7
Correct! You got it!
Common Mistakes Beginners Make
1. Forgetting Colons After if, else, elif, for, while
Python uses colons to start code blocks. if x > 5: is correct. if x > 5 without a colon causes an error.
2. Messing Up Indentation
Python uses indentation (spaces or tabs) to group code. All code inside an if block must be indented the same amount. Mixing spaces and tabs causes confusing errors.
3. Forgetting to Convert Input to Integer
input() always returns text. If you type 5, you get the string "5", not the number 5. Use int() to convert.
4. Using Wrong Quote Types
Single quotes (') and double quotes (") both work, but they must match. print("hello) causes an error because the quotes do not match.
5. Misspelling Variable Names
Python is case-sensitive. name and Name are different variables. Use the same spelling everywhere.
6. Not Installing Python Added to PATH
On Windows, if you forget to check "Add Python to PATH" during installation, the python command will not work. Reinstall and check the box.
7. Naming Files random.py or Other Module Names
If you name your file random.py, it conflicts with Python's built-in random module. Use unique filenames.
Practice Questions
1. How do you print text to the screen in Python?
Use the print() function with the text inside quotes: print("Hello").
2. What does the input() function return?
It returns whatever the user typed as a string (text). You must convert it with int() if you need a number.
3. Why does Python care about indentation? Python uses indentation to know which code belongs inside a block (like an if statement or loop). Inconsistent indentation causes errors.
4. What does int() do?
It converts a value (usually a string) to an integer. For example, int("5") returns the number 5.
5. Challenge: Modify the number guessing game to give the user only 5 attempts. If they do not guess in 5 tries, show "Game over. The number was X."
Try It Yourself
Open a new file called calculator.py. Write a program that asks the user for two numbers and an operator (+, -, *, /). Perform the calculation and display the result. Test it with different inputs. Then add a check that prevents division by zero.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro