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 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.
ADD, Add & add are considered as different identifiers
#Python valid identifiers example _alpha , __beta, Theta_1, _1day, Day, WEEK, M!
7colours # invalid identifier example _7colours #valid identifier
%per, $twenty, #tag #invalid identifiers
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.
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.
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.
first_name = ‘Robert’ print(first_name) first_name = ‘Charles’ print(first_name) first_name = “Charles” print(first_name)
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.
Python permits the assigning of multiples values to multiple variables in a line. See below
a, b, c = “Apple”, “Ball” ,”Cat” I, F, S = 10, 9.99, TEN print (a) print (b) print (c) print (I, F, S)
Apple Ball Cat 10, 9.99, TEN
Also in python, we can assign a single value to multiples variables using the below format.
A = B = C = “Apple” print (A) print (B) print (C)
Apple Apple Apple
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
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
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.
PI = 3.14 GRAVITY = 9.8
import constant print(constant.PI) print(constant.GRAVITY)