Rules for creating variables
Python has some predefined rules that need to be followed while creating a variable. Those are:
- Variable can only contain numbers, letters, or underscore characters (_).
- Variables cannot start with a number.
- Variables cannot be a keyword.
- Variables cannot have spaces.
If these rules are not followed, then python will show an error. Let's look into some examples.
Code:
first integer =2334
Output:
File "<string>", line 1
first integer =2334
^
SyntaxError: invalid syntax
Types of the scope of the variables
In python, the scope of variables is of four types:
1. Local scope
2. Global scope
3. Enclosing scope
4. Build-in scope
Local scope
Local scope variable in python can only be accessed inside a block. Here, a variable's scope lies within that function whenever a variable is defined. It is accessible till the end of the function, and hence its value cannot be accessed or changed outside the function. Let's look into some examples:
Code:
def function():
n = 20
print("The number is called inside the function,n= ",n)
function()
Output:
The number is called inside the function,n= 20
Let's see another example when the variable is accessed outside the function.
Code:
def print_number():
n = 1
print_number()
print("The number called outside the function,n= ", first_num)
Output:
Traceback (most recent call last):
File "<string>", line 5, in <module>
NameError: name 'first_num' is not defined
Global scope
Global variables can be accessed from anywhere in the program. In python, whenever a variable is declared outside a function, It can be accessed and changed from anywhere in the program and hence is called a global variable. Let's look into some examples:
Code
hi = "hello "
def hello():
name="Raj"
surname="Arora"
print(hi, name,surname)
hello()
Output:
hello Raj Arora
Here, variable ‘hi’ is declared outside the function but is still accessible in the function.
Enclosing scope
A scope that is neither global nor local is an enclosing scope. These are used when we have a nested function. Let's look into some examples:
Code:
def big():
a = 1
def small():
b = 2
print("accessing a from big : ", a)
print("accessing b from small : ",b)
small()
print("accessing b from small: ", b)#outside small
big()
Output:
accessing a from big : 1
accessing b from small : 2
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "<string>", line 8, in big
NameError: name 'b' is not defined
Here we can see that variable ‘b’ is not accessible from the function ‘big’.But variable ‘a’ can be accessed from ‘small’ because a has a larger scope.
You can practice by yourself with the help of online python compiler.
Built-in scope
In built-in scope, these are predefined in python—all the keywords used in python fall in this category. Keywords are special words whose functions are predefined in python and cannot be used for any other purpose.
Commonly used keywords are ‘false’, ‘and’, ‘as’ ,’ return’, ‘not’ etc.
Lets look into some example:
Code:
a = 5.5
int(a)
print(a)
print(type(a))
Output:
5.5
<class 'float'>
Global keyword
A global keyword is a keyword used to define the keyword globally. We need global keywords in a function only when we want to change them. Let's look into it through some examples:
Code:
name="raj"
print(name)
def change_name(new_name):
global name
name=new_name
def print_name():
print(name)
change_name("rahul")
print_name()
Output:
raj
rahul
Nonlocal Keyword
The nonlocal keyword is used in nested loops or functions. It increases the scope of the variable and prevents it from binding locally. We declare a nonlocal keyword when we want to use or point the variable outside its function. Let's look into some examples:
Code:
def big():
num1 = 1
def small():
nonlocal num1
num1 = 0
num2 = 1
print("small - num2 is: ", num2)
small()
print("big - num1 is: ", num1)
big()
Output:
small - num2 is: 1
big - num1 is: 0
Also See, Python Round Function.
Frequently asked questions
Why is python called an interpreted language?
An interpreted language is a language in which statements are executed line by line. Since python runs its statement line by line, it is an interpreted language.
How is memory managed in Python?
In python, private heaps are used to store and manage memory.
What do you mean by pythonpath?
In python,pythonpath tells the interpreter the location of modules files imported in the program.
Conclusion
This blog has discussed the scope of variables in python by implementing code. We hope that this blog has helped you enhance your knowledge regarding trackers and their uses and if you would like to learn more, check out our articles related to Python. Do upvote our blog to help other ninjas grow. Happy Coding!”
Recommended Readings: