Variables
Variables are storage locations that are used to store information that can later be referenced and manipulated. You can consider a variable as a sort of a container where you store an information that can later be accessed with the variable name.
1. Assigning Values to Variables
In Python, variables are created when they are first assigned values. Values are assigned using the assignment operator (=).
name = "Packing box" # A string
height = 10 # An integer assignment
width = 20.5 # A floating point
print (name)
print (height)
print (width)
When you want to print the content of a variable, quotes are not used. Python considers the text inside the quotes to be a string and not a variable name.
2. Common Rules for Naming Variables in Python
Camel Case - In the camel case, each word or abbreviation in the middle of begins with a capital letter. There is no intervention of whitespace. For example - nameOfStudent, valueOfVaraible, etc.
Pascal Case - It is the same as the Camel Case, but here the first word is also capital. For example - NameOfStudent, etc.
Snake Case - In the snake case, Words are separated by the underscore. For example - name_of_student, etc.
- Variable names are case-sensitive.
- Variable names must begin with a letter or underscore.
- A variable name can only contain alphanumeric characters and underscore, such as (a-z, A-Z, 0-9, and _ ).
- A variable name can not contents space.
- Reserved words cannot be used as a variable name.
3. Reserved Python words
Words | Words | Words |
---|---|---|
and | assert | in |
del | else | raise |
from | if | continue |
not | pass | finally |
while | yield | is |
as | break | return |
elif | except | def |
global | import | for |
or | lambda | |
with | class | try |
4. Deletion Python Variable from Memory
# Assigning a value to x
x = 6
print(x)
# deleting a variable.
del x
print(x)
5. Object References
Python is the highly object-oriented programming language; that’s why every data item belongs to a specific type of class. Consider the following example. In Python, variables are a symbolic name that is a reference or pointer to an object. The variables are used to denote objects by that name.
a) Let’s assign the variable a to an integer object
a = 50
b) Suppose we assign the integer value 50 to a new variable b
a = 50
b=a
c) The variable b refers to the same object that a points to because Python does not create another object.
d) Let’s assign the new value to b. Now both variables will refer to the different objects.
a = 50
b =100
6. Object Identity
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.
a = 50
b = a
print(id(a))
print(id(b))
# Reassigned variable a
a = 500
print(id(a))
Output:
140734982691168
140734982691168
2822056960944
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.
7. Multiple Assignment - Single value to multiple variables
Python allows us to assign a value to multiple variables in a single statement, which is also known as multiple assignments.
Example:
x=y=z=50
print(x)
print(y)
print(z)
Output:
50
50
50
8. Multiple Assignment - Assigning multiple values to multiple variables
Example:
a,b,c=5,10,15
print a
print b
print c
Output:
5
10
15
9. Local Variable
Local variables are the variables that declared inside the function and have scope within the function. Let’s understand the following example
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
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. We tried to use local variable outside their scope; it threw the NameError.
add()
# Accessing local variable outside the function
print(a)
The sum is: 50
print(a)
NameError: name 'a' is not defined
10. Global Variable
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.
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 Javatpoint'
print(x)
mainFunction()
print(x)
Output:
101
Welcome To Javatpoint
Welcome To Javatpoint
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.