Python Tutorial: Variable Scope - Understanding the LEGB rule and global/nonlocal statements

In this Python Tutorial, we will be going over variable scope in Python. Scope is important because we need to understand it in just about every program we write. It allows us to understand where our variables can be seen from within our program and also what values these variables hold. It also helps with debugging, because scope is a common problem when errors are thrown. Let's get started.
The code from this video can be found at:
github.com/CoreyMSchafer/code...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

Пікірлер: 360

  • @julianmahler2388
    @julianmahler23882 жыл бұрын

    I think many people don't even realize how well-thought-out and well-prepared your videos are. Your step by step explanation skills are unsurpassable

  • @saitaro
    @saitaro7 жыл бұрын

    Corey. Man. It's so great that you exist. Thank you.

  • @User-nq9ee

    @User-nq9ee

    6 жыл бұрын

    :D

  • @dequationblog

    @dequationblog

    5 жыл бұрын

    Ex Corey Schafer Tutorials nulla salus

  • @shazkingdom1702

    @shazkingdom1702

    4 жыл бұрын

    You are right!

  • @plazmikpond

    @plazmikpond

    4 жыл бұрын

    :D

  • @alitajvidi5610

    @alitajvidi5610

    3 жыл бұрын

    Can't agree more with this comment

  • @kobas8361
    @kobas83615 жыл бұрын

    Mate, I'm being forced to watch some random Python tutorial at my Python Internship and it's a real pain in the ass to do so once one knows your channel. I just realized that theese might honestly be the best tutorials in the web...

  • @DisgruntledVet

    @DisgruntledVet

    4 жыл бұрын

    Definitely!

  • @falsedragon33

    @falsedragon33

    4 жыл бұрын

    RIP

  • @quant.481

    @quant.481

    4 жыл бұрын

    RIP

  • @plazmikpond

    @plazmikpond

    4 жыл бұрын

    RIP

  • @kristiyanivanov7414

    @kristiyanivanov7414

    4 жыл бұрын

    RIP

  • @ousmanetall1286
    @ousmanetall12864 ай бұрын

    I have never seen a better explain tutorial of LEGB, thank corey, wish you all the best!

  • @cemalcakir
    @cemalcakir3 жыл бұрын

    Just wanted to write this after what you said about global variables (not to overuse them). You are not just teaching stuff, also giving great informations and insights about programming. That's a gem!

  • @PedroHenrique-ps8kj
    @PedroHenrique-ps8kj2 жыл бұрын

    Amazing work, we are really living a great era of transformation where such high level of information is widely available and accessible , what a blessing !!

  • @gideonfelt2819
    @gideonfelt28197 жыл бұрын

    Man you are a heaven send! I literally was thinking I need to study scope more since being new to python and programming in general I was having lots of problems, and then Google notifies me about this video being released 10 mins latter, wow its just perfect, absolutely perfect, it even covered everything I was thinking about. Thank You so much.

  • @coreyms

    @coreyms

    7 жыл бұрын

    Awesome. Glad to hear that!

  • @coreyms

    @coreyms

    7 жыл бұрын

    Oh wow. Thanks a lot. I really appreciate that.

  • @sanjaykumarchoudhury4522

    @sanjaykumarchoudhury4522

    4 жыл бұрын

    Agree, he is great teacher..Best in world

  • @pixelatedreverie

    @pixelatedreverie

    2 жыл бұрын

    Plot twist.. It was Google along who knew both what you thought and @Corey thought.

  • @DevaKalidass

    @DevaKalidass

    Жыл бұрын

    😂

  • @janick9945
    @janick99455 жыл бұрын

    this video has been an awakening. I don't understand why anyone has come up with such an eloquent explanation as yours before.

  • @nitkap441
    @nitkap4414 жыл бұрын

    Again a very crisp video. I learnt programming in 1994 on IBM mainframe and did work on Cobol/DB2/CICS etc. but soon moved into managerial role. One of my biggest problems with lot of videos is that they all spend lot of time on useless explanations... I need someone like you who is short and crip and to the point(because I know programming as such..but need specifics)... I get every minute worth in your videos with no waste of time.

  • @jazmonark3309
    @jazmonark33095 жыл бұрын

    People has tutorials..... But YOU are a real teacher who says the truth ... The "shape of my heart" song is meant for you❤

  • @wizard-28
    @wizard-284 жыл бұрын

    Awesome video, It cleared out my many doubts. Thumbs up to Mr. Schafer. Keep up the good work. I wish I found you earlier when I was struggling to learn java but I hated Python because I couldn't find a good instructor like you, the instructors I found made this language look very hard but now I have found you, I am changed my ambition from becoming a java developer to a python developer. Love you, Corey.

  • @davorm4634
    @davorm46347 жыл бұрын

    I`ve just start to learn Python and find yours tutorials best here, seriously. ( I look many of them ). Thanks a lot man !

  • @liesdamnlies3372
    @liesdamnlies33727 жыл бұрын

    I'm glad you added the part about "Look, that's how it works, but you shouldn't overuse it." Globals are dangerous beasts. Certain (now deceased) Toyota owners learned that the hard way. It's an easy thing to have overlooked, but very important.

  • @the_xibalba
    @the_xibalba3 жыл бұрын

    I has been frantically searching for a proper tutorial on sending emails using Python. I was literally pulling my hair off by reading and implementing solutions from Stack Overflow which introduced some other bus in my project. I accidentally stumbled upon your channel and boy am I glad that I did. Thank you so much for doing this mate, you are making a difference in the lives of other people, I hope you are doing well in these times. Thank you once again!

  • @Ninja-iq2xt
    @Ninja-iq2xt7 жыл бұрын

    Hey Corey, you cant imagine how helpful this video turned out to me, i was stuck in one project for a long time, but you just made my day, my solution was right into the variable scope, Thanks a lot!!!

  • @thevgancheetah
    @thevgancheetah4 жыл бұрын

    Your teaching skills are truly amazing! I understood everything crystal clear! Thank you so much!

  • @andresvodopivec5950
    @andresvodopivec59505 жыл бұрын

    Corey I have never seen anyone to explain this basic and very important information. Thanks.

  • @Airless_Grind
    @Airless_Grind3 жыл бұрын

    Every single thing i look up during my python course always leads me to your channel, you're an absolute blessing.

  • @davidalexander829
    @davidalexander8296 жыл бұрын

    Masterful clarity in simple conversational language. What a relief

  • @jonathanessien4360
    @jonathanessien43603 ай бұрын

    1st year compSci student here and this helped in more ways than you can imagine. started coding in delphi so there was no need for Enclosing or Builtins and didnt know why my global values were not changing but thank God for you. Much appreciated 🙏

  • @hussainmadar4
    @hussainmadar44 жыл бұрын

    Wonderful work @Corey Schafer. I have seen so many tutorials, but your tutorials are the best. Many thanks to @Krish naik for suggesting me your tutorials.

  • @DisgruntledVet
    @DisgruntledVet4 жыл бұрын

    My MAN!!! Thank you so much! First video that explained it where I could understand it. I tried a couple other channels and couldn't get it. I was so confused until I watched this video. I'm new to Python and this helped out a lot. Thank you so much!

  • @NaderTaghinia
    @NaderTaghinia5 жыл бұрын

    Every second of your videos is highly worthwhile Mr.Schafer...Thank you so much and God bless you!

  • @rockingraphsody3019
    @rockingraphsody30196 жыл бұрын

    the only professional content creator of python in youtube.. you rock corey !!

  • @SpreadHumanity118
    @SpreadHumanity1185 жыл бұрын

    corey your videos are so compelling ....before going to interview i refer them ..great work,keep going .you are helping a lots of tech lovers to learn.

  • @ArchitecturalAesthetics2046
    @ArchitecturalAesthetics20465 жыл бұрын

    great tutorial. answered so many of my questions. I've been teaching myself python by reading head first python but the author didn't explain the variable scopes, which caused me a lot of confusions when I was writing custom functions.

  • @dkq986
    @dkq9864 жыл бұрын

    Clarity in the way you speak is amazing!!

  • @soumyazyx
    @soumyazyx4 жыл бұрын

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  • @exili
    @exili6 жыл бұрын

    Thank You so much, for all the time you put in to your videos! If ever you do a weeklong coding-bootcamp class I'm there!

  • @sueholder703
    @sueholder7035 жыл бұрын

    This was an excellent way to present this confusing topic - thanks!!

  • @anguspearson1
    @anguspearson15 жыл бұрын

    So clearly explained. Thank you for an excellent tutorial!

  • @commandprompt7171
    @commandprompt71714 жыл бұрын

    i was experimenting with functions, and this is exactly what i was looking for, thanks a lot man

  • @Mrjarnould
    @Mrjarnould7 жыл бұрын

    Always a good day when I see you've released a new video! I'm looking forward to seeing a tutorial on Pandas and Sublime Text Setup for Python Development. All the best, Jacob.

  • @brukselkowyben7628
    @brukselkowyben76286 жыл бұрын

    Your tutorials are great, I really appreciate your effort.

  • @appletreegfx806
    @appletreegfx806 Жыл бұрын

    I totally agree on Julian M's comments. This lecture is very well-planned and highly organized. I came to Corey's video after several stops that I wasn't satisfied much. Happy to find Corey S videos.

  • @caydanphoenix9067
    @caydanphoenix90673 жыл бұрын

    if i had access to a million mail id's, i would have subscribed this channel with each of them...this is in my opinion #1 channel for python..this video cleared every doubt i had in this topic..tysm dude!!

  • @aijaz4441
    @aijaz44418 ай бұрын

    You are the best teacher to explain the core concepts so neatly. I always check your channel for any content that I need help with. Thanks. God Bless you.

  • @manharts
    @manharts6 жыл бұрын

    You are an outstanding teacher! Although I get most of this content now, it took me a long time to get this concept in my head. Wish I'd seen this demonstration around 25 years ago. I'd say even if the rules may be slightly different in other languages, the logic still applies.

  • @yingbowang3068
    @yingbowang30682 жыл бұрын

    that nonlocal is amazing..Thank you Mr Schafer for those excellent tutorials

  • @sak8485
    @sak84856 жыл бұрын

    this tutorial series is awesome, Thank you so much man :)

  • @newsjuice7404
    @newsjuice74042 жыл бұрын

    I learned python from you including frameworks like Flask & Django plus pandas, Numpy, mayplotlib, SQL also & I guess I am job ready now & the very first thing I ll do is to support this channel inshallah

  • @ricardo.fontanelli
    @ricardo.fontanelli4 жыл бұрын

    Your videos are great for programmers moving from other languages to python, thank you

  • @peek-a-boosurprisetoysandf5794
    @peek-a-boosurprisetoysandf57947 жыл бұрын

    You are one of the best Python instructors on KZread! Your tutorials explain complicated concepts in a beautifully succinct and clear manner. Just became a patron! 👍👍👍Thanks for sharing your knowledge with us!

  • @coreyms

    @coreyms

    7 жыл бұрын

    Thank you! That really helps a lot!

  • @isaacfinch2141

    @isaacfinch2141

    Жыл бұрын

    5 years later, you're still one of the best!

  • @randomsamurai8759
    @randomsamurai87593 жыл бұрын

    Just an organic comment to show some appreciation. Best python tutorials on the internet!

  • @tanuj05
    @tanuj05Ай бұрын

    Awesome video Corey , your videos are awesome !

  • @ajeet3259
    @ajeet32595 жыл бұрын

    Thank you for such crystal clear explanation.

  • @chenxingliu2675
    @chenxingliu26757 жыл бұрын

    You tutorials are the best!!!

  • @vinces.9990
    @vinces.99904 жыл бұрын

    I go ahead and like your videos before I even finish watching. I already know from previous videos that it will be what i'm looking for.

  • @ManuelShelton
    @ManuelShelton6 жыл бұрын

    Very clear structured lession about LEGB.

  • @supersaiyan0x016
    @supersaiyan0x0164 жыл бұрын

    Definitely worth it.. You explain far better than my college teacher ...

  • @pranavpawar1894
    @pranavpawar18944 жыл бұрын

    Thanks Corey for uploading such a wonderful playlist

  • @vijay270886
    @vijay270886 Жыл бұрын

    Thanks a lot Corey Schafer.

  • @RameezRaja-qc9fi
    @RameezRaja-qc9fi Жыл бұрын

    Thank you so much Corey! This video really helped me to understand this concept completely now!

  • @sanjayshr1921
    @sanjayshr19217 жыл бұрын

    awesome :) looking forward more videos on Python . thank you

  • @muhammadkarim4231
    @muhammadkarim42313 жыл бұрын

    Loved the way you explained the scope so well

  • @zehuawang5765
    @zehuawang57653 жыл бұрын

    Very clear and easy understanding, thanks !

  • @davey7205
    @davey7205 Жыл бұрын

    Just an outstanding explanation. Cheers for Corey. Really grateful for your content.

  • @pranav6610
    @pranav66104 жыл бұрын

    It's awesome that your alive thanks for this

  • @angelak3060
    @angelak30604 жыл бұрын

    Really great tutorial!

  • @brpawankumariyengar4227
    @brpawankumariyengar42276 жыл бұрын

    Great Video and very informative. Thank you so very much for posting.

  • @nazzak2093
    @nazzak2093 Жыл бұрын

    Thank you for this great explanation, I never heard of LEGB and I never noticed or had to use global/nonlocal keywords before, probably because I code in C++ style where global variables are, kind of, forbidden. Thank you again for the good explanation. Now I know.

  • @Roule_n_Scratche
    @Roule_n_Scratche2 жыл бұрын

    Thanks for a easy explanation on the topic

  • @ramveerla
    @ramveerla2 жыл бұрын

    Excellent! Straight and to the point

  • @kinkdaka1
    @kinkdaka13 жыл бұрын

    A perfect explanation!

  • @akira_asahi
    @akira_asahi2 жыл бұрын

    Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

  • @raibahati
    @raibahati3 жыл бұрын

    I am gonna contribute because i have learned so much thanks to you!

  • @SoroushAzizzadeh
    @SoroushAzizzadeh4 жыл бұрын

    Great content Corey, Keep up the good work :)

  • @hubertcombomarketing2693
    @hubertcombomarketing26934 жыл бұрын

    Great tutorial. Thank you.

  • @saidhussainmusawi
    @saidhussainmusawi2 жыл бұрын

    Thanks a lot Corey, you explained it in a best possible way

  • @GraciaEdhyta
    @GraciaEdhyta Жыл бұрын

    very very clear and complete explanation, thank you so much!!!

  • @abinandankumar5283
    @abinandankumar52835 жыл бұрын

    Great, You just made it simple. Thanks

  • @dubanaveen5678
    @dubanaveen56784 жыл бұрын

    Outstanding!!

  • @bostevens236
    @bostevens2364 жыл бұрын

    Love your videos. Thank you so much!

  • @anuragkadam7935
    @anuragkadam79352 жыл бұрын

    Corey's python videos are "global" treasure

  • @nicolasgaritagoitia5536
    @nicolasgaritagoitia55363 жыл бұрын

    This is the best, thank you very much. The explanations in the python official documentation is a lot harder to understand

  • @rawankhaled8394
    @rawankhaled83945 жыл бұрын

    You're really awesome. thank you so much, and please keep on doing more videos

  • @MohSo14
    @MohSo149 ай бұрын

    How great explanation on this topic. Great job!

  • @semih3704
    @semih37045 жыл бұрын

    Awesome tutorial!!!

  • @0x656e
    @0x656e4 жыл бұрын

    hey Corey! if you reading this I hope you get all the happiness in this world because you deserve them thank you so much for these beautiful and great tutorials you're wonderful

  • @coreyms

    @coreyms

    4 жыл бұрын

    Thanks!

  • @regivm123
    @regivm1236 жыл бұрын

    Too good. The explanation of these concepts

  • @thelurkingpanda3605
    @thelurkingpanda36054 жыл бұрын

    This is the perfect tutorial.

  • @tymothylim6550
    @tymothylim65503 жыл бұрын

    Thank you very much for this video! It was very interesting and I didn't know we could use "global" and "nonlocal" :)

  • @CsabaKissColorado
    @CsabaKissColorado6 жыл бұрын

    Perfect explanation. Well done!

  • @sharonsnail2954
    @sharonsnail29542 жыл бұрын

    Excellent!! Best tutorial I've ever seen on this topic 👍👍👍👍👍

  • @manivannan33
    @manivannan332 жыл бұрын

    Excellent explanation!

  • @anup2000ster
    @anup2000ster6 жыл бұрын

    Beautifully explained!!!

  • @leonardopetribu7207
    @leonardopetribu72073 жыл бұрын

    Wow, that was excellent!! Thanks!!

  • @jamescagney5314
    @jamescagney531411 ай бұрын

    Well-explained!

  • @MiledRizk
    @MiledRizk4 жыл бұрын

    Thanks a lot! A very informative and clear explanation!

  • @radhakrishnavarukuti1601
    @radhakrishnavarukuti16015 жыл бұрын

    Couldn't go without liking the video !!

  • @cataphrac
    @cataphrac6 жыл бұрын

    That was a thorough explanation, I grok scope now thanks!

  • @eriscrub8807
    @eriscrub88074 жыл бұрын

    Thank you for explaining it very well. i finally understood it

  • @minimumeffectivedose4408
    @minimumeffectivedose44083 жыл бұрын

    Really helpful - thanks!

  • @rockershocker007
    @rockershocker0073 жыл бұрын

    Thanks a lot. You explained it so beautifully.

  • @ardcapture3D
    @ardcapture3D Жыл бұрын

    Corey great tutorial as always. The scope of lists and dictionaries seem to behave differently in this situation -would love to have one of your vids going into that. Many thanks Adrian

  • @user-bx1or2rr6e
    @user-bx1or2rr6e7 жыл бұрын

    high quality tutorial, thanks.

  • @heshankumarasinghe3159
    @heshankumarasinghe31593 жыл бұрын

    Thank you for this video..... This video has been really helpful.....

  • @anisha9299
    @anisha92992 жыл бұрын

    really good tutorial! thanks

  • @tylersnard
    @tylersnard Жыл бұрын

    Easily understood, thank you.

  • @starplatinumrqm
    @starplatinumrqm Жыл бұрын

    simply amazing video