Python Identifiers & Variables

This python tutorial is the continuation of the last tutorial which discussed about Keywords. Identifiers and Variables are the other basic building blocks of any programming language. In this tutorial, you will learn in detail about these fundamental tools of python.

Python Identifiers

Python identifiers are names defined by users to several entities like variables, functions, class, etc. Users can define valid names for python entities by following the rules mentioned below.

  1. Python identifiers are case sensitive so uppercase, lowercase, and its mixed representation are considered as different. For instance
    ADD, Add & add are considered as different identifiers
    
  2. Python identifiers must start either with an alphabet (uppercase or lowercase) or an underscore (_) followed by alphanumeric letters or underscores.
    #Python valid identifiers example  
    _alpha , __beta, Theta_1,  _1day, Day, WEEK, M!
    
  3. Python identifiers never start with digits (0 to 9). For instance, 7Days is an invalid identifier.
    7colours  # invalid identifier example
    _7colours #valid identifier

     

  4. Python identifiers can have alphanumeric letters but no special character like %, @, #, $... can be included. For example, #tag is an invalid identifier.
    %per, $twenty,  #tag  #invalid identifiers

     

Python Variables

Python variables act as a container to store data. In other words, a specific amount of space is allocated in memory to store the data whenever a variable gets created. This allocation of space depends on the datatype of a variable which you will learn in the upcoming tutorials.

Variable Declaration and Assignment in Python

Compared to other programming languages, Python does not require a specific variable declaration. On assigning a value to a variable, python involuntarily declares the variable with the appropriate data type resultantly allocating enough memory for the value.

Example 1: Simple Python Program

age = 17
first_name = ‘Robert’
print(age)
print(first_name) 

In the above example, age is a container that keeps the value 17. We have constructed a python variable named age by assigning an integer value 17 using the assignment operator =. Here, python automatically infers this as integer type and in memory, a 2 byte(or 4 bytes) space is allocated for this python variable age. Similarly, the python variable first_name stores the value Robert as a string type.

Output:

17
Robert

Example 2: Simple Python Program

first_name = ‘Robert’
print(first_name)
first_name = ‘Charles’
print(first_name)
first_name = “Charles”
print(first_name) 

Output:

Robert
Charles
Charles

In this instance, the variable first_name was initially assigned to the value Robert and later on, modified to Charles which tells that value of a python variable can be replaced at any time.

Also python consider both single quote( ‘ ) and double quote( “ ) representation as same for a string variable.


Setting Multiple Values to Multiple Variables

Python permits the assigning of multiples values to multiple variables in a line. See below

Example 3: Multiple Variables

a, b, c = “Apple”, “Ball” ,”Cat”
I, F, S = 10, 9.99, TEN
print (a)
print (b)
print (c)
print (I, F, S) 

Output:

Apple
Ball
Cat
10, 9.99, TEN

Setting Single Value to Multiple Variables

Also in python, we can assign a single value to multiples variables using the below format.

Example 4: Setting Single Value to Multiple Variables

A = B = C = “Apple” 
print (A)
print (B)
print (C)

Output:

Apple
Apple
Apple

Global and Local Variables in python

In python, Variables can be declared either inside or outside of a python function. Based on the declaration we can determine the scope of the variable, whether the scope is inside or is outside of the function. Thus python variables are categorized into two types, namely local variables and global variables

Now let us compare global and local python variables in the python programming language

Global Variables

  1.  Global variables can be defined as variables having life throughout the function.
  2.  They are active both inside and outside the function and hence the scope of the variable is global.
  3.  Global variables declared within a function are always prefixed with Python Keyword “ Global”
  4.  Global variables are accessible by all functions in the program.

Local Variables

  1.  Local variables can be defined as variables having life inside the function only.
  2.  They are inactive outside the function and hence the scope of the variable is restricted locally to the given function.
  3.  Local variables are declared as usual variables with no specific notation of keywords.
  4.  Local variables accessibility is limited to the function in which the variable is created. Hence other functions cannot access local variables.

Variables

The above image shows the generalized view of global and local variables in a python program. A python program PGM contains the variables X, Y, Z, and A and function F1, F2, and F3. From the image, we can infer below points

  1. In the Python programming language, python variable X which is declared in the program and outside of all functions is always considered as a global variable. And it has access to all functions within the python program.
  2. Function F1 contains its local variable Y and if needed can access variable X.
  3. Function F2 contains its local variable Z and if needed can access variable X and Y as F2 is a nested function
  4. Function F3 contains its local variable A and if needed can access variable X You will learn about Python function in our upcoming tutorials.

The constant variable in Python

A constant can be defined as a sort of variable whose value remains the same over a period. To be specific, the value cannot be altered.

In practice, constants are not used frequently in the python programming language. Constants are typically included in a module. A module is plainly a file or a container that keeps variables, functions, etc. 

Constant variable declaration and assignment

  1. Create a module / file with .py extension, for instance, constant.py.
  2. Set the variables with their value. Here Constant variable representation must be in the capital to separate it from usual variables. PI = 3.14 GRAVITY = 9.8
  3. Now import this newly created module costant.py to main.py
    import constant
    print(constant.PI)
    print(constant.GRAVITY) 
    
    Output:
    3.14
    9.8