Running Your First Python Program

Back to Home Page


Before You Begin

This guide assumes you have already installed Python on your computer. If you haven't, please follow one of our installation guides first:


Creating Your First Python Program

Let's create a simple "Hello, World!" program - the traditional first program in any programming language.

Step 1: Open Your Text Editor

You can use any text editor to write Python code:

Step 2: Write Your First Code

Type the following code in your text editor:

# This is my first Python program
print("Hello, World!")
print("Welcome to Python programming!")

# Let's make it interactive
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")
  

What this code does:

Step 3: Save Your File

Save your file with a .py extension, which tells your computer it's a Python file. For example: hello.py

Step 4: Run Your Program

Now it's time to run your program! You'll need to use the command line for this.

On Windows:

  1. Open Command Prompt or PowerShell
  2. Navigate to the folder where you saved your file using the cd command
    cd path\to\your\folder

    For example, if your file is on the Desktop:

    cd C:\Users\YourUsername\Desktop
  3. Run your program with Python:
    python hello.py

On Mac or Linux:

  1. Open Terminal
  2. Navigate to the folder where you saved your file:
    cd path/to/your/folder

    For example, if your file is on the Desktop:

    cd ~/Desktop
  3. Run your program with Python:
    python3 hello.py

Step 5: See Your Program in Action!

When you run your program, you should see:

Hello, World!
Welcome to Python programming!
What's your name? 
  

Type your name and press Enter. You should then see something like:

Nice to meet you, YourName!
  

Congratulations! You've just written and run your first Python program!


Understanding Your First Program

Line by Line Explanation

  1. # This is my first Python program - This is a comment. Python ignores comments, but they help you understand your code
  2. print("Hello, World!") - This prints the text "Hello, World!" to the screen
  3. print("Welcome to Python programming!") - This prints another message
  4. # Let's make it interactive - Another comment
  5. name = input("What's your name? ") - This asks the user for their name and stores it in a variable called 'name'
  6. print(f"Nice to meet you, {name}!") - This prints a message that includes the name the user entered

Key Concepts


Try It Yourself: Modifying the Program

Now try making some changes to see how your program works:

Challenge 1: Add a Question

Modify your program to ask the user for their age too. Add these lines after the name input:

age = input("How old are you? ")
print(f"Wow, {age} is a great age to learn programming!")
  

Challenge 2: Create a Simple Calculator

Create a new file called calculator.py with this code:

print("Simple Calculator")
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

# Convert input strings to numbers
num1 = float(num1)
num2 = float(num2)

sum_result = num1 + num2
print(f"{num1} + {num2} = {sum_result}")
  

Run this program the same way you ran your first one!


What's Next?

Now that you've run your first Python program, you can:


Back to Home Page