Introduction To Variables in Python
Introduction To Variables in Python
a=100
print (a)
Re-declare a Variable
You can re-declare Python variables even after you have declared once.
Python 3 Example
a="Guru"
b = 99
print a+b
Once the integer is declared as string, it can concatenate both "Guru" + str("99")=
"Guru99" in the output.
a="Guru"
b = 99
print(a+str(b))
Let's understand this Python variable types with the difference between local and
global variables in the below program.
1. Let us define variable in Python where the variable "f" is global in scope
and is assigned value 101 which is printed in output
2. Variable f is again declared in function and assumes local scope. It is
assigned value "I am learning Python." which is printed out as an output.
This Python declare variable is different from the global variable "f" defined
earlier
3. Once the function call is over, the local variable f is destroyed. At line 12,
when we again, print the value of "f" is it displays the value of global
variable f=101
Python 2 Example
Python 3 Example
# Declare a variable and initialize it
f = 101
print(f)
# Global vs. local variables in functions
def someFunction():
# global f
f = 'I am learning Python'
print(f)
someFunction()
print(f)
While Python variable declaration using the keyword global, you can reference the
global variable inside a function.
1. Variable "f" is global in scope and is assigned value 101 which is printed in
output
2. Variable f is declared using the keyword global. This is NOT a local
variable, but the same global variable declared earlier. Hence when we print
its value, the output is 101
3. We changed the value of "f" inside the function. Once the function call is
over, the changed value of the variable "f" persists. At line 12, when we
again, print the value of "f" is it displays the value "changing global
variable"
Python 2 Example
f = 101;
print f
# Global vs.local variables in functions
def someFunction():
global f
print f
f = "changing global variable"
someFunction()
print f
Python 3 Example
f = 101;
print(f)
# Global vs.local variables in functions
def someFunction():
global f
print(f)
f = "changing global variable"
someFunction()
print(f)
Delete a variable
You can also delete Python variables using the command del "variable name".
In the below example of Python delete variable, we deleted variable f, and when
we proceed to print it, we get error "variable name is not defined" which means
you have deleted the variable.
Example of Python delete variable or Python clear variable :
f = 11;
print(f)
del f
print(f)
Summary:
Variables are referred to "envelop" or "buckets" where information can be
maintained and referenced. Like any other programming language Python
also uses a variable to store the information.
Variables can be declared by any name or even alphabets like a, aa, abc, etc.
Variables can be re-declared even after you have declared them for once
In Python you cannot concatenate string with number directly, you need to
declare them as a separate variable, and after that, you can concatenate
number with string
Python constants can be understood as types of variables that hold the value
which can not be changed. Usually Python constants are referenced from
other files. Python define constant is declared in a new or separate file which
contains functions, modules, etc.
Types of variables in Python or Python variable types : Local & Global
Declare local variable when you want to use it for current function
Declare Global variable when you want to use the same variable for rest of
the program
To delete a variable, it uses keyword "del".