0% found this document useful (0 votes)
8 views364 pages

Full Python

The document provides an overview of algorithms and flowcharts, detailing their definitions, characteristics, advantages, and control structures. It also covers how to write algorithms, the significance of flowcharts in programming, and introduces Python, its features, installation, and basic syntax. Additionally, it discusses identifiers, variables, input/output statements, comments, and the importance of indentation in Python programming.

Uploaded by

bhuvan18reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views364 pages

Full Python

The document provides an overview of algorithms and flowcharts, detailing their definitions, characteristics, advantages, and control structures. It also covers how to write algorithms, the significance of flowcharts in programming, and introduces Python, its features, installation, and basic syntax. Additionally, it discusses identifiers, variables, input/output statements, comments, and the importance of indentation in Python programming.

Uploaded by

bhuvan18reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 364

ALGORITHM & Lecture - 2

FLOWCHART
ALGORITHM
ØThe word algorithm relates to the name of mathematician Al-
Khowarizmi, which means a procedure or a technique.
ØAn algorithm is a sequence of steps to solve a particular
problem or algorithm is an ordered set of unambiguous steps
that produces a result and terminates in a finite time.
ØAlgorithm has the following characteristics
ØInput: An algorithm may or may not require input
ØOutput: Each algorithm is expected to produce at least one result
ØDefiniteness: Each instruction must be clear and unambiguous.
ØFiniteness: If the instructions of an algorithm are executed, the
algorithm should terminate after finite number of steps
ADVANTAGES OF ALGORITHM

ØIt is a step-wise representation of a solution to a given


problem, which makes it easy to understand.
ØAn algorithm uses a definite procedure.
ØIt is not dependent on any programming language, so it is easy
to understand for anyone even without programming
knowledge.
ØEvery step in an algorithm has its own logical sequence so it is
easy to debug.
THREE TYPES OF CONTROL
STRUCTURES
The algorithm (flowchart) include following three types of control structures:
Sequence: In the sequence structure, statements are placed one after the other
and the execution takes place starting from up to down.
Branching (Selection): In branch control, there is a condition and according to a
condition, a decision of either TRUE or FALSE is achieved. In the case of TRUE,
one of the two branches is explored; but in the case of FALSE condition, the
other alternative is taken. (IF-THEN is used to represent branch control).
Loop (Repetition): The Loop or Repetition allows a statement(s) to be executed
repeatedly based on certain loop condition e.g. WHILE, FOR loops.
HOW TO WRITE ALGORITHMS

Step 1 Define your algorithms input: Many algorithms take in data to be


processed,
­ e.g. to calculate the area of rectangle input may be the rectangle height and rectangle width.

Step 2 Define the variables: Algorithm's variables allow you to use it for more
than one place.
­ We can define two variables for rectangle height and rectangle width as HEIGHT and WIDTH (or H
& W). We should use meaningful variable name e.g. instead of using H & W use HEIGHT and
WIDTH as variable name.
HOW TO WRITE ALGORITHMS

Step 3 Outline the algorithm's operations: Use input variable for computation
purpose,
­ e.g. to find area of rectangle multiply the HEIGHT and WIDTH variable and store the value in new
variable (say) AREA.
­ An algorithm's operations can take the form of multiple steps and even branch, depending on the
value of the input variables.

Step 4 Output the results of your algorithm's operations:


­ In case of area of rectangle output will be the value stored in variable AREA. if the input variables
described a rectangle with a HEIGHT of 2 and a WIDTH of 3, the algorithm would output the value
of 6.
FLOWCHART

qThe first design of flowchart goes back to 1945 which was designed by John Von
Neumann.
qUnlike an algorithm, Flowchart uses different symbols to design a solution to a
problem.
qIt is another commonly used programming tool.
qBy looking at a Flowchart one can understand the operations and sequence of
operations performed in a system.
qFlowchart is often considered as a blueprint of a design used for solving a
specific problem.
ADVANTAGES OF FLOWCHART
vFlowchart is diagrammatic /Graphical representation of sequence of steps to
solve a problem
vFlowchart is an excellent way of communicating the logic of a program.
vEasy and efficient to analyze problem using flowchart.
vDuring program development cycle, the flowchart plays the role of a blueprint,
which makes program development process easier.
vAfter successful development of a program, it needs continuous timely
maintenance during the course of its operation.
vThe flowchart makes program or system maintenance easier.
vIt is easy to convert the flowchart into any programming language code.
STANDARD SYMBOLS
vThe language used to write algorithm is simple and similar to day-to-day life
language.
vThe variable names are used to store the values. The value store in variable can
change in the solution steps.
vIn addition some special symbols are used as below:
vAssignment Symbol ( <- or =) is used to assign value to the variable.
e.g. to assign value 5 to the variable HEIGHT, statement is
HEIGHT <- 5
or
HEIGHT = 5

vThe statement C = A + B means that add the value stored in variable A and
variable B then assign/store the value in variable C.
vThe statement R = R + 1 means that add I to the value stored in variable R and
then assign/store the new value in variable R, in other words increase the value
of variable R by 1
ALGORITHM & FLOWCHART TO
FIND THE SUM OF TWO NUMBERS
ALGORITHM & FLOWCHART TO CONVERT
TEMPERATURE FROM CELSIUS TO FAHRENHEIT
ALGORITHM & FLOWCHART TO CONVERT
TEMPERATURE FROM FAHRENHEIT TO CELSIUS
14-11-2022 Dr Aravapalli Rama Satish 1
Python Introduction -
Operators & Branching
Module - 2
Python - History

• Started development of Python Language in Dec,1989.


• Version – 1 released in Jan,1994.
• Version – 2 released in Oct, 2000.
• Version – 3 released in Dec, 2008. (Now, Python 3.11)

14-11-2022 Dr Aravapalli Rama Satish 3


Why to learn python?

• It is a General Purpose Language


• Backend web development, data analysis, AI and scientific
computing, game development, desktop applications, etc.,
• Beginner Friendliness
• Easy to understand and flexible

14-11-2022 Dr Aravapalli Rama Satish 4


