Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Python Basics: Functions and Loops (Overview)

Functions are the building blocks of almost every Python program. They’re where the real action takes place!

In your Python Basics journey, you’ve probably encountered functions such as print(), len(), and round(). These are all built-in functions because they come built into the Python language itself. You can also create user-defined functions that perform specific tasks.

Functions break code into smaller chunks and are great for defining actions that a program will execute several times throughout your code. Instead of writing the same code each time the program needs to perform the same task, just call the function!

But sometimes you do need to repeat some code several times in a row. This is where loops come in.

In this Python Basics video course, you’ll learn:

  • How to create user-defined functions
  • How to write for and while loops

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. You can also check out the other Python Basics courses.

Note that you’ll be using the REPL to interact with Python in this course.

Download

Sample Code (.zip)

552 bytes
Download

Course Slides (.pdf)

9.0 MB

00:00 Welcome to Python Basics: Functions and Loops. I’m Philipp with Real Python, and today we’ll talk about—well, you guessed it—functions and loops.

00:12 Functions are the building blocks of almost every Python program. They are where the real action takes place. Functions break code into smaller chunks. They are great for defining actions that a program will execute several times.

00:25 So instead of writing the same code each time, the program needs to perform the task, just call a function. You can think of a function like it being mixed dough.

00:35 The dough for pancakes may contain a bunch of ingredients, like flour, eggs, butter, and milk. But later, when you bake the pancakes, you just work with the dough instead of the individual ingredients.

00:50 But sometimes you need to repeat some code several times in a row. This is where loops come in. A loop comes in handy when you want to perform a task over and over again.

01:00 You can think of loops like it being a donut. I don’t know about you, but I can’t just eat one donut. To be really satisfied, I need to eat multiple donuts in a row. In this course, we’ll have a closer look at both functions and loops.

01:16 And we’ll also talk a little bit about pancakes and donuts.

01:21 Let me show you the structure of this course. First, we’ll have a look at functions that you may already know. You will learn how to call a function and what happens when Python executes a function. With the knowledge of the first lesson, you will be ready to create your own function, and we will have a look at the anatomy of a function and explore some details.

01:42 In the third lesson, I will show you how you can document your newly created function.

01:49 After this deep dive into functions, we will hop over to the next big part of the course: loops. You will learn about while loops and for loops—how to write them and what’s the difference between them.

02:01 You will cover quite some ground during this course, so I’ll give you a bunch of resources at the end to continue your journey as a Python developer. Sounds good? Okay, then let’s get started.

Become a Member to join the conversation.