Built with Tutify.io

    AIStudyLabs

    Running Your First Python Program

    Running Your First Python Program

    Introduction

    You now have Python and an editor/IDE ready from the previous lessons. In this lesson, you will actually run Python code—both interactively and from a script file. You’ll:

    By the end, you’ll have a complete end‑to‑end workflow: type code → run it → see results → fix issues.

    Using the Python Interactive Interpreter (REPL)

    The Python interpreter lets you run Python statements interactively, one at a time. This is great for experimentation.

    Starting the Interpreter

    In your terminal, run:

    python
    

    or:

    python3
    

    You should see something like:

    Python 3.11.6 (main, ...) 
    [GCC ...] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    

    The >>> prompt means Python is ready for commands.

    Running Simple Commands Interactively

    Try a few expressions:

    >>> 2 + 3
    >>> print("Hello from the interpreter!")
    >>> 10 * 5
    

    Expected output:

    >>> 2 + 3
    5
    >>> print("Hello from the interpreter!")
    Hello from the interpreter!
    >>> 10 * 5
    50
    

    Notice:

    Exiting the Interpreter

    To exit:

    Alternatively, type:

    >>> exit()
    

    Writing Your First Python Script

    Interactive mode is great for quick tests, but real programs are saved in files (scripts).

    You can use any editor or IDE you configured earlier. The steps are similar regardless of tool.

    Step 1: Create a New File

    Create a new file named hello.py in a folder you can easily find, for example:

    Step 2: Add Python Code

    In your editor/IDE, add this code:

    # hello.py
    
    print("Hello, Python world!")
    print("This is your first Python script.")
    

    Explanation and Output

    This script:

    Expected output when run:

    # output
    Hello, Python world!
    This is your first Python script.
    

    Save the file before running it.


    Running a Python Script from the Terminal

    Now you’ll run hello.py from the command line.

    Step 1: Navigate to the Script Folder

    In your terminal, change directory to where you saved hello.py.

    Example (adjust path as needed):

    cd C:\Users\YourName\python-projects      # Windows
    

    or

    cd /Users/YourName/python-projects          # macOS
    cd /home/YourName/python-projects           # Linux
    

    Step 2: Run the Script

    Run one of the following commands (whichever matches your installation):

    python hello.py
    

    or

    python3 hello.py
    

    You should see:

    # output
    Hello, Python world!
    This is your first Python script.
    

    If you see that output, congratulations—you’ve successfully run your first Python program from a file.


    A Slightly More Interesting First Program

    Let’s create a small program that does a calculation and prints a formatted result.

    Edit hello.py or create a new file greeting.py:

    # greeting.py
    
    name = "Alice"
    age = 25
    
    # Use f-string formatting to combine variables with text
    print(f"Hello, {name}!")
    print(f"You are {age} years old.")
    print(f"Next year, you will be {age + 1}.")
    

    Explanation and Output

    Expected output:

    # output
    Hello, Alice!
    You are 25 years old.
    Next year, you will be 26.
    

    Run it the same way:

    python greeting.py
    

    Debugging Common First‑Run Errors

    New Python users often hit a few predictable issues. Learning to recognize them early will save time.

    1. Syntax Errors

    A syntax error means Python can’t understand your code structure.

    Example mistake:

    print("Hello world!"   # missing closing parenthesis
    

    When you run this, you might see:

      File "hello.py", line 1
        print("Hello world!"   # missing closing parenthesis
             ^
    SyntaxError: '(' was never closed
    

    How to fix:

    2. Indentation Errors

    Python is sensitive to indentation (spaces at the start of a line).

    Example mistake:

    print("First line")
      print("Second line")  # extra spaces at the beginning
    

    Possible error:

    IndentationError: unexpected indent
    

    Fix:

    3. File Not Found / Wrong Directory

    If you run:

    python hello.py
    

    and see:

    python: can't open file 'hello.py': [Errno 2] No such file or directory
    

    It means Python can’t find hello.py in the current directory.

    Fixes:

    python C:\Users\YourName\python-projects\hello.py
    

    4. Using the Wrong Python Command

    If python runs Python 2 or fails, but python3 works, always use:

    python3 script_name.py
    

    You can confirm which command maps to Python 3 with:

    python --version
    python3 --version
    

    Summary / Key Takeaways

    In this lesson Running Your First Python Program, you have learned:

    Python Beginner Tutorial with Examples — Getting Started with Python and the Course

    Getting Started with Python and the Course

    Python Basics

    Control Flow in Python

    Data Structures in Python

    Functions and Scope in Python

    Error Handling and Debugging

    Working with Libraries

    Script Writing and Automation

    Real-world Applications of Python

    Advanced Python Concepts

    Best Practices in Python

    Tutorial Not Found