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 see our video player troubleshooting guide to resolve the issue.

Python Basics: Scopes (Overview)

On your Python journey, you’ve probably learned about functions and loops. To fully understand functions and loops in Python, you need to be familiar with the issue of scope.

By the end of this video course, you’ll know:

  • What a scope is and why it’s important
  • How Python applies the LEGB rule for scope resolution

Scope can be one of the more difficult programming concepts to understand, so in this video course, you’ll get a gentle introduction to it.

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 IDLE to interact with Python throughout this course. If you’re just getting started, then you might want to check out Python Basics: Setting Up Python before diving into this course.


Sample Code (.zip)

1.3 KB

Course Slides (.pdf)

4.2 MB

00:00 Welcome to Python Basics: Scopes. I’m Philipp with Real Python, and today we’ll talk about scopes. Have you ever spotted unusual behavior in your Python code—for example, when variables didn’t have the value you expected them to have, or assigning variables didn’t work? In those cases, you may have made contact with the concept of scope. In this course, you’ll get an introduction to scope, and as you will learn, scope in Python is not just something that makes your code behave weird.

00:32 Quite the contrary—once you learn about scope, you gain better control of how your Python scripts work.

00:39 Here is the map of the journey that will go on together. In Understanding Scope, I will describe to you what scope is in Python and why it’s important. You’ll learn about terms like names and namespace.

00:53 To understand how Python resolves names, I’ll introduce the LEGB rule in the second lesson. LEGB stands for local, enclosing, global, and built-in scope.

01:04 You’ll explore all of them in lesson three and four.

01:09 After that, I’ll show you the global statement. It’s important to know why the global statement exists. I will also show you how to use it, but a word of caution already: using the global statement can cause issues with your code down the line.

01:25 That’s why we will explore good programming practices in lesson six, preventing pitfalls. At the end of this course, I’ll also give you additional resources if you want to continue your journey into the vast Python sphere. All right, let’s start exploring.

Become a Member to join the conversation.