Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Basics: Scopes (Summary)

In this video course, you’ve learned about scopes. You can think of a scope as a set of names mapped to objects. When you use a particular name in your code, such as a variable or a function name, Python checks the current scope to determine whether that name exists.

A useful way to remember how Python resolves scope is with the LEGB rule. LEGB is an initialism for Local, Enclosing, Global, Built-in, which describes the order by which Python resolves scope.

In this video course, you’ve learned:

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

Scope can be confusing, and it takes some practice for the concept to feel natural. Don’t worry if it doesn’t make sense at first. Just keep practicing and use the LEGB rule to help you figure things out.

For more information on the concepts covered in this course, check out:

If you’re ready to continue your Python journey, then check out the other Python Basics courses.

Download

Sample Code (.zip)

1.3 KB
Download

Course Slides (.pdf)

4.2 MB

00:00 Congratulations, you’ve made it to the last lesson of the Python Basics: Scope course.

00:06 In this course, you learned what scopes are and how they work in Python, why it’s important to know about the Python scope, and what the LEGB rule is, and how Python uses it to resolve names.

00:20 You also learned how to use the global statement and how to implement good programing practices to actually prevent using the global statement.

00:31 Here are a few additional resources for you to dig deeper into the topics that we tackled in this course. In Namespaces and Scope in Python, you learn about Python namespaces, the structures used to store and organize the symbolic names created during execution of a Python program.

00:49 You’ll learn when namespaces are created, how they are implemented, and how they define variable scope. Another course on the topic of scopes is Exploring Scopes and Closures in Python. In this Code Conversation video course, you’ll take a deep dive into how scopes and closures work in Python.

01:08 To do this, you’ll use a debugger to walk through some sample code, and then you’ll take a peek under the hood to see how Python holds variables internally. If you want to learn more about nested functions, then our Python Inner Functions tutorial is perfect for you.

01:24 You’ll learn what inner functions are in Python, how to define them, and what their main use cases are. And last but not least, f-strings. As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable and concise, f-strings are also evaluated at runtime, so you can put Python expressions in them.

01:48 This allows you to do some nifty things. In this tutorial, you will learn all the cool things that you can do with f-strings.

01:57 With your knowledge of scopes in the back seat, you are well equipped to continue your Python journey. You’re now able to take advantage of Python scopes to avoid or at least minimize bugs related to name collision.

02:09 Now grab your sunglasses and start coding. Well, actually, I don’t know if it’s a good idea to wear sunglasses while coding. I mean, yeah, it looks cool, but I don’t know if you can see the screen properly. Yeah.

02:22 Anyway, you know what’s best for you. My name is Philipp. It was fun being your instructor today. Have safe travels and see your next time at realpython.com.

Become a Member to join the conversation.