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

preview_player
Показать описание
🎓 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:

#Python #Tutorial #Beginners #Shorts

***

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

***

♡ FOLLOW US ♡

♡ SUPPORT US ♡

***
Рекомендации по теме
Комментарии
Автор

Do you know differences between LOCAL and GLOBAL variables? 🤔

DigitalAcademyOnline
Автор

can you please tell me how to make this series using loop in python
#-4, -6, -6, -4, 0, 6, 14, 24, 36

siddharth