PYTHON
PYTHON
5.3 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.
5.4 VARIABLES
Variable is an identifier whose value can change. For example variable age can have different values
for different people. Variable names should be unique in a program. Value of a variable can be string
(for example, ‘b’, ‘Global Citizen’), number (for example 10,71,80.52) or any combination of
alphanumeric (alphabets and numbers for example ‘b10’) characters. In Python, we can use an
assignment statement to create new variables and assign specific values to them.
gender = 'M'
price = 987.9
Variables must always be assigned values before they are used in the program, otherwise it will lead
to an error. Wherever a variable name occurs in the program, the interpreter replaces it with the
value of that variable.
5.5.1 NUMBER
Number data type stores numerical values only. It is further classified into three different types: int,
float and complex.
Boolean data type (bool) is a subtype of integer. It is a unique data type, consisting of two constants,
True and False. Boolean True value is non-zero. Boolean False is the value zero.
Variables of simple data types like integer, float, Boolean etc. hold single value. But such variables are
not useful to hold multiple data values, for example, names of the months in a year, names of
students in a class, names and numbers in a phone book or the list of artefacts in a museum. For this,
Python provides sequence data types like Strings, Lists, Tuples, and mapping data type Dictionaries.
5.5.2 SEQUENCE
A Python sequence is an ordered collection of items, where each item is indexed by an integer value.
Three types of sequence data types available in Python are Strings, Lists and Tuples. A brief
introduction to these data types is as follows:
1)STRING
String is a group of characters. These characters may be alphabets, digits or special characters
including spaces. String values are enclosed either in single quotation marks (for example ‘Hello’) or
in double quotation marks (for example “Hello”). The quotes are not a part of the string, they are
used to mark the beginning and end of the string for the interpreter. For example,
2) List
List is a sequence of items separated by commas and items are enclosed in square brackets []. Note
that items may be of different date types.
3) TUPLE
Tuple is a sequence of items separated by commas and items are enclosed in parenthesis (). This is
unlike a list, where values are enclosed in brackets []. Once created, we cannot change items in the
tuple. Like List, items may be of different data types.
3.5.3 MAPPING
Mapping is an unordered data type in Python. Currently, there is only one standard mapping data type
in Python called Dictionary.
5.6 OPERATOR
An operator is used to perform specific mathematical or logical operations on values. The values that
the operator works on are called operands. For example, in the expression 10 + num, the value 10, and
the variable num are operands and the + (plus) sign is an operator. Python supports several kinds of
operators their categorization is briefly explained in this section.
1) in-Returns True if the variable or value is found in the specified sequence and False otherwise
2) not-in-Returns True if the variable/value is not found in the sequence and False otherwise
5.7 EXPRESSIONS
An expression is defined as a combination of constants, variables, and operators. An expression always
evaluates to a value. A value or a standalone variable is also considered as an expression, but a
standalone operator is not an expression. Some examples of valid expressions are given below.
(i) num – 20.4 (iii) 23/3 -5 * 7(14 -2) (ii) 3.0 + 3.14 (iv) "Global “+” Citizen"
5.8 DEBUGGING
Due to errors, a program may not execute or may generate wrong output:
Python has many predefined functions called built-in functions. We have already used two built-in
functions print () and input(). A module is a python file in which multiple functions are grouped
together. These functions can be easily used in a Python program by importing the module using
import command. To use a built-in function we must know the following about that function:
• Return Value − A function may or may not return one or more values. A function performs
operations based on argument (s) passed to it and the result is passed back to the calling point.
Some functions do not return any value.
• if...elif....else is use dot check multiple conditions and execute statements accordingly. Meaning of
elif is elseif. We can also write elseif instead of elif for more clarity.