Concepts / Running Your First Python Program

Running Your First Python Program

Your first print statement demonstrates the complete learning cycle: write an instruction, run it through the interpreter, and inspect the output. The same cycle will support later work with data, decisions, repetition, and larger programs.

  • Programming

One Program, Many Systems

Imagine writing a Python program on a Windows laptop and sending it to friends who use macOS and Linux. In many cases, the same Python code can run on all three systems without changes. This ability is called portability. Portability means that a program can run on different operating systems without requiring its code to be rewritten for each one.

Definition

Portability is Python's ability to run on many different operating systems without code changes, made possible by its open-source nature.

Python codesame instructionsPython interpretertranslation layerWindowsmacOSLinuxreadrunrunrun
How does the same Python code move from a source file through the Python interpreter to run on Windows, macOS, and Linux?

Portable Code Choices

Portability is not automatic. It depends partly on the choices you make while writing code. Basic data types, control flow statements, and cross-platform libraries are examples of features that can behave consistently across systems. Code becomes less portable when it depends on a particular operating system's files, commands, paths, or libraries.

More portable choicesChoices that can reduce portability
Basic data typesHard-coded file paths
Control flow statementsOperating-system-specific system calls
Cross-platform librariesLibraries not ported to all platforms
The pathlib module for file pathsPaths written for only one operating system
The os module for common system operationsFeatures available on only one operating system

Portability depends on whether the features and libraries used by a program are available across the systems where it needs to run.

Writing Versus Running

Writing code means creating Python instructions. Running code means giving those instructions to the Python interpreter so it can read and execute them. A line stored in a file has not necessarily produced a result yet. The result appears when the interpreter executes the instruction.

Write statementinstruction existsStore codefile or interactive inputRun codeinterpreter executesOutputresult appearssave or enterexecuteproduce
What happens after a Python statement is written, and how is executing it different from simply storing it in a file?
REPLScript file
Runs code line by lineRuns a complete saved file
Useful for experimenting quicklyUseful for organizing multiple lines of code
Reads, evaluates, prints, and waits for the next lineContains multiple lines that can be run together

The REPL and script files use the same Python language but organize execution differently.

REPL stands for Read-Evaluate-Print Loop. The REPL reads one line, evaluates or runs it, prints the result, and waits for another line. A script is a saved file containing multiple lines of Python code that can be run as a complete unit. This course introduces your first statements through the REPL, and later lessons will also use complete scripts.

Your First Statement

Code walkthrough
print("Hello, Python")
  1. 1
    Python reads print and recognizes it as a function, which is a reusable instruction.
  2. 2
    The parentheses show that the function is being called.
  3. 3
    The text inside the parentheses is the input, called an argument.
  4. 4
    The print function displays that text on the screen.
  • print is the function name
  • Parentheses mark a function call
  • Hello, Python is the text given as input
Output
Hello, Python

A Python statement is a complete instruction. The print function is the simplest statement introduced here. Its function name is followed by parentheses, and the argument inside those parentheses is the text to display. When the interpreter executes the statement, the text appears as output.

Worked example

Tracing a print statement

Determine what happens when the interpreter runs print("Welcome to Python").

  1. 1
    Read

    The interpreter reads the statement and identifies print as a function.

  2. 2
    Interpret

    The parentheses show that print is being called, and the text inside them is its argument.

  3. 3
    Execute

    The print function displays the argument on the screen.

Result

The output is Welcome to Python.

The Course Execution Path

In this course, Python code runs in the cloud on a server rather than directly on your computer. When you write a line and press Run, the code is sent to that server. The Python interpreter on the server reads and executes it, and the result is sent back to your browser for display.

Your codein the browserCloud serverexecution environmentPython interpreterreads and runs codeCourse outputback in the browsersendexecute withreturn result
How does code delivered by the course reach the execution environment, and where does its output appear?

Mistakes That Stop Execution

  • Common mistake

    Confusing writing code with running code.

    What it looks like

    A statement has been entered or saved, but Run has not been used.

    Why it fails

    Storing an instruction does not by itself make the interpreter execute it or produce output.

    Do this instead

    Run the statement in the REPL or run the complete script file.

  • Common mistake

    Treating the parentheses in a function call as optional.

    What it looks like
    print "Hello, Python"
    Why it fails

    A function call follows the pattern of a function name followed by parentheses containing its arguments.

    Do this instead

    Write the function call with parentheses around the input, such as print("Hello, Python").

  • Common mistake

    Assuming Python code is automatically portable.

    What it looks like

    Using a hard-coded path or an operating-system-specific library.

    Why it fails

    Some paths, system calls, and libraries work only on particular systems.

    Do this instead

    Prefer cross-platform features and libraries when the program must run on multiple platforms.

  • Common mistake

    Thinking the course executes code on the learner's computer.

    What it looks like

    Expecting the browser lesson to require a local Python installation.

    Why it fails

    The course sends code to a cloud-based interpreter running on a server.

    Do this instead

    Use the course environment to write, run, and inspect the result in the browser.

First Practice Run

EASY

Run a statement that displays your own short greeting. Then change the text inside the parentheses and run it again. Observe that writing a different argument changes the output only after the statement is executed.

Hints
  • Use the print function.
  • Put the message inside parentheses.
  • Run the statement after changing the message.
EASY

Create a variable named message, assign it a short text value, and print the variable. Identify the name, the equals sign, the value, and the function call in your code.

Hints
  • Assignment connects a name with a value.
  • Use the equals sign for assignment.
  • Pass the variable name to print.

What do you think happens?

What output will appear after this code is run?

  • The word greeting

  • The text stored in greeting

  • No output because assignment is not execution of print

Reveal answer
Answer:

The text stored in greeting

The assignment gives greeting a value, and the later print call retrieves and displays that value.

What You Can Now Trace

  1. 1
    Portability means that Python code can run on different operating systems without code changes when it avoids system-dependent features.
  2. 2
    The Python interpreter acts as a translation layer between Python instructions and the operating system where they run.
  3. 3
    Writing code creates instructions; running code sends those instructions to the interpreter for execution.
  4. 4
    The REPL runs Python one line at a time, while a script file contains multiple lines that can be run together.
  5. 5
    In this course, code is sent from your browser to a cloud-based Python interpreter, and the result is returned to the browser.
Key point

Your first print statement demonstrates the complete learning cycle: write an instruction, run it through the interpreter, and inspect the output. The same cycle will support later work with data, decisions, repetition, and larger programs.

Key Takeaways

  • Python portability allows the same code to run across many operating systems when the code avoids system-dependent features.
  • The open-source Python ecosystem and the Python interpreter help make this portability possible.
  • A statement becomes active only when the interpreter runs it; writing or storing code is different from executing it.
  • The print function is a simple Python instruction with a function name, parentheses, and an argument.
  • This course runs Python in a cloud-based interpreter and returns the output to your browser.

Related concepts

More concepts starting with R