Concepts / Running Your First Program

Running Your First Program

A portable program is not guaranteed merely because it is written in Python.

  • Programming

From Code to Output

When you write a Python instruction, you are not writing instructions for only one specific operating system. You are writing instructions that the Python interpreter can read and execute. In this course, you will write code in your browser, run it in the course environment, and observe the result. The important distinction is that writing code creates an instruction, while running code causes the interpreter to execute that instruction.

Python statementwritten in the browserCourse serverreceives the codePython interpreterreads and executes itProgram resultshown in the browserRundelivers codeproduces
What happens to a Python statement after it is written, and how does it produce output in the course environment?

Portability Through Interpretation

Portability is Python's ability to run on many different operating systems without code changes. Python can support this breadth because it is open-source: its source code is publicly available and can be modified. As a result, Python has been adapted and installed on dozens of platforms, including GNU/Linux, Windows, FreeBSD, and Macintosh, as well as specialized hardware.

The Python interpreter acts as an abstraction layer between your Python code and the underlying operating system. It reads your code and translates it into platform-specific instructions for systems such as Windows, macOS, and Linux. This is why the same Python source can often run on different computers without being rewritten.

Python source codeone programPython interpretertranslation layerWindowsplatform-specificinstructionsmacOSplatform-specificinstructionsLinuxplatform-specificinstructionsreadtranslatetranslatetranslate
How can one Python program run on Windows, macOS, and Linux when each computer uses a different operating system and processor?
Worked example

The Same Program Across Computers

Suppose you write a Python program on a Windows laptop and send it to friends using macOS and Linux.

  1. 1
    Write once

    The program is written as Python source code rather than as instructions for only one operating system.

  2. 2
    Use each platform's interpreter

    The Python interpreter on each platform translates the Python code into instructions suitable for that system.

  3. 3
    Run the program

    The same source code can run on all three systems when it avoids system-dependent features.

Result

Portability means the same Python code can run on different operating systems without changes, provided the code uses portable features.

Choosing Portable Features

Portability is not automatic. It is influenced by the choices you make while writing code. Basic data types, control flow statements, and cross-platform libraries are examples of portable features. By contrast, hard-coded file paths, operating-system-specific system calls, and libraries that have not been ported to all platforms can make a program dependent on one system.

ChoiceEffect on portabilityReason
Basic data typesPreserves portabilityThey are identified as portable Python features.
Control flow statementsPreserves portabilityThey are identified as portable Python features.
Cross-platform librariesUsually preserves portabilityLibraries such as pathlib are designed to work across platforms.
Hard-coded file pathsCan break portabilityWindows and Unix systems use different path conventions.
Operating-system-specific system callsCan break portabilityThe call may exist on only one operating system.
Unported librariesCan break portabilityThe library may not be available on every platform.

Portability depends on whether the features used by a program work across the platforms that need to run it.

Kivy and Cross-Platform Games

Kivy demonstrates how Python portability can be extended into game development. It allows you to write a single game in Python and deploy it to computers, iPhones, iPads, and Android devices without rewriting the code for each platform.

Kivy provides a portable layer on top of Python. You write code that Kivy understands instead of writing separate code specifically for iOS or Android. Kivy then handles the translation to each platform's native capabilities, allowing one game codebase to reach multiple device types.

Python gamesingle codebaseKivyportable layerComputersplatform applicationiPhoneplatform applicationiPadplatform applicationAndroidplatform applicationusessupportssupportssupportssupports
How does Kivy connect one Python game codebase to multiple platform-specific applications?

Your Course Environment

This course runs Python code in your browser using a cloud-based interpreter, so you do not need to install Python or configure a local environment. When you write a line of code and press Run, the code is sent to a server. The Python interpreter on that server reads and executes it, and the result is sent back to your browser.

Editable codeinstruction is beingwrittenRunsend code to serverExecuting codeinterpreter processesinstructionDisplayed outputresult returns to browserpressstartsproduces
What changes when code moves from an editable text area into an executing program?
ActivityWhat it meansWhat you observe
Writing codeCreating a Python instruction in the editorThe instruction is present but has not yet been executed.
Running codeSending the instruction to the server for the Python interpreter to executeThe result is returned and displayed in the browser.

