Variables and Identifiers
Variables and Identifiers
Python Variables
Variable is a name that is used to refer to memory location. Python variable is also
known as an identifier and used to hold value.
In Python, we don't need to specify the type of variable because Python is a infer
language and smart enough to get variable type.
Variable names can be a group of both the letters and digits, but they have to begin
with a letter or an underscore.
It is recommended to use lowercase letters for the variable name. Rahul and rahul
both are two different variables.
Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals
used in the program. The rules to name an identifier are given below.
We don't need to declare explicitly variable in Python. When we assign any value to
the variable, that variable is declared automatically.
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.
1. print("John")
Output:
John
The Python object creates an integer object and displays it to the console. In the
above print statement, we have created a string object. Let's check the type of it
using the Python built-in type() function.
1. type("John")
Output:
<class 'str'>
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.
1. a = 50
a = 50
b=a
The variable b refers to the same object that a points to because Python does not
create another object.
Let's assign the new value to b. Now both variables will refer to the different objects.
a = 50
b =100
Python manages memory efficiently if we assign the same variable to two different
values.
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
Variable Names
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.
name = "Devansh"
age = 20
marks = 80.50
print(name)
print(age)
print(marks)
Output:
Devansh
20
80.5
name = "A"
Name = "B"
naMe = "C"
NAME = "D"
n_a_m_e = "E"
_name = "F"
name_ = "G"
_name_ = "H"
na56me = "I"
print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_name, n
a56me)
Output:
A B C D E D E F G F I
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.
o 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.
o Pascal Case - It is the same as the Camel Case, but here the first word is also capital.
For example - NameOfStudent, etc.
o Snake Case - In the snake case, Words are separated by the underscore. For example
- name_of_student, etc.
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.
Eg:
x=y=z=50
print(x)
print(y)
print(z)
Output:
50
50
50
Eg:
1. a,b,c=5,10,15
2. print a
3. print b
4. print c
Output:
5
10
15
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:
Explanation:
add()
# Accessing local variable outside the function
print(a)
Output:
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.
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
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.
Syntax -
1. 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.
Example -
# 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/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py",
line 389, in
print(x)
NameError: name 'x' is not defined
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
Python doesn't have any special data type to store larger numbers.
# printing single value
a = 5
print(a)
print((a))
Output:
5
5
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
Basic Fundamentals:
This section contains the fundamentals of Python, such as:
ii) Comments
a) Tokens:
o The tokens can be defined as a punctuator mark, reserved words, and each word in
a statement.
o The token is the smallest unit inside the given program.
o Keywords.
o Identifiers.
o Literals.
o Operators.