Variables in Python
Syntax:
variable_name = value
- There is no datatype declaration concept in python, datatype of variable will be decided at run time
- Datatype is dynamic type
Important
- type(variable_name) will provide datatype of variable
- id(variable_name) will provide address variable_name is referring.
Lets have a look at below example, this will prove 2 points discussed in "important points to remember section"
- Datatype is a run time allocation.
- Everything in Python is Object
>>> a = 10
>>> b = 10.25
>>> c = 'AETies'
>>> print(type(a))
>>> <class 'int'>
>>> print(type(b))
>>> <class 'float'>
>>> print(type(c))
>>> <class 'str'>
The above example have a, b and c variables.
We have not defined any variable with datatype, datatype is decided dynamically at run time.
so we can see that
- a belongs to int class, so a is int class object
- b belongs to float class, so b is float class object
- c belongs to str class, so c is as str class object (str is string)
Types of Variables in Python
We have 5 types of variables in Python. Among 5 types, 2 totally belongs to Class.
Example:
#program
def add(a, b) :
print("a: ", a, "\nb: ", b, "\nd :", d)
c = a + b
global e # declaring global variable inside function
e = "global Variable defined inside a function with keyword global"
return c
d = 10.25
result = add (5, 10)
print(e) # trying to print global variable defined inside function
print("sum :",result)
print(c) # trying to print local variable outside function
#output
a: 5
b: 10
d : 10.25
global Variable defined inside a function with keyword global
sum : 15
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 11, in <module>
print(c) # trying to print local variable outside function
NameError: name 'c' is not defined
Process finished with exit code 1
LOCAL Variable
- Variables defined inside a function.
- Scope is in the defined function only.
- Memory will be allocated at function calling time.
- As per above example,
- 'c' is a local variable as it is defined inside the function.
- when we tried to call outside function, it thrown error.
GLOBAL Variable
- Variables defined inside a module , but outside the functions.
- Scope is inside the defined module and other modules.
- Memory will be allocated, when application execution starts.
- We can define a global variable inside a function with the help of keyword "global".
- As per example,
- 'd' is a global variable defined outside a function.
- 'e' is a global variable declared inside function with keyword 'global'.
PARAMETERIZED Variable
- Variables defined in Function/Method definition inside parameters.
- Scope is through out the function.
- As per example
- 'a' and 'b' are parameterized variables
STATIC Variable
- Variables defined inside a Class but outside the method.
- Memory is allocated at Class loading time.
- Without creating Object to Class, we can access Static variables with Class name
read more >>>
INSTANCE Variable
- Variable defined inside Method with keyword self
- Memory is created at Class Object creation time
read more >>>
Comments
Post a Comment