Python is one of the easiest programming languages to learn and use. Whether you are completely new to programming or just exploring Python, this guide will walk you through everything you need to know about writing and running a Python program.
Step 1: What is Python?
Python is a programming language used for many things:
- Automating tasks
- Building websites
- Analyzing data
- Making games
Python code is written in text files with the extension .py
(e.g., myprogram.py
).
Step 2: Installing Python
Before running a Python program, you need to have Python installed on your computer. Follow these steps:
- Check if Python is installed:
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type:python --version
If you see something likePython 3.x.x
, you’re good to go. If not: - Download Python:
Go to the official Python website and download the latest version for your operating system. - Install Python:
During installation, make sure to check the box “Add Python to PATH”. This makes it easier to run Python from the command line.
Step 3: Writing Your First Python Program
Let’s create a simple Python program. Follow these steps:
- Open any text editor (like Notepad, Notepad++, or Visual Studio Code).
- Write the following code:
# This is a simple Python program print("Hello, World!")
- Save the file as
hello.py
.
Step 4: Running Your Python Program
Once you have written your Python program, here’s how to run it:
- Open the terminal or Command Prompt.
- Navigate to the folder where your Python file is saved. For example, if it’s saved in
Documents
, type:cd Documents
- Run the program by typing:
python hello.py
You should see the following output:
Hello, World!
Step 5: Breaking Down the Code
Here’s what the code does:
#
is used for comments. Anything written after#
is ignored by Python. Comments help explain the code.print("Hello, World!")
tells Python to display the text “Hello, World!” on the screen.
Step 6: Adding User Interaction
Let’s make the program more interactive by asking the user for their name. Update your code to:
# Asking for the user's name
name = input("What is your name? ")
# Displaying a personalized message
print("Hello, " + name + "! Welcome to Python.")
Save and Run the Program:
- Save the file as
greet.py
. - Run it the same way:
python greet.py
Sample Output:
What is your name? Clifford
Hello, Clifford! Welcome to Python.
Step 7: Using Variables and Math
Let’s create a program that calculates the area of a rectangle. Here’s the code:
# Rectangle Area Calculator
# Get the width and height from the user
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Calculate the area
area = width * height
# Print the result
print("The area of the rectangle is:", area)
Save and Run the Program:
- Save the file as
rectangle.py
. - Run it:
python rectangle.py
Sample Output:
Enter the width of the rectangle: 5
Enter the height of the rectangle: 10
The area of the rectangle is: 50.0
Step 8: Writing Programs with Conditions
Here’s a program that checks if a number is even or odd:
# Even or Odd Checker
# Get a number from the user
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Sample Output:
Enter a number: 7
The number is odd.
Step 9: Creating a Loop
Want to repeat a task multiple times? Use a loop! Here’s an example:
# Countdown Timer
# Get the starting number
countdown = int(input("Enter a number to start the countdown: "))
# Countdown loop
while countdown > 0:
print(countdown)
countdown -= 1 # Subtract 1 from countdown
print("Blast off!")
Sample Output:
Enter a number to start the countdown: 5
5
4
3
2
1
Blast off!
Step 10: Organizing Code with Functions
To make programs reusable, you can use functions. Here’s an example:
# Function to greet the user
def greet_user(name):
print("Hello, " + name + "!")
# Get the user's name
user_name = input("What is your name? ")
# Call the function
greet_user(user_name)
Sample Output:
What is your name? Alice
Hello, Alice!
Step 11: Debugging Tips
- Error Messages: If something goes wrong, Python shows an error message. Read it carefully—it often tells you what’s wrong and where.
- Indentation: Python relies on indentation (spaces or tabs) to structure code. Make sure your code lines are properly aligned.