Features of Python
• Simple
• Feel like reading English structures
• More clarity and less stress in understanding the syntax of language
• Easy to Learn
• Few keywords, simple structures
• Open Source
• Freely downloadable – no need to pay to work with it
• Source code is also available
• Dynamically Typed
• No need to declare any variable – Assignment statement binds a
name to an object of any type.
14-11-2022 Dr Aravapalli Rama Satish 5
Features of Python
• Platform Independent
• Compiling python program generates byte code (fixed set of
instructions that run on all OSs and hardware
• Python Virtual Machine(PVM) can run these byte code on any
platform.
• Portable
• When a program yields the same result on any computer then it is a
portable program
• Procedure and Object-oriented
• Interpreted
• PVM converts byte code into Machine code

14-11-2022 Dr Aravapalli Rama Satish 6


Features of Python
• Extensible and Embeddable
• C/C++ → Python programs
• Huge Library
• Database Connectivity
• Scalable
• It can take the advantage of computing power when a program is
moved from one hardware to another.
• REPL Environment (Since it is a scripting language uses
interpreter)
• Read – Evaluate – Print – Loop

14-11-2022 Dr Aravapalli Rama Satish 7


Python Installation

• Refer the following page:


• https://data-flair.training/blogs/install-python-windows/

14-11-2022 Dr Aravapalli Rama Satish 8


Execution Sequence of a python program

Execution
Execution
in
in
Interactive
Script mode
mode

Way - I C:\> python x.py  for Execution

C:\> python –m py_compile x.py  To store the byte code in separate file in
Way - II the directory “__pycache__” as “x.cpython-34.pyc”
C:\> python x.cpython-34.pyc

14-11-2022 Dr Aravapalli Rama Satish 9


Identifier and Variable
• Identifier:
• It is a name given to a variable, function, or class etc.,
• Letters, numbers, and the underscore characters included.
• Always start with non-numeric character.
• Special symbols are not allowed.
• Case sensitive – Treats upper case and lower case differently.
• Variable:
• Variables are containers for storing data values.
• Unlike other programming languages, Python has no command for
declaring a variable.
• A variable is created the moment you first assign a value to it.

14-11-2022 Dr Aravapalli Rama Satish 10


Identifier and Variable
• Variables do not need to be declared with any particular type
and can even change type after they have been set.

14-11-2022 Dr Aravapalli Rama Satish 11


Invalid Variable Names & its Correction

14-11-2022 Dr Aravapalli Rama Satish 12


How a variable is seen inside python ?
• A variable is seen as a tag(name) that is
tied to some value.
• Ex: a = 1 means the value ‘1’ is created first
in memory and then a tag by the name ‘a’ is
created to show that value.
• Python considers the values (i.e. 1 or 2) as
‘objects’.
• If we change the value of ‘a’ to a new value
as a = 2, then the tag is simply changed to
the new value(or object) as shown.
• Since the value ‘1’ becomes unreferenced
object, it is removed by garbage collector.

14-11-2022 Dr Aravapalli Rama Satish 13


Assignment statement(operator)
• It is used to store the output of computed value(mathematical
expression, logical expression, relational expression), a
literal, or a existing variable value etc., to a variable.
• Ex: >>> b = a

14-11-2022 Dr Aravapalli Rama Satish 14


Assignment statement(operator)
• Python allows to
• assign values to multiple variables in one line
• assign the same value to multiple variables in one line

14-11-2022 Dr Aravapalli Rama Satish 15


Reserved Words (Keywords)

We cannot use a
keyword as a
variable name,
function name or
any other
identifier.

14-11-2022 Dr Aravapalli Rama Satish 16


Reserved Words (Keywords)
• The above keywords may get altered in different versions of
Python.
• Some extra might get added or some might be removed.
• You can always get the list of keywords in your current version
by typing the following in the prompt.

14-11-2022 Dr Aravapalli Rama Satish 17


Input Statement – input()
• Accept input from the user or keyboard and return input as a
string.

14-11-2022 Dr Aravapalli Rama Satish 18


Output Statement – print()
• print() – When it is called, it display a blank line and cursor thrown
to the next line.
• print(“string”) – A String represents group of characters. When a
string is passed to the print(), the string is displayed as it is.
• We can use escape sequence characters inside the print(). These
characters have special meaning. Ex:‘\n’ -> new line,‘\t’ -> tab.
• To remove the effect of these characters, we add ‘\’ character before them.
Ex:‘\\n’ -> \n

14-11-2022 Dr Aravapalli Rama Satish 19


Output Statement – print()
• * - Repetition Operator – to
repeat the string ‘n’ times in the
output.
• + - Concatenation Operator – to
join two strings with out adding
any character between them.
• print(variables list)
• To display the values of all
variables in the list separated by
comma.
• Default separator between
variable values is space while
printing on the screen.
14-11-2022 Dr Aravapalli Rama Satish 20
Output Statement – print()
• To change the default
separator we use ‘sep’
attribute.
• When several print() are
used to display output, each
print() will display the output
in a separate line.
• To change this behavior we
use ‘end’ attribute.

14-11-2022 Dr Aravapalli Rama Satish 21


Output Statement – print()

14-11-2022 Dr Aravapalli Rama Satish 22


Output Statement – print()

• print(“string”, variables list)


• print(object)

14-11-2022 Dr Aravapalli Rama Satish 23


Comments – Single line, Multi line
• # - Single Line Comment
• It is used to describe the purpose of program, statement, function,…
• In general, comments are started at beginning of the line headed with a
symbol ‘#’.
• Comments are not executed by machine and also not translated to byte
code.
• Comments are used only for the end user understanding.
• # - Multi Line Comment
• We need to write ‘#’ at the beginning of every line.
• “ “ “ or ‘ ‘ ‘ – Multi Line Comment
• Pair of triple double(single) quotes can be used to hold multi line
comments.

14-11-2022 Dr Aravapalli Rama Satish 24


Comments – Doc Strings
• Python doesn’t support Multi Line Comments
• Triple double(single) quotes are regular strings span multiple
lines, means that memory is allocated to these string
internally.
• If these strings are not assigned to any variable, then they are
removed from memory by the garbage collector and hence these
can be used as comments. – But not recommended.
• What ever the strings inside triple quotes and are written
immediately after declaration of module, function, class, or a
method, then these strings are called “Doc Strings”.
• Useful to create and API documentation file from a python program

14-11-2022 Dr Aravapalli Rama Satish 25


Printing Doc Strings

To create API Documentation:


C:\> python –m pydoc –w ex
Where ex is the name of the python source file “ex.py”

14-11-2022 Dr Aravapalli Rama Satish 26


Indentation & Code blocks
• It refers to spaces that are used in the beginning of as statement.
• The statements (contiguous) with same indentation belongs to
same group called a suite.
• By default python uses 4 spaces, but it can be increased or
decreased.

14-11-2022 Dr Aravapalli Rama Satish 27


Indentation & Code blocks
• Compilers/interpreters generally do not know the sequence in
which they need to execute the statements in a piece of code.
• Hence, to make it easier, we divide the code into several blocks of code
and indent it.
• This indentation helps them understand the order in which each
block/statement should be executed.
• Python indentation is a way of telling the Python interpreter that a
series of statements belong to a particular block of code.
• All the statements with the same distance (space) to the right,
belong to the same block.
• In other words, statements belonging to a block will necessarily start from
the same vertical line.

14-11-2022 Dr Aravapalli Rama Satish 28


Indentation & Code blocks

14-11-2022 Dr Aravapalli Rama Satish 29


Indentation & Code blocks

• Python uses whitespace indentation, rather than curly


brackets or keywords, to delimit blocks.
• An increase in indentation comes after certain statements
represents beginning of new block; a decrease in indentation
signifies the end of the current block.
• Thus, the program's visual structure accurately represents the
program's semantic structure.
• This feature is sometimes termed the off-side rule, which some other
languages share, but in most languages indentation doesn't have any
semantic meaning.

14-11-2022 Dr Aravapalli Rama Satish 30


Indentation & Code blocks
• When writing Python code, we have to
define a group of statements for
functions and loops. This is done by
properly indenting the statements for
that block.
• The leading whitespaces (space and
tabs) at the start of a line are used to
determine the indentation level of the
line.
• We have to increase the indent level to
group the statements for that code block.
Similarly, reduce the indentation to close
the grouping.
• The four white spaces or a single tab
character are used to create or increase
the indentation level of the code.

14-11-2022 Dr Aravapalli Rama Satish 31


Python Indentation Rules
• We can’t split indentation into multiple lines using a backslash.
• The first line of Python code can’t have an indentation, it will throw
IndentationError.
• You should avoid mixing tabs and whitespaces to create an
indentation.
• It is preferred to use whitespaces for indentation than the tab
character.
• The best practice is to use 4 whitespaces for the first indentation
and then keep adding additional 4 whitespaces to increase the
indentation.
14-11-2022 Dr Aravapalli Rama Satish 32
Indentation & Code blocks
• One of the most distinctive feature of python is its used of
indentation to mark blocks of code.
• Lines 2 & 4 are two separate code blocks contains only a single line -also
allows multiple lines of statements – all statements (contiguous) with the
same indentation considered as single code block.
• Other Prog. Lang, uses indentation to make the code looks pretty, where as
in python it is required to indicate block the statement belongs to.

14-11-2022 Dr Aravapalli Rama Satish 33


Data Types

• A data type represents the type of data stored into a variable


or memory.
• Built-in data types: Already available in python
• User-defined data types: Created by programmer for their
development of applications with the help of class, module, array,…

14-11-2022 Dr Aravapalli Rama Satish 34


Taxonomy of Built-in Data Types

14-11-2022 Dr Aravapalli Rama Satish 35


Numeric Types
• Represents numbers – 3 sub types – int, float, complex
• int:
• Represents an integer – with out decimal or fraction part – no limit on the size of
number – can store very large numbers conveniently.
• Ex: 200, -50, 0, 9888998700
• float:
• Contains a decimal point – can also written in scientific notation – e or E is used to
represent the power of 10 – Scientific notation occupies less memory for big
numbers.
• Ex: 0.5, -3.4567, 0.001, 2.5E4, 22.55e3
• complex:
• A number written in the form of a+bj or a+bJ, a is a real part and b is a imaginary
part, can be int or float
• The suffix j/J indicates square root of -1 : √(−1)
• Ex: -1 - 5.5J

14-11-2022 Dr Aravapalli Rama Satish 36


Numeric Types

• Representing Binary, Octal, and Hexadecimal Numbers:


• Binary – Prefixed with 0b or 0B – Ex: 0b110110
• Octal – Prefixed with 0o or 0O – Ex: 0o773
• Hexadecimal – Prefixed with 0x or 0X – Ex: 0xA180
• Explicit Data Type Conversion:
• Depending on the type of data, python internally assumes the data
type for the variable.
• But some times, the programmer wants to convert one data type into
another type on his own – Type Conversion or Coercion.
• It can be done by mentioning the data type with parentheses.

14-11-2022 Dr Aravapalli Rama Satish 37


Numeric Types
Numeric Conversions Conversion to Decimal from Other Bases Conversion from Decimal to
binary, octal, and hexadecimal

int( string, base) – base refers to number


system in which the string lies.
14-11-2022 Dr Aravapalli Rama Satish 38
Boolean Type
• “bool” keyword is used – Refers Boolean values: True or False
as 1 or 0.
• A Blank String like “” is also considered as False
• Conditional expressions will be evaluated internally either
True or False

14-11-2022 Dr Aravapalli Rama Satish 39


Sequences

• A Sequence represents a group of elements or items. For


example, a group of integers will form a sequence.
• 6 types of sequences: str, bytes, bytearray, list, tuple, range

14-11-2022 Dr Aravapalli Rama Satish 40


String data type
• “str” represents string data type.
• A string is represented by a group of characters.
• Strings are encoded in single quotes or double quotes or triple
single/double quotes(are used to cover the strings spans multiple
lines).
• We can also embed another string into a string using triple single or
double quotes.

14-11-2022 Dr Aravapalli Rama Satish 41


String data type
• The slice operator ([]) is used to retrieve pieces of the string.
• The indexing of string started from 0 –means that characters
in a string are counted from 0 onwards.
• Ex: name[0] indicates 0th character of string “name”
• * - repetition operator to repeat the string for several times.

14-11-2022 Dr Aravapalli Rama Satish 42


List Data type
• Group of elements – heterogeneous – size can grow
dynamically – creation can be done by [] or list() and
elements separated by ‘,’.

14-11-2022 Dr Aravapalli Rama Satish 43


Tuple Data type
• Similar to List – supports heterogeneous elements – created
by () or set() and elements separated by ‘,’.
• Not able to modify the elements one we created where as list
elements can modify – Read Only List.

14-11-2022 Dr Aravapalli Rama Satish 44


Range Data type
• Represents sequence of numbers – are not modifiable – used
to create range of numbers – used to repeat a for loop for a
specific number of times

14-11-2022 Dr Aravapalli Rama Satish 45


Bytes & Bytearray Data types
bytes bytearray
• Represents group of byte • Similar to bytes with an
numbers – any positive exception that the elements
integer from 0 to 255 – can be modified.
Doesn’t allow to edit the
values once we created.

14-11-2022 Dr Aravapalli Rama Satish 46


Sets Data type
• A set is an unordered collection of elements – Order of elements is
not maintained – does not accept duplicate elements – duplicate
elements replaced by only one copy – 2 types - set, frozenset.
• Set:
• Creation - enclose the elements in { } or set( ) separated by ‘,’.
Slicing and
indexing are
not allowed.

14-11-2022 Dr Aravapalli Rama Satish 47


Sets Data type

• frozenset:
• Similar to set but once it is created it can’t be modified.
• Can be create by using frozenset().

14-11-2022 Dr Aravapalli Rama Satish 48


Mapping Data type
• A Map represents group of elements in the form of Key-Value
pairs so that when the key is given we can retrieve the value
associated with it.
• “dict” data type is an example data type of Map.
• “dict” represents a Dictionary – Pair of elements: first one is key and
second is a value.
• Created by { } or dict( ), Key-Value pairs separated by ‘,’,Key and
Value is separated by ‘:’.
• To retrieve all keys and values, we can use keys() and values()
methods respectively.
• We can update and delete a value of a key by indexing with key.

14-11-2022 Dr Aravapalli Rama Satish 49


Dictionary Data type

14-11-2022 Dr Aravapalli Rama Satish 50


NoneType Data type

• The None data type represents an object that does not contain
any value
• In a python program, maximum of only one ‘None’ object is provided.
• One of the uses of ‘None’ is that it is used inside a function as a
default value of the arguments.
• When calling a function, if no value is passed, then the default value will be
taken as ‘None’. If some value is passed to the function, then that value is used
by the function.
• In Boolean expression,‘None’ data type represents False.

14-11-2022 Dr Aravapalli Rama Satish 51


NoneType Data type

14-11-2022 Dr Aravapalli Rama Satish 52


NoneType Data type

14-11-2022 Dr Aravapalli Rama Satish 53


Literals
• A Literal is a constant value that is stored into a variable in a
program.
• Ex: a = 15, Here 15 is a numeric or integer literal.
• Numeric Literals
• Integer: 450, -15
• Float: 3.14286, -10.6,1.25E4
• Hexadecimal: 0x5a1c
• Boolean Literals: True, False
• String Literals:“Rama”,“Krishna”
• Escape Sequences:
• \ → new line continuation
• \\ → to print single ‘\’ - \t → Horizontal Tab - \v → Vertical Tab
• \’ → to print single quote - \” → to print double quote - \n →new line
• No char data type in python – No concept of constants in python
14-11-2022 Dr Aravapalli Rama Satish 54
Naming Conventions in Python
• The rules related to writing names of packages, modules, classes,
variables, etc., are called “Naming Conventions”.
• Packages:
• Lower case letters – when multiple words are used to name then they are
separated by underscore(_).
• Applicable to modules, global variables, module level variables, instance
variables, functions, methods.
• Classes:
• Should start with a capital letter but system defined classes all have lower
case letters.
• When a class represents exception, then its name should end with a word
“Error”
• Non public instance variable name should begin with an underscore(_).
14-11-2022 Dr Aravapalli Rama Satish 55
Naming Conventions in Python
• Method Arguments:
• In case of instance methods, their first argument name should be
‘self’.
• In case of class methods, their first argument name should be ‘cls’.
• Constants:
• Written in Capital Letters and multiple words separated by
underscore(_)
• Non accessible entities:
• Few variables, functions, and methods are not accessible outside and
they should be used as they are in the program.
• Such entity names headed and trailed by double underscores(__)
• Ex: __init__(self)  To initialize variables of a class.

14-11-2022 Dr Aravapalli Rama Satish 56


Operators
• An operator is a symbol that • Classification of Operators
performs an operation. It acts depending on the nature
on some variables called • Arithmetic
operands. • Relational
• Single variable – Unary Operator • Logical
• Two variables – Binary Operator • Bitwise
• Three variables – Ternary • Assignment
Operator • Boolean
• Membership
• Identity
• Unary Minus

14-11-2022 Dr Aravapalli Rama Satish 57


Arithmetic Operators
Operator Meaning Example
• Arithmetic operators are used to
+ Add two operands or unary plus x + y+ 2
perform mathematical operations
like addition, subtraction, -
Subtract right operand from the left
x - y- 2
multiplication, etc. or unary minus
* Multiply two operands x*y
• Order of evaluation of expression
when several arithmetic /
Divide left operand by the right one
x/y
(always results into float)
operators included:
1. Parenthesis x%y
Modulus - remainder of the division
% (remainder of
2. Exponentiation of left operand by the right
x/y)
3. Mul, div, mod, and floor division Floor division - division that results
4. Add and sub // into whole number adjusted to the x // y
5. Assignment operation left in the number line

• D = (1+2)*3**2//2+3 **
Exponent - left operand raised to the x**y (x to the
power of right power y)

14-11-2022 Dr Aravapalli Rama Satish 58


Assignment Operators & Unary Minus
• Assignment operators are used in Python Operator Example Equivalent to

to assign values to variables. = x=5 x=5


+= x += 5 x=x+5
• a = 5 is a simple assignment operator that
-= x -= 5 x=x-5
assigns the value 5 on the right to the *= x *= 5 x=x*5
variable a on the left. /= x /= 5 x=x/5
• There are various compound operators in %= x %= 5 x=x%5
Python like a += 5 that adds to the variable //= x //= 5 x = x // 5
and later assigns the same. It is equivalent **= x **= 5 x = x ** 5
to a = a + 5. &= x &= 5 x=x&5
|= x |= 5 x=x|5
• Unary minus denoted by symbol minus (-)
^= x ^= 5 x=x^5
– Used before the variable (name) to >>= x >>= 5 x = x >> 5
negate the value. <<= x <<= 5 x = x << 5
14-11-2022 Dr Aravapalli Rama Satish 59
Comparison (Relational) operators
Operator Meaning Example
Greater than - True if left operand
> x>y
• Comparison operators is greater than the right

are used to compare <


Less than - True if left operand is
less than the right
x<y
values. It returns either Equal to - True if both operands
== x == y
True or False according are equal

to the condition. !=
Not equal to - True if operands are
not equal
x != y

• Used to construct Greater than or equal to - True if


conditions in if >= left operand is greater than or
equal to the right
x >= y

statements. Less than or equal to - True if left


<= operand is less than or equal to the x <= y
right

14-11-2022 Dr Aravapalli Rama Satish 60


Logical Operators
• Useful to construct compound conditions – a combination of more than one
simple condition.
• Each of the simple condition is evaluated to True or False and then the decision
is taken to know whether the total condition is True or False.
• Note: False indicates 0 and True indicates any other number.

Operator Example Meaning (X=1 and Y=2) Result


and X and Y If X is False, it returns X, otherwise it returns Y 2
or X or Y If X is False, it returns Y, otherwise it returns X 1
not Not X If X is False, it returns True, otherwise False False

14-11-2022 Dr Aravapalli Rama Satish 61


Boolean Operators
• Acted upon ‘bool’ type literals and provide ‘bool’ type output.
Operator Example Meaning (X=True and Y=False) Result
and X and Y If both X and Y are True, then it returns True, False
otherwise False.
or X or Y If either X or Y is True, then it returns True, else True
False
not Not X If X is True, it returns False, else True False

14-11-2022 Dr Aravapalli Rama Satish 62


Bitwise operators
Example
• Bitwise operators Operator Meaning x = 10 (0000 1010 in binary) and
act on operands as if y = 4 (0000 0100 in binary)
they were strings of & Bitwise AND x & y = 0 (0000 0000)
binary digits. They
| Bitwise OR x | y = 14 (0000 1110)
operate bit by bit,
hence the name. ~ Bitwise NOT ~x = -11 (1111 0101)

• Result of these ^ Bitwise XOR x ^ y = 14 (0000 1110)


operators are
>> Bitwise right shift x >> 2 = 2 (0000 0010)
always in the form of
integers. << Bitwise left shift x << 2 = 40 (0010 1000)

14-11-2022 Dr Aravapalli Rama Satish 63


Bitwise operators
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
Signed right Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost
>> shift bits fall off

A B AND: A & B OR: A | B XOR: A ^ B NOT: ~A


T T 1 1 0 F
T F 0 1 1 F
F T 1 1 1 T
F F 0 0 0 T
14-11-2022 Dr Aravapalli Rama Satish 64
Bitwise operators
Bitwise Right Shift operation Bitwise Left Shift operation

14-11-2022 Dr Aravapalli Rama Satish 65


Membership Operators
• Useful to test for membership in a sequence such as strings,
lists, tuples, or dictionaries. – If an element is found in the
sequence or not can be asserted using these operators.
• 2 operators – in and not in
• ‘in’ returns True if an element is found in the specified sequence,
otherwise returns False.
• ‘not in’ works quite opposite to ‘in’.

14-11-2022 Dr Aravapalli Rama Satish 66


Identity Operators

• Used to compare memory locations of two objects.


• It is possible to know whether the two objects are same or not.
• Memory location of an object can be seen using the id() –
returns an integer number – identity number refers memory
location of the object.
• 2 identity operators – ‘is’ and ‘is not’

14-11-2022 Dr Aravapalli Rama Satish 67


Identity Operators
• ‘is’: compare identity numbers of two objects
– return True if they are same, otherwise
return False

• ‘is not’: opposite to ‘is’ operation.

• Note: These operators not compare the values


of the two objects, just identity numbers. To
compare values, we should use equality
operator ‘==‘.

14-11-2022 Dr Aravapalli Rama Satish 68


Precedence and Associativity of Operators

• The combination of values, variables, operators, and function


calls is termed as an expression. The Python interpreter can
evaluate a valid expression.
• To evaluate these types of expressions there is a rule of
precedence in Python. It guides the order in which these
operations are carried out.
• The operator precedence in Python is listed in the next slide.
It is in descending order (upper group has higher
precedence than the lower ones).

14-11-2022 Dr Aravapalli Rama Satish 69


Precedence and Associativity of Operators
Operators Meaning
• We can see in the above table that more
than one operator exists in the same group. () Parentheses
These operators have the same ** Exponent
precedence. +x, -x, ~x Unary plus, Unary minus, Bitwise NOT
• When two operators have the same *, /, //, % Multiplication, Division, Floor division, Modulus
precedence, associativity helps to
+, - Addition, Subtraction
determine the order of operations.
<<, >> Bitwise shift operators
• Associativity is the order in which an
expression is evaluated that has multiple & Bitwise AND
operators of the same precedence. Almost ^ Bitwise XOR
all the operators have left-to-right | Bitwise OR
associativity. ==, !=, >, >=, <, <=,
Comparisons, Identity, Membership operators
• Exponent operator ** has right-to-left is, is not, in, not in
associativity in Python not Logical NOT
and Logical AND
or Logical OR

14-11-2022 Dr Aravapalli Rama Satish 70


Precedence and Associativity of Operators

• Non associative operators


• Some operators like assignment operators and comparison
operators do not have associativity in Python. There are separate
rules for sequences of this kind of operator and cannot be expressed
as associativity.
• For example, x < y < z neither means (x < y) < z nor x < (y < z). x < y
< z is equivalent to x < y and y < z, and is evaluated from left-to-right.
• Furthermore, while chaining of assignments like x = y = z = 1 is
perfectly valid, x = y = z+= 2 will result in error.

14-11-2022 Dr Aravapalli Rama Satish 71


Mathematical Functions
• To calculate square root value of a number, we need not
develop any logic. We can simply use the sqrt() that is already
given in the “math” module.
• A module is a file that contains a group of useful objects like
functions, classes or variables.
• ‘math’ is a module that contains several functions to perform
mathematical operations.
• If we want to use any module, first we should import the
module by writing “import” statement.
import name_of_the_module
Ex: import math
14-11-2022 Dr Aravapalli Rama Satish 72
Mathematical Functions
• Now we can use any function of math module by prefixing module
name before the function like math.sqrt()
• We can also use import statement as
import math as m
• Then we use ‘m’ instead of ‘math’ as prefix. ‘m’ is a name given to math
module in our program.
• This import statement imports all modules, functions, etc., of specific
module into the current program.
• On the other hand if the programmer interested in only one or two
functions of math module we can write as:
from math import sqrt
from math import sqrt, factorial
x= sqrt(16)

14-11-2022 Dr Aravapalli Rama Satish 73


Mathematical Functions
List of Functions in Python Math Module log2(x) Returns the base-2 logarithm of x

Function Description log10(x) Returns the base-10 logarithm of x

ceil(x) Returns the smallest integer greater than or equal to x. pow(x, y) Returns x raised to the power y

copysign(x, y) Returns x with the sign of y sqrt(x) Returns the square root of x

fabs(x) Returns the absolute value of x acos(x) Returns the arc cosine of x

factorial(x) Returns the factorial of x asin(x) Returns the arc sine of x

floor(x) Returns the largest integer less than or equal to x atan(x) Returns the arc tangent of x

fmod(x, y) Returns the remainder when x is divided by y atan2(y, x) Returns atan(y / x)

frexp(x) Returns the mantissa and exponent of x as the pair (m, e) cos(x) Returns the cosine of x

fsum(iterable) Returns an accurate floating point sum of values in the iterable hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y)

isfinite(x) Returns True if x is neither an infinity nor a NaN (Not a Number) sin(x) Returns the sine of x

isinf(x) Returns True if x is a positive or negative infinity tan(x) Returns the tangent of x

isnan(x) Returns True if x is a NaN degrees(x) Converts angle x from radians to degrees

ldexp(x, i) Returns x * (2**i) radians(x) Converts angle x from degrees to radians

modf(x) Returns the fractional and integer parts of x acosh(x) Returns the inverse hyperbolic cosine of x

trunc(x) Returns the truncated integer value of x asinh(x) Returns the inverse hyperbolic sine of x

exp(x) Returns e**x atanh(x) Returns the inverse hyperbolic tangent of x

expm1(x) Returns e**x - 1 cosh(x) Returns the hyperbolic cosine of x

