Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 07

Platform Independent Programming

Why the same Python program runs on Windows, Linux, and macOS.

Beginner10 min

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 platform module?

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.

Python program above OS above hardware

Examples of Operating Systems

Windows desktopWindows desktop

Windows

Linux / Ubuntu desktopLinux / Ubuntu desktop

Linux / Ubuntu

macOS desktopmacOS desktop

macOS

Unix / Solaris style desktopUnix / Solaris style desktop

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.

Same hello.py runs on Windows, Linux, and macOS

Python is platform independent.

Why Python Runs Everywhere

Python doesn't execute your source code directly.

Instead, it follows this pipeline.

.py to byte code to PVM to Windows Linux macOS

Step-by-step

  1. You write a .py file.
  2. Python compiles it into byte code.
  3. The PVM on that operating system executes it.
  4. 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.

python
import platform

print("Operating System:", platform.system())
print("Python Version:", platform.python_version())

Example output on macOS

text
Operating System: Darwin
Python Version: 3.12.0

Example output on Windows

text
Operating System: Windows
Python Version: 3.12.0

Notice 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

python
import platform

print(platform.system())
print(platform.machine())
print(platform.python_version())

Possible output:

text
Darwin
arm64
3.12.0

Visualizing Different Platforms

Same program runs when Python is installed on each OS

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:

  1. Operating System
  2. Machine Type
  3. Python Version
python
import platform

print("Operating System :", platform.system())
print("Machine Type     :", platform.machine())
print("Python Version   :", platform.python_version())

Sample output

text
Operating System : Darwin
Machine Type     : arm64
Python Version   : 3.12.0

Run 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 .py file can run on Windows, Linux, and macOS.
  • Byte code and the PVM make cross-platform execution possible.
  • Use the platform module to inspect the current system.

Interview Questions

  1. What is meant by a platform in programming?
  2. Why is Python called platform independent?
  3. What is the role of the PVM in cross-platform execution?
  4. Which module provides operating system information?
  5. 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.