Variables
Python variables are containers which store values. Python is not "statically typed". We do not need to declare variables before using them or declare their type. A variable is created when a value is assigned to it. The value stored in a variable can be changed during program execution. A Python variable is only a name given to a memory location, all the operations done on the variable affects that memory location. Everything in Python is treated as an object so every variable is nothing but an object in Python. A variable can be either mutable or immutable. If the value of a variable is changed then the object is called mutable, otherwise the object is called immutable.
Rules for Creating Variables in Python
A name in a Python program is called an identifier. An identifier can be a variable name, class name, function name, and module name.
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable can only contain alpha-numeric characters and underscores (A-Z, a-z, 0-9 and _)
- Variable names are case-sensitive (
name,NameandNAMEare three different variables) - The reserved words (Keywords) cannot be used for naming the variable.
Creating a Variable and Assigning Values to Variable
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the values to read in the variable.
Syntax:
<Variable_name> = <Variable_value>
Example:
counter = 100 # An integer value assignment
miles = 1000.0 # A floating point assignment
name = "John" # A string assignment
print(counter)
print(miles)
print(name)
Output:
100
1000.0
John
Get the Data Type of a Variable
No matter what is stored in a variable (object), a variable can be any type like int, float, str, list, tuple, dict, etc. There is a built-in function called type() to get the data type of any variable.
Syntax:
type(<Variable_name>)
Example:
var = 10
print(var)
print(type(var)) # print its type
var = "Now I'm a string" # change var to string
print(var)
print(type(var)) # print its type
var = 35.69 # change var to float
print(var)
print(type(var)) # print its type
Output:
10
<class 'int'>
Now I'm a string
<class 'str'>
35.69
<class 'float'>
Variable Scope
The scope of a variable refers to the places where we can access a variable. Depending on the scope, the variable can be categorized into two types: local variable and global variable.
Local Variable
A local variable is a variable that is accessible inside a block of code only where it is declared. That means, if a variable is declared inside a method, the scope of the local variable is limited to the method only. So it is not accessible from outside of the method. If we try to access it, we will get an error.
Example:
def test1(): # defining 1st function
price = 500 # local variable
print("Value of price in test1 function :", price)
def test2(): # defining 2nd function
# NameError: name 'price' is not defined
print("Value price in test2 function:", price)
# calling functions
test1()
test2()
Output:
Value of price in test1 function : 500
NameError: name 'price' is not defined
In the above example, a function is created with the name test1. Inside it, we created a local variable price. Similarly, we created another function with the name test2 and tried to access price, but we got an error "price is not defined" because its scope is limited to function test1(). This error occurs because we cannot access the local variable from outside the code block.
Global Variable
A Global variable is a variable that is defined outside of the method (block of code). That is accessible anywhere in the code file.
Example:
price = 500 # Global variable
def test1(): # defining 1st function
print("price in 1st function :", price)
def test2(): # defining 2nd function
print("price in 2nd function :", price)
# calling functions
test1()
test2()
Output:
price in 1st function : 500
price in 2nd function : 500
In the above example, we created a global variable price and tried to access it in test1 and test2. In return, we got the same value because the global variable is accessible in the entire file. The global variable must be declared outside the function.