log(x[, b]) Returns the logarithm of x to the base b (defaults to e) sinh(x) Returns the hyperbolic cosine of x

log1p(x) Returns the natural logarithm of 1+x tanh(x) Returns the hyperbolic tangent of x
pi Mathematical constant, the ratio of circumference of a circle to it's diameter (3.14159...)
https://docs.python.org/3/library/math.html
e mathematical constant e (2.71828...)
14-11-2022 Dr Aravapalli Rama Satish 74
Math module

14-11-2022 Dr Aravapalli Rama Satish 75


Control Structures

• Normal Execution – The statements in the program are


executed one by one in the sequence.
• Sometimes the programmer should be able to change the
flow of execution as needed by him.
• For example to repeat a statement several times or to jump from one
statement to another statement.
• Control statements(structures) are the statements which
control or change the flow of execution.

14-11-2022 Dr Aravapalli Rama Satish 76


Control Statements
• The following are the control statements available in python:
• if
• if else
• if elif else
• while loop
• for loop
• else suite
• break statement
• continue statement
• pass statement
• assert statement
• return statement
14-11-2022 Dr Aravapalli Rama Satish 77
if and its variants

14-11-2022 Dr Aravapalli Rama Satish 78


Examples for if

14-11-2022 Dr Aravapalli Rama Satish 79


Examples for if

14-11-2022 Dr Aravapalli Rama Satish 80


Loops and Regular
Expressions
Module - 3

14-11-2022 Dr Aravapalli Rama Satish 81


While Loop
• A Loop is used to
execute a statement(s)
repeatedly.
• While loop is useful to
execute a group of
statements several times
repeatedly depending
on whether a condition
is True or False.

14-11-2022 Dr Aravapalli Rama Satish 82


While Loop

14-11-2022 Dr Aravapalli Rama Satish 83


For Loop
• The for loop in Python is used to iterate over a sequence (list,
tuple, string) or other iterable objects. Iterating over a
sequence is called traversal.
for val in sequence:
loop body
• Here, val is the variable that takes the value of the item inside
the sequence on each iteration.
• Loop continues until we reach the last item in the sequence.
• The body of for loop is separated from the rest of the code
using indentation.
14-11-2022 Dr Aravapalli Rama Satish 84
For Loop

14-11-2022 Dr Aravapalli Rama Satish 85


Infinite Loops
• If we forget to write loop termination statement in the while loop,
the loop always iterates with initial value of loop variable.
• So loop iteratively continued with out stopping or terminating –
Infinite Loop.
• Another way to create an infinite loop is to write ‘True’ in the
condition part of the while loop so that python interpreter treats
that the condition is always True and hence it executes forever.

14-11-2022 Dr Aravapalli Rama Satish 86


Nested Loops
• Writing a loop inside another loop is called Nested Loops.
• for inside for, for inside while, while inside while, while inside
for – all combinations are valid.

14-11-2022 Dr Aravapalli Rama Satish 87


The else Suite

• A for/while loop can have an optional else block as well.


• The else part is executed if the items in the sequence used in
for loop exhausts or Condition in the while loop exhausts.
• The break keyword can be used to stop a for/while loop. In
such cases, the else part is ignored.
• Hence, a for/while loop's else part runs if no break occurs.
• The else suite always executed irrespective of the statements
in the loop are executed or not. But this is exceptional in the
case of loop termination with break.
14-11-2022 Dr Aravapalli Rama Satish 88
The else Suite

14-11-2022 Dr Aravapalli Rama Satish 89


break and continue statements

• break:
• It is used inside a for loop or while loop to come out of them.
• When break is executed, the python interpreter jumps out of the loop
to process the next statement in the program.
• continue:
• It is used in a loop to go back to the beginning of the loop.
• When continue is executed, the next repetition will start and all the
subsequent statements in the current iteration will be skipped from
the execution.

14-11-2022 Dr Aravapalli Rama Satish 90


break and continue statements

14-11-2022 Dr Aravapalli Rama Satish 91


break and continue statements

14-11-2022 Dr Aravapalli Rama Satish 92


pass Statement
• The pass statement is a null statement.
• The difference between a comment and a pass statement in Python
is that while the interpreter ignores a comment entirely, pass is not
ignored.
• However, nothing happens when the pass is executed. It results in
no operation (NOP).
pass
• We generally use it as a placeholder.
• Suppose we have a loop or a function that is not implemented yet, but we
want to implement it in the future.
• They cannot have an empty body. The interpreter would give an error.
• So, we use the pass statement to construct a body that does nothing.

14-11-2022 Dr Aravapalli Rama Satish 93


pass Statement

14-11-2022 Dr Aravapalli Rama Satish 94


assert Statement
• It is useful to check if a particular condition is fulfilled or not.
assert expression[, message]
Ex: assert x > 10, “Wrong input entered”
• The python interpreter checks if x>10 is True or not.
• If it is True, then the next statements will execute, else it will display
AssertionError along with the message “Wrong input entered”
• The AssertionError generated in execution is called
Exception – It is a kind of error occurs during run time.
• To avoid this kind of error we need to handle with try…except
statement.
14-11-2022 Dr Aravapalli Rama Satish 95
assert Statement

14-11-2022 Dr Aravapalli Rama Satish 96


return Statement
• It is used inside a function to return some value to the caller
return expression_list
Ex: return (a+b)
• When a function does not return anything, no need to write return
statement in the function definition.

2
4

14-11-2022 Dr Aravapalli Rama Satish 97


Regular Expressions
• A Regular Expression (RegEx) is a
sequence of characters that Expression String Matched?
defines a search pattern. abs No match
• For example, alias Match
^a...s$
^a...s$ abyss Match
• The above code defines a RegEx
pattern. The pattern is: any five letter Alias No match
string starting with a and ending with
An abacus No match
s.
• A pattern defined using RegEx can
be used to match against a string.

14-11-2022 Dr Aravapalli Rama Satish 98


Regular Expressions
• Python has a module named
“re” to work with RegEx. Here's
an example
• Here, we used re.match()
function to search pattern
within the test_string. The
method returns a match object
if the search is successful. If not,
it returns None.
• There are other several
functions defined in the re
module to work with RegEx
14-11-2022 Dr Aravapalli Rama Satish 99
Regular Expressions – Specifying Patterns

• To specify regular expressions, metacharacters are used. In


the above example, ^ and $ are metacharacters.
• MetaCharacters
• Metacharacters are characters that are interpreted in a special way
by a RegEx engine. Here's a list of metacharacters:
• [] . ^ $ * + ? {} () \ |

14-11-2022 Dr Aravapalli Rama Satish 100


Regular Expressions – Specifying Patterns
• [] - Square brackets
• Square brackets specifies a set of characters you
wish to match.
• Here, [abc] will match if the string you are Expression String Matched?
trying to match contains any of the a, b or c. a 1 match
• You can also specify a range of characters using -
inside square brackets. ac 2 matches
• [a-e] is the same as [abcde]. [abc]
Hey Jude No match
• [1-4] is the same as [1234].
• [0-39] is the same as [01239]. abc de ca 5 matches
• You can complement (invert) the character
set by using caret ^ symbol at the start of a
square-bracket.
• [^abc] means any character except a or b or c.
• [^0-9] means any non-digit character.

14-11-2022 Dr Aravapalli Rama Satish 101


Regular Expressions – Specifying Patterns
• . - Period Expression String

a
Matched?

No match
• A period matches any single ac 1 match
character (except newline ..
'\n’). acd 1 match

acde 2 matches (contains 4 characters)


• ^ - Caret a 1 match
• The caret symbol ^ is used to ^a abc 1 match
check if a string starts with a bac No match
certain character.
abc 1 match
• $ - Dollar ^ab
acb No match (starts with a but not followed by b)

• The dollar symbol $ is used to a 1 match


check if a string ends with a a$ formula 1 match
certain character. cab No match

14-11-2022 Dr Aravapalli Rama Satish 102


Regular Expressions – Specifying Patterns
• * - Star Expression String Matched?

• The star symbol * matches zero mn 1 match


or more occurrences of the man 1 match
pattern left to it. ma*n maaan 1 match
• + - Plus main No match (a is not followed by n)
• The plus symbol + matches one woman 1 match
or more occurrences of the mn No match (no a character)
pattern left to it.
man 1 match
• ? - Question Mark ma+n maaan 1 match
• The question mark symbol ? main No match (a is not followed by n)
matches zero or one occurrence woman 1 match
of the pattern left to it.
14-11-2022 Dr Aravapalli Rama Satish 103
Regular Expressions – Specifying Patterns
• {} - Braces Expression String Matched?

• Consider this code: {n,m}. This mn 1 match


means at least n, and at most m man 1 match
repetitions of the pattern left to No match (more than one a
it. ma?n
maaan
character)

Expression String Matched? No match (a is not followed by


main
n)
1 match
ab123csde (match at woman 1 match
ab123csde)
abc dat No match
[0-9]{2,4}
3 matches
12 and 345673 abc daat 1 match (at daat)
(12, 3456, 73) a{2,3}
aabc daaat 2 matches (at aabc and daaat)
1 and 2 No match
aabc daaaat 2 matches (at aabc and daaaat)

14-11-2022 Dr Aravapalli Rama Satish 104


Regular Expressions – Specifying Patterns
• | - Alternation Expression String Matched?
• Vertical bar | is used for alternation (or
operator). cde No match
• () - Group 1 match (match at
• Parentheses () is used to group sub-patterns. For ade
a|b ade)
example, (a|b|c)xz match any string that
matches either a or b or c followed by xz 3 matches (at
acdbea
• \ - Backslash acdbea)
• Backlash \ is used to escape various characters ab xz No match
including all metacharacters. For example,
• \$a match if a string contains $ followed by a. 1 match (match at
Here, $ is not interpreted by a RegEx engine in a abxz
special way.
(a|b|c)xz abxz)
• If you are unsure if a character has special 2 matches (at axzbc
meaning or not, you can put \ in front of it. This axz cabxz
makes sure the character is not treated in a cabxz)
special way.
14-11-2022 Dr Aravapalli Rama Satish 105
Regular Expressions – Special Sequences
• Special sequences make commonly used patterns easier to write. Here's a
list of special sequences:
• \A - Matches if the specified characters are at the start of a string.
• \b - Matches if the specified characters are at the beginning or end of a word.
• \B - Opposite of \b. Matches if the specified characters are not at the beginning or
end of a word.
• \d - Matches any decimal digit. Equivalent to [0-9]
• \D - Matches any non-decimal digit. Equivalent to [^0-9]
• \s - Matches where a string contains any whitespace character. Equivalent to [
\t\n\r\f\v]
• \S - Matches where a string contains any non-whitespace character. Equivalent to [^
\t\n\r\f\v]
• \w - Matches any alphanumeric character (digits and alphabets). Equivalent to [a-
zA-Z0-9_]. By the way, underscore _ is also considered an alphanumeric character.
• \W - Matches any non-alphanumeric character. Equivalent to [^a-zA-Z0-9_]
• \Z - Matches if the specified characters are at the end of a string.
14-11-2022 Dr Aravapalli Rama Satish 106
Regular Expressions – Special Sequences
Expression String Matched? Expression String Matched?

the sun Match the foo No match


\Athe
In the sun No match foo\B the afoo test No match

football Match the afootest Match

\bfoo a football Match 12abc3 3 matches (at 12abc3)


\d
afootball No match Python No match

the foo Match 1ab34"50 3 matches (at 1ab34"50)


\D
foo\b the afoo test Match 1345 No match

the afootest No match Python RegEx 1 match


\s
football No match PythonRegEx No match

\Bfoo a football No match ab 2 matches (at a b)


\S
afootball Match No match

14-11-2022 Dr Aravapalli Rama Satish 107


Regular Expressions – Special Sequences
Expression String Matched?
12&": ;c 3 matches (at 12&": ;c)
\w
%"> ! No match
1a2%c 1 match (at 1a2%c)
\W
Python No match
I like Python 1 match
Python\Z I like Python Programming No match
Python is fun. No match

To build and test regular expressions, you can use RegEx tester tools such as regex101. This tool not
only helps you in creating regular expressions, but it also helps you learn it.

14-11-2022 Dr Aravapalli Rama Satish 108


‘re’ module
• Python has a module named re to
work with regular expressions. To
use it, we need to import the
module.
import re
• The module defines several functions
and constants to work with RegEx.
• re.findall():
• The re.findall() method returns a list of
strings containing all matches.
• If the pattern is not found, re.findall()
returns an empty list.
14-11-2022 Dr Aravapalli Rama Satish 109
‘re’ module
• re.split()
• The re.split() splits the string
where there is a match and
returns a list of strings where the
splits have occurred.
• If the pattern is not found,
re.split() returns a list containing
the original string.
• You can pass maxsplit argument
to the re.split() method. It's the
maximum number of splits that
will occur.
• By the way, the default value of
maxsplit is 0; meaning all
possible splits.
14-11-2022 Dr Aravapalli Rama Satish 110
‘re’ module
• re.sub()
• re.sub(pattern, replace, string)
• The method returns a string
where matched occurrences are
replaced with the content of
replace variable.
• If the pattern is not found,
re.sub() returns the original
string.
• You can pass count as a fourth
parameter to the re.sub()
method. If omited, it results to 0.
This will replace all occurrences.

14-11-2022 Dr Aravapalli Rama Satish 111


‘re’ module
• re.subn()
• The re.subn() is similar to
re.sub() except it returns a
tuple of 2 items containing the
new string and the number of
substitutions made.

14-11-2022 Dr Aravapalli Rama Satish 112


‘re’ module
• re.search()
• The re.search() method takes
two arguments: a pattern and a
string. The method looks for
the first location where the
RegEx pattern produces a
match with the string.
• If the search is successful,
re.search() returns a match
object; if not, it returns None.
• match = re.search(pattern, str)
• Here, match contains a match
object.

14-11-2022 Dr Aravapalli Rama Satish 113


‘re’ module
• Match object
• You can get methods and attributes
of a match object using dir()
function.
• Some of the commonly used
methods and attributes of match
objects are:
• match.group()
• The group() method returns the part
of the string where there is a match.
• Here, match variable contains a
match object.
• Our pattern (\d{3}) (\d{2}) has two
subgroups (\d{3}) and (\d{2}).
• You can get the part of the string of
these parenthesized subgroups.
Here's how:

14-11-2022 Dr Aravapalli Rama Satish 114


‘re’ module
• match.start(), match.end() and
match.span()
• The start() function returns the
index of the start of the matched
substring. Similarly, end() returns
the end index of the matched
substring.
• The span() function returns a
tuple containing start and end
index of the matched part.
• The re attribute of a matched
object returns a regular
expression object. Similarly,
string attribute returns the
passed string.
14-11-2022 Dr Aravapalli Rama Satish 115
‘re’ module
• Using r prefix before RegEx
• When r or R prefix is used
before a regular expression, it
means raw string. For
example, '\n' is a new line
whereas r'\n' means two
characters: a backslash \
followed by n.
• Backlash \ is used to escape
various characters including
all metacharacters. However,
using r prefix makes \ treat as
a normal character.

14-11-2022 Dr Aravapalli Rama Satish 116


Basic Data Structures
Module 4
Revisiting Data Types
print( ) – old formatting method
• Often you’ll want more control over the formatting of your output than simply
printing space-separated values.
• Python uses C-style string formatting to create new, formatted strings.
• The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed
size list), together with a format string, which contains normal text together with
"argument specifiers", special symbols like "%s" and "%d".
• Let's say you have a variable called "name" with your user name in it, and you would then like
to print(out a greeting to that user.)
# This prints out “Hello, John!”
name = “John”
print("Hello, %s!" % name)
• General Syntax:
print(“formatted string” % (variable list))

04-01-2023 Dr Aravapalli Rama Satish 2


print( )
The general syntax for a format placeholder is:

%[flags][width][.precision]type

https://docs.python.org/2/library/stdtypes.html#string-
formatting
04-01-2023 Dr Aravapalli Rama Satish 3
print( )
• Any object which is not a string can be formatted using the %s operator
as well.
• The string which returns from the "repr" method of that object is
formatted as the string.
• For example:

04-01-2023 Dr Aravapalli Rama Satish 4


print( )
• Here are some basic argument specifiers you should know:
%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
• Exercise
• You will need to write a format string which prints out the data using the following syntax: Hello John
Doe. Your current balance is $53.44.

04-01-2023 Dr Aravapalli Rama Satish 5


