Running Your First Program
A program written in a compiled language like C or C++ is converted from the source language i.e. C or C++ into a language that is spoken by your computer (binary code i.e. 0s and 1s) using a compiler with various flags and options. When you run the program, the linker/loader software copies the program from hard disk to memory and starts running it.
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.
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.
The Same Program Across Computers
Suppose you write a Python program on a Windows laptop and send it to friends using macOS and Linux.
Write once: The program is written as Python source code rather than as instructions for only one operating system.
Use each platform's interpreter: The Python interpreter on each platform translates the Python code into instructions suitable for that system.
Run the program: The same source code can run on all three systems when it avoids system-dependent features.
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.
| Choice | Effect on portability | Reason |
|---|---|---|
| Basic data types | Preserves portability | They are identified as portable Python features. |
| Control flow statements | Preserves portability | They are identified as portable Python features. |
| Cross-platform libraries | Usually preserves portability | Libraries such as pathlib are designed to work across platforms. |
| Hard-coded file paths | Can break portability | Windows and Unix systems use different path conventions. |
| Operating-system-specific system calls | Can break portability | The call may exist on only one operating system. |
| Unported libraries | Can break portability | The library may not be available on every platform. |
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.
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.
| Activity | What it means | What you observe |
|---|---|---|
| Writing code | Creating a Python instruction in the editor | The instruction is present but has not yet been executed. |
| Running code | Sending the instruction to the server for the Python interpreter to execute | The result is returned and displayed in the browser. |
Your First Statement
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.
Hello, PythonThe 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?
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.
Alice
25Following Assignment
Explain what Python does when it runs the name and age example.
First assignment: The text Alice is assigned to the variable name.
Second assignment: The number 25 is assigned to the variable age.
First print call: Python looks up name and displays the value associated with it: Alice.
Second print call: Python looks up age and displays the value associated with it: 25.
Variable names let a program refer to stored values instead of repeating those values directly.
Mistakes That Stop Portability
Assuming every Python feature works on every operating system.
Some system calls exist only on one operating system.
Fix:
Prefer features and libraries that work across the platforms you need to support.Using hard-coded file paths without considering platform differences.
Different operating systems use different file-path conventions.
Fix:
Use cross-platform approaches such as the pathlib module for file paths.Confusing writing code with running code.
The code must be sent to the interpreter and executed before it produces output.
Fix:
Run the code and then inspect the result returned to the browser.Thinking the course requires a local Python installation.
This course runs Python in a browser through a cloud-based interpreter.
Fix:
Use the course editor and Run action to send code to the course server.
Practice in the Course Environment
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.
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
- Portability means Python code can run on many operating systems without code changes when it uses portable features.
- The open-source Python interpreter acts as a translation layer between Python code and platform-specific instructions.
- Hard-coded paths, operating-system-specific calls, and unported libraries can break portability.
- In this course, code is sent from your browser to a cloud-based Python interpreter, and the result is returned to the browser.
- A print call is a complete Python statement: the function name is followed by parentheses containing an argument.
Key Takeaways
- 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.