Python Basics Exercises: Scopes (Summary)
In this video course, you’ve practiced working with scopes. Remember, 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 practiced:
- Identifying the scope of objects
- Working with the
global
keyword - Exploring the LEGB rule for scope resolution
- Using the
return
statement in a function
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:
- Namespaces and Scope in Python
- Python Scope & the LEGB Rule: Resolving Names in Your Code
- Exploring Scopes and Closures in Python
- Python Inner Functions: What Are They Good For?
- Python 3’s f-strings: An Improved String Formatting Syntax (Guide)
If you’re ready to continue your Python journey, then check out the other Python Basics courses.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
Congratulations on organizing such a great party and on finishing this Python Basics Exercises course. In this course, you practiced how to spot scopes, work with the global
keyword, explore the locals()
function, and use a return
statement.
00:16 And of course, you organized a fun party.
00:21 If you want to deepen your knowledge about scopes, I have some additional resources for you as usual. In “Namespaces and Scope in Python”, you’ll learn about Python namespaces, the structures used to store and organize the symbolic names created during execution of a Python program.
00:38 You’ll also learn when namespaces are created, how they are implemented, and how they define variable scope. This tutorial or video course gives you a great overview about what you have learned in the Python Basics course about scopes or this exercise course, and it goes way deeper, so it’s definitely worth a watch.
00:58 Another course on the topic of scope is “Exploring Scopes and Closures in Python”. This video course is a code conversation, and you’ll take a deep dive into how scopes and closures work in Python.
01:12 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.
01:22 During the former lessons, you also experienced nested functions, so if you want to learn more about them, our “Python Inner Functions” tutorial is perfect for you.
01:32 You learn what inner functions are in Python, how to define them, and what their main use cases are except of hiding the cookies, of course.
01:43 And that’s all I got for you today. Thanks for joining me on this video course and helping me out to organize a little party together. See you around and until next time, at realpython.com.
Become a Member to join the conversation.
Pankaj Poddar on Nov. 19, 2024
Dear Team, In the summary it is mentioned that we have practiced global keyword, however I don’t recollect we have used the global keyword in this course yet.