print( ) – new formatting method
• Using formatted string literals
• Begin a string with f or F before the opening quotation mark or triple quotation mark.
• Inside this string, you can write a Python expression between { and } characters that can refer to
variables or literal values.
• Using str.format( ).
• It requires more manual effort.
• You’ll still use { and } to mark where a variable will be substituted and can provide detailed
formatting directives, but you’ll also need to provide the information to be formatted.

04-01-2023 Dr Aravapalli Rama Satish 6


print( ) – new formatting method
• When you don’t need fancy output but just want a quick display of some
variables for debugging purposes, you can convert any value to a string with
the repr() or str() functions.
• The str() function is meant to return representations of values which are fairly
human-readable, while repr() is meant to generate representations which can
be read by the interpreter (or will force a SyntaxError if there is no equivalent
syntax).
• For objects which don’t have a particular representation for human consumption, str()
will return the same value as repr().
• Many values, such as numbers or structures like lists and dictionaries, have the same
representation using either function. Strings, in particular, have two distinct
representations.

04-01-2023 Dr Aravapalli Rama Satish 7


print( ) – new formatting method

04-01-2023 Dr Aravapalli Rama Satish 8


print( ) – Formatted String Literals
• Formatted string literals (also called f-strings for short) let you include the
value of Python expressions inside a string by prefixing the string with f
or F and writing expressions as {expression}.
• An optional format specifier can follow the expression.
• This allows greater control over how the value is formatted.
• The following example rounds pi to three places after the decimal.
• Passing an integer after the ':' will cause that field to be a minimum number of
characters wide. This is useful for making columns line up.

04-01-2023 Dr Aravapalli Rama Satish 9


print( ) – Formatted String Literals
• Other modifiers can be used to convert the value before it is formatted.
'!a' applies ascii( ), '!s' applies str( ), and '!r' applies repr( ).
• The = specifier can be used to expand an expression to the text of the
expression, an equal sign, then the representation of the evaluated
expression.

04-01-2023 Dr Aravapalli Rama Satish 10


