Variable Scope and Lifetime in Python (Local vs Global) - Python Tutorial for Beginners

Veröffentlicht am: 27 Juni 2025
auf dem Kanal: Digital Academy
134
3

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Variable Scope in Python (Local vs Global) and Lifetime of a variable in Python

If you are familiar with Python, or any other programming languages, you will certainly know that variables need to be defined, before they can be used in your program. And, depending on HOW and WHERE it was defined, a variable will have to be accessed in different ways.

Not all variables are accessible, from all parts of your program. And not all variables exist, for the same amount of time! Where a variable is accessible, and how long it exists, depend on how it is defined. We call the part of a program where a variable is accessible, its SCOPE. And the duration for which the variable exists, its LIFETIME.

The SCOPE of a variable in Python, refers to the places where you can see or access a variable. Some variables are defined globally, others locally. And this means that a variable referring to an entity in a certain part of a program, may refer to something different in another part of the program, even at different points in time!

Thus, if you define a variable at the top of your script, it will be a GLOBAL variable. This means that it is accessible, from anywhere in your script: including from a class, defined in the same module. Or both, inside and outside a function.

On the other hand, A variable which is defined inside a function, is LOCAL to that function. It is accessible from the point at which it was defined, and until the end of the function. And it exists for as long as the function is executing.

For instance, the parameter names in the function definition, behave like local variables. But they contain the values that we pass into the function, when we call it. When you use the assignment operator equals inside a function, its default behaviour is to create a new local variable, unless a variable with the same name is already defined, in the local scope.

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. If you create a variable, with the same name inside a function, the global variable with the same name, will remain as it was - and will keep its original value. Thus, in order to create a global variable inside a function, you can use the global reserved keyword. Also, use the global keyword if you want to change: a global variable inside a function, but that was declared outside.

Let's play this video, stick around and watch until the end of this video! 👍🏻

Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, Strings, Li...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assignmen...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF, ELIF...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Continu...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for Abso...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Auf dieser Seite können Sie das Online-Video Variable Scope and Lifetime in Python (Local vs Global) - Python Tutorial for Beginners mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Digital Academy 27 Juni 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 134 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!