Python Programming Tutorial #19 - Global vs Local Variables

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·9y ago
Global vs Local Variables in Pyhton. This is the 19th video in my python programming series and in today's video I talk about local and global variables. Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/global-vs-local/ Please LIKE ans SUBSCRIBE for more content. Video Tags: python,python tutorial,python language,python full course,python course,learn python,learn python programming,python tutorial for beginners,python tutorial 2018,python programming tutorial,python programming language,software development,programming tutorial,tech with tim

What You'll Learn

This Python programming tutorial covers the concept of global vs local variables, including how to declare and access variables within functions and outside of them, and how to use the global keyword to modify global variables from within a function.

Full Transcript

hey guys welcome back to another video this is the 19th video in my Python programming series and today we're going to be talking about a fairly complex topic some people find kind of confusing in their programs it's very useful to understand and its global versus local variables so global um pretty much means everywhere can be used in anything and local means specific to a certain a block of code or to a certain class so let's just dive in right away with an example so in our program we have things called variables again we've talked about them before so I'm just going to say var up here equals 9 we'll say are true this is lowercase here is just equal to true or actually let's call it loop instead just not to confuse anyone so loop is equal to true and then maybe let's make a function we're just going to call this function func we'll give it a parameter of X and we're just going to say if X is equal to 2 equals sign 5 then return true and we'll set a variable up at the top of this function and we'll just call it a new var is equal to 7 or yes 7 let's let's do that and then instead of returning to you will actually return to var my god gave our just like that ok so here we have three variables we have the variable var which is equal to 9 the variable loop which is equal to true and then the variable new var which is inside of our function and that's equal to 7 and then the parameter X which again can kind of be considered a variable but it is more specifically a parameter so I just want to show you right away what tries to happen if I print out the variable noubar so outside of my function in its own line I'm just going to say print new var like that we click f5 and we get an error here it says the name new var is not defined but how can that be possible we just made new var right here in our phone well this is where we get into local versus global variables so the variable nu bar is actually known to be a local variable to the function func so that means the only thing that's able to access the variable change the variable and use the variable new var is the function func so again if I put the print statement inside of my function and then I call my function it will work so here if I type func now and I'll give it value to the new var does print to the screen and we don't get an error so that is how the local variable works so again it's the same thing in any other type of function so if I write another function I do define other func this one I won't give it a parameter and I'll just say you var is equal to five like that so different than seven and then this function will print new var well do we think it's going to print out seven or do we think it's going to print out five so we'll show you like this if we run take zero positional arguments other func oh my brother's are two in there sorry it prints out five right so it doesn't look at this variable here it looks at the local variable in here called new var now same thing if I put new var up here outside of all of these functions and I set it equal to 23 and then I click f5 here to run it's still going to print out five that's because these functions they can see these variables up here but if there is a variable already declared inside of their function they're going to use that one instead I know it's kind of confusing I'm going to talk a little bit more about how this works I just want to give some examples to start off okay so now we will talk about global variables so local variables are ones that are declared usually inside of a function so that means outside of the function so again if we tried to print new var out here and I got rid of this up here we get the error like we got before right it says it's not defined so what about global variables well the global variables are ones that anything in the program can see so var and loop would be known as global variables in here if I try to print out our variable var and then I'll just call that function by doing func and I'll give it a value of two once again it will be able to see var and it does print it to the screen as 9 that is because this is global it's not defined inside of a function or inside of a class and classes we'll talk about later don't worry about that same thing if we try to print out a loop so if I change this to loop again it should work we get true printed out to the screen just like that so now there's the issue with changing variables inside of a function so typically you want to avoid this you don't want to be dependent on these global variables up here to make your functions work this is because if you remember in my other video I talked to a modular programming where we can reuse functions well if I have a variable defined up here and my function is referencing that variable meaning it requires that variable to work then if I try to use this function in another one of my programs it's not going to function properly because that loop variable or that global variable that I try to use is not going to be defined in that other program right so let's do an example here of how we can actually change a global variable so if I go here and I say loop is equal to 7 so I call my function and it's going to set loop equal to 7 is and then I try to print out the variable loop well what do you think is going to print to the screen give me a guess is either going to be 7 or it's either going to be true what do you think right so I'm printing it underneath after I call this function let's see we get true now even though we what looks like changed the variable in this function instead of actually changing this global variable instead we created a new variable which is local to the function called loop if we want to change this variable we have to use something called the global keyword so in Python what we have to do is at the top of our function we just type the word global followed by the variable name that we want to change so in this case I'm going to say loop now if we run this program we print out 7 that's because this tells our function that we're going to look outside of the function here for the variable if it exists then we're going to change it just like we did there so I know this has been kind of confusing I've kind of jumped around try to play around with these things and see if you're getting errors see if you can figure out what's going to print at the screen here if you want to change a global variable so one that you define at the top of your program here or outside of your functions then you must type the global keyword inside of your function followed by the name here again if I try to change the variable down here so not inside of a function it will work so if I do loop is equal to false then oh we get seven again just because I called it up here so I won't call this function this time but I say loop is equal to false then we will get false because it's not inside of a function so it is able to see and change this variable so really using the global keyword it's just inside of functions and just remember that if you do create a variable inside of your function it's local to your function meaning that unless you type this global keyword first and then followed by the name of that variable everything outside of the function will not be able to see that variable so if you like if you like this video and you learn something please like and subscribe and I will see you again in another video
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 21 of 60

1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches the difference between global and local variables in Python, how to access and modify them, and how to use the global keyword to change global variables from within a function. It also covers the importance of modular programming and avoiding dependence on global variables.

Key Takeaways
  1. Declare a global variable
  2. Declare a local variable within a function
  3. Try to access a local variable from outside the function
  4. Use the global keyword to modify a global variable from within a function
  5. Test the scope of variables with different function calls
💡 The global keyword is used to modify global variables from within a function, and it's essential to understand variable scope to write effective and modular code.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →