Save this video free

Python (Fall 2026) Module 7-7: Variable Scope and Globals

Bill Moseley, Ph.D. - Computer Science Educator · 9:11 · Watch on YouTube

Python (Fall 2026) Module 7-7: Variable Scope and Globals Watch on YouTube →

Overview

Bill Moseley explains Python variable scope through an `x` example: an `x` assigned inside a function is local and does not change an `x` outside it, while a function can read a global variable when no local `x` is defined. Declaring `global x` lets the function reassign the outer variable, but ties the function to its surrounding program; Moseley recommends parameters and return values for reusable black-box functions.

Key takeaways

Chapters

0:00 Function Black Boxes and Local Variable Scope
3:24 Reading Global Variables and Avoiding Confusing Names
5:39 Using `global x` and the Trade-Off with Return Values

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

0:01 Now, reviewing what we talked about last time,  remember that when we build a function, if we, if we're building it well, we should be able  to use that function in a variety of different programs. And so, the way we communicate using  a function is this whole black box idea, which means we don't worry about how it works. All we  need to know is kind of what it does, as well as what goes into it and what comes out of it. If we  understand those things about a function, we can use and reuse a function. We can share functions  with other programmers. And honestly, that's one of the great things about Python, is that people  have written all kinds of functions and libraries and stuff out there that they share to allow us to  do all kinds of things with Python without having to know all the nitty-gritty of how that works.  So anytime we're doing this, we want to build our

1:00 functions so that they operate with parameters  and return values as sort of a black box. And so that brings me to variable scope. Variable  scope refers to where information is available. Now in this very simple function, showVars,  or show variables, all this does is it says, inside the function I'm creating a variable called  x, I'm setting it to the value of 33, and then I'm printing, right now x equals, and it prints the  value of x. So all it's going to do every time I run that function is going to say x = 33. But if I  write this code and I start out with x = 20, okay, outside the function, in my main line of code, I'm  saying x = 20. And then I'm printing it, x = 20.

This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.

Related notes

Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.