print( ) – String format() Method
• Basic usage of the str.format() method looks like this:
• print('We are the {} who say "{}!"'.format('knights', 'Ni’)) and the output is
We are the knights who say “Ni!”
• The brackets and characters within them (called format fields) are
replaced with the objects passed into the str.format() method.
• A number in the brackets can be used to refer to the position of the
object passed into the str.format() method.

04-01-2023 Dr Aravapalli Rama Satish 11


print( ) – String format() Method
• If keyword arguments are used in the str.format() method, their values
are referred to by using the name of the argument.
• Positional and keyword arguments can be arbitrarily combined.

04-01-2023 Dr Aravapalli Rama Satish 12


print( ) – String format() Method
• If you have a really long format string that you don’t want to split up, it would
be nice if you could reference the variables to be formatted by name instead
of by position.
• This can be done by simply passing the dict and using square brackets '[]' to
access the keys. (or) also can be done by passing the table dictionary as
keyword arguments with the ** notation.

04-01-2023 Dr Aravapalli Rama Satish 13


print( ) – String format() Method
As an example, the following lines produce a tidily aligned set of
columns giving integers and their squares and cubes:

https://docs.python.org/3/library/stdtypes.html#str.
format
https://docs.python.org/3/library/string.html#form
atstrings

04-01-2023 Dr Aravapalli Rama Satish 14


print( ) – Formatting Types
Format Meaning
:< Left aligns the result (within the available space)
:> Right aligns the result (within the available space)
:^ Center aligns the result (within the available space)
txt = "We have {:<8} chickens." := Places the sign to the left most position
print(txt.format(49)) :+ Use a plus sign to indicate if the result is positive or negative
Num=5000
:- Use a minus sign for negative values only
print(‘{:*>15d}’.format(num))
: Use a space to insert an extra space before positive numbers
print(‘{:.>15x} \n {:.<15b}’.format(num))
(and a minus sign before negative numbers)
print(‘{:.>#15x} \n
{:.<#15b}’.format(num)) :, Use a comma as a thousand separator
:_ Use a underscore as a thousand separator
:b Binary format
:c Converts the value into the corresponding unicode character
:d Decimal format
• Inside the placeholders you can add a formatting type to format the result
04-01-2023 Dr Aravapalli Rama Satish 15
print( ) – Formatting Types
Format Meaning
:e Scientific format, with a lower case e
:E Scientific format, with an upper case E
:f Fix point number format
:F Fix point number format, in uppercase format
(show inf and nan as INF and NAN)
:g General format
:G General format (using a upper case E for scientific notations)
:o Octal format
:x Hex format, lower case
:X Hex format, upper case
:n Number format
:% Percentage format

04-01-2023 Dr Aravapalli Rama Satish 16


Strings
• To nullify the effect of escape sequences, we can write the string as a
“raw string” by adding ‘r’ before the string as:
Ex: s1 = r“Welcome to the \t Core Python \n learning”
print(s1)
• To create a string with Unicode characters, we should add ‘u’ at the
beginning of the string.
• Unicode is the standard to include the alphabet of various human languages into
the programming languages like python or Java.
• For example, it is possible to display the alphabets of Hindi, French, German
Languages.
• Each Unicode character contains 4 digits, preceded by ‘\u’.
Ex: name = u‘\u0915\u094b\u0930\u0920\u0948\u0925\u0964\u0928’
print(name)
04-01-2023 Dr Aravapalli Rama Satish 17
Strings

04-01-2023 Dr Aravapalli Rama Satish 18


Strings
• Indexing:
• Accessing each element of a string in forward and reverse order using the while loop.
• We can also use the for loop to access each element(char) of a string.

04-01-2023 Dr Aravapalli Rama Satish 19


Strings

04-01-2023 Dr Aravapalli Rama Satish 20


Strings
• To check a string is a member of another string or not using ‘in’ or ‘not
in’ operator. – Case Sensitive comparison.

04-01-2023 Dr Aravapalli Rama Satish 21


Strings
• Comparing two strings: We can
use relational operators to
compare two strings and returns
Boolean value either True/False
depending on the strings being
compared – Follows English
Dictionary Order.

04-01-2023 Dr Aravapalli Rama Satish 22


Strings
• Removing spaces from a string:
• A Space is also considered as a
character in a string. Some times
unnecessary spaces in a string will
lead to wrong results.
• To remove such spaces we can use:
rstrip() – removes the spaces right
end of the string.
lstrip() – removes the spaces left end
of the string.
strip() – removes the spaces at both
ends of the string.

04-01-2023 Dr Aravapalli Rama Satish 23


Strings
• Finding Substrings:
• find( ), rfind( ), index( ), rindex( )
• These are useful to locate substrings
in a string and return the location
of the first occurrence of the
substring in the main string.
• find( ) returns -1 if the substring is
not found in the main string.
• index( ) returns ‘ValueError’
exception.
• Syntax: main_str.find(sub_str, begin,
end) – remaining methods follows
the same syntax.

04-01-2023 Dr Aravapalli Rama Satish 24


Strings
• find() and index() returns only
first occurance.
• When substring occurs several
times, they can not return all those
occurrences.
• To know all occurrences we need
to develop additional logic.

04-01-2023 Dr Aravapalli Rama Satish 25


Strings
• Counting substrings in a string:
• count( ) – To count the number of
occurrences of a sub string in a
main string.
• It returns an integer to represent
how many times the substring is
found.
• Syntax:
• str_var.count(sub_str)
• Str_var.count(sub_str, begin, end)

04-01-2023 Dr Aravapalli Rama Satish 26


Strings
• Replacing a string with another
string
• replace( ) – To replace a substring
in a string with another substring.
• Syntax: str_name.replace(old, new)
s1 = “This is cool python”
s2 = “cool”
s3 – “hard”
s4 = s1.replace(s2, s3)

04-01-2023 Dr Aravapalli Rama Satish 27


Strings
• Splitting Strings
• split( ) – To break a string into
pieces based on a character and
returns as a list.

04-01-2023 Dr Aravapalli Rama Satish 28


Strings
• Joining of strings
• When a group of strings are given,
it is possible to join them all and
make into a single string. – join( )
• Syntax:
separator.join(list of strings / tuple of
strings)

04-01-2023 Dr Aravapalli Rama Satish 29


Strings
• Changing case of a string:
• upper(), lower(), swapcase(), title()
• Checking starting & ending of a
string:
• startswith(), endswith()
• To test the nature of characters:
• isalpha(), isalnum(), isdigit(),
islower(), isupper(), istitle(), isspace()

04-01-2023 Dr Aravapalli Rama Satish 30


Strings
• Sorting strings
• sort( ) or sorted( ) – To sort group
of strings in alphabetical order
• Syntax:
list_of_str.sort() – Sorts the original
array in alphabetical order and
insertion/original order will be lost.
list_of_sorted_str = sorted(list_of_str)
– It retains the original array as it is and
returns the sorted list as output.

04-01-2023 Dr Aravapalli Rama Satish 31


Strings

• Searching a String:
• To find out whether a
given key string is
available in the list of
strings or not and
provide the location of
the string in the list.

04-01-2023 Dr Aravapalli Rama Satish 32


Lists
• Access values
• Lists can be indexed, sliced, and concatenated.
• To access values, square brackets are used to slice along with the index or indices to get
value stored at that index.
• Syntax for slicing: list_name[start:stop:step]
• Ex: seq = L[::2] – Get every other element, starting at index 0.
• Updating values
• One or more elements of a list can be easily updated by giving the slice on the left hand
side of the assignment operator.
• Append new values(append()) and removing the values(del).
• append(), insert() are the list methods. They can not be called on other values such as
strings or integers.
• Ex: num_list[5] = 100 |num_list.append(200) |del num_list[3]

04-01-2023 Dr Aravapalli Rama Satish 33


Lists
• del num_list[2:4] → deletes numbers at index 2 and 3.
• del num_list[:] → deletes all the numbers from the list.
• del num_list →deletes the entire variable num_list
• To insert items from another list or sequence at a particular location, you
can use slice operation. – It results in the creation of a list with in
another list.
• Ex: num_list = [1,9,11,13,15]
• num_list[2] = [3,5,7]
• print(num_list) → [1,9,[3,5,7],13,15]

04-01-2023 Dr Aravapalli Rama Satish 34


Lists
• Nested Lists – A list with in another list. You can specify an element in the
Nested List by using a set of indices.
• To print the second element of the nested list, we will write print(lst[3][1])
specifies starting location of the nested list and the index of the element with
in the Nested List.

04-01-2023 Dr Aravapalli Rama Satish 35


Lists
• Cloning Lists
• If we want to modify a list and also keep a copy of the original list, then you
should create a separate copy of the list(not just reference). This process is called
cloning.
• The slice operation is used to clone a list.

04-01-2023 Dr Aravapalli Rama Satish 36


List Operations
SNo Operation Description Example Output
1 Length (len) Returns length of the list len([1,2,3,4,5,6,7,8,9,10]) 10
2 Concatenation(+) Joins two Lists [1,2,3]+[4,5] [1,2,3,4,5]
3 Repetition(*) Repeat the elements in the list [“He”,”Him”]*2 [“He”,”Him”,”He”,
”Him”]
4 in Checks if the value is present in the list ‘a’ in [‘a’,’e’,’i’,’o’,’u’] True
5 not in Checks if the value is not present in the list 3 not in [0,2,4,6,8] True
6 max Returns maximum value in the list Lst1= [6,3,0,7,1,2,4,9] 9
max(Lst1)
7 min Returns minimum value in the list min(Lst1) 0
8 sum Adds the values in the list that has numbers Lst1=list(range(1,11)) 55
sum(Lst1)

04-01-2023 Dr Aravapalli Rama Satish 37


List Operations
SNo Operation Description Example Output
9 all( ) Returns True if all the elements of the list n_l = [0,1,2,3] False
are True or if the list is empty all(n_l)
10 any( ) Returns True if any element of the list is any(n_l) True
True. If the list is empty returns False
11 list( ) Converts an iterable (tuple, string, set, lst1 = list(“Hello”) [‘H’,’e’,’l’,’l’,’o’]
dictionary) to a list print(lst1)
12 sorted( ) Returns a new sorted list. The original list is lst1 = [3,4,1,2,7,8] [1,2,3,4,7,8]
not modified. lst2 = sorted(lst1)
print(lst2)
13 append( ) Appends element to the list. n_l = [0,1,2,3] [0,1,2,3,4]
n_l.append(4)
print(n_l)

04-01-2023 Dr Aravapalli Rama Satish 38


List Operations
SNo Operation Description Example Output
14 count( ) Counts the number of times a element appears in n_l.count(4) 1
the list.
15 index( ) Returns the lowest index of the object in the list. n_l = [6,3,7,0,3,7,6,0] 2
Gives a ValueError if object is not present in the n_l.index(7)
list.
16 insert( ) Inserts an object at specified index in the list n_l.insert(3,100) [6,3,7,100,0,3,7,6,0]
print(n_l)
17 pop( ) Removes the element at the specified index from print(n_l.pop(3)) 100
the list. Index is an optional parameter. If it is not print(n_l) [6,3,7,0,3,7,6,0]
specified, last element is removed.
18 remove( ) Deletes the object from the list. ValueError is n_l.remove(0) [6,3,7,3,7,6,0]
generated if the object is not present in the list. If print(n_l)
multiple copies of the object is presented, then
first occurance is removed.
04-01-2023 Dr Aravapalli Rama Satish 39
List Operations
SNo Operation Description Example Output
19 reverse( ) Reverse the elements of the list. n_l.reverse() [0,6,7,3,7,3,6]
print(n_l)
20 sort( ) Sort the elements of the list. Original list is n_l.sort() [0,3,3,6,6,7,7]
modified.
21 extend( ) Adds the elements in a list to the end of another n_l1 = [1,2,3,4] [1,2,3,4,5,6,7,8]
list. Using + or += on a list is similar to using n_l2 = [5,6,7,8]
extend( ). n_l1.extend(n_l2)
print(n_l1)

04-01-2023 Dr Aravapalli Rama Satish 40


Comments on List operations
• insert(), remove(), and sort() modify the list and do not return any value.
If you try to print the return values of these methods, they return None.
• When one list is assigned to another list using the assignment
operator(=), then a new copy of the list is not made.
• Instead assignment makes the two variables point to the one list in the memory –
aliasing.
• It is safer to avoid aliasing when you are working with mutable objects. Since lists
are mutable objects, changes made with one alias affect the other.
• sort() uses ASCII values to sort the values in the list.
• Items in a list can also be deleted by assigning an empty list to a slice of
elements. Ex: lst1[2:5]=[]

04-01-2023 Dr Aravapalli Rama Satish 41


List Comprehensions

• List comprehension offers a shorter syntax when you want to create a new list
based on the values of an existing list.
• Python also supports computed lists called ‘List Comprehensions’ having the
following syntax:
newlist = [expression for item in iterable if condition == True]
Where expression is evaluated once for every item in the sequence.

04-01-2023 Dr Aravapalli Rama Satish 42


List Comprehensions
• It helps programmers to create lists in concise way.
• It is mainly beneficial to make new lists where each element is obtained
by applying some operations to each member of another sequence or
iterable.
• It is also used to create a sub sequence of those elements that satisfy a
certain condition.

04-01-2023 Dr Aravapalli Rama Satish 43


List Comprehensions
• It is also used to combine the elements of the two lists.
• Ex: Cartesian product of two lists(considered as two element sets) when
the two values are different.

• Exercise: Write a program to find the mean and sum of elements of a


list.

04-01-2023 Dr Aravapalli Rama Satish 44


List Comprehensions

04-01-2023 Dr Aravapalli Rama Satish 45


List Comprehensions
# using lambda to print table of 10
numbers = list(map(lambda i: i*10, [i for i in range(1, 6)]))
print(numbers)

lis = ["Even number" if i % 2 == 0 else "Odd number" for i in range(8)]


print(lis) # prints even number or odd number for every number

lis = [num for num in range(100) if num % 5 == 0 if num % 10 == 0]


print(lis) #Nested If conditions in List Comprehension

# Assign matrix
twoDMatrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
# Generate transpose
trans = [[i[j] for i in twoDMatrix] for j in range(len(twoDMatrix[0]))]
print(trans)
04-01-2023 Dr Aravapalli Rama Satish 46
List Comprehensions
# Initializing string # Explicit function to calculate sum of digits
string = 'Geeks4Geeks' def digitSum(n):
# Toggle case of each character dsum = 0
List = list(map(lambda i: chr(ord(i) ^ 32), string))
# Display list for ele in str(n):
print(List) dsum += int(ele)
return dsum
# Reverse each string in tuple # Initializing list
List = [string[::-1] for string in ('Geeks', 'for', List = [367, 111, 562, 945, 6726, 873]
'Geeks')] # Using the function on odd elements of the list
# Display list newList = [digitSum(i) for i in List if i & 1]
print(List)
# Displaying new list
print(newList)
04-01-2023 Dr Aravapalli Rama Satish 47
List - Enumeration
• enumerate( ) is used.
• It is used when we want to print both index as well as an item in the list.
• It returns an enumerate object which contains the index and the value of all the
items of the list as a tuple.
• To print index, you also can use range( ).

04-01-2023 Dr Aravapalli Rama Satish 48


Functional Programming
• The following three functions formed as part of functional
programming tools that work on all list items.
• filter( ), map( ), reduce( )
• filter( ): It constructs a list from source list, the items which satisfy a
condition through a function and returns True.
• Syntax: filter(function, sequence)
• If sequence is a string, Unicode, or a tuple, then the result will be of same type;
otherwise it always is a list.
• Functions which return either True or False are called predicates.

04-01-2023 Dr Aravapalli Rama Satish 49


Functional Programming
• map( ): It applies a function to every element of a list and returns result
as a new list. – Syntax: map(function, sequence)
• You can even pass more than one sequence in the map() by remembering two
things:
• The function must have as many arguments as there are sequences.
• Each argument is called with the corresponding item from each sequence. (Or None if one
sequence is shorter than another).

04-01-2023 Dr Aravapalli Rama Satish 50


Functional Programming
• reduce( ): It returns a single value generated by calling the function on
the first two items of the sequence, then on the result and the next item,
and so on.
• Syntax: reduce(function, sequence) – Here function should work with two values.
• Should import functools module.

• If there is only one item in the sequence, then its


value returned
• If the sequence is empty, an exception is raised.
• Creating a list in a very extensive range will
generate a MemoryError or OverflowError

04-01-2023 Dr Aravapalli Rama Satish 51


Tuples
• Syntax:
• t = ()  creates an empty tuple.
• t = (5)  create an integer
• t = (5,)  creates a tuple with single element
• Any set of multiple, comma separated values
written with out an identifying symbol like
brackets([]), parentheses(()), curly braces({}), etc.,
are treated as tuple by default.
• If we want to create a tuple with single element,
then we must add comma after the element,
otherwise considered as ordinary data type.
04-01-2023 Dr Aravapalli Rama Satish 52
Tuples
• Tuples are used to store related information about a subject together, and
information belongs to different data types.
• Ex: In case of a student, a student can have elements like rollno, name, course,
total_marks, average, etc.,
• Few built-in functions return tuple as output. Ex: divmod(x,y)
• Accessing values:
• Index for a tuple starts from 0.
• Slicing, concatenation operations are allowed.
• Updation of a tuple can not be done as tuples are immutable. You can
only extract values from a tuple to form another tuple.
04-01-2023 Dr Aravapalli Rama Satish 53
Tuples
• You can not delete values from a
tuple. However we can create a new
tuple that has all elements except the
elements that you don’t need.
• You can always delete the entire tuple
by using the del statement.
• We can also perform operations on
tuples like lists but the difference is
that a new tuple should be created
when a change is required in an
existing tuple.

04-01-2023 Dr Aravapalli Rama Satish 54


Tuples - Operations
Operation Expression Output
Length len((1,2,3,4,5,6)) 6
Concatenation (1,2,3)+(4,5,6) (1,2,3,4,5,6)
Repetition (‘Good…’,)*3 ('Good...', 'Good...', 'Good...')
Membership 5 in (1,2,3,4,5,6,7,8) True
for i in (1,2,3,4,5):
Iteration 12345
print( i, end = ‘ ‘)
t1 = (1,2,3,4); t2 = (1,2,3,4)
Comparison False
print(t1 > t2)
Maximum max(1,0,3,8,2,9) 9
Minimum min(1,0,3,8,2,9) 0
tuple(‘Hello’) (‘H’,’e’,’l’,’l’,’o’)
Converting to tuple
tuple([1,2,3,4]) (1,2,3,4)
04-01-2023 Dr Aravapalli Rama Satish 55
Tuple Assignment
• It allows a tuple of variables on the
left side of the assignment operator
to be assigned values from a tuple
given on the right side of the
assignment operator. Each value is
assigned to its respective variable.
• While assigning values to a tuple,
you must ensure that number of
values on both the sides of the
assignment operator are same.
Otherwise, an error will be
generated.
04-01-2023 Dr Aravapalli Rama Satish 56
Tuples
• A function can return a value. But at times, we
need to return more than one value from a
function.
• In these cases, it is preferable to group together
multiple values and return them together.
• Unlike lists, tuples do not support remove(),
pop(), append(), sort(), reverse() and insert()
methods.
• A tuple can be defined inside another tuple.
You can also even specify a list with in a tuple.

04-01-2023 Dr Aravapalli Rama Satish 57


Tuples
• The index of an element in tuple can be obtained by using the index( ).
If the element being searched is not present in the list, then error is
generated.

04-01-2023 Dr Aravapalli Rama Satish 58


Tuples
• Counting the elements: count( )
• It returns the number of elements with a specific value in a tuple.

04-01-2023 Dr Aravapalli Rama Satish 59


Tuples
• zip( ): It takes two or more sequences and “zips” them into a list of
tuples. The tuples thus formed has one element from each sequence.
• All sequences which are given as input to zip function have equal length.
• If the sequences have different length then the result has the length of the
shorter one.

[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
[(1, 'f'), (2, 'g'), (3, 'h')]

04-01-2023 Dr Aravapalli Rama Satish 60


Tuples
• We can even print the elements of a tuple using the for statement.
• Using enumerate( ) we can traverse the elements of a sequence to print
the indices along with values.
1a
2b
3c
4d
5e
0A
1B
2C
3D
4E
5F
04-01-2023 Dr Aravapalli Rama Satish 6G 61
Tuples
• Tuples can be converted to a list and vice-versa.
• You can not divide or subtract tuples. If we try to do we will get a
“TypeError” with unsupported operand type.
• sort( ), reverse( ) methods are not supported since tuples are immutable.
However sorted( ) takes any sequence and returns a new list with the
same elements in different order.
• Iterating through tuples are faster than iterating over list.
• Tuples can act as a key for dictionary but list cannot be used as keys.
• Best for storing data that are to be write protected.
• Multiple values from a function can be returned using a tuple.
04-01-2023 Dr Aravapalli Rama Satish 62
Sets
• Sets are same as lists but not allows duplicate entries and does not
maintain an order of items.
• Sets are mutable means that we can easily add or remove items from it.
• Like mathematical set operations, python also has ability to calculate
differences and intersection between sets.
• Two sets are equal if every element of a set is available in the other set.
• A set is less than another set iff the first set is a subset of the second set.
• A set is greater than another set iff the first set is a super set of the
second set.

04-01-2023 Dr Aravapalli Rama Satish 63


Set Operations
Operation Description Code Output
update(t) Adds elements of set ‘t’ in the set ‘s’ provided that all s = set([1,2,3,4,5]) {1,2,3,4,5,6,7,8}
duplicates are avoided. ‘t’ may be tuples, strings, lists. t = set([6,7,8])
s.update(t); print(s)
add(x) Adds element ‘x’ to the set ‘s’ s = set([1,2,3,4,5]) {1,2,3,4,5,6}
s.add(6)
remove(x) Removes the element ‘x’ from set ‘s’. Return s.remove(3) {1,2,4,5}
KeyError if ‘x’ is not present.
discard(x) Same as remove but not generate error if element is s.discard(3) {1,2,4,5}
not in the set.
pop() Removes and return any arbitrary element from set s.pop() {2,3,4,5}
and raised KeyError if set is empty.
clear() Removes all elements from the set. s.clear() {}
len(s) Return the length of the set. print(len(s)) 5
04-01-2023 Dr Aravapalli Rama Satish 64
Set Operations
Operation Description Code Output
x in s Return True/False based on x is present in s print(3 in s) True
x not in s Return True/False based on x is not present in s print(3 not in s) False
issubset(t) True – if every element of a set is present in ‘t’, else False s = set(range(1,6)) True
(or) s <= t t = set(range(1,11))
print(s<=t)
issuperset(t) True – if every element of t is present in s, else False. print(s>=t) False
(or) s >= t
union(t)/s|t Returns new set: 𝑠 ∪ 𝑡 print(s|t) {1,2,3,4,5,6,7,8,9,10}
intersection(t) Returns new set: 𝑠 ∩ 𝑡 print(s & t) {1,2,3,4,5}
intersection_upd Update set s with 𝑠 ∩ 𝑡 s.intersection_update(t) {1,2,3,4,5}
ate(t) print(s)
difference(t) Return 𝑠 − 𝑡 print(s-t) {}
04-01-2023 Dr Aravapalli Rama Satish 65
Set Operations
Operation Description Code Output
difference_updat Update set s with 𝑠 − 𝑡 s.difference_update(t) {}
e(t) print(s)
symmetric_differ Return 𝑠 ∪ 𝑡 − (𝑠 ∩ 𝑡) z=s^t {6, 7, 8, 9, 10}
ence(t) print(z)
copy() Return a copy of set s. u = s.copy() id(s) 1464029862528
print("id(s)", id(s)) id(u) 1464029863648
print("id(u)", id(u))
isisjoint(t) Returns True if two sets have no common element print(s.isisjoint(t)) True
s = set([1,2,3]); t = set([4,5,6])
all(s) True if all elements are True, otherwise False. s = set([0,1,2,3]); all(s) False
any(s) True if any of the element is True, otherwise False any(s) True
max(s) Returns max value max(s) 3

04-01-2023 Dr Aravapalli Rama Satish 66


Set Operations
Operation Description Code Output
min(s) Returns minimum value min(s) 0
sum(s) Returns sum of all elements sum(s) 6
enumerate(s) Returns an enumerate object, contains index as well as s = set([‘a’,’b’,’c’,’d]) (0,’a’) (1,’b’) (2,’c’)
value as a pair for i in enumerate(s): (3,’d’)
print(i, end = ‘ ‘)
sorted(s) Returns a new sorted list from elements in the set. s = set([5,4,3,2,1,0]) [0,1,2,3,4,5]
print(sorted(s))
s == t and s!=t It checks if two sets are equal or not. s = set(["a", "b", "c"]) True
t = set("abc") False
z = set(tuple("abc"))
print(s == t)
print(s != z)

04-01-2023 Dr Aravapalli Rama Satish 67


Comments on sets
• A set cannot contain other mutable objects like lists. It generated
TypeError.
• To create an empty set, we should use set() and not to use {}. By default
pair of curly braces refer creation of dictionary.
• Indexing and slicing are not allowed as sets not follow ordering.
• A set can be created from a list but can not contain list as a element.
• copy() makes a shallow copy of the set, means that the objects in the
new set are references to the same objects as the original object.
• To add a single element we use add(), multiple elements use update().

04-01-2023 Dr Aravapalli Rama Satish 68


Dictionaries
• It is a data structure in which we store values as a pair of key and value.
• Syntax: dict_name = { k1:v1,k2:v2,k3:v3,….}
• Keys - Must be Unique and be of any immutable type like strings, numbers, or
tuples – case sensitive.
• Values - Need not be unique and be of any type.
• dict( ) can be used to construct a dictionary from a sequence of key-value
pairs.
• print(dict([(‘Roll_No’, ‘16001’), (‘Name’, ‘Arav’), (‘Course’, ‘Btech’)]))
• Dictionary Comprehension is another way of creating dictionary – It is a
Syntactic construct which creates a dictionary based on existing dictionary.
04-01-2023 Dr Aravapalli Rama Satish 69
Dictionary - Comprehension
• Syntax:
• D = { expression for var in sequence [if condition]}
• We place dictionary comprehension with in {}- 3 parts: for loop, condition, expr.
• for loop is used to go through the sequence.
• if condition is optional and if specified, only those values in the sequence are
evaluated using the expression which satisfy the condition.
• The expression generates elements of dictionary from items in the sequence that
satisfy the condition.
• Ex: d = {x:2*x for x in range(1,11)} output: {1:2,2:4,3:6,…,10:20}
• To access values of a dictionary, square brackets are used along with the
key to obtain its value. If the key is not available, “KeyError” is
generated. Ex: print(d[‘Roll_No’])
04-01-2023 Dr Aravapalli Rama Satish 70
Dictionary – Adding/Modifying/Deleting
Items
• Adding a new key-value pair / Modifying a existing pair done by,
dict_var[key]=value
• Deleting a one or more items can be done using ‘del’ keyword. To delete
all the items, use ‘clear’ method and to delete entire dictionary we use
‘del’ keyword: del dict_var[key], dict_var.clear(), del dict_var
• To delete a particular key from the dictionary, we can also use pop().
• dict_var.pop(key[,default_val]) – It removes the key form dictionary and returns
its value. If the key is not found, default value is returned, else, KeyError is
generated.
• popitems() – randomly pops and return an item from dictionary.

04-01-2023 Dr Aravapalli Rama Satish 71


Dictionary
• in keyword is used to check
whether a single key is present in
the dictionary.
• Sorting: keys() returns a list of all
keys in arbitrary order and
sorted() is used to sort the keys.
Ex: print(sorted(dict_var.keys()))
• Looping over a dictionary: You
can loop over a dictionary to
access values alone, keys alone, or
both keys and values using for:
04-01-2023 Dr Aravapalli Rama Satish 72
Dictionary
• Nested Dictionaries:
• We can define a dictionary inside another dictionary.

04-01-2023 Dr Aravapalli Rama Satish 73


Dictionary Methods
Operation Description Example Output
len(d) returns the number of items d = {‘R_no’: ‘001’, 3
‘Name’: ‘Arav’, ‘Course’:
B.Tech}
len(d)
str(d) returns a string representation of the dictionary print(str(d)) {‘R_no’: ‘001’, ‘Name’: ‘Arav’,
‘Course’: B.Tech}
d.clear() delete all elements in the dictionary d.clear() {}
print(d)
d.copy() returns a shallow copy of the dictionary, i.e., the d2 = d.copy() Run in python terminal to
dictionary returned will not have a duplicate copy print(d, d2) show the result.
of ‘d’ but will have the same reference. d2[‘Name’] = ‘Raj’
print(d, d2)

04-01-2023 Dr Aravapalli Rama Satish 74


Dictionary Methods
Operation Description Example Output
dict.fromkeys(seq[,val]) creates a new dict with keys from seq and sub = [‘csa’, ‘cpp’, ‘ds’, ‘os’] {‘csa’:-1, ‘cpp’:-1, ‘ds’: -
values set to val. If no val is specified then mar = dict.fromkeys(sub,-1) 1, ‘os’:-1}
None is assigned as a default value. print(mar)
d.get(key) returns the value for the key. If key is not print(d.get(‘Name’)) ‘Arav’
present it returns default value, else return
None
d.has_key(key) return True if the key is present, else it print(‘marks’ in d) False
returns False.
d.items() return list of tuples(K-V pairs) print(d.items()) [(‘Course’, ‘B.Tech’,…)]
d.keys() return list of keys of a dictionary print(d.keys()) [‘Course’, ‘Name’,…]
d.setdefault(key, value) sets a default value for a key that is not d.setdefault(‘marks’,0)
present in the dictionary

04-01-2023 Dr Aravapalli Rama Satish 75


Dictionary Methods

Operation Description Example Output


d.update(d2) Adds the key-value pairs of d2 to the d2={‘marks’:90, ‘grade’: ‘O’} {‘R_no’: ‘001’, ‘Name’: ‘Arav’,
key-value pairs of d d.update(d2); print(d) ‘Course’: ‘B.Tech’,
‘marks’:90, ‘grade’: ‘O’}

d.values() Returns a list of values of a dictionary. print(d.values()) [‘001’, ‘Arav’, ‘B.Tech’}


d.iteritems() Used to iterate through items in the for i,j in d.iteritems(): --Run in terminal to see the
dictionary print(i,j) result.
in, not in Checks whether a key is present in print(‘Name’ in d) True
dictionary or not. print(‘marks’ not in d) True

04-01-2023 Dr Aravapalli Rama Satish 76


NumPy package
• NumPy (Numerical Python) is an open source Python library that’s used
in almost every field of science and engineering.
• It’s the universal standard for working with numerical data in Python.
• The NumPy library contains multidimensional array and matrix data
structures.
• It provides ndarray, a homogeneous n-dimensional array object, with methods
to efficiently operate on it.
• NumPy can be used to perform a wide variety of mathematical operations on
arrays.
• It adds powerful data structures to Python that guarantee efficient calculations
with arrays and matrices and it supplies an enormous library of high-level
mathematical functions that operate on these arrays and matrices.

04-01-2023 Dr Aravapalli Rama Satish 77


NumPy package
• What’s the difference between a Python list and a NumPy array?
• NumPy gives you an enormous range of fast and efficient ways of creating arrays
and manipulating numerical data inside them.
• While a Python list can contain different data types within a single list, all of the
elements in a NumPy array should be homogeneous.
• The mathematical operations that are meant to be performed on arrays would be
extremely inefficient if the arrays weren’t homogeneous.
• NumPy arrays are faster and more compact than Python lists. An array consumes
less memory and is convenient to use.
• NumPy uses much less memory to store data and it provides a mechanism of
specifying the data types. This allows the code to be optimized even further.

04-01-2023 Dr Aravapalli Rama Satish 78


NumPy - What is an array?
• An array is a central data structure of the NumPy library.
• An array is a grid of values and it contains information about the raw data, how
to locate an element, and how to interpret an element.
• It has a grid of elements that can be indexed in various ways.
• The elements are all of the same type, referred to as the array dtype.
• An array can be indexed by a tuple of nonnegative integers, by booleans, by
another array, or by integers.
• The rank of the array is the number of dimensions.
• The shape of the array is a tuple of integers giving the size of the array along
each dimension.
• One way we can initialize NumPy arrays is from Python lists, using nested lists
for two- or higher-dimensional data.

04-01-2023 Dr Aravapalli Rama Satish 79


NumPy - What is an array?

• We can access the elements in the array using square brackets.


• When you’re accessing elements, remember that indexing in NumPy starts at 0.
• That means that if you want to access the first element in your array, you’ll be accessing element
“0”.

04-01-2023 Dr Aravapalli Rama Satish 80


NumPy - What is an array?
• You might occasionally hear an array referred to as a “ndarray,” which is
shorthand for “N-dimensional array.”
• An N-dimensional array is simply an array with any number of dimensions.
• You might also hear 1-D, or one-dimensional array, 2-D, or two-dimensional array, and
so on.
• The NumPy ndarray class is used to represent both matrices and vectors.
• A vector is an array with a single dimension (there’s no difference between row and
column vectors), while a matrix refers to an array with two dimensions. For 3-D or
higher dimensional arrays, the term tensor is also commonly used.
• An array is usually a fixed-size container of items of the same type and size.
• The number of dimensions and items in an array is defined by its shape. The shape of
an array is a tuple of non-negative integers that specify the sizes of each dimension.

04-01-2023 Dr Aravapalli Rama Satish 81


NumPy - What is an array?
• In NumPy, dimensions are called axes. This means that if you have a 2D array that
looks like this:
• Your array has 2 axes. The first axis has a length of 2 and the second axis has a length of 3.

• Just like in other Python container objects, the contents of an array can be accessed
and modified by indexing or slicing the array.
• Unlike the typical container objects, different arrays can share the same data, so
changes made on one array might be visible in another.
• Array attributes reflect information intrinsic to the array itself.
• If you need to get, or even set, properties of an array without creating a new array,
you can often access an array through its attributes.
04-01-2023 Dr Aravapalli Rama Satish 82
NumPy – Creation of basic array
• To create a NumPy array, you can use the function np.array().
• All you need to do to create a simple array is pass a list to it. If you choose to, you can
also specify the type of data in your list.

You can visualize your array this way:

04-01-2023 Dr Aravapalli Rama Satish 83


NumPy – Creation of basic array
• Besides creating an array from a sequence of elements, you can easily
create an array filled with 0’s Or an array filled with 1’s Or even an empty
array!

• The function empty creates an array whose initial content is random and
depends on the state of the memory. The reason to use empty over zeros (or
something similar) is speed - just make sure to fill every element afterwards!
• You can create an array with a range of elements. And even an array that
contains a range of evenly spaced intervals. To do this, you will specify the
first number, last number, and the step size.
04-01-2023 Dr Aravapalli Rama Satish 84
NumPy – Creation of basic array

• You can also use np.linspace() to create an array with values that are spaced
linearly in a specified interval.
• While the default data type is floating point (np.float64), you can explicitly specify
which data type you want using the dtype keyword.

04-01-2023 Dr Aravapalli Rama Satish 85


NumPy - Sorting Elements
• Sorting an element is simple with np.sort(). You can specify the axis,
kind, and order when you call the function.

04-01-2023 Dr Aravapalli Rama Satish 86


NumPy – Concatenate two Arrays

04-01-2023 Dr Aravapalli Rama Satish 87


NumPy - knowing the shape and size of an array.
• ndarray.ndim will tell you the number of axes, or dimensions, of the
array.
• ndarray.size will tell you the total number of elements of the array. This
is the product of the elements of the array’s shape.
• ndarray.shape will display a tuple of integers that indicate the number
of elements stored along each dimension of the array.
• If, for example, you have a 2-D array with 2 rows and 3 columns, the shape of
your array is (2, 3).

04-01-2023 Dr Aravapalli Rama Satish 88


NumPy - knowing the shape and size of an array.

04-01-2023 Dr Aravapalli Rama Satish 89


NumPy – Reshaping an Array
• Using arr.reshape() will give a new shape to an array without changing
the data.
• Just remember that when you use the reshape method, the array you
want to produce needs to have the same number of elements as the
original array.
• If you start with an array with 12 elements, you’ll need to make sure that your
new array also has a total of 12 elements.

04-01-2023 Dr Aravapalli Rama Satish 90


NumPy – Reshaping an Array

04-01-2023 Dr Aravapalli Rama Satish 91


NumPy - Convert a 1D array into a 2D array - Add a new
axis to an array
• You can use np.newaxis and
np.expand_dims to increase the
dimensions of your existing array.
• Using np.newaxis will increase
the dimensions of your array by
one dimension when used once.
• This means that a 1D array will
become a 2D array, a 2D array will
become a 3D array, and so on.

04-01-2023 Dr Aravapalli Rama Satish 92


NumPy - Convert a 1D array into a 2D array - Add a new
axis to an array
• You can explicitly convert a 1D
array with either a row vector or a
column vector using np.newaxis.
• For example, you can convert a 1D
array to a row vector by inserting an
axis along the first dimension,
• Or, for a column vector, you can
insert an axis along the second
dimension.

04-01-2023 Dr Aravapalli Rama Satish 93


NumPy - Convert a 1D array into a 2D array - Add a new
axis to an array
• You can also expand an array by
inserting a new axis at a specified
position with np.expand_dims.
• You can use np.expand_dims to
add an axis at index position 1 or
can add an axis at index position 0
with:

04-01-2023 Dr Aravapalli Rama Satish 94


NumPy – Indexing and Slicing
• You can index and slice NumPy
arrays in the same ways you can
slice Python lists.

04-01-2023 Dr Aravapalli Rama Satish 95


NumPy – Indexing and Slicing
• You may want to take a section of your array or specific array elements
to use in further analysis or additional operations. To do that, you’ll need
to subset, slice, and/or index your arrays.
• If you want to select values from your array that fulfill certain conditions,
it’s straightforward with NumPy.

04-01-2023 Dr Aravapalli Rama Satish 96


NumPy – Indexing and Slicing
• You can also select, for example, numbers that are equal to or greater
than 5, and use that condition to index an array or you can select
elements that are divisible by 2 or you can select elements that satisfy two
conditions using the & and | operators

You can also make use of the logical operators & and |
in order to return Boolean values that specify whether
or not the values in an array fulfill a certain condition.
This can be useful with arrays that contain names or
other categorical values.
04-01-2023 Dr Aravapalli Rama Satish 97
NumPy – Indexing and Slicing
• You can also use np.nonzero() to select elements or indices from an
array.
• You can use np.nonzero() to print the indices of elements that are, for example,
less than 5

04-01-2023 Dr Aravapalli Rama Satish 98


NumPy – Indexing and Slicing
• In the example, a tuple of arrays was returned: one for each dimension. The
first array represents the row indices where these values are found, and the
second array represents the column indices where the values are found.
• If you want to generate a list of coordinates where the elements exist, you can zip the
arrays, iterate over the list of coordinates, and print them.
• You can also use np.nonzero() to print the elements in an array that are less than 5 with:

04-01-2023 Dr Aravapalli Rama Satish 99


NumPy - Create an array from existing data
• You can easily create a new array from a section of an existing array any
time by specifying where you want to slice your array.

• You can also stack two existing arrays, both vertically and horizontally.
• vstack or hstack.

04-01-2023 Dr Aravapalli Rama Satish 100


NumPy - Create an array from existing data

• You can split an array into several smaller arrays using hsplit.
• You can specify either the number of equally shaped arrays to return or the columns
after which the division should occur.

04-01-2023 Dr Aravapalli Rama Satish 101


NumPy - Create an array from existing data

04-01-2023 Dr Aravapalli Rama Satish 102


NumPy - Create an array from existing data
• You can use the view method to create a new array object that looks at
the same data as the original array (a shallow copy).
• Views are an important NumPy concept!
• NumPy functions, as well as operations like indexing and slicing, will return views
whenever possible.
• This saves memory and is faster (no copy of the data has to be made).
• However it’s important to be aware of this - modifying data in a view also
modifies the original array!

04-01-2023 Dr Aravapalli Rama Satish 103


NumPy - Create an array from existing data

04-01-2023 Dr Aravapalli Rama Satish 104


NumPy - Basic array operations
• Let’s say, for example, that you’ve created two arrays, one called “data”
and one called “ones”

04-01-2023 Dr Aravapalli Rama Satish 105


NumPy - Basic array operations
• Basic operations are simple with NumPy.
• If you want to find the sum of the elements in an array, you’d use sum().
• This works for 1D arrays, 2D arrays, and arrays in higher dimensions.
• To add the rows or the columns in a 2D array, you would specify the axis.

04-01-2023 Dr Aravapalli Rama Satish 106


NumPy - Broadcasting
• There are times when you might want to carry out an operation between
an array and a single number (also called an operation between a vector
and a scalar) or between arrays of two different sizes.
• For example, your array (we’ll call it “data”) might contain information about
distance in miles but you want to convert the information to kilometers.
• You can perform this operation with:

04-01-2023 Dr Aravapalli Rama Satish 107


NumPy - Broadcasting
• NumPy understands that the multiplication should happen with each cell.
That concept is called broadcasting.
• Broadcasting is a mechanism that allows NumPy to perform operations
on arrays of different shapes.
• The dimensions of your array must be compatible, for example, when
the dimensions of both arrays are equal or when one of them is 1.
• If the dimensions are not compatible, you will get a ValueError.

04-01-2023 Dr Aravapalli Rama Satish 108


NumPy: More useful array operations
• NumPy also performs aggregation functions. In addition to min, max,
and sum, you can easily run mean to get the average, prod to get the
result of multiplying the elements together, std to get the standard
deviation, and more.

04-01-2023 Dr Aravapalli Rama Satish 109


NumPy: More useful array operations
• It’s very common to want to aggregate along a row or column. By
default, every NumPy aggregation function will return the aggregate of
the entire array.
• To find the sum or the minimum of the elements in your array, run:

04-01-2023 Dr Aravapalli Rama Satish 110


NumPy: More useful array operations
• You can specify on which axis you want the aggregation function to be
computed. For example, you can find the minimum value within each
column by specifying axis=0.
• The four values listed above correspond to the number of columns in
your array. With a four-column array, you will get four values as your
result.

04-01-2023 Dr Aravapalli Rama Satish 111


NumPy - Creating matrices
• You can pass Python lists of lists to create a 2-D array (or “matrix”) to
represent them in NumPy.
• Indexing and slicing operations are useful when you’re manipulating matrices:

04-01-2023 Dr Aravapalli Rama Satish 112


NumPy - Creating matrices
• You can aggregate matrices the same way you aggregated vectors.
• You can aggregate all the values in a matrix and you can aggregate them across
columns or rows using the axis parameter.

04-01-2023 Dr Aravapalli Rama Satish 113


NumPy - Creating matrices
• Once you’ve created your matrices, you can add and multiply them using
arithmetic operators if you have two matrices that are the same size.
• You can do these arithmetic operations on matrices of different sizes, but only
if one matrix has only one column or one row. In this case, NumPy will use its
broadcast rules for the operation.

04-01-2023 Dr Aravapalli Rama Satish 114


NumPy - Creating matrices
• Be aware that when NumPy prints N-dimensional
arrays, the last axis is looped over the fastest while
the first axis is the slowest.
• There are often instances where we want NumPy
to initialize the values of an array.
• NumPy offers functions like ones() and zeros(),
and the random.Generator class for random
number generation for that.
• All you need to do is pass in the number of
elements you want it to generate:
04-01-2023 Dr Aravapalli Rama Satish 115
NumPy – Creating Matrices

You can also use ones(), zeros(), and random() to create a 2D array
if you give them a tuple describing the dimensions of the matrix:

04-01-2023 Dr Aravapalli Rama Satish 116


NumPy - Generating random numbers
• The use of random number generation is an important part of the configuration and
evaluation of many numerical and machine learning algorithms.
• Whether you need to randomly initialize weights in an artificial neural network, split data into
random sets, or randomly shuffle your dataset, being able to generate random numbers (actually,
repeatable pseudo-random numbers) is essential.
• With Generator.integers, you can generate random integers from low (remember that
this is inclusive with NumPy) to high (exclusive). You can set endpoint=True to make
the high number inclusive.
• You can generate a 2 x 4 array of random integers between 0 and 4 with:

04-01-2023 Dr Aravapalli Rama Satish 117


NumPy - Get unique items and counts
• You can find the unique elements in an array easily with np.unique.
• To get the indices of unique values in a NumPy array (an array of first index positions of
unique values in the array), just pass the return_index argument in np.unique() as well as your
array.
• You can pass the return_counts argument in np.unique() along with your array to get the
frequency count of unique values in a NumPy array.

04-01-2023 Dr Aravapalli Rama Satish 118


NumPy - Get unique items and counts

• If the axis argument isn’t passed, your 2D array will be flattened.


• If you want to get the unique rows or columns, make sure to pass the axis argument. To find the unique rows, specify
axis=0 and for columns, specify axis=1.

04-01-2023 Dr Aravapalli Rama Satish 119


NumPy - Transposing and reshaping a matrix
• It’s common to need to transpose your matrices. NumPy arrays have the
property T that allows you to transpose a matrix.
• You may also need to switch the dimensions of a matrix.
• This can happen when, for example, you have a model that expects a certain input shape
that is different from your dataset.
• This is where the reshape method can be useful. You simply need to pass in the new
dimensions that you want for the matrix.

04-01-2023 Dr Aravapalli Rama Satish 120


NumPy – Reverse an Array
• You can also use .transpose() to reverse or change the axes of an array
according to the values you specify.

• NumPy’s np.flip() function allows you to flip, or reverse, the contents of an


array along an axis.
• When using np.flip(), specify the array you would like to reverse and the axis.
• If you don’t specify the axis, NumPy will reverse the contents along all of the
axes of your input array.
04-01-2023 Dr Aravapalli Rama Satish 121
NumPy – Reverse an Array

04-01-2023 Dr Aravapalli Rama Satish 122


NumPy – Reverse an Array

04-01-2023 Dr Aravapalli Rama Satish 123


NumPy - Flattening multidimensional arrays
• There are two popular ways to flatten an array: .flatten() and .ravel().
• The primary difference between the two is that the new array created
using ravel() is actually a reference to the parent array (i.e., a “view”).
• This means that any changes to the new array will affect the parent array
as well. Since ravel does not create a copy, it’s memory efficient.
• When you use flatten, changes to your new array won’t change the parent
array.

04-01-2023 Dr Aravapalli Rama Satish 124


NumPy - Flattening multidimensional arrays

04-01-2023 Dr Aravapalli Rama Satish 125


NumPy - Working with mathematical formulas
• The ease of implementing mathematical formulas that work on arrays is
one of the things that make NumPy so widely used in the scientific
Python community.
• For example, this is the mean square error formula (a central formula
used in supervised machine learning models that deal with regression):

04-01-2023 Dr Aravapalli Rama Satish 126


NumPy - Working with mathematical formulas

04-01-2023 Dr Aravapalli Rama Satish 127


NumPy - Dot Product
• Dot product is available both as a function in the NumPy module
np.dot() and as an instance method of array objects <ndarray>.dot().

04-01-2023 Dr Aravapalli Rama Satish 128


NumPy - Alternate Ways to obtain Dot product

np.dot() is strongly recommended since it accepts both NumPy


arrays and Python lists.

04-01-2023 Dr Aravapalli Rama Satish 129


NumPy - Dot Product
• Note that while np.dot() accepts 2D arrays (and carries out matrix
multiplication), using np.matmul() or a @ b for NumPy arrays is preferred.

04-01-2023 Dr Aravapalli Rama Satish 130


NumPy - Matrix Product
• Return matrix product of two arrays.

04-01-2023 Dr Aravapalli Rama Satish 131


NumPy - Matrix Product
# For 2D arrays, np.matmul() is the conventional matrix product.
a = np.array([[1, 0], [0, 1]])
b = np.array([[4, 1], [2, 2]])
print(np.matmul(a, b)) # Prints [[4 1]
# [2 2]]

# For 2D mixed with 1D, the result is the usual., np.matmul() is the conventional matrix product.
a = np.array([[1, 0], [0, 1]])
b = np.array([1, 2])
# The output matrices below are both 1D after the removal of the appended/prepended 1 dimension
print(np.matmul(a, b)) # Prints [1 2]
print(np.matmul(b, a)) # Prints [1 2]

04-01-2023 Dr Aravapalli Rama Satish 132


NumPy - Matrix Product

a = np.ones([9, 5, 7, 4])
c = np.ones([9, 5, 4, 3])
print(np.dot(a, c).shape) # Prints (9, 5, 7, 9, 5, 3)
print(np.matmul(a, c).shape) # Prints (9, 5, 7, 3) / n is 7, k is 4, m is 3 per the stacked-matrices rule

04-01-2023 Dr Aravapalli Rama Satish 133


Functions
Module 5
Function
• A function is a block of code that performs a specific task.
• Suppose, you need to create a program to create a circle and color it. You
can create two functions to solve this problem:
• create a circle function
• create a color function
• Dividing a complex problem into smaller chunks makes our
program easy to understand and reuse.
• Types of function: There are two types of function in Python
programming:
• Standard library functions - These are built-in functions in Python that are
available to use.
• User-defined functions - We can create our own functions based on our
requirements.

18-01-2023 Dr Aravapalli Rama Satish 2


Function Declaration
Here,
• def - keyword used to declare a function
• function_name - any name given to the function
• arguments - any value passed to function
• return (optional) - returns value from a function

• Here, we have created a function named greet().


• It simply prints the text Hello World!.
• This function doesn't have any arguments and doesn't
return any values.

18-01-2023 Dr Aravapalli Rama Satish 3


Calling a Function
• To use the function, we need to call it. Here's how we can call
the greet() function in Python.

Here's how the program works:


Output • When the function is called, the control of the
program goes to the function definition.
• All codes inside the function are executed.
• The control of the program jumps to the next
statement after the function call.

18-01-2023 Dr Aravapalli Rama Satish 4


Function Arguments
A arguments is a value that is If we create a function with arguments, we
accepted by a function. For ex: need to pass the corresponding values while
calling them. For ex:

• In the example, we have created a function named add_numbers()


with arguments: num1 and num2.
• We can also call the function by mentioning the argument name as:
add_numbers(num1 = 5, num2 = 4)
• In Python, we call it Keyword Argument (or named argument). The
code above is equivalent to add_numbers(5, 4)

18-01-2023 Dr Aravapalli Rama Satish 5


return Statement
• A Python function may or may not return a value. If we want our
function to return some value to a function call, we use the return
statement. For example,
def add_numbers():
...
return sum

• Here, we are returning the


variable sum to the function
call.
• The return statement also
denotes that the function has
ended. Any code after return is
not executed.

18-01-2023 Dr Aravapalli Rama Satish 6


Benefits of Using Functions
• Code Reusable - We can use the same function multiple times
in our program which makes our code reusable.
• Code Readability - Functions help us break our code into
chunks to make our program readable and easy to
understand.

18-01-2023 Dr Aravapalli Rama Satish 7


Function Argument with Default Values
• In Python, we can provide default values to function
arguments.
• We use the = operator to provide default values.
Here, we have provided default values 7 and 8 for parameters a and b
respectively. Here's how this program works:

1. add_number(2, 3): Both values are passed during the function call.
Hence, these values are used instead of the default values.
2. add_number(2): Only one value is passed during the function call.
So, according to the positional argument 2 is assigned to argument a,
and the default value is used for parameter b.
3. add_number(): No value is passed during the function call. Hence,
default value is used for both parameters a and b.

18-01-2023 Dr Aravapalli Rama Satish 8


Keyword Arguments
• In keyword arguments, arguments are assigned based on the
name of arguments. For example,

• Here, we have assigned names to arguments during the function call.


• Hence, first_name in the function call is assigned to first_name in the function
definition. Similarly, last_name in the function call is assigned to last_name in the
function definition.
• In such scenarios, the position of arguments doesn't matter.

18-01-2023 Dr Aravapalli Rama Satish 9


Function With Arbitrary Arguments
• Sometimes, we do not know in
advance the number of arguments
that will be passed into a function. To
handle this kind of situation, we can
use arbitrary arguments in Python.
• Arbitrary arguments allow us to pass a
varying number of values during a
function call.
• We use an asterisk (*) before the
parameter name to denote this kind of
argument. For example:
• After getting multiple values, numbers
behave as an array so we are able to
use the for loop to access each value.

18-01-2023 Dr Aravapalli Rama Satish 10


Function With Arbitrary Arguments
• We can put * before a parameter name to indicate that it is a
variable-length tuple of positional parameters, and we can
use ** to indicate that a parameter is a variable-length
dictionary of keyword parameters.
• Inside the function,
• we can access args as a normal tuple, but the * means that args isn’t
passed into the function as a single parameter which is a tuple:
instead, it is passed in as a series of individual parameters.
• Similarly, ** means that kwargs is passed in as a series of individual
keyword parameters, rather than a single parameter which is a
dictionary.

18-01-2023 Dr Aravapalli Rama Satish 11


Function With Arbitrary Arguments
Passing parameters in Normal way

We can use * or ** when we are calling a function to unpack a sequence or a dictionary into a
series of individual parameters:

This makes it easier to


build lists of parameters
programmatically.

18-01-2023 Dr Aravapalli Rama Satish 12


Function With Arbitrary Arguments
• We can mix ordinary parameters, *args and **kwargs in the
same function definition.
• *args and **kwargs must come after all the other parameters, and
• **kwargs must come after *args.
• You cannot have more than one variable-length list parameter or
more than one variable dict parameter.

18-01-2023 Dr Aravapalli Rama Satish 13


Function With Arbitrary Arguments
• If we use a * expression
when you call a function, it
must come after all the
positional parameters, and if
we use a ** expression it
must come right at the end.

18-01-2023 Dr Aravapalli Rama Satish 14


Recursion
• Recursion is the process of defining something in terms of
itself.
• A physical world example would be to place two parallel mirrors
facing each other. Any object in between them would be reflected
recursively.

• we know that a function can


call other functions.
• It is even possible for the
function to call itself. These
types of construct are termed as
recursive functions.

18-01-2023 Dr Aravapalli Rama Satish 15


Recursion Example – Factorial of a integer
• Factorial of a number is the
product of all the integers
from 1 to that number.
• For example, the factorial of 6
(denoted as 6!) is
1*2*3*4*5*6 = 720.
• In the example, factorial() is a
recursive function as it calls
itself.
• When we call this function
with a positive integer, it will
recursively call itself by
decreasing the number.
18-01-2023 Dr Aravapalli Rama Satish 16
Recursion Example – Factorial of a integer
• Each function multiplies the
number with the factorial of
the number below it until it is
equal to one.
• This recursive call can be
explained in the following
steps.

18-01-2023 Dr Aravapalli Rama Satish 17


Recursion Example – Factorial of a integer
• Our recursion ends when the
number reduces to 1. This is
called the base condition.
• Every recursive function must
have a base condition that stops
the recursion or else the function
calls itself infinitely.
• The Python interpreter limits the
depths of recursion to help avoid
infinite recursions, resulting in
stack overflows.
• By default, the maximum depth of
recursion is 1000. If the limit is
crossed, it results in
RecursionError.
18-01-2023 Dr Aravapalli Rama Satish 18
RecursionError
• Advantages:
• Recursive functions make the code
look clean and elegant.
• A complex task can be broken down
into simpler sub-problems using
recursion.
• Sequence generation is easier with
recursion than using some nested
iteration.
• Disadvantages of Recursion
• Sometimes the logic behind
recursion is hard to follow through.
• Recursive calls are expensive
(inefficient) as they take up a lot of
memory and time.
• Recursive functions are hard to
debug.

18-01-2023 Dr Aravapalli Rama Satish 19


Lambda/Anonymous Function
• In Python, a lambda function is a special type of function without
the function name.
lambda : print('Hello World')
• Here, we have created a lambda function that prints 'Hello World’.
• We use the lambda keyword instead of def to create a lambda
function. Here's the syntax to declare the lambda function
lambda argument(s) : expression
• Here,
• argument(s) - any value passed to the lambda function
• expression - expression is executed and returned

18-01-2023 Dr Aravapalli Rama Satish 20


Lambda Function with(out) an Argument
• We have defined a lambda function and assigned it to the variable
named greet.
• To execute this lambda function, we need to call it.
• Similar to normal functions, a lambda function can also accept
arguments.

18-01-2023 Dr Aravapalli Rama Satish 21


How to use the lambda function with filter()?
• The filter() function in Python
takes in a function and an
iterable (lists, tuples, and
strings) as arguments.
• The function is called with all
the items in the list and a
new list is returned which
contains items for which the
function evaluates to True.

18-01-2023 Dr Aravapalli Rama Satish 22


How to use the lambda function with map()?
• The map() function in Python
takes in a function and an
iterable (lists, tuples, and
strings) as arguments.
• The function is called with all
the items in the list and a
new list is returned which
contains items returned by
that function for each item.

18-01-2023 Dr Aravapalli Rama Satish 23


Examples on Lambda

Recursion in gcd = lambda x,y: x if y == 0 else gcd(y, x%y)


Lambda → lcm = lambda a,b: (a * b)/gcd(a,b)

names = ['Alan', 'Gregory', 'Zlatan', 'Jonas', 'Tom', 'Augustine']


names.sort()
print(names)
names = ['Alan', 'Gregory', 'Zlatan', 'Jonas', 'Tom', 'Augustine']
names.sort(key=lambda x: len(x))
print(names)

18-01-2023 Dr Aravapalli Rama Satish 24


Variable Scope
• A variable scope specifies the region where we can access a
variable.
• In Python, we can declare variables in three different scopes:
local scope, global, and nonlocal scope.
• For example,
def add_numbers():
sum = 5 + 4
• Here, the sum variable is created inside the function, so it can
only be accessed within it (local scope). This type of variable
is called a local variable.
18-01-2023 Dr Aravapalli Rama Satish 25
Variable Scope
• Based on the scope, we can classify Python variables into
three types:
• Local Variables
• Global Variables
• Nonlocal Variables
• Local Variables
• When we declare variables inside a function, these variables will
have a local scope (within the function). We cannot access them
outside the function. - These types of variables are called local
variables.

18-01-2023 Dr Aravapalli Rama Satish 26


Variable Scope
• Here, the message variable
is local to the greet()
function, so it can only be
accessed within the function.
• That's why we get an error
when we try to access it
outside the greet() function.
• To fix this issue, we can make
the variable named message
global.
18-01-2023 Dr Aravapalli Rama Satish 27
Variable Scope
• Global Variables
• A variable declared outside of the
function or in global scope is known
as a global variable.
• This means that a global variable
can be accessed inside or outside of
the function.
• This time we can access the
message variable from outside of
the greet() function. This is because
we have created the message
variable as the global variable. Now, message will be accessible from any
scope (region) of the program.
18-01-2023 Dr Aravapalli Rama Satish 28
Nonlocal Variables
• Nonlocal variables are used in nested
functions whose local scope is not
defined. This means that the variable
can be neither in the local nor the
global scope.
• We use the nonlocal keyword to create
nonlocal variables. For example:
• There is a nested inner() function. We have
used the nonlocal keyword to create a
nonlocal variable.
• The inner() function is defined in the scope
of another function outer().
• If we change the value of a nonlocal
variable, the changes appear in the local
variable.

18-01-2023 Dr Aravapalli Rama Satish 29


Global Keyword
• In Python, the global
keyword allows us to modify
the variable outside of the
current scope.
• It is used to create a global
variable and make changes
to the variable in a local
context.
• First let's try to access a
global variable from the
inside of a function:

18-01-2023 Dr Aravapalli Rama Satish 30


Global Keyword
• However, if we try to modify
the global variable from
inside a function as:
• Generates UnboundLocalError
• This is because we can only
access the global variable but
cannot modify it from inside
the function.
• The solution for this is to use
the global keyword.

18-01-2023 Dr Aravapalli Rama Satish 31


Global Keyword
• We have defined c as the
global keyword inside add().
• Then, we have incremented
the variable c by 2, i.e c = c
+ 2.
• As we can see while calling
add(), the value of global
variable c is modified from 1
to 3.

18-01-2023 Dr Aravapalli Rama Satish 32


Global Keyword in Nested Functions
• We declared a global variable inside the
nested function inner_function().
• Inside outer_function(), num has no effect of
the global keyword.
• Before and after calling inner_function(), num
takes the value of the local variable i.e num =
20.
• Outside of the outer_function() function, num
will take the value defined in the
inner_function() function i.e 25.
• This is because we have used the global
keyword in num to create a global variable
inside the inner_function() function (local
scope).
• So, if we make any changes inside the
inner_function() function, the changes appear
outside the local scope, i.e. outer_function().

18-01-2023 Dr Aravapalli Rama Satish 33


Rules of global Keyword
• When we create a variable inside a function, it is local by
default.
• When we define a variable outside of a function, it is global
by default.You don't have to use the global keyword.
• We use the global keyword to read and write a global
variable inside a function.
• Use of the global keyword outside a function has no effect.

18-01-2023 Dr Aravapalli Rama Satish 34


Namespaces and Scope in Python
• A namespace is a system that has a unique name for each and
every object in Python.
• An object might be a variable or a method.
• Python itself maintains a namespace in the form of a Python
dictionary.
• Let’s go through an example, a directory-file system structure in
computers.
• Needless to say, that one can have multiple directories having a file
with the same name inside every directory.
• But one can get directed to the file, one wishes, just by specifying the
absolute path to the file.
18-01-2023 Dr Aravapalli Rama Satish 35
Namespaces and Scope in Python
• Real-time example, the role of a namespace is like a surname.
• One might not find a single “Alice” in the class there might be multiple
“Alice” but when you particularly ask for “Alice Lee” or “Alice Clark” (with a
surname), there will be only one (time being don’t think of both first name
and surname are same for multiple students).
• On similar lines, the Python interpreter understands what exact
method or variable one is trying to point to in the code, depending
upon the namespace.
• So, the division of the word itself gives a little more information.
• Its Name (which means name, a unique identifier) + Space(which talks
something related to scope).
• Here, a name might be of any Python method or variable and space depends
upon the location from where is trying to access a variable or a method.
18-01-2023 Dr Aravapalli Rama Satish 36
Types of namespaces
• When Python interpreter runs solely
without any user-defined modules,
methods, classes, etc. Some functions
like print(), id() are always present,
these are built-in namespaces.
• When a user creates a module, a
global namespace gets created, later
the creation of local functions creates
the local namespace.
• The built-in namespace
encompasses the global namespace
and the global namespace
encompasses the local namespace.

18-01-2023 Dr Aravapalli Rama Satish 37


Lifetime of a namespace
• A lifetime of a namespace depends
upon the scope of objects, if the
scope of an object ends, the lifetime
of that namespace comes to an end.
• Scope refers to the coding region
from which a particular Python
object is accessible.
• Hence one cannot access any
particular object from anywhere
from the code, the accessing has to
be allowed by the scope of the
object.
• Hence, it is not possible to access
the inner namespace’s objects from
an outer namespace.
18-01-2023 Dr Aravapalli Rama Satish 38
Lifetime of a namespace
• As shown in the following figure,
the same object name can be
present in multiple namespaces as
isolation between the same name is
maintained by their namespace.

18-01-2023 Dr Aravapalli Rama Satish 39


Lifetime of a namespace
• A scope defines the hierarchical
order in which the namespaces
have to be searched in order to
obtain the mappings of name-to-
object(variables).
• It is a context in which variables
exist and from which they are
referenced.
• It defines the accessibility and
the lifetime of a variable.
• Let us take a simple example as
shown below:
18-01-2023 Dr Aravapalli Rama Satish 40
Lifetime of a namespace
• The above program gives different outputs because the same
variable name pi resides in different namespaces, one inside
the function print_pi and the other in the upper level.
• When print_pi() gets executed, ‘inner pi variable‘ is printed
as that is pi value inside the function namespace.
• The value ‘outer pi variable‘ is printed when pi is referenced
in the outer namespace.
• From the above example, we can guess that there definitely is
a rule which is followed, in order in deciding from which
namespace a variable has to be picked.
18-01-2023 Dr Aravapalli Rama Satish 41
Scope resolution: LEGB rule
• In Python, the LEGB rule is used
to decide the order in which the
namespaces are to be searched
for scope resolution.
• The scopes are listed below in
terms of hierarchy(highest to
lowest/narrowest to broadest):
• Local(L): Defined inside
function/class
• Enclosed(E): Defined inside
enclosing functions(Nested function
concept)
• Global(G): Defined at the uppermost
level
• Built-in(B): Reserved names in
Python builtin modules

18-01-2023 Dr Aravapalli Rama Satish 42


Modules
• Reusing particular piece of code can be done by functions.
Modules goes a step ahead.
• It allows you to reuse one or more functions in our programs,
even in the programs in which those functions have not been
defined.
• A module is a file with .py extension that has definitions of all
functions and variables that you would like to use even in
other programs.
• The program in which you want to use functions or variables
defined in the module will simply import that particular
module (or .py file).
18-01-2023 Dr Aravapalli Rama Satish 43
Modules
• A module imported in a program must be located and loaded
into memory before it can be used.
• Python first searches in current working directory. If module is not
found then,
• It looks for the module in the directories specified in the “PYTHON
PATH” environment variable.
• If the module is still not found or if the PYTHONPATH variable is not
defined, then a Python installation specific path is searched.
• If the module is not located even after, then an error “ImportError”
exception is generated.

18-01-2023 Dr Aravapalli Rama Satish 44


Modules
• If we want the module to be available to other programs as
well then the module should be either saved in the directory
specified in the PYTHONPATH or stored in python installation
directory.
• Once a module is located, it is loaded in memory. A compiled
version of the module ‘.pyc’ is generated to reduce the
compilation time for further importing.

18-01-2023 Dr Aravapalli Rama Satish 45


Modules
• Every module has a name.
• We can find the name of the module by using the __name__
attribute of the module.
• Ex: print(“Name of the module is:”,__name__)

18-01-2023 Dr Aravapalli Rama Satish 46


Modules
• dir() – It lists the identifiers defined in a module. These
identifiers may include functions, classes and variables.
• If no name is specified, this function will return the list of names
defined in the current module.
• dir(__builtins__) – lists the defined identifiers in sys module.
• By Convention, modules are named using lowercase letters
and optional underscore characters.
• When a python file is executed directly, it is considered main
module of a program.
• Main module are given the special name “__main__” and provide the
basis for a complete python program.

18-01-2023 Dr Aravapalli Rama Satish 47


Modules and namespaces
• A namespace is a container that provides a named context for
identifiers.
• Two identifiers with the same name in the same scope will lead to a
name clash.
• Python does not allow to have two different identifiers with the same
name.
• But in some cases we need to have same identifiers. To cater
this situation, namespaces are used.
• Namespaces enables programs to avoid potential name clashes by
associating each identifier with the namespace from which it
originates.

18-01-2023 Dr Aravapalli Rama Satish 48


Modules and namespaces

18-01-2023 Dr Aravapalli Rama Satish 49


Module private variables
• All identifiers defined in a module by default public. So all
identifiers are available to the modules who imported.
• If we want some variables (or) functions in a module to be
privately used with in the module, but not to be accessed from
outside it, then you need to be declare those identifiers as
“private”.
• Identifers whose name starts with two underscores(__) are known as
private identifiers. – These identifiers can be used only with in the module.
• A module can import other modules
• It is customary but not mandatory to place all import statements at the
beginning of the module.
• A module is loaded only once, irrespective of the number of times it is
imported.

18-01-2023 Dr Aravapalli Rama Satish 50


File Handling
Module 6
File Handling – Opening/Closing Files
• Python provides functions or methods to handle files associated
with file object.
• open( ):
• Before reading from or writing to a file, you must first open it using built-in
open().
• It creates/returns a file object (or file handler), which will be used to invoke
methods associated to it.
Syntax: fileObj = open(filename[, access_mode]) access_mode is optional by
Ex: default the access mode is ‘r’
file = open(“File.txt”,“rb”)
print(file)
Output: <open file “File.txt”, mode “rb” at 0x02a85000>

26-01-2023 Dr Aravapalli Rama Satish 2


File Access Modes
Mode Purpose
r Opens a file in read only.
rb Opens a binary file in read only.
File pointer is placed at the beginning of the file.
r+ Opens for both reading & writing.
rb+ Opens for both reading & writing a binary file.
w Opens the file for writing only.
wb Opens the binary file for writing. File does not exist: new file created, otherwise
w+ Opens a file for writing and reading. overwritten.
wb+ Opens a binary file for writing and reading.
a Opens a file for appending.
File pointer at the end of the file
ab Opens a binary file for appending
a+ Opens a file for both reading & appending. File pointer at the end of the file. If file doesn’t exist it
ab+ Opens a binary file for both reading & appending. creates a new file for writing.

26-01-2023 Dr Aravapalli Rama Satish 3


File object Attributes
• fileObj.closed – Returns True if the file is closed and False
otherwise.
• fileObj.mode – Returns access mode with which file has been
opened.
• fileObj.name – Returns name of the file.
• Ex:
file = open(“File1.txt”,“wb”)
print(‘Name of the file:’,file.name)
print(‘File is closed:’, file.closed)
print(‘File has been opened in’,file.mode,‘mode’)
26-01-2023 Dr Aravapalli Rama Satish 4
File Handling – Opening/Closing Files
• close() is used to close the file object.
• you cannot further read from or write into the file associated
with the file object.
• It flushes any unwritten information.
• Python automatically closes a file when the reference object
of a file is reassigned to another file, but as a good
programming practice you should always explicitly use the
close() to close the file.
Syntax: fileObj.close()

26-01-2023 Dr Aravapalli Rama Satish 5


File Handling – Opening/Closing Files
• close() frees up any system
resources such as file descriptors,
file locks, etc. that are associated file = open(“File1.txt”, “wb”)
with a file.
print(“Name of the file:”, file.name)
• There is an upper limit to the number
of files a program can open. If that print(“File is Closed:”, file.closed)
limit is exceeded then the program file.close()
may even crash or work in an
unexpected manner. print(“File is Closed:”, file.closed)
• Thus you can waste lots of memory if print(file.read())
you keep many files open • Last line generates ValueError: I/O
unnecessarily and also remember operation on closed file.
that open files always stand a chance
of corruption and data loss. • It is generated since we tried read
operation on closed file.
• Any attempt to use the file object will
result an error once file is closed.

26-01-2023 Dr Aravapalli Rama Satish 6


Writing of Files
• write() • writelines()
• Used to write a string to an • Used to write a list of strings.
already opened file.
• Once you have stored data in
• String may include number, a file, you can always open
special characters or other
symbols. the file to write more data or
• It does not add a new line append data to it.
character to the end of the • To append contents to a file
string. you should open a file in
• It returns None. append mode.
• Syntax: fo.write(string) • Do not use write, it
overwrites the data.

26-01-2023 Dr Aravapalli Rama Satish 7


Writing of Files
file = open(“File1.txt”,“w”)
file.write(“Hello All, Hope you are enjoying learning python”)
file.close()
file = open(“File1.txt”,“w”)
lines = [“Hello World”, “Welcome to the world of python”, “Enjoying in learning
python”]
file.writelines(lines)
file.close()

file = open(“File1.txt”,“a”)
file.write(“\n python is very simple yet powerful language”)
file.close()
26-01-2023 Dr Aravapalli Rama Satish 8
Reading of Files
• read() is used to read a string from an already opened file.
• syntax: fo.read(count)
• count is an optional parameter which if passed to the read() specifies the
number of bytes to be read from the opened file.
• It starts reading from the beginning of the file.
• If count is missing or has negative value, then it reads entire file contents.
• read() returns new line as ‘\n’
• If you try to open a file for reading that does not exist, then you will get an
error.
file = open(“File1.txt”, “r”)
print(file.read(10))
file.close()
26-01-2023 Dr Aravapalli Rama Satish 9
Reading of Files
• readline() is used to read a single line from the file.
• It returns an empty string when the end of the file is reached.
• The blank line is represented by ‘\n’ and the readline() returns a string
containing only a single new line character.
• After reading a line the control automatically goes to the next line.
• readlines() is used to read all lines in the file.
file = open(“File1.txt”, “r”)
print(“First Line:”, file.readline())
print(“Second Line:”, file.readline())
file.close()
(or)
file = open(“File1.txt”, “r”)
print(file.readlines())
file.close()

26-01-2023 Dr Aravapalli Rama Satish 10


Reading of Files
• list() is also used to display entire contents of the file.
• We just need to pass the file object as an argument to the list().
• print(list(fo))
• The last and probably a very fast, simple, and efficient way to
display a file is the loop over the file object to print every line
in it.
for line in file:
print(line)
file.close()
• All reading methods returns an empty string when the end-of-
the-file is reached.

26-01-2023 Dr Aravapalli Rama Satish 11


Opening files using with keyword
• It is a good programming practice.
• It has the advantage that the file is properly closed after it is used
even if an error occurs during read or write operation or even
when you forget to explicitly close the file.
with open(“file1.txt”, “rb”) as file:
for line in file:
print(line)
print(“Let’s check if the file is closed:”, file.close())
• Calling close() on a file object that is already closed does not raise
any error but fails silently.
• When you open a file for reading or writing, the file is searched in
the current working directory. if the file exists somewhere else
then you need to specify the path of the file.
26-01-2023 Dr Aravapalli Rama Satish 12
Often used File object functions
• fileno() – Returns the file number of the file(fo.fileno())
• flush() – Flushes the write buffer of the file stream.(fo.flush())
• truncate() – Resizes the file into ‘n’ bytes.(fo.truncate(5))
• rstrip() – Strips off white spaces including new line characters
from the right side of the string read from the file.
line = file.readline()
line.rstrip()

26-01-2023 Dr Aravapalli Rama Satish 13


File Positions
• A file is associated with a file pointer that facilitates the movement
across the file for reading and / or writing data.
• The file pointer specifies a location from where the current read or
write operation is initiated.
• Once the read or write operation is completed, the pointer is
automatically updated.
• tell(): It tells the current position with in the file at which next read
or write operation will occur.
• seek(offset[,from]): It sets the position of the file pointer, in other
terms, moves the file pointer to a new location.
• offset: number of bytes to be moved
• from: specifies the reference position from where the bytes are to be
moved.
• 0: beginning of the file, 1: current position, 2:end of the file.
26-01-2023 Dr Aravapalli Rama Satish 14
Examples
• To copy first 10 bytes of binary file to another.

with open(“File1.txt”, “rb”) as file1:


with open(“File2.txt”, “wb”) as file2:
buf = file1.read(10)
file2.write(buf)
print(“File Bytes are copied.”)

26-01-2023 Dr Aravapalli Rama Satish 15


Examples
• To copy a python script from source file to destination file such that
comment lines should not appear in destination.

with open(“f.py”, “rb”) as f1:


with open(“s.py”, “wb”) as f2:
while True:
buf = file1.readline()
if len(buf)!=0:
if buf[0]==‘#’:
continue
else:
f2.write(buf)
else:
break
print(“File copied.”)
26-01-2023 Dr Aravapalli Rama Satish 16
Examples
• Counting characters, lines, and words of a file

def charcount(filename):
return len(open(filename).read())
def wordcount(fn):
return len(open(filename).read().split())
def linecount(fn):
return len(open(fn).readlines())

26-01-2023 Dr Aravapalli Rama Satish 17


Examples
# Write a program to count the number of upper-case alphabets
present in a text file “python.txt”.
def uppercount():
c=0
f1=open("python.txt",'r')
line=f1.read()
for i in line:
if (i.isupper() == True):
c+=1
print("Total no. of upper-case alphabets :",c)

uppercount()

26-01-2023 Dr Aravapalli Rama Satish 18


Examples
• A text file “python.txt” contains alphanumeric text. Write a
program that reads this text file and writes to another file
“python_new.txt” entire file except the numbers or digits in the
file.

fh=open("python.txt","r")
fw=open("python_new.txt","w")
rec=fh.read()
for a in rec:
if (a.isdigit() != True):
fw.write(a)
fh.close()
fw.close()
26-01-2023 Dr Aravapalli Rama Satish 19
Examples
• Write a program to count the words “to” and “the” present in
a text file “python.txt”

fname = "python.txt"
num_words = 0
f= open(fname, 'r')
words = f.read().split()
for a in words:
if (a.lower() == "to" or a.lower() == "the" ):
num_words = num_words + 1
print("Number of words:", num_words)
f.close()

26-01-2023 Dr Aravapalli Rama Satish 20


Examples
• Write a program to display all the lines in a file “python.txt”
along with line/record number.

fh=open("python.txt","r")
count=0
lines=fh.readlines()
for a in lines:
count=count+1
print(count,a)
fh.close()

26-01-2023 Dr Aravapalli Rama Satish 21


Examples
• Write a program to display all the lines in a file “python.txt”
which have the word “to” in it.

fh=open("python.txt","r")
count=0
lines=fh.readlines()
for a in lines:
if (a.count("to") > 0) :
print(a)
fh.close()

26-01-2023 Dr Aravapalli Rama Satish 22


Examples
• Working with CSV files

import csv
with open('innovators.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)

26-01-2023 Dr Aravapalli Rama Satish 23


Examples
• Working with CSV files
import csv
with open('employee_file2.csv', mode='w') as csv_file:
fieldnames = ['emp_name', 'dept', 'birth_month’]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'emp_name': 'John Smith', 'dept': 'Accounting’,
'birth_month': 'November’})
writer.writerow({'emp_name': 'Erica Meyers', 'dept': 'IT’,
'birth_month': 'March'})

26-01-2023 Dr Aravapalli Rama Satish 24


Renaming and Deleting files
• The ‘os’ module in python has various methods that can be
used to perform file processing operations.
• rename(): Used to rename the name of the file to another.
• os.rename(“file1.txt”,“students.txt”)
• remove(): Used to delete files. It takes filename as an
argument and deletes the file.
• os.remove(“file1.txt”)

26-01-2023 Dr Aravapalli Rama Satish 25


Directory operations
• mkdir() – creates a directory in the current directory.
• os.mkdir(“Dir1”)
• getcwd() - Used to display the current working directory
• print(os.getcwd())
• chdir() – Used to change the current working directory.
• os.chdir(“Dir1”)
• An error will be displayed if we try to change to directory that does
not exist.
• rmdir() – Used to remove or delete a empty directory
• os.rmdir(“Dir1”)
26-01-2023 Dr Aravapalli Rama Satish 26
Directory operations
• To remove nonempty directory, use the rmtree() of ‘shutil’
module. Ex: shutil.rmtree(“dir2”)
• makedirs() – Used to create more than one folder.
• If we pass a string “C:\python311\Dir1\Dir2\Dir3” as an argument, it
will create a folder Dir1 in python 311, Dir2 in Dir1, Dir3 in Dir2.
• os.makedirs(“C:\\python311\\Dir1\\Dir2\\Dir3”)
• To make our program portable across different OSs and to
mention the path we use os.path.join().
• print(os.path.join(“C:”,“students”,“ug”,“Btech.docx”))

26-01-2023 Dr Aravapalli Rama Satish 27


Directory operations
• os.path.abspath() – Constructs absolute path from the passed
string. Ex: print(os.path.abspath(“python\\String.docx”))
• os.path.dirname(path) – returns directory name
• os.path.basename(path) – returns file name
• os.path.split(path) – return directory name and file name
• os.path.getsize(path) – returns size of the file
• os.listdir(path) – returns list of file names in the specified path
• os.path.exists(path) – returns True if the file/dir is available
• os.path.isfile(path) – returns True if the path is file
• os.path.isdir(path) - returns True if the path is directory
26-01-2023 Dr Aravapalli Rama Satish 28

You might also like