Lesson 07
Platform Independent Programming
Why the same Python program runs on Windows, Linux, and macOS.
What you will learn
By the end of this lesson, you'll understand:
- What does platform mean in programming?
- What is the difference between platform-dependent and platform-independent languages?
- Why does Python run on Windows, Linux, and macOS?
- How do you inspect your operating system using the
platformmodule?
What is a Platform?
In this lesson, a platform means the Operating System (OS) where a program runs.
The operating system acts as the interface between your program and the computer hardware.
Examples of Operating Systems
Windows
Linux / Ubuntu
macOS
Unix / Solaris
The OS manages:
- Memory
- CPU
- Storage
- Keyboard
- Display
- Files
Two Types of Programming Languages
1. Platform-Dependent Languages
These languages may behave differently across operating systems when platform-specific details are used.
- Compiled for one platform
- May rely on OS-specific behavior
- Examples in teaching: C, C++
Note: Your source lists C and C++ as classic teaching examples of platform-dependent languages.
2. Platform-Independent Languages
A platform-independent language lets you write one program and run it on multiple operating systems, provided the runtime is installed.
Python is platform independent.
Why Python Runs Everywhere
Python doesn't execute your source code directly.
Instead, it follows this pipeline.
Step-by-step
- You write a
.pyfile. - Python compiles it into byte code.
- The PVM on that operating system executes it.
- The same program works on different OSes.
This is the idea behind Write Once, Run Anywhere.
Example: Inspect Your Platform
Python provides a built-in module called platform.
import platform
print("Operating System:", platform.system())
print("Python Version:", platform.python_version())Example output on macOS
Operating System: Darwin
Python Version: 3.12.0Example output on Windows
Operating System: Windows
Python Version: 3.12.0Notice that only the OS name changes.
The source code remains identical.
Understanding the platform Module
The platform module gives information about the current machine.
| Function | Purpose |
|---|---|
platform.system() |
Operating system name |
platform.machine() |
Machine architecture |
platform.python_version() |
Installed Python version |
Example
import platform
print(platform.system())
print(platform.machine())
print(platform.python_version())Possible output:
Darwin
arm64
3.12.0Visualizing Different Platforms
This is why Python is widely used in cloud computing and enterprise software.
Why Platform Independence Matters
Imagine your team has developers using different operating systems.
| Developer | Operating System |
|---|---|
| Alice | Windows |
| Bob | Ubuntu |
| Charlie | macOS |
They can all run the same Python project without rewriting the business logic.
This greatly improves collaboration.
Practice Exercise
Write a program that displays:
- Operating System
- Machine Type
- Python Version
import platform
print("Operating System :", platform.system())
print("Machine Type :", platform.machine())
print("Python Version :", platform.python_version())Sample output
Operating System : Darwin
Machine Type : arm64
Python Version : 3.12.0Run the same file on another operating system and compare the results.
Key Points
- Platform means the operating system environment.
- Python is a platform-independent language.
- The same
.pyfile can run on Windows, Linux, and macOS. - Byte code and the PVM make cross-platform execution possible.
- Use the
platformmodule to inspect the current system.
Interview Questions
- What is meant by a platform in programming?
- Why is Python called platform independent?
- What is the role of the PVM in cross-platform execution?
- Which module provides operating system information?
- What does
platform.machine()return?
Summary
| Concept | Remember |
|---|---|
| Platform | Operating System |
| Examples | Windows, Linux, macOS, Unix |
| Platform Independent | Same source runs on multiple OSes |
| Python Flow | .py → Byte Code → PVM |
| System Module | platform |
Python's platform independence allows developers to write one program and execute it across multiple operating systems, making it ideal for enterprise software, cloud applications, automation, and AI development.
Next
Next lesson: Dynamically Typed Programming — how Python determines variable types automatically at runtime and why explicit type declarations are usually unnecessary.