Running Your First Python Program
Portability is Python's ability to run on many different operating systems without code changes, made possible by its open-source nature.
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.
Portability is Python's ability to run on many different operating systems without code changes, made possible by its open-source nature.
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 choices | Choices that can reduce portability |
|---|---|
| Basic data types | Hard-coded file paths |
| Control flow statements | Operating-system-specific system calls |
| Cross-platform libraries | Libraries not ported to all platforms |
| The pathlib module for file paths | Paths written for only one operating system |
| The os module for common system operations | Features available on only one operating system |
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.
| REPL | Script file |
|---|---|
| Runs code line by line | Runs a complete saved file |
| Useful for experimenting quickly | Useful for organizing multiple lines of code |
| Reads, evaluates, prints, and waits for the next line | Contains multiple lines that can be run together |
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
print("Hello, Python")
Hello, PythonA 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.
Tracing a print statement
Determine what happens when the interpreter runs print("Welcome to Python").
Read: The interpreter reads the statement and identifies print as a function.
Interpret: The parentheses show that print is being called, and the text inside them is its argument.
Execute: The print function displays the argument on the screen.
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.
Mistakes That Stop Execution
Confusing writing code with running code.
Storing an instruction does not by itself make the interpreter execute it or produce output.
Fix:
Run the statement in the REPL or run the complete script file.Treating the parentheses in a function call as optional.
A function call follows the pattern of a function name followed by parentheses containing its arguments.
Fix:
Write the function call with parentheses around the input, such as print("Hello, Python").Assuming Python code is automatically portable.
Some paths, system calls, and libraries work only on particular systems.
Fix:
Prefer cross-platform features and libraries when the program must run on multiple platforms.Thinking the course executes code on the learner's computer.
The course sends code to a cloud-based interpreter running on a server.
Fix:
Use the course environment to write, run, and inspect the result in the browser.
First Practice Run
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.
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?
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
- Portability means that Python code can run on different operating systems without code changes when it avoids system-dependent features.
- The Python interpreter acts as a translation layer between Python instructions and the operating system where they run.
- Writing code creates instructions; running code sends those instructions to the interpreter for execution.
- The REPL runs Python one line at a time, while a script file contains multiple lines that can be run together.
- In this course, code is sent from your browser to a cloud-based Python interpreter, and the result is returned to the browser.
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.