This free Python tutorial contains carefully crafted, logically ordered, free Python articles full of information, advice, and Python practice! This tutorial is ideal for both complete beginners and those with prior experience. You learn at your own pace, and I’ll provide you with many examples to explain all the concepts clearly.
Table of contents
In a hurry to learn Python?
If you’re in a hurry to learn Python, I’ll give you some shortcuts right now to get you started quickly.
First, if you need to install Python, check out the section on Installing Python.
With Python installed, you can do two things:
- head directly to the Introduction to Python to start learning the language! From there, you can follow the free Python tutorial using the navigational links and menus.
- Support my work and go for the premium course, Python Fundamentals I. It’s designed to learn Python quickly but properly, without distractions, lots of quizzes, exercises, and a certificate of completion that you can add to your resume.
Why this free Python tutorial?
Here’s why you should read this one instead of all the others:
- This free Python tutorial is written in easy-to-read, plain English.
- I’m an experienced writer and tutor who puts great care into the learning material and the order in which it is presented.
- This tutorial contains interactive example code you can edit and run. It’s great fun and helps you to learn concepts much faster.
- This tutorial is practical. While focussing on getting stuff done in the real world, I also explain how and why things work instead of teaching you tricks.
- I provide you with carefully vetted links on most pages to deepen your knowledge.
- Did I mention it’s completely free, with no strings attached? I do offer premium Python courses for those looking for a premium experience, extra practice, and a (PDF) certificate of completion.
Interactive example code
Here’s an example of how I included interactive, runnable code throughout the tutorial. Feel free to play around with this ‘Hello World’ example. You can edit and run it:
What you’ll learn from this free Python tutorial
You will learn about computer programming using the most popular programming language in the world. My goal is to make you understand the language and the ecosystem. After reading this tutorial, you can continue exploring Python on your own. You won’t feel lost anymore; instead, you will know where to look when you’re trying to solve a problem.
This Python tutorial for beginners covers a broad range of topics that will get you productive with Python in no time. I won’t teach you just the basics, but we’ll also dip our toes in advanced topics, like deploying your code and properly using virtual environments and package management.
Who am I?
Let me introduce myself. After all, you should ask yourself the question: what makes me eligible to teach you Python through this tutorial?
I’m Erik, and I’ve worked as a professional software engineer for over 25 years. I used many programming languages in my career, but Python is my absolute favorite! I love programming and building complex systems, but I also love to write. That’s why I decided to combine these two by writing this free Python tutorial for beginners. After that, I started working on my premium courses: Python Fundamentals I and II. They are pretty successful and, so far, have gotten some great reviews!
Eventually, I got fed up with the limited copy-and-paste code examples and wanted example code that is editable and runnable in-page. It resulted in a side project (crumb.sh) that offers a generic way to do this. It’s still under development, but the tutorial and courses already have many of these useful code crumbs sprinkled throughout it!
If you’re on Twitter, you can follow me (@erikyan) to get updates on new content. If you prefer e-mail, try my Python newsletter as well! I try to share interesting code snippets, exercises, and quizzes regularly.
Let’s start by defining exactly what Python is. Python is a computer programming language. Or, in other words, a vocabulary and set of grammatical rules for instructing a computer to perform tasks. Its original creator, Guido van Rossum, named it after the BBC television show ‘Monty Python’s Flying Circus.’ Hence, you’ll find that Python books, code examples, and documentation sometimes contain references to this television show.
Python is considered easy to learn, and it’s designed around a set of clearly defined principles (the Zen of Python) that encourage Python core developers to make a language that is unambiguous and easy to use.
What is Python used for?
People use Python in many places. Its rich base library makes it excellent for all kinds of little helper scripts. But it scales just as well to large systems. To illustrate, YouTube’s original creators used Python for the most part! As far as I know, Dropbox is primarily written in Python. And did you know Instagram’s entire backend and website are also written in Python?
You can use Python to automate tasks, perform calculations, create user interfaces, create website backends, access databases, download information from the Internet, etc. It’s a versatile language that is easy to learn and write, and although perfect for beginning programmers, is just as useful and powerful for seasoned professionals.
Python is extremely popular in a quickly growing field of expertise called data science. Many data scientists use Python for their day-to-day work. And these are just a few examples. If you start looking closely, Python is very ubiquitous.
Many people say that Python comes with batteries included. It’s a fun way to state that it includes a comprehensive base library. In addition to this, you can find hundreds of thousands of external packages contributed by the enormous community. You’ll find supporting base libraries and packages for pretty much anything you want to accomplish.
Python’s popularity is a great advantage. There are vast amounts of tutorials, books, Python courses, sample code, and help available. Python is here to stay, so if you want to learn Python: it’s a safe bet! Python jobs are ranking high on the pay scale. This free Python tutorial for beginners will give you a great start in thoroughly learning Python and advancing your career.
Python’s major features
So what makes Python such a popular programming language? You’ll find out when reading this Python tutorial for beginners, but I can already show you some of the advantages to whet your appetite!
It’s easy to read and write
One of Python’s most notable features is the way it enforces the use of indentation for readability. Without proper indentation, your code won’t even run. We need to indent all code blocks in Python to the same level. Here’s an example of this at work. If you don’t understand the code yet, don’t worry:
def bigger_than_five(x): # The contents of a function are indented if x > 5: # This is another, even more indented block of code print("X is bigger than five") else: # And one more! print("x is 5 or smaller")
Because indentation is required, the Python language does not need curly braces to group code blocks like Java, C, and C#. This fact alone removes a lot of clutter and symbols from your code. Although subjective, people generally agree that it makes Python easier on the eyes.
You don’t need to compile your programs manually
Many languages require a manual compilation step before you can run your program, while other languages are so-called interpreted languages: they can be run directly by the interpreter. Python is somewhere in between these two worlds.
When you create a Python program, you can run it directly without a manual compilation step. This is the case with all interpreted languages, and that’s why most people will tell you it is an interpreted language. However, internally Python compiles your code into lower-level code called bytecode. This code is not specific to a system architecture (like Intel vs. ARM), but it is faster to read and parse than plain text files.
In some situations, this bytecode is cached on disk in files ending with the *.pyc extension. But when used as a scripting language, Python won’t cache the bytecode. To us, it doesn’t matter. We can run our code directly without worrying about bytecode since Python handles it all automatically. This has several advantages:
- You can write your code in a text editor and execute it directly. No additional steps like compilation and linking are necessary.
- Because it’s plain text, you can simply open a program and inspect its contents. In contrast, compiled code is not human-readable. You would have to look up the source code (if it’s available at all).
- It is platform-independent. Your code will work as long as the platform has a Python interpreter. Compiled code often ties itself to a specific platform, like Windows and Linux, and specific processor architecture, like Intel or ARM (unless it gets compiled to intermediate bytecode, like with Java and .NET).
Some of these advantages can also be a disadvantage. As already mentioned, interpreted languages are not high-performance languages. Also, the fact that the source code is easy to read and modify is not an advantage to vendors that want to protect their copyright.
Another advantage of interpreted languages is that it opens the door to dynamic typing. What does that mean? I’ll demonstrate it with some simple code.
Here are a few variable declarations in Java:
String myName = "Erik"; int myAge = 37; float mySalary = 1250.70;
In a strongly typed language, you need to specify the exact type of each variable, like
float. It gets even uglier when objects are involved.
Now let’s look at Python variables. In Python, we can do exactly the same without types:
my_name = "Erik" my_age = 37 my_salary = 1250.70
As you can see, the Python variant is a lot cleaner and easier on the eyes!
When running this code, Python dynamically finds out the type of our variables. Say, for example, I’d like to know my yearly income by multiplying my salary by 12. I’d need to do the following:
my_income = my_salary * 12
Python will look at
my_salary, see that it is a floating-point value, and perform the math. If
my_salary would have been a string, Python wouldn’t complain though. It would detect a string and just create a new one, consisting of 12 repetitions of that string! Java, however, would fail with an error in such cases.
Dynamic typing has many advantages. In general, it makes it easier to get started quickly. Some will tell you that it’s more error-prone. A strongly typed language like Java won’t compile when there’s a type error. Python will probably continue running, but the output will be unexpected. It is my experience that it doesn’t happen that often. In addition, you’ll find out soon enough during testing and fix the error before the software goes to production.
Python does support typing
I’m not arguing that typing is a bad thing, though. Python has supported type hints since Python 3.5. It’s an optional feature, but many programmers embrace it since it has several advantages, like better auto-completion in your Python IDE. I love typing because it takes away the guessing. Explicit typing is a form of documentation, and I use it where appropriate in my own day-to-day work.
Python has the concept of variables. A variable allows you to store any value, like a number, a string of text, or even bigger objects.
Each variable you declare takes up space in your computer’s memory. This can add up quickly, especially when you create programs that run for a long time. So you need a way to clean up variables you don’t use anymore.
In some languages, you need to perform this cleanup explicitly. This is prone to a type of error called a memory leak. If you make a little mistake and forget to clean up, your software will slowly eat up available memory. Lucky for us, Python’s garbage collector automatically cleans up unused variables!
I’m not going into the nitty-gritty details here, but you can rest assured that Python will do a perfect job and will never accidentally clean up a variable you still need.
We’ve touched the subject a little already, but let’s explore why Python came to be. It all started on a cold, foggy night in December 1987 when a Dutch scientist called Guido van Rossum woke up in the middle of the night. He just had a profound dream, and although he didn’t know it at the time, that dream would eventually change his life and the lives of many others.
So he got out of bed and slipped into his pantofles. After throwing some wood in the almost smothered fireplace, he started jotting down as much of this dream as he could remember. A new programing language was born: Python.
The inception of Python
OK, I got a little carried away there. The only truth from the above story is the name Guido van Rossum and the inception date. In 1987, Guido worked on a big distributed operating system at the CWI, a national research institute for mathematics and computer science in the Netherlands. Within that project, he had some freedom to work on side projects. Armed with the knowledge and experience he had built up in the years before, working on a computer language called ABC, he started writing the Python programming language.
In a 2003 interview with Bill Venners, Guido mentioned what was probably the biggest innovation in the new language:
I think my most innovative contribution to Python’s success was making it easy to extend. That also came out of my frustration with ABC. ABC was a very monolithic design. There was a language design team, and they were God. They designed every language detail and there was no way to add to it. You could write your own programs, but you couldn’t easily add low-level stuff.Guido van Rossum
He decided that you should be able to extend the language in two ways: by writing Python modules or by writing a module entirely in C. It was a success because his CWI colleagues, the users, and Guido himself immediately started writing their own extension modules. The extension modules let you do all sorts of things. Just a small selection of modules that exist today:
- graphics libraries,
- data processing and data science libraries,
- libraries to work with all sorts of file formats (like JSON, YAML),
- communicate over the network
- build websites and website backends
- … and so on
Since its inception, Guido has been actively involved in Python’s development until this day. After a short retirement, he returned to work. Microsoft currently employs him, and his main focus is improving Python’s speed.
The following figure shows a global timeline of Python’s historical and most defining releases:
Python 2 vs. Python 3
As you can see from the Python history timeline, Python 2 and 3 have been developed and maintained side by side for an extended period. The primary reason is that Python 3 code is not entirely backward compatible with Python 2 code. This incompatibility caused a prolonged adoption rate. Many people were happy with version 2 and didn’t see much reason to upgrade. On top of that, Python 3 was initially slower than Python 2. As Python 3 kept improving and receiving new features, eventually, it started to take off.
This guide focuses entirely on Python 3 since it is now the default and only supported version. In the real world, you may encounter Python 2 code. I shared some tips on migrating from such code in the chapter Migrating from Python 2 to 3. In case of doubt: if you want to learn Python, go for Python 3.
Navigating the free Python tutorial
I did everything I could to make browsing the free Python tutorial as easy as can be! Primarily, you can use the menu. In addition, there are navigational links at the top and end of each page to guide you to the next topic or go back to the previous one. I also link to related pages to deepen your knowledge.
I carefully ordered the tutorial topics so that you can start from the beginning and work your way up. However, feel free to browse around!
How can you help me?
You, yes, that’s you, can help me improve this Python tutorial for beginners. There are several things you can do to help.
1. Get in touch if you…
- find any mistake,
- think something can be improved,
- or something is unclear to you.
2. Donate or buy the course
I’ve been working on this site for about three years now, spending most of my spare time here with all my heart and soul. I hope it shows, and I truly hope you have a lot of fun learning Python here.
The best way to support my work is by buying the course, but I understand that not everyone has the budget. If you still want to show your appreciation, you can buy me a coffee. All the support I have gotten so far encourages me to keep writing and updating the content!
3. Disable your adblocker
If you block ads and don’t want to donate or buy the course, I kindly ask you to disable your adblocker on this free Python tutorial. You might think it won’t help, but please realize that writers like me struggle to earn from their hard work because of it. I only run safe, trustworthy ads that are often extremely well targetted to the site’s topics too.
4. Follow me
You can follow me (@erikyan) to get updates on new content. I also tend to post interesting code snippets and quizzes occasionally. This site also has a dedicated mailing list; click here to subscribe to my newsletter. It’s low volume and mostly contains new articles I wrote, interesting links to read, and the occasional discounts for my premium courses!
Ready? Why not make it official and announce to your followers (and me!) that you’re starting this tutorial? It’s a great way to get some extra motivation, and perhaps someone will follow your lead!
if ready: print("Let's learn Python programming!")
Head over to the next section, installing Python, to get started.