Writing creates code; running causes the interpreter to act on it.

Your First Statement

Definition

A Python statement is a complete instruction. The simplest statement is a call to the print function, which displays what you give it on the screen.

python
Output
Hello, Python

The word print is the function name. The parentheses show that you are calling the function, and the text inside the parentheses is the input, called an argument. When the interpreter reads this statement, it executes print with Hello, Python as its input, so that text appears as output.

What do you think happens?

What will appear when this statement is run?

  • The word print

  • Hello, Python

  • Nothing

Reveal answer
Answer:

Hello, Python

The interpreter recognizes print as a function and displays the text supplied inside its parentheses.

Names and Stored Values

Variables are names that hold values. Assignment uses the equals sign to associate a value with a variable name. When Python later encounters the variable name, it looks up the associated value and can use it in another statement.

python
Output
Alice
25
Worked example

Following Assignment

Explain what Python does when it runs the name and age example.

  1. 1
    First assignment

    The text Alice is assigned to the variable name.

  2. 2
    Second assignment

    The number 25 is assigned to the variable age.

  3. 3
    First print call

    Python looks up name and displays the value associated with it: Alice.

  4. 4
    Second print call

    Python looks up age and displays the value associated with it: 25.

Result

Variable names let a program refer to stored values instead of repeating those values directly.

Mistakes That Stop Portability

  • Common mistake

    Assuming every Python feature works on every operating system.

    What it looks like

    Using an operating-system-specific system call.

    Why it fails

    Some system calls exist only on one operating system.

    Do this instead

    Prefer features and libraries that work across the platforms you need to support.

  • Common mistake

    Using hard-coded file paths without considering platform differences.

    What it looks like

    Writing a path that depends on Windows backslashes or Unix forward slashes.

    Why it fails

    Different operating systems use different file-path conventions.

    Do this instead

    Use cross-platform approaches such as the pathlib module for file paths.

  • Common mistake

    Confusing writing code with running code.

    What it looks like

    Expecting output immediately after typing a print statement.

    Why it fails

    The code must be sent to the interpreter and executed before it produces output.

    Do this instead

    Run the code and then inspect the result returned to the browser.

  • Common mistake

    Thinking the course requires a local Python installation.

    What it looks like

    Trying to configure Python on the learner's computer before using the course environment.

    Why it fails

    This course runs Python in a browser through a cloud-based interpreter.

    Do this instead

    Use the course editor and Run action to send code to the course server.

Practice in the Course Environment

EASY

Run a Python statement that displays a greeting. Then change the text inside the print function and run it again. Observe the difference between editing the statement and executing it.

Hints
  • Start with the print function.
  • Put the message inside parentheses.
  • Press Run after editing the statement.
MEDIUM

Write two assignment statements that associate values with two variable names. Then use print to display each variable. Before pressing Run, predict which values will appear and in what order.

Hints
  • Use the equals sign for assignment.
  • Use a separate print call for each variable.
  • The output follows the order of the print statements.

Key Takeaways

  1. 1
    Portability means Python code can run on many operating systems without code changes when it uses portable features.
  2. 2
    The open-source Python interpreter acts as a translation layer between Python code and platform-specific instructions.
  3. 3
    Hard-coded paths, operating-system-specific calls, and unported libraries can break portability.
  4. 4
    In this course, code is sent from your browser to a cloud-based Python interpreter, and the result is returned to the browser.
  5. 5
    A print call is a complete Python statement: the function name is followed by parentheses containing an argument.

At a glance

  • Python portability comes from the interpreter's role as a translation layer and from Python's open-source, widely ported implementation.
  • Portable programming requires avoiding system-dependent features and choosing cross-platform libraries and practices.
  • Writing code and running code are different actions: execution begins when the interpreter receives and processes the code.
  • This course runs Python in a cloud-based interpreter through your browser, without requiring installation.
  • The simplest Python instruction is a print function call, and variables let names refer to stored values.

Related concepts

More concepts starting with R