You are on page 1of 7

IDENTIFIERS

In programming languages, identifiers are names used to identify a variable,


function, or other entities in a program. The rules for naming an identifier in
Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or an
underscore sign (_). This may be followed by any combination of characters a–z,
A–Z, 0–9 or underscore (_). Thus, an identifier cannot start with a digit.
• It can be of any length. (However, it is preferred to keep it short and meaningful).
• It should not be a keyword or reserved word
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
PYTHON KEYWORDS

Keywords are reserved words. Each keyword has a specific meaning to the
Python interpreter, and we can use a keyword in our program only for the purpose
for which it has been defined. As Python is case sensitive.

Python contains thirty-five keywords in the most recent version, i.e., Python
3.8. Here we have shown a complete list of Python keywords for the reader's
reference.

False await else import pass

None break except in raise

True class finally is return

and continue for lambda try

as def from nonlocal while

assert del global not with

async elif if or yield

VARIABLES

A variable in a program is uniquely identified by a name (identifier).


Variable in Python refers to an object — an item or element that is stored in the
memory. Value of a variable can be a string (e.g., ‘b’, ‘Global Citizen’), numeric
(e.g., 345) or any combination of alphanumeric characters (CD67). In Python we
can use an assignment statement to create new variables and assign specific values
to them.

gender = 'M'
message = "Keep Smiling"
price = 987.9

Program 5-2 Write a program to display values of


variables in Python.

#Program 5-2
#To display values of variables

message = "Keep Smiling"


print(message)
userNo = 101
print('User Number is', userNo)

Output:
Keep Smiling
User Number is 101

In the program 5-2, the variable message holds string type value and so its content
is assigned within double quotes " " (can also be within single quotes ' '), whereas
the value of variable userNo is not enclosed in quotes as it is a numeric value.
Variable declaration is implicit in Python, means variables are automatically
declared and defined when they are assigned a value the first time. Variables must
always be assigned values before they are used in expressions as otherwise it will
lead to an error in the program. Wherever a variable name occurs in an expression,
the interpreter replaces it with the value of that particular variable.

Program 5-3 Write a Python program to find the area


of a rectangle given that its length is 10
units and breadth is 20 units.

#Program 5-3
#To find the area of a rectangle
length = 10
breadth = 20
area = length * breadth
print(area)
Output:

200

Consider the following valid variables name.

1. name = "A"  
2. Name = "B"  
3. naMe = "C"  
4. NAME = "D"  
5. n_a_m_e = "E"  
6. _name = "F"  
7. name_ = "G"  
8. _name_ = "H"  
9. na56me = "I"  
10.  
11.print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_nam
e, na56me)  

Output:
ABCDEDEFGFI

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.

Example -
1. # Declaring a function  
2. def add():  
3.     # Defining local variables. They has scope only within a function  
4.     a = 20  
5.     b = 30  
6.     c = a + b  
7.     print("The sum is:", c)  
8.   
9. # Calling a function  
10.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.

1. add()  
2. # Accessing local variable outside the function   
3. 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 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 -

1. # Declare a variable and initialize it  
2. x = 101  
3.   
4. # Global variable in function  
5. def mainFunction():  
6.     # printing a global variable  
7.     global x  
8.     print(x)  
9.     # modifying a global variable  
10.    x = 'Welcome To Javatpoint'  
11.    print(x)  
12.  
13.mainFunction()  
14.print(x)  

Output:

101

Welcome ToJavatpoint

Welcome ToJavatpoint

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 -

1. # Assigning a value to x  
2. x = 6  
3. print(x)  
4. # deleting a variable.   
5. del x  
6. print(x)  

Output:

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

Print Single and Multiple Variables in Python

We can print multiple variables within the single print statement. Below are the
example of single and multiple printing values.
Example - 1 (Printing Single Variable)

1. # printing single value   
2. a = 5  
3. print(a)  
4. print((a))  
Output:

Example - 2 (Printing Multiple Variables)

1. a = 5  
2. b = 6  
3. # printing multiple variables  
4. print(a,b)  
5. # separate the variables by the comma  
6. Print(1, 2, 3, 4, 5, 6, 7, 8)   

Output:

56

12345678

You might also like