Chapter 3– Python Programming Fundamentals
1. Tokens:
Tokens are the smallest element of a programming language.
Following are the categories of tokens:
(i) Keywords
(ii) Identifiers
(iii) Operators
(iv) Literals
(v) Delimiters
2. Keywords:
Keywords are the reserved words for special purpose.
Eg. if, else, def, return, while, for, and, or, not
3. Identifiers:
The name of any variable, constant, function, or module is called an identifier.
4. Rules for identifiers:
Identifier must begin with an alphabet or underscore
Only alphabet, underscore or number is allowed, no other character is allowed
Keywords cannot be used as identifier.
5. Operators:
These are symbols or a word that performs some of kind of operation. Eg. +, -, >, = , and, in etc.,
6. Literals:
A fixed numeric or non numeric value is called literal.
Eg. String literal – “hello”
integer literal – 2, 200, -34
float literal – 34.33, 12.88, -34.44
boolean literal – True, False
Collection – list [3,2,5,4], tuple (4,32,6)
7. Delimiters:
Delimiters are the symbols which can be used as separators of values or to enclose some values.
Eg. (), [], comman(,), colon( ;), semicolon(:)
8. Variable:
• Variable is like a container that stores values.
• Variable in python is created in the following way
a = 30
where
a is variable name
= is assignment operators
30 is the value assigned to the variable.
9. id() :
id() is a built in function in python which returns the address of object in the memory
Eg.
a = 20
print(id(a))
output:
16143422345 <----- address of memory location storing value 20
10. Type of Values or Data-type in python
(a) Numbers:
(i) integers: Integers represent whole numbers without any fractional part. Eg. 34, 88, -232
➔ boolean(bool): Boolean is a sub-type of integer. It represents only two values – True(1)
and False(0)
(ii) float: Float represent numbers with decimal point. Eg. 44.23, -33.22
(iii) complex: They are made up of pairs of real and imaginary numbers. Eg. 2 + 5j
(b) Sequences:
Strings : Strings are sequence of characters enclosed within quotes. Eg. “hello”, ‘python’
list: List is a sequence of items/elements separated by commas and are enclosed in square
brackets [].
Eg. [3, 5, ‘python’, 33.4]
tuple: Tuple is sequence of items/elements separated by commas and are enclosed in parentheses
().
Eg. (30, 4.5, ‘hello’, ‘s’)
List are mutable datatype whereas tuple are immutable datatypes.
(c) Mapping:
Dictionary:
Dictionary is a collection of elements in from of key:value pairs and are enclosed in curly
braces {}.
Eg. {‘Name’: ‘Hari’, “age” : 20}
(d) None:
None is used to signify the absence of of value.
11. mutable/immutable datatypes:
mutable:
• mutable means changeable or modifiable.
• The mutable datatype in python are list, dictionary and set
immutable:
• immutable values are those that cannot be modified once created.
• The immutable datatype in python are int, float, complex, string, tuple, boolean and None.
12. type():
type() is built in function that returns the type/data type of the value or object passed to it.
Eg:
a = 20
type(a) <------ will return <class integer> denoting a is of integer type.
13. Operators and Operands:
Operators:
These are symbols or a word that performs some of kind of operation.
Operands:
These the values or variables on which the operation is being performed.
Eg:
a = 20 + 30
in the above expression
a, 20, 30 are operands
= and + are operators
14. Types of Operators:
(i) Arithmetic Operators:
These are simple mathematical operators.
• Among the above Arithmetic operators the Exponent Operator (**) has got the highest
priority/precedence
• Python follows the PEMDAS rule to solve an expression involving mathematical operators.
(ii) Relational Operators:
Relational operators are used to compare two values and provides result in either True or False.
(iii) Logical Operator:
Logical Operators are used to check for multiple conditions
Operator Description Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is True not(x < 5 and x < 10)
Priority in Logical Operators is: not, and, or (first “not” to performed, then “and”, finally “or”)
(iv) Assignment operator: (=)
Assignment Operator is used to assign a value to a variable.
Eg: a = 10
Shorthand/Augmented Assignment Operator:
They perform the operation and assign the result back to the variable
(v) Membership Operator:(in, not in)
It is used to check whether a particular character or an element is present in the given string or sequence.
Operator Syntax Example
x = "Hello, World!"
in x in y
print("ello" in x) <--- gives True
x = "Hello, World!"
not in x not in y
print("hello" not in x) <--- gives True
(vi) Identity Operator:(is, is not)
Identity operators are used to compare the objects if both the objects are actually of the same data type and share
the same memory location
15. input():
input() is an built function which is used to take user input.
By default, it returns the user input in form of a string.
Syntax:
input(prompt)
where:
prompt [optional]: any string value to display as input message
Eg: input(“What is your name? “)