Are you ready to learn Python programming? Look no further and start with my free Python tutorial for beginners. We’ll dive into the basics and work our way up to advanced concepts. I’ll provide you with many examples to explain all the concepts clearly.
Table of contents
Why this Python tutorial?
I admit there are many terrific Python tutorials out there. Why should you read this one, instead of the others?
Here are a few reasons:
- This tutorial is easy to read and ideal for beginning programmers. I do my best to explain stuff in simple terms, making it easy to understand for everyone.
- Interactive example code: edit and run the interactive examples. It’s great fun and helps you to learn concepts much better.
- This Python tutorial is practical. I kept the dry theory to an absolute minimum and focused on getting stuff done in the real world instead. But at the same time, I try to explain how things really work instead of teaching you tricks.
- On most pages, I provide you with carefully vetted links to deepen your knowledge.
- It’s a completely free Python tutorial for beginners and intermediate programmers. There are no strings attached.
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:
Want the premium experience?
You could see this tutorial as a collection of articles put into the right order. I offer an alternative that is more streamlined and offers a superior learning experience: my Python course for beginners. It’s based on the articles on this site but is built on a fully-featured learning platform. The course includes:
- A streamlined introduction to the Python language
- Many quizzes that test your knowledge and increase your understanding of the topics discussed
- An easy way to keep track of your progress
- The end project in which we put the material into practice
- An official certificate of completion (PDF) that you can download and use on your LinkedIn page or on your resume.
Besides these advantages, buying my course helps me to improve this site and write more content. Because I just launched the course, I’m looking for your feedback and comments. For this same reason, I’m offering it at a 50% discount (for lifetime access):
What you’ll learn from this Python tutorial for beginners
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 will be able to continue exploring Python on your own. You won’t feel lost anymore, but 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 been a professional software engineer for more than 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 Python tutorial for beginners. I started writing in 2019, and regularly write new articles. I also made a habit of tweaking the existing articles, making sure everything is in sync and up-to-date.
Eventually, I got fed up with the limited code examples that you need to copy and paste and wanted example code that is actually 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 already has 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. I also tend to post interesting code snippets and quizzes from time to time.
What is Python?
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.
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: the original creators of YouTube used Python for the most part! Dropbox, as far as I know, is primarily written in Python as well. And did you know Instagram’s entire backend and website are written in Python as well?
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 of stating 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.
What are 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. In Python, we need to indent all code blocks 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 it’s a subjective matter, people generally agree that it makes Python easier on the eyes.
You don’t need to manually compile your program
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 actually 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 even 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 just run our code directly and don’t have to worry 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. As long as the platform has a Python interpreter, your code will work. 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 it’s 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 ever goes to production.
Python does support typing
I’m not arguing that typing is a bad thing, though. In fact, Python supports type hints since Python 3.5. It’s an optional feature, but many programmers embrace it since it has quite a few 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 that 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 Python will do a perfect job, and it will never accidentally clean up a variable that you still need.
What is Python’s popularity?
Python’s popularity is a great advantage. There are vast amounts of tutorials, books, courses, sample code, and help available. Python is here to stay; learning it is a safe bet! In fact, Python jobs are generally raking high on the pay scale. This Python tutorial for beginners should give you a great start in thoroughly learning Python and advancing your career.
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 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 turned out to be a success because immediately his CWI colleagues, the users, and Guido himself 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 now lies in 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.
Navigating the Python tutorial for beginners
You can browse the Python tutorial by using 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.
Even though each page tries to stand on its own, the tutorial has several sections that I carefully ordered so that you can start from the beginning and work your way up. However, feel free to browse around! Especially if you’re not a complete beginner.
For reference, here’s a global walkthrough of the Python tutorial:
- Install Python: learn how to install and open Python.
- Introduction to Python: start with the basics! This section is suitable for beginners, but I recommend everyone browse through it before continuing with the other sections. You’ll learn about Python variables, strings, functions, conditional programming with booleans, loops, and more.
- Creating Python programs: when you know the basics, it’s time to create actual programs. For this, you need a proper Python IDE like VSCode with Python extensions.
- Classes and objects: a continuation of the introduction to Python. This subject is essential knowledge, and I advise you not to skip it.
- Python data types: in this section, we take a closer look at Python data types like strings, integers, tuples, sets, lists, and dictionaries.
- Virtual environments: these are an important part of Python, so we have a comprehensive section on how to create, use, and delete a venv. We’ll also look at installing packages with Pip, and we’ll dive into some tools to make life easier, like Pipenv.
- Data science using Python: we offer a learning path to using Python for data science, with links to articles from Python Land and external resources. We’ll also look at Jupyter Notebooks, which are ideal for this type of work.
This tutorial is constantly evolving. New pages are added weekly, and I’m working on a couple of awesome features to improve the learning experience. Make sure to check back regularly and sign up for the newsletter to stay up-to-date!
How you can help me
You, yes, that’s you, can help me improve this Python tutorial for beginners. There are two things you can to do help.
Get in touch if you…
- find any mistake,
- think something can be improved,
- or something is unclear to you.
If it’s unclear to you, it’s probably unclear to many. I’m not perfect, and there are no silly questions. Don’t hesitate to drop me a message; I love hearing from my readers. Please use the contact form. Even though you can add comments below most articles, I’d prefer to reserve those for discussions about the page topic itself.
I’ve been working on this site for about 3 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. If you want to show your appreciation, you can buy me a coffee. All the support I got so far is what encourages me to keep writing and keep updating the content!
You can follow me (@erikyan) to get updates on new content. I also tend to post interesting code snippets and quizzes from time to time. This site has a dedicated mailing list as well. You can subscribe at the bottom of each page. It’s low volume and mostly contains stuff like new articles I wrote and interesting links to read.
if ready: print("Let's start learning Python programming")