Chapter – 1
Review of Python Basics
Python - Python is a powerful, modern, high-level programming language. It is an
interpreted language as its programs are executed by interpreter.
IDLE - IDLE is Integrated Development and Learning Environment which provides two
working modes.
• Interactive mode
• Script mode
Interactive mode – It is also known as Python Shell Window. Python commands
directly typed, and when we press Enter key, the interpreter displays the result.
Script mode – Script mode save the set of commands and execute as a whole program.
Structure of a Python Program
Expression – An expression represents something, like a number, a string or an element.
Statements – Any assignment to a variable or function call is a statement.
Comments – Comments are the additional information against a statement or a block of
code for the better clarity of code.
# symbol is used for single-line comments
‘’’ or “”” Triple Quotation Marks are used to write multiple line comments.
Functions – Functions is a set of instructions defined under a particular name, which
once written can be called whenever and wherever required.
Block – A block refers to a group of statements which are part of another
statement or function. All statements inside a block are intended at the
same level.
Tokens - A token is the smallest element of a Python script that is meaningful to the
interpreter.
Types of tokens are:
Identifiers: The name of any variable, constant, function or module is called an identifier.
Ex abc, add_stu, pi etc.
Keywords: The reserved words for Python, which have a special fixed meaning for the
interpreter. No keyword can be used as identifier. Example for, if define etc.
Literals: A fixed numeric or non-numeric value is called a literal. It can be define as a
number, text or other data that represents values to stored in variables. Examples
– 6, -9.87, “Ram”.
Operators: A symbol or a word that performs some kind of operation on given values and
returns the result. Example +, -, *, /, % etc.
Delimiters: Delimiters are the symbols which can be used as separators of values or to be
enclose some values. Example – ( ), { }, [ ], ;, :
Note – Comments are not considered as token.
Variables- A variable is like a container that stores values, that can access or change. It is a
name that refers to a value at a particular memory location. Example : x = 20
Data Type
1. Numeric Data Type
A. Integer and Long
B. Floating Point
C. Complex Numbers
D. Boolean
2. Sequence
A. String
B. Tuples
C. Lists
3. Sets
4. Mappings
5. None
Operators
Arithmetic Operators
Assignment Operators
Relational or Comparison Operator
Logical Operator
Identity Operator
Bitwise Operator
Membership Operator
Arithmetic Operator
Operator Description Example
+ (unary) Express a positive number +3
- (unary) Express a negative number - 3
+ (binary) To add numbers Value of 2+3 is 5
- (binary) Subtract one value from the other Value of 5 – 2 is 3
* Product of two numbers Value of 2*3 is 6
/ To find the quotient Value of 5/2 is 2.5
// Return integer part of the quotient Value of 9//2 is 4
% Return reminder Value of 18%5 is 3
** Exponent 3**2 is 9
Assignment Operators
Operator Description
= Right value assign to Left value
+= R-value adds it to L-value
-= R-value subtracts from L-value
*= R-value multiplies with L-value
/= R-value divides the L – value
%= Divides L-value with R-value. The reminder is
assigned to L-value
//= Divides the L-value with R-value. The quotient assign
to L-value
**= Calculate (L-value)R-value. The final result is assign to
L-value
Relational Operator
Operator Description
== (Equality) Compares two values for equality.
!=(Inequality) Compares two values for inequality.
< (Less than) Return true if first value is smaller than second value
< = (Less than or equal to) Return true if first value is smaller than or equal to
second value
> (Greater than) Return true if first value is greater than second value
>= (Greater than or equal to) Return true if first value is greater than or equal to
second value
Logical Operators
Operator Description
NOT Negates a condition and return True if the condition is false.
AND Combines two conditions and return TRUE if both the
conditions are true, otherwise returns False
OR Combines two conditions and return True if at least one of the
conditions is true, otherwise returns False.
Flow of Execution
Sequential Statements
Selection / Conditional Statements
If
If …. Else
If …. Elif…… else
Iteration or Looping Constructs
For Loop
While Loop
Functions – A function is a group of statements that exists within a program for the purpose
of performing a specific task.
Instead of writing a large program as one long sequence of instructions, it can be
written as several small functions, each performing a specific part of the task.
Functions can be categories into the following three types:
I. Built – in
II. Modules
III. User-define
Built –in Function – Built in functions are pre-define functions that are already available in
Python. These are always available in the standard library and don’t have to import any
module (file) for using them. Example:
Type conversion functions – int(), float(),
Input function – Input()
Eval(), min(), max(), abs(), type(), len(), round(), range() etc.
Module – A module is a file that contains Python code or a series of function. There are
some commonly used modules in Python used for certain predefine tasks and they are called
libraries. Example –[Link], random
User – defined function – A function is a set of statements that performs a specific task.
Types –
No argument and no return value
Argument and no return value
Argument with return value (return keyword is used)
Types of arguments
1. Positional arguments
2. Default arguments
3. Keyword arguments
4. Variable length arguments
Recursion – Recursion is a method in which function calling itself again and again in its
body.