In Python, every created object identifies uniquely in Python. Python provides the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example.
We assigned the b = a, a and b both point to the same object. When we checked by the id() function it returned the same number. We reassign a to 500; then it referred to the new object identifier.
We have already discussed how to declare the valid variable. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following example of valid variables names.
In the above example, we have declared a few valid variable names such as name, name , etc. But it is not recommended because when we try to read code, it may create confusion. The variable name should be descriptive to make code more readable
.
The multi-word keywords can be created by the following method.
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement, which is also known as multiple assignments.
We can apply multiple assignments in two ways, either by assigning a single value to multiple variables or assigning multiple values to multiple variables.
Consider the following example.
1. Assigning single value to multiple variables
1. x=y=z=50
2. print(x)
3. print(y)
4. print(z)
output:
50
50
50
2. Assigning multiple values to multiple variables:
1. a,b,c=5,10,15
2. print a
3. print b
4. print c
output:
5
10
15
The values will be assigned in the order in which variables appear.
Python Variable Types
There are two types of variables in Python - Local variable and Global variable. Let's understand the following variables.
Local Variable
Local variables are the variables that declared inside the function and have scope within the function. Let's understand the following example.
# Declaring a function
def add():
# Defining local variables. They has scope only within a function
a = 20
b = 30
c = a + b
print("The sum is:", c)
# Calling a function
add()
output:
The sum is: 50
Explanation:
In the above code, we declared a function named add() and assigned a few variables within the function. These variables will be referred to as the local variables which have scope only inside the function. If we try to use them outside the function, we get a following error.
add()
# Accessing local variable outside the function
print(a)
The sum is: 50
print(a)
NameError: name 'a' is not defined
We tried to use local variable outside their scope; it threw the NameError.
Global Variables
Global variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function.
A variable declared outside the function is the global variable by default. Python provides the global keyword to use global variable inside the function. If we don't use the global keyword, the function treats it as a local variable. Let's understand the following example.
# Declare a variable and initialize it
x = 101
# Global variable in function
def mainFunction():
# printing a global variable
global x
print(x)
# modifying a global variable
x = 'Welcome To PythonNotes'
print(x)
mainFunction()
print(x)
output:
101
Welcome To PythonNotes
Welcome To PythonNotes
Explanation:
In the above code, we declare a global variable x and assign a value to it. Next, we defined a function and accessed the declared variable using the global keyword inside the function. Now we can modify its value. Then, we assigned a new string value to the variable x.
Now, we called the function and proceeded to print x. It printed the as newly assigned value of x.
Delete a variable
We can delete the variable using the del keyword. The syntax is given below.
[ del <variable_name>
In the following example, we create a variable x and assign value to it. We deleted variable x, and print it, we get the error "variable x is not defined". The variable x will no longer use in future.
# Assigning a value to x
x = 6
print(x)
# deleting a variable.
del x
print(x)
output:
6
Traceback (most recent call last):
File "C:/Users/pythonnotes/PycharmProjects/Hello/multiprocessing.py", line 389, in
print(x)
NameError: name 'x' is not defined
Maximum Possible Value of an Integer in Python
Unlike the other programming languages, Python doesn't have long int or float data types. It treats all integer values as an int data type. Here, the question arises. What is the maximum possible value can hold by the variable in Python?
Consider the following example.
# A Python program to display that we can store
# large numbers in Python
a = 10000000000000000000000000000000000000000000
a = a + 1
print(type(a))
print (a)
output:
<class 'int'>
10000000000000000000000000000000000000000001
As we can see in the above example, we assigned a large integer value to variable x and checked its type. It printed class <int> not long int. Hence, there is no limitation number by bits and we can expand to the limit of our memory.
Python doesn't have any special data type to store larger numbers.
We can print multiple variables within the single print statement. Below are the example of single and multiple printing values.
Ex:-1( printing single value)
# printing single value
a = 5
print(a)
print((a))
output:
5
5
Ex:-2(printing multiple variables)
a = 5
b = 6
# printing multiple variables
print(a,b)
# separate the variables by the comma
Print(1, 2, 3, 4, 5, 6, 7, 8)
output:
5 6
1 2 3 4 5 6 7 8