A variable is the name given to a memory location. A value-holding Python variable is also known as an identifier.
Since Python is an infer language that is smart enough to determine the type of a variable, we do not need to specify its type in Python.
Variable names must begin with a letter or an underscore, but they can be a group of both letters and digits.
The name of the variable should be written in lowercase. Both Rahul and rahul are distinct variables.
Identifier Naming
Identifiers are things like variables. An Identifier is utilized to recognize the literals utilized in the program. The standards to name an identifier are given underneath.
- The variable’s first character must be an underscore or alphabet (_).
- Every one of the characters with the exception of the main person might be a letter set of lower-case(a-z), capitalized (A-Z), highlight, or digit (0-9).
- White space and special characters (!, @, #, %, etc.) are not allowed in the identifier name. ^, &, *).
- Identifier name should not be like any watchword characterized in the language.
- Names of identifiers are case-sensitive; for instance, my name, and MyName isn’t something very similar.
- Examples of valid identifiers: a123, _n, n_9, etc.
- Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values
- Python doesn’t tie us to pronounce a variable prior to involving it in the application. It permits us to make a variable at the necessary time.
- In Python, we don’t have to explicitly declare variables. The variable is declared automatically whenever a value is added to it.
- The equal (=) operator is utilized to assign worth to a variable.
Object References
When we declare a variable, it is necessary to comprehend how the Python interpreter works. Compared to a lot of other programming languages, the procedure for dealing with variables is a little different.
Python is the exceptionally object-arranged programming language; Because of this, every data item is a part of a particular class. Think about the accompanying model.
print("John")
Output:John
The Python object makes a integer object and shows it to the control center. We have created a string object in the print statement above. Make use of the built-in type() function in Python to determine its type.
type("John")
Output:<class ‘str’>
In Python, factors are an symbolic name that is a reference or pointer to an item. The factors are utilized to indicate objects by that name.
Let’s understand the following example
a = 50

In the above image, the variable a refers to an integer object.
Suppose we assign the integer value 50 to a new variable b.
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
Every object created in Python has a unique identifier. Python gives the dependable that no two items will have a similar identifier. The object identifier is identified using the built-in id() function. consider about the accompanying model.
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, an and b both highlight a similar item. The id() function that we used to check returned the same number. We reassign a to 500; The new object identifier was then mentioned.
Variable Names
The process for declaring the valid variable has already been discussed. Variable names can be any length can have capitalized, lowercase (start to finish, a to z), the digit (0-9), and highlight character(_). Take a look at the names of valid variables in the following example.
name = "Devansh"
age = 20
marks = 80.50
print(name)
print(age)
print(marks)
Output:Devansh 20 80.5
Consider the following valid variables name.
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, na56me)
Output:A B C D E D E F G F I
We have declared a few valid variable names in the preceding example, such as name, _name_, and so on. However, this is not recommended because it may cause confusion when we attempt to read code. To make the code easier to read, the name of the variable ought to be descriptive.
The multi-word keywords can be created by the following method.
- 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.
Multiple Assignment
Multiple assignments, also known as assigning values to multiple variables in a single statement, is a feature of Python.
We can apply different tasks in two ways, either by relegating a solitary worth to various factors or doling out numerous qualities to different factors. Take a look at the following example.
1. Assigning single value to multiple variables
Eg:
x=y=z=50
print(x)
print(y)
print(z)
Output:50 50 50
2. Assigning multiple values to multiple variables:
Eg:
a,b,c=5,10,15
print a
print b
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
The variables that are declared within the function and have scope within the function are known as local variables. Let’s examine the following illustration.
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:
We declared the function add() and assigned a few variables to it in the code above. These factors will be alluded to as the neighborhood factors which have scope just inside the capability. We get the error that follows if we attempt to use them outside of the function.
add()
# Accessing local variable outside the function
print(a)
Output: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 utilized all through the program, and its extension is in the whole program. Global variables can be used inside or outside the function.
By default, a variable declared outside of the function serves as the global variable. Python gives the worldwide catchphrase to utilize worldwide variable inside the capability. The function treats it as a local variable if we don’t use the global keyword. Let’s examine the following illustration.
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 give out a value to it. We then created a function and used the global keyword to access the declared variable within the function. We can now alter its value. After that, we gave the variable x a new string value and then called the function and printed x, which displayed the new value.=
Delete a variable
We can delete the variable using the del keyword. The syntax is given below.
Syntax –
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
Maximum Possible Value of an Integer in Python
Python, to the other programming languages, does not support long int or float data types. It uses the int data type to handle all integer values. The query arises here. In Python, what is the maximum value that the variable can hold? Take a look at the following example.
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 find in the above model, we assigned a large whole number worth to variable x and really look at its sort. It printed class <int> not long int. As a result, the number of bits is not limited, and we are free to use all of our memory.
There is no special data type for storing larger numbers in Python.
Print Single and Numerous Factors in Python
We can print numerous factors inside the single print explanation. The examples of single and multiple printing values are provided below.
Example – 1 (Printing Single Variable)
# printing single value
a = 5
print(a)
print((a))
Output:5 5
Example – 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
Basic Fundamentals:
This section contains the fundamentals of Python, such as:
i)Tokens and their types.
ii) Comments
a)Tokens:
- The tokens can be defined as a punctuator mark, reserved words, and each word in a statement.
- The token is the smallest unit inside the given program.
There are following tokens in Python:
- Keywords.
- Identifiers.
- Literals.
- Operators.
Leave a Reply