Python is one of the most popular programming languages in the world — and for good reason. Clean syntax, massive community, thousands of ready-made libraries. It works equally well for absolute beginners and for experienced engineers. This Python basics guide walks you from a blank editor to a working industrial script — variables, conditionals, loops, functions, file I/O, error handling, modules — every core concept with an automation-context example.
What Python Is and Why You Should Learn It
Python is an interpreted, dynamically-typed language with a readability-first philosophy. Created by Guido van Rossum in 1991, it took off because the language itself gets out of your way — no semicolons, no curly braces, no declarations. You indent a block, write what you want to do, and it runs.
Python is used in industrial automation, data analysis, machine learning, web development (Django, Flask), DevOps scripting, game development, and yes — factory floors. The TIOBE index consistently places it in the top 3 most-used languages worldwide.
Installation — VS Code and the Python Extension
You need two things: a code editor and the Python interpreter itself. The easiest path is Visual Studio Code (free, from Microsoft) with the Python extension, which installs everything else automatically.
Step by step:
- Download and install VS Code from code.visualstudio.com.
- Open VS Code, go to Extensions (square icon on the left sidebar, or
Ctrl+Shift+X). - Search for “Python” and install the Microsoft extension.
- On first
.pyfile open, VS Code will prompt you to install the Python interpreter — click Install and you’re done.
If you see a version number, the environment is ready. VS Code automatically recognizes .py files, highlights syntax, autocompletes functions, and lets you run your script with one click (the green ▶ in the top-right).
First Program — Hello World
Tradition demands a Hello World. In Python it’s exactly one line:
Save as hello.py and run with python hello.py in the terminal. The text appears on screen. Congratulations — that’s a working Python program.
Variables and Data Types
Variables in Python are containers for data. You do not declare types; Python infers them. Just assign with =:
The core types you need from day one:
| Type | Description | Example |
|---|---|---|
int | Integer | 42, -7, 0 |
float | Floating-point | 3.14, -0.5, 100.0 |
str | Text (string) | "hello", 'Python' |
bool | Boolean | True, False |
list | Ordered collection | [1, 2, 3] |
dict | Key-value mapping | {"key": "value"} |
Operators
Operators act on data — arithmetic, comparison, logical.
Conditionals — if, elif, else
Conditionals let the program make decisions. Python uses if, elif (else if) and else:
Note the indentation. Python uses indentation (not curly braces) to define code blocks. The standard is 4 spaces. Mixing tabs and spaces is a classic source of bugs — VS Code’s Python extension handles it automatically.
Loops — for and while
Loops repeat operations without copy-pasting code. Python has two: for (iterate over a collection) and while (repeat while a condition holds).
Functions — Organize Your Code
Functions group reusable logic under a name. Define with def. Functions can take arguments and return values:
Lists — Ordered Collections
A list is an ordered collection, potentially of mixed types. One of the most-used structures in Python — you’ll store sensor readings, file paths, computation results.
Dictionaries — Key-Value Data
A dict stores data in key: value pairs. Perfect for configuration, sensor metadata, or device parameters — lookup is by name, not index.
String Operations
Strings in Python are a powerful type with dozens of built-in methods. Here are the ones you’ll use daily:
File Handling
Python handles file I/O cleanly. The with open() pattern automatically closes the file when done — use it always.
File modes: "r" read, "w" write (overwrites), "a" append, "rb" / "wb" binary.
Error Handling — try / except
Errors are not failures if you catch them. try / except lets you handle expected exceptions cleanly — log the error, warn the user, retry, fail gracefully.
else runs only when no exception was raised. finally runs always — even if an exception was raised and re-raised. Use finally to close connections, release locks, or flush buffers.
Modules and Libraries
Python’s real power is its ecosystem. The standard library (os, math, datetime, json) is huge, and thousands more libraries are installable via pip.
Practical Project — Temperature Analyzer
Let’s tie it all together. The script below ingests a list of temperature readings, computes statistics, and writes a text report.
This script combines functions, lists, dictionaries, list comprehension, file I/O, and string formatting. It’s a compact demonstration of how a handful of Python fundamentals already give you a real, useful tool.
FAQ
Is Python hard to learn?
Not really. Python was designed to be readable, and the basic syntax is closer to English than to most programming languages. Expect roughly 20-30 hours of focused practice to reach the point where you can write the kind of analyzer script above from scratch.
Do I need to install anything besides Python and VS Code?
For the basics in this guide — no. For industrial work you will eventually install additional packages via pip: pandas for data, pymodbus for Modbus communication, asyncua for OPC UA, and so on. All are free.
What’s the difference between Python 3.10, 3.11, 3.12?
Each release brings speed improvements, better error messages, and new language features. Unless a library you need pins a specific version, install the latest stable release (3.12 or newer). Don’t install Python 2 — it’s been deprecated since 2020.
Can I run Python on Windows, macOS and Linux?
Yes, on all three natively. Your scripts will run identically across platforms as long as you avoid OS-specific file paths. Use pathlib for cross-platform paths.
Is there a difference between print("text") and print('text')?
No. Python treats single and double quotes identically. Pick one and stay consistent within a project — double quotes are the more common convention in modern codebases.
Should I use tabs or spaces for indentation?
Spaces, 4 of them, per PEP 8. VS Code with the Python extension enforces this automatically.
How do I reuse code across files?
Put shared functions in a module (a .py file) and import it from your main script: from myutils import analyze_temperature. For bigger projects, create a package (a directory with an __init__.py).
What’s Next
You now have all the ingredients: variables, types, conditionals, loops, lists, dicts, strings, functions, files, error handling, modules. That’s the full toolkit for writing useful scripts. The next step is volume — write a lot, break things, fix them, repeat.
If you want to apply Python specifically to industrial automation — with pandas, Modbus, Snap7, HTML dashboards and a full end-to-end project — our Python for Automation Engineers course takes you from this guide to deploying scripts next to real PLCs. Every exercise is framed in a plant-floor context, so you learn the language while already building something you can use at work.
For a broader overview of where Python fits next to automation, see our companion article Python for Industrial Automation.



