0% found this document useful (0 votes)
0 views

Python

The document provides an overview of Python for CBSE Class 12 for the academic year 2023-24, detailing its history, features, execution modes, syntax, data types, and operators. It highlights Python's characteristics as a high-level, interpreted, and dynamically typed language, along with its rich library and open-source nature. Additionally, it covers the structure of Python code, including comments, identifiers, keywords, literals, and built-in data types, emphasizing the importance of syntax and variable assignment.

Uploaded by

asdf lkjh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python

The document provides an overview of Python for CBSE Class 12 for the academic year 2023-24, detailing its history, features, execution modes, syntax, data types, and operators. It highlights Python's characteristics as a high-level, interpreted, and dynamically typed language, along with its rich library and open-source nature. Additionally, it covers the structure of Python code, including comments, identifiers, keywords, literals, and built-in data types, emphasizing the importance of syntax and variable assignment.

Uploaded by

asdf lkjh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Python for CBSE Class 12 year 2023-24

Introduction

History
• Guido van Rossum is Python's principal author
• First version was released in 1994

Python Features
• High-level language
• Interpreted language
• Dynamically typed
• Case-sensiBve for keywords and idenBfiers
• Uses indentaBon for blocks and nested blocks
• Free and open source
• Portable and available in many plaHorm
• Has a rich library of predefined funcBons

References

Official Website
https://www.python.org/

Documentation
https://docs.python.org/

CPython GitHub
https://github.com/python/cpython

PEP 8 – Style Guide for Python Code


https://peps.python.org/pep-0008/

PEP 20 – The guiding principles for Python's design is referred to as The Zen of Python. It lists 19
principles that are applicable for all good Python programs.
https://peps.python.org/pep-0020/
Execution Modes of Python Interpreter
Interactive Mode
• Python's Integrated Development and Learning Environment (IDLE) opens the IDLE Shell,
which runs Python in interactive mode.
• Python statements are executed one statement at a time.
• The statement should be typed-in at the prompt.
• The statement is executed on pressing enter key.
• A statement can span across multiple lines, the statement will be executed when enter key
is pressed after the complete statement is typed-in.
• When an expression is keyed in as a statement, it is evaluated and the evaluated value is
printed.
• Previously typed-in statements can be executed again by moving the input cursor to that
statement and pressing enter key.

Script Mode

• Python program (i.e., script) is saved in a file.

• Executing script file from operating system command prompt:


python <file path>

• A script file cannot be directly executed from the shell (interactive mode) prompt.

• When the script file is open, menu option RunàRun Module (or shortcut key F5) will
execute the script.

• When a script file is run, syntax check is done for the entire file before starting execution.
• When a line contains an expression (not a statement), it is evaluated and the evaluated
value discarded (not printed to output as in case of interactive mode).

Example: Let’s say a script file contains the following two lines of code.

print('Hello')
print(10 + '20')

On running the script file, Hello will be printed and then an error occurs in the second line as
it is trying to add an integer and a string. This is a run-time error.

Let’s introduce a syntax error in the second line by removing one of the single quotes in '20'.
If the modified script file is run, Hello will NOT be printed and only a compile-time syntax
error occurs. This is because syntax check is done for the entire script before the start of
execution.

If a script file has multiple syntax errors, Python interpreter stops at the first syntax error.

Python Syntax

Character Set

• For language elements outside string literals and comment texts, Python uses only ASCII
printing characters and whitespace characters. All printing ASCII characters are used except
three: $ ? and ` (grave accent).

• Any ASCII or non-ASCII character can be present in string literals and comments.

Comments
Starts with # and ends with newline.

Line Continuation

Implicit Line Continuation

Expressions in parentheses, square brackets or curly braces can continue to multiple lines till the
closing parentheses/brackets/braces.

Example:

if x > y:
x = ( a * b # continuation
+ c * d) # indentation of continuation lines doesn't matter
y = 5 # indentation matters here

Explicit Line Continuation

If a line ends with a backlash, the next line is treated as a continuation. Not character (not even a
space) can follow backslash.
Example:

if x > \
y: # explicit continuation line
x = 4
y = 5

Tokens

Identifiers
• Names used to identify a variable, function, or other entities in a program.

• Identifier can contain:


o Uppercase and lowercase letters A through Z
o Underscore _
o Digits 0 through 9 but an identifier cannot start with a digit

• From Python 3.0, certain non-ASCII characters are allowed in identifiers.

Keywords

• Set of identifiers reserved for the Python language features.


• Also called reserved words
• Cannot be used as ordinary identifiers
• There are 35 keywords.
• Keywords are case-sensitive.
• All keywords except three (True False and None) have only lower-case letters.

Constants Operators Control Flow Exception


None in if-elif-else try-except-finally
True is for Raise
False not while assert
and break
or continue
return

Import Scope Async Others


import global async with-as
from nonlocal await def
yield del
pass
class
lambda
Soft Keywords

Soft keywords are reserved only under certain specific contexts. During tokenization, they are NOT
treated as keywords. Later, during parsing, they are treated as keywords in certain specific contexts.
There are three soft keywords: match case _
Built-in Constants
None True False

Literals
String Literals

• Enclosed in single or double quotes – cannot span multiple lines


• Raw string of the form r'……' or r"……." - escape sequences are not processed in raw string
literals. The prefix can be r or R.
• Enclosed by a sequence of three single (or double) quotes – can span across multiple lines
• Adjacent string literals are concatenated into a single string literal

Escaping

• In strings enclosed in single quotes, double quotes do not need escaping.


• In strings enclosed in double quotes. single quotes do not need escaping.
• In multi-line strings, escaping is needed only when a three single/double quote sequence is
present in the string

• Note: Two single quotes or two double quotes is not an escape sequence (different from
MySQL). 'Tom''s idea' will be treated are two adjacent literals 'Tom' and 's idea',
which will get concatenated.

• Escape sequences in string literals.

Escape
Character Represented by Sequence
Sequence
\0 ASCII NUL (X'00') character
\' Single quote (') character
\" Double quote (") character
\a Alert (bell)
\b Backspace
\f Form feed
\n Newline (linefeed)
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
\ooo One to three octal digits
\xhh Exactly two hexadecimal digits
\uhhhh Exactly four hexadecimal digits
\Uhhhhhhhh Exactly eight hexadecimal digits
Bytes Literals

• Strings of the form b'……' or b"……."


• The prefix can be b or B.
• The characters inside the quote can be only ASCII characters
• Escape sequences other than \u and \U are allowed.

Numeric Literals

Integer Literals
• Decimal integer – Sequence of digits 0 to 9. Cannot start with 0
• Hexadecimal integer – Sequence of hexadecimal digits prefixed with 0x
• Octal integer – Sequence of octal digits prefixed with 0o
• Binary integer – Sequence of 0s and 1s prefixed with 0b
• The prefixes and hexadecimal digits can be in upper or lower case

FloaZng-point Literals
• Should contain a decimal point or an exponent part. Examples:
o 4.5
o 4.
o 4.1e3
o 4e3
o 4E-1

Imaginary Number Literals


• An integer or a floaBng-point literal suffixed with j (lower or upper case)

Note:
• An underscore _ can be added between two digits. This is for readability. Example: 123_000
is same as 123000 and 0x_7A is same as 0x7A.
• A leading - or + is considered as unary operators by Python. They are is not considered as
part of the numeric literal. For example, -10 is a literal expression, not an integer literal.
Similarly, 5+6j is a literal expression, not a complex number literal.

Interning

Python implementaBons have the freedom to merge matching literals (includes all immutable literal
expressions) into a single instance.

For example,

'aa' is 'a'*2 can give True if the implementaBon merges 'aa' and 'a'*2 into a single string instance.

The same implementaBon can give False for 'aa'*20000 is 'a'*40000. This difference is because the
choice of when and when not to do interning is lep to the implementaBon.

The approach of merging mulBple literals into a single instance is called interning. This can be done
for strings, numeric types and tuples. For example, the tuple (10, 20, 30) can be interned.
Note that interning can be done only for immutable types. Mutable types like list and dicBonary
should not be interned. For example, [10, 20, 30] and (2, 3, [4,5]) should not be
interned.

Interning is based on the final value of the expression, not the form of the expression. For example,
2+3 and 4+1 can be merged.

Example:

x = (2, 3, (22,33), 4)
y = 22,33
print(y is x[2]) # can print True

Operators

As per the syntax of Python, following are operators that can be used in expressions.

+ - * ** / // % @
<< >> & | ^ ~ :=
< > <= >= == !=

Punctuations

• Following are the punctuaBons in Python:


o Single character delimiter tokens ( ) [ ] { } , . : ;
o PunctuaBons that are part of other tokens ' " # \
o FuncBon annotaBon ->
o Assignment =
o Augmented assignment
+= -= *= /= //= %= @= &= |= ^= >>= <<= **=

Assignment Operators are Delimiters

Assignment symbol = and augmented assignment symbols like += and *= are all delimiters in Python.
This is different from other languages like C/C++/C#/Java, where they are treated as operators.

C/C++/C#/Java does not put restricBons on how assignment operators can be used – they can be
used anywhere in expressions. For example, the following is valid in C/C++/C#/Java.

while ((x += 2) < 10)

However, this is not allowed in Python as one of the goals of Python was to not have any side effect
in expressions. Python allows = only in assignment statements and augmented assignment operators
only in augmented assignment statements.

Note: Operator := (called the walrus operator) was introduced in Python 3.8. This is the Python
version of the assignment operator = in C/C++/C#/Java. This is the only operator that has side-effects
in Python.
Trivia: IntroducBon of the walrus operator created a controversy and led to the primary creator of
Python, Guido van Rossum, distancing himself from Python development.
htps://medium.com/pythonistas/the-story-of-guido-van-rossum-and-his-walrus-operator-quick-
blog-a0481af61bd8

Variables
• Variable in Python is a name that refers to a value/object stored in the memory.
• IdenBfiers are used as variable names.
• A variable can refer to a value/object of any type (not strongly typed as in C/C++/C#/Java).
• Variable declaraBon is implicit in Python - variables get created automaBcally when a value is
assigned to it for the first Bme.
• Variables should be assigned values before they are used in expressions.

Built-in Data Types

Numeric Types

Data Type DescripZon Constructor Range


int Integers int() No limit
float FloaBng point numbers float() Depends on implementaBon - usually
float("inf") 64-bit IEEE. Can be more.
float("-inf")
float("nan")

complex Complex numbers complex() Uses floaBng point for real and
imaginary parts

c = 2.1+5.2j # complex number


c.real # real part
c.imag # imaginary part

bool Boolean. Subtype of integer. bool() True or False


(Equivalent to 1 or 0)

None Type

None is a literal of type NoneType. It represents the absence of a value. None is the only possible
value for NoneType.
Sequence Types
Types holding an ordered sequence of items.

Built-in Sequence Types

Data Type DescripZon Mutability Constructor


list Ordered list of items Mutable list()

tuple Ordered list of items Immutable tuple()

str Text sequence. Immutable str()


range Range of integers Immutable range()

Items can be referred to by their posiBon in the sequence. Non-negaBve indices starBng from lep at
zero or negaBve indices starBng from right at -1. Example:

Non-negaBve indices ---> 0 1 2 3 4 5


Items ---> A B C D E F
NegaBve indices ---> -6 -5 -4 -3 -2 -1

Slicing
Regular Slice
All sequence types support slicing. The result of the slice will be the same type on which the slice is
done.

L = ['A', 'B', 'C', 'D', 'E', 'F']


print(L[2:4]) # prints ['C', 'D']

T = ('A', 'B', 'C', 'D', 'E', 'F')


print(T[2:4]) # prints ('C', 'D')

S = 'ABCDEF'
print(S[2:4]) # prints CD

R = range(10, 16)
print(R[2:4]) # prints range(12, 14)

Note that the slice [start:stop] begins from the start posiBon and includes items unBl before the stop
posiBon. For example, [3:9] refers to posiBons 3 to 8.

The start and stop arguments in the slice can be given using non-negaBve or negaBve indices or a
mix. For example, if the length of a sequence is 6, [2:4], [-4:4], [2:-2] and [-4:-2] all give the same
result.

Extended Slice
Regular slices use a step value of one to move from start posiBon towards stop posiBon. Any non-
zero step value can be specified by adding a third parameter to the slice like [start:stop:step]. If the
step value is any non-zero value other than 1, the slice is called an extended slice.

For example, 'ABCDEF'[1:5:2] gives 'BD'


The step can also be negaBve:
'ABCDEF'[5:0:-1] gives 'FEDCB'
'ABCDEF'[5:0:-2] gives 'FDB'

Default values for start & stop

If the step is posiZve.


If start argument is omited or None, it is taken as the 0th posiZon.
If stop argument is omited, or None it is taken as one posiZon beyond the last posiZon
If the step is negaZve.
If start argument is omited, it is taken as last posiZon.
If stop argument is omited, it is taken as one posiZon beyond the 0th posiZon

Examples:
[:5] is same as [0:5]
For a sequence of length 6, [2:] is same as [2:6]
[:1:-1] is same as [-1:1:-1]
For a sequence of length 6, [-1::-1] is same as [-1:-7:-1]
[:] gives the enBre sequence
[::-1] gives the enBre sequence in reverse
Handling out of range start/stop values

The start and stop can be given values beyond the range of the indices of the items in the sequence.
For example, the items in sequence 'ABCDEF' have indices in the range -6 to 5. Values outside this
range are treated as below:
- For start
o PosiBve values beyond range are treated as the last posiZon
o NegaBve values beyond range are treated as the 0th posiZon
- For stop
o PosiBve values beyond range are treated as one posiZon beyond the last posiZon
o NegaBve values beyond range are treated as one posiZon beyond 0th posiZon

Examples: For a sequence of length 6.


[10:0:-1] is same as [-1:0:-1]
[-10:4] is same as [0:4]
[1:10] is same as [1:7]
[-1:-10:-1] is same as [-1:-7:-1]

Sequence Type Operations


The sequence types have many funcBons and operators. Refer Python Built-in Containers.xlsx for
details.

Mapping (Dictionary)

A mapping object maps hashable objects to arbitrary objects. Mappings are mutable objects. There
is only one built-in mapping type, the dicBonary - dict.

Only hashable objects can be used as keys in the dicBonary.

Hashable types
• Numeric types – int, float, complex, bool. Numeric values that compare equal have the same
hash value.
• String
• None
• Range

Unhashable types
• List
• DicBonary
• Set

Hashable or Unhashable depending on the elements


• Tuple is hashable if all its elements are hashable

DicBonary has many funcBons and operators. Refer Python Built-in Containers.xlsx for details.

Set
A set object is an unordered collecBon of disBnct hashable objects. Duplicates are idenBfied based
on the hash value.

Set has many funcBons and operators. Refer Python Built-in Containers.xlsx for details.

Iterable Types

Container objects are those that can contain mulBple other objects as its elements. List, tuple,
dicBonary, set, string and range are all containers.
Iterable objects are containers that provide the ability to iterate through each of its elements in the
container one-by-one. All built-in containers listed above are iterable.

If contobj is iterable, it is possible to use it in a for loop as below.

for elem in contobj:

The above for loop iterates through the default iterator of contobj. The default iterator can also be
directly accessed using iter(contobj).

The elements can be iterated in reverse order using reversed() funcBon.

for elem in reversed(contobj):

Reverse iteraBon is supported by all built-in containers except set. As set maintains no order for the
elements, reverse order has no meaning.

The default iterator of dicBonary iterates through the keys in the dicBonary. It is also possible to
iterate values or items in the dicBonary.

Example: If map is a dicBonary

for key in map: print(key) # prints keys


for key in map.keys(): print(key) # prints keys
for value in map.values(): print(value) # prints values
for item in map.items(): print(item) # prints (key, value) tuples

Iterable types have many funcBons and operators. Refer Python Built-in Containers.xlsx for details.

Mutable and Immutable Types

Immutable objects are those whose state (internal values) cannot be changed aper it is iniBalized at
the Bme of creaBon. Immutable types are types whose objects are always immutable.

Mutable objects are those whose state (internal values) can be changed aper creaBon. Mutable
types are types whose objects are always mutable.

Immutable types
• Numeric types – int, float, complex, bool
• String
• Tuple
• None
• Range

Mutable types
• List
• DicBonary
• Set

Immutable types to not support funcBons/operators that can change its state. Examples

t = 10,20,30
t[1] = 25 # not possible as tuple is immutable

s = 'abc'
s[1] = 25 # not possible as string is immutable

c = 2+3j
c.real = 25 # not possible as complex is immutable

Mutable type examples

L = [10,20,30]
L[1] = 25 # changing element allowed in list

S = {10,20,30}
S.add(25) # adding element allowed in set

D = {1:10,2:20,3:30}
D[2] = 25 # changing element allowed in dictionary
Expressions
Operator Precedence
Following is a subset of Python operators in order of precedence. Parentheses can be used to
override precedence.
Precedence Operators DescripZon
1 ** ExponenBaBon
2 - + ~ Unary minus, unary plus and complement
3 * / % // MulBplicaBon & division
4 + - AddiBon & subtracBon
5 << >> Bit ship operators
6 & Bitwise AND
7 ^ Bitwise XOR
8 | Bitwise OR
< <= > >= Order comparison operators
== != Equality comparison operators
is IdenBty test operators
9
is not
in Membership test operators. 2nd operand should be an iterable
not in
Logical NOT. True when the operand is zero/empty/None.
10 not
Logical AND.
Result is the 1st operand if it is zero/empty/None.
11 and
Else the result is the 2nd operand.

Logical OR.
Result is the 1st operand if it is non-zero/non-empty/not-None.
12 or
Else the result is the 2nd operand.

• Order comparison operators:


o For numeric comparison, both operands should be non-complex numeric (int, float, bool)
o For string comparison, both operands should be strings. String comparisons are
case-sensiBve.

• Membership test operators: if the 2nd operand is a string, 1st operand should also be a string

• Note: Python does not have a logical exclusive-or (XOR) operator.

Associativity and Evaluation Order

AssociaZvity
Among operators with same precedence:
• ExponenBaBon (**) – right-to-len associaBvity.
• Unary prefix operators – right-to-len associaBvity.
• All other operators – len-to-right associaBvity.
EvaluaZon order is always len-to-right

Numeric Operators

Behaviours

• % / and // give ZeroDivisionError when the 2nd operand is zero.


• Floor division // gives floor of the quotient. x // y is calculated as floor(x/y).
Example, –10.1 // 3 is –4.
• // will return an int or float depending on the type of operands but the result will always be
an integer value.
• x % y is calculated as x – (x//y) * y. Example: –10.1 % 3 is 1.9.
• x % y, if non-zero, will have the sign of the y (2nd operand).

• A negative number raised to fractional power with ** gives a complex result.


• 0**0 gives 1 (no error).

• ~ (bit inversion) gives a negative result for a non-zero argument and vice versa.

Restrictions on Operand Types

• ArithmeBc operators – All numeric types (int, float, complex, bool) allowed with the
following excepBons
o // and % not allowed for complex

• Ship and bitwise operators – Only integer (int, bool) operands allowed

Implicit Type Conversion

Implicit conversion, also known as coercion, for numeric values is done when an expression is
evaluated. Operands of operators get implicitly converted as below

• First, all bool operands are converted to int

• Then,
o If one of the operands is complex, the other is converted to complex
o Else if one of the operands is float, the other is converted to float

Conversion of an int to float/complex will result in OverflowError if the integer value cannot
be represented by float.

• In case of / (division) operator between two integers, the arguments are converted to float
and division is done in floating-point
Explicit Type Conversion

Function Convert From Convert To Examples


int() float int int(123.9) gives 123
str int(True) gives 1
bool int(False) gives 0
int('123') gives 123
int('123.9') gives ValueError

float() int float float(100) gives 100.0


str float(True) gives 1.0
bool float('1e2') gives 100.0

complex() Int complex complex(0) gives 0j


float complex(0.1) gives (0.1+0j)
str complex(100) gives (100+0j)
bool complex('2+5j') gives (2+5j)
complex('-2+5j') gives (-2+5j)
complex(' 2+5j') gives (2+5j)
complex('5j+2') gives ValueError
complex('2+ 5j') gives ValueError

str() Any value str String form of the value

bool() Any value bool bool(0.0) gives False


bool(100) gives True
bool(6j) gives True
bool('x') gives True
bool('') gives False
bool([]) gives False
bool([1, 2]) gives True
bool(None) gives False

Conversion between Unicode/ASCII value and character.

Function Convert From Convert To Examples


chr() Unicode/ASCII String of chr(65) gives 'A'
value. Should be length 1 chr(87) gives 'a'
integer. chr(48) gives '0'

ord() String of length 1 Unicode/AS ord('A') gives 65


CII value. ord('AB') gives TypeError
Should be ord(' A') gives TypeError
integer. ord('') gives TypeError
Non-Numeric Operators

Operators behave differently based on the data type of the operands.

String operators

• ConcatenaZon - string + string will perform concatenaBon of the two strings

• RepeZZon - string * int and int * string will give a string that is string repeated int Bmes. If
the integer operand is non-posiBve, an empty string is returned.

• String formarng or interpolaZon operator (%) – Used for C prins() style forma{ng.
o First argument should be a string containing prinH() style format string.
o If only one value is required, the second argument be the value matching the format
specified
o If mulBple values are required, the second argument should be a tuple with exact
number of values specified in the format string
o The second argument can be a mapping (dicBonary) for name-based matching with
format string.

Examples:
'QuanZty: %d units' % 1000 gives 'QuanZty: 100 units'
'Time: %02d:%02d' % (2, 30) gives 'Time: 02:30'
'Time: %(hrs)02d:%(mins)02d' % {'hrs':2, 'mins':30} gives 'Time: 02:30'

List and Tuple Operators


• ConcatenaZon and RepeZZon operators as in string

DicBonary Operators
• Pipe operator | merges the second dicBonary into the first one and returns a new dicBonary,
without modifying the first dicBonary.

Set Operators
• Refer Python Built-in Containers.xlsx for details.

Short-Circuited Evaluation

In the below expression, exp1, expr2, … etc are evaluated in order and the evaluaBon stops at the
first expression that is false. The value of the whole expression is the last expression evaluated.

expr1 and expr2 and expr3 and ….

If the last expression in the and sequence is evaluated, that becomes the value of the expression.

Examples:

Expression Value
10 and 'abc' and { } and 4.2 { }
10 and 'abc' and 4.2 4.2
10 and 'abc' and True and 0 0

Similar to logical and, logic or stops at finding first expression that is true.

Examples:

Expression Value
0 or { } or 'abc' or 4.2 'abc'
0 or { } or 4.2 4.2
0 or [ ] or dict() { }

Chaining of Relational Operators

RelaBonal operators can be chained lep-to-right as below.

expr1 rel-op1 expr2 rel-op2 expr3 rel-op3 ….

The expression is equivalent to except that expr2, expr3, … are evaluated only once.

expr1 rel-op1 expr2 and expr2 rel-op2 expr3 and expr3 rel-op3 ….

The relaBonal operators can be any of the following with no restricBon on the mix of operators in a
chain:
= >= > <= < !=
is
is not
in
not in

Identity and Equality Operators with Constants

Comparison Use DON’T Use


Comparing expr with a literal == is
!= is not
Literals include immutable
compile-Bme literal Reason: The behaviour is
expressions. unpredictable due to
possible interning.

Python gives a syntax


warning if such idenBty
checks are done.

Comparing expr with None expr is None ==


expr is not None !=

Note: None is expr & None is not expr will also Reason: Equality operators
work but the above style is used for code can have custom
readability. implementaBons for some
types.
Checking if cond-expr is # Directly use cond-expr as condition ==
True if cond-expr: !=
... is
is not
# Use bool() function to store
X = bool(cond-expr) Reason: The behaviour of
these operators does not
match the way condiBon
expressions are interpreted
in Python.

Checking if cond-expr is # Use not operator ==


False !=
if not (cond-expr): is
... is not

X = not (cond-expr) Reason: Same as above.

Assignment
L-value and R-value

L-value is a reference to a locaBon in memory that can store a value. L stands for locaBon or lep (as
L-values appear on the lep side of assignment operaBons). Only L-values can be given on the lep side
of an assignment.

Examples:

Variable myvar
List item mylist[5]
Slice of a list mylist[5:10]
DicBonary item mdict['a']
Object atribute myobj.myval

R-value is a value that does not refer to any locaBon in memory. R stands for right (as R-values
appear on the right side of assignment operaBons)

Assignment Statement
Simple Assignment

a = x + y*z #simple assignment

Unpacking Assignment

a, b, c, d = (11, 22, 33, 44) #simple unpacking

a, b, *v = (11, 22, 33, 44, 55, 66) # starred target at the end
print(a, b, v) # prints 11 22 [33, 44, 55, 66]

*v, a, b = (11, 22, 33, 44, 55, 66) # starred target at the start
print(v, a, b) # prints [11, 22, 33, 44] 55 66
a, b, *v, c, d = (11, 22, 33, 44, 55, 66) # starred target in the middle
print(a, b, v, c, d) # prints 11 22 [33, 44] 55 66

a, b, c = (11, 22, 33, 44) # error - too many values to unpack


a, b, c, d, e = (11, 22, 33, 44) # error - not enough values to unpack

The right-side of the assignment can be any iterable. All of the below assignments give the same
values for a, b, c, d.

a, b, c, d = 11, 22, 33, 44 # tuple


a, b, c, d = [ 11, 22, 33, 44 ] # list
a, b, c, d = { 11, 22, 33, 44 } # set
a, b, c, d = range(11, 55, 11) # range
a, b, c, d = { 11:111, 22:111, 33:111, 44:111 } # dictionary

Unpacking string:

a, b, c, d = 'pqrs'
print(a, b, c, d) # prints p q r s

Unpacking iterables inside iterables:

[x, y, z] = ((11, 22), (33, 44), 55) # contained tuples not unpacked
print(x, y, z) # prints (11, 22) (33, 44) 55

[[a, b], [c,d], e] = ((11, 22), (33, 44), 55) # contained tuples uunpacked
print(a, b, c, d, e) # prints 11 22 33 44 55

Every box bracketed part in the target should match an iterable on the right-side of the assignment.

[a], [b] = [1, 2] # error - [a] is matched with integer 1 (not iterable)
[a], [b] = [[1], [2]] # no error [a] matched with [1] & [b] with [2]
[a, b] = [1, 2] # no error
a, b = [1, 2] # no error

Parentheses can be used like box brackets but with the below excepBons:
• Redundant parentheses are ignored – ((…)) (((…))) are both same as (…)
• Parentheses around a single non-star variable are ignored – (a) and a are both same.

(a), (b) = [1, 2] # no error unlike in case of box brackets


[a, b], [c,d], e = ((11, 22), (33, 44), 55)
(a, b), (c,d), e = ((11, 22), (33, 44), 55) # same as previous

Slice Assignment

List is mutable sequence type. So, it supports assignment to a slice.

Example:

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[1:4] = ['X', 'Y']
print(L) # prints ['A', 'X', 'Y', 'E', 'F']

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[1:5:2] = ['X', 'Y']
print(L) # prints ['A', 'X', 'C', 'Y', 'E', 'F']

Any iterable can be assigned to the slice.

Examples:

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[1:4] = 'XY'
print(L) # prints ['A', 'X', 'Y', 'E', 'F']

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[1:4] = range(2,4)
print(L) # prints ['A', 2, 3, 'E', 'F']

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[1:4] = { 1:111, 2:222 }
print(L) # prints ['A', 1, 2, 'E', 'F']

Assignment is in reverse order if the step is negaBve

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[-2:-4:-1] = 'XY'
print(L) # prints ['A', 'B', 'C', 'Y', 'X', 'F']

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[-2:-6:-2] = 'XY'
print(L) # ['A', 'B', 'Y', 'D', 'X', 'F']

When assigning to an extended slice (step other than 1), the size of the assigned sequence should
exactly match the slice size. Otherwise, an error occurs.

L = ['A', 'B', 'C', 'D', 'E', 'F']


L[1:5:2] = 'XYZ' # error - slice size: 2 assigned sequence size: 3

Multiple Assignments

MulBple assignments can be done with a single assignment statement:

target1 = target2 = target3 = …… = expr

This is equivalent to the below assignments except that expr is evaluated only once,
target1 = expr
target2 = expr
target3 = expr


• target1, target2, etc can be any mix variables, unpacking targets or slices
• Assignment happens from lep to right target1 is assigned first, then target2 and so on.
• If the same variable is assigned by mulBple targets, the assignment done by the right-most
target overwrites all other assignments.

Examples:

a,b,c = x = p,q,r = 1,2,3


print(a,b,c,x,p,q,r) # prints 1 2 3 (1, 2, 3) 1 2 3

a,b,c = x = p,q,a = 1,2,3


print(a,b,c,x,p,q,r) # prints 3 2 3 (1, 2, 3) 1 2 3

a,b,c = x = p,q,x = 1,2,3


print(a,b,c,x,p,q,r) # prints 1 2 3 3 1 2 3

L = [1, 2, 3, 4, 5, 6]
X = a, *b = L[1:3] = [11, 22, 33]
print(a, b) # prints 11 [22, 33]
print(L) # prints [1, 11, 22, 33, 4, 5, 6]
print(X) # prints [11, 22, 33]

Augmented Assignment Statement


Operations

+= -= *= /= //= %=
&= |= ^= >>= <<= **=

Assignment to Immutable Types

The statement will be of the form:

immutable-type-lvalue aug-assignment-opr expression

Example:

num1 = num2 = 10
print(num1 is num2) # prints True

num1 += 5 # new instance of int containing (num1+5) is created


print(num1 is num2) # prints False as num1 now points to the new instance

print(num1, num2) # prints 15 10

L = [1, 2, 3, 4, 5]
L[1] += 555
print(L) # prints [1, 557, 3, 4, 5]

s = 'ab'
s *= 3
print(s) # prints ababab

t = (1, 2, 3)
t += 44, 55
print(t) # prints (1, 2, 3, 44, 55)

Augmented assignment is supported when the corresponding operaBon is supported. For example,
string supports += *= and %= and while tuple supports += and *=

For immutable types, a new instance is created and the lvalue is made to point to the new instance.

Assignment to Mutable Types

The statement will be of the form:

mutable-type-lvalue aug-assignment-opr expression

Example:

L = [1, 2]
L *= 3 # no new instance of list created
print(L) # prints [1, 2, 1, 2, 1, 2]

L = [1, 2, 3, 4, 5]
L += [111, 222] # no new instance of list created
print(L) # prints [1, 2, 3, 4, 5, 111, 222]

L = [1, 2, 3, 4, 5]
L[1:4] += [111, 222] # no new instance of list created by the assignment
print(L) # prints [1, 2, 3, 4, 111, 222, 5]

In case of mutable types, the operaBon is performed in-place, without creaBng a new instance of the
mutable.

Augmented assignment is supported when the corresponding operaBon is supported. Following are
the augmented assignments supported by built-in mutable types.

Type Augmented Assignment Right-side type


List += Iterable
*= Integer
DicBonary |= DicBonary or (key,value) iterable
Set |= Set
&= Set
-= Set
^= Set

Statements

Statement Types

A simple statement is comprised within a single logical line. Examples: import, del, global
One line can contain multiple simple statements separated by semocolon

A compound statement is Compound statements contain (groups of) other statements, Examples:
if-else, for, while.
Flow Control Statements

if-elif-else
for assignment-target in expression
for-else
while
while-else
break
continue
return

Other Miscellaneous Statements

import – For imporBng modules. from-import is an alternate form

del – To delete any of the following


• A name
• An element or a slice form a list
• A mapping from a dicBonary

global – Used within funcBons to declare a name as global

try – For excepBon handling

raise – To raise an excepBon

assert – To asset for a condiBon. AsserBonError raised if the condiBon evaluates to False

pass – No operaBon. Used as a placeholder where a statement block is required by the syntax but is
not yet to be coded (or is not needed)

FuncZon definiZon – starts with def

with – Used to automaBcally do exit acBons for an object on leaving a statement block. AlternaBve
to using try-finally.

Exception Handling
Types of Errors

Syntax Errors

Example: SyntaxError

Occurs when a code does not confirm to the Python syntax rules. Code is executed only aper syntax
check completes without errors. Syntax errors are also called parsing errors.

RunZme Errors

Examples: ZeroDivisionError OverflowError NameError TypeError ValueError KeyError


Occurs when python code is running. These errors, if not handled by try…except, will abnormally
terminate the program.

Logical Errors

A logical error is a bug in the program that causes it to behave not as expected. These errors are
difficult to idenBfy as no excepBon is raised as in case of runBme errors.

Exceptions

• An excepBon is a Python object that represents an error.

• When an error occurs during the execuBon of a program, an excepBon is said to have been
raised.

• An excepBon that is not handled by the program causes the program to terminate
abnormally.

• ExcepBon objects include a stack Traceback, which contains informaBon about the sequence
of funcBon calls that led to the excepBon.

Built-in Exceptions

ExcepZon name CondiZon that raises the excepZon


SyntaxError An error is found in the syntax of the Python code. This can occur at runBme
in exec() and eval() funcBons.

ValueError A funcBon or operaBon received an argument with correct data type but an
inappropriate value.

KeyboardInterrupt User hit the the interrupt key (normally Control-C or Delete) while program
was execuBng.

ImportError Error occurred when execuBng an import statement

EOFError The input() funcBon reached end-of-file condiBon (EOF) without reading any
data

ZeroDivisionError The second argument of a division or modulo operaBon is zero

IndexError A sequence subscript is out of range. Not raised for indices in slice.

NameError Atempt to access a name that is not a local or a global name.

IndentaBonError Incorrect indentaBon in the program code. Raised during syntax check.

TypeError An operaBon or funcBon is applied to an object of inappropriate type

OverflowError Result of a calculaBon exceeded the maximum limit for a numeric data type.
AsserBonError An assert statement failed

RunBmeError An error is detected that doesn’t fall in any of the other categories,

Raising Exceptions

raise Statement

Raising an excepBon.

raise RuntimeError("Something bad happened")

Raising an excepBon from within an except block specifying a current excepBon (i.e., exc) as the
cause.

try:
print(10 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened") from exc

Raising an excepBon from within an except/finally block suppressing of the current excepBon.

try:
print(10 / 0)
except Exception:
raise RuntimeError("Something bad happened") from None

If raise is done inside an except/finally block without specifying from, any current excepBon is
included and the raised excepBon becomes and excepBon that occurred while handling the current
excepBon.

assert Statement

An assert statement is used to test a condiBon that needs to be saBsfied at the point of asserBon
(i.e., where the assert statement is present).

assert condition-expr

Is equivalent to:

if __debug__:
if not condition-expr: raise AssertionError

The assert statement

assert condition-expr, arg-expr

Is equivalent to:
if __debug__:
if not condition-expr: raise AssertionError(arg-expr)

Handling Exceptions

try:

# block of code for which exceptions need to be handled

except Exception1 as ex:

# the except block - code to handle Exception1.


# ex refers to the exception object
# the name 'ex' will be removed when control leaves the except block

except Exception2 as ex:

# code to handle Exception2

else: # executed when no exception occurs and


# break/continue/return was not called from the try block

# code to be executed on normal completion of try block

finally: # executed after except/try blocks - always executed

# final cleanup code to be executed always

• A try block can have any number of except blocks.

• There can be only a single else block and an else block is allowed only when there is at least
one except block.

• There can be only a single finally block.

• A try block should have at least one except block or a finally block.

Built-in Functions

Input/Output

print()
input()

Conversion to numeric types

int()
float()
complex()
bool()
Unicode/ASCII conversion

chr(u) – Returns the character for the Unicode value.


ord(c) – Returns the Unicode for a character. Argument should be a string of length one.

Conversion to string

str(object) – Converts an object of any type to a readable string format

repr(object) – Returns a string containing a printable representaBon of an object. In case of


most built-in types, returns a string that will give back the object with the same value when
passed to eval()

ConstrucBon of a container instance (Refer Python Built-in Containers.xlsx for details)

list()
tuple()
dict()
set()
range()

OperaBons on iterable (Refer Python Built-in Containers.xlsx for details)

len() – Returns the number of elements of an iterable


sum() – Finds the sum of elements of an iterable
sorted() – Returns a sorted list of elements of an iterable
max() – Returns the largest element in an iterable
min() – Returns the smallest element in an iterable

MathemaBcal funcBons

For built-in mathemaBcal funcBons, the data type of the return value will be based on the
argument data types.

abs(x) – Gives absolute value. Magnitude in case of complex


round(number, ndigits=None) – round(1245.61, -1) gives 12450.0
pow(base, exp, mod=None) – FuncBonally equivalent to base**exp % mod but more
efficient. IdenBcal to ** if mod is None.
divmod(x, y) – Calculates x//y and x%y and returns the results as a tuple (x//y, x%y).

Other miscellaneous funcBons

id(object) – Return the "idenBty" of an object. This is an integer that is guaranteed to be


unique across all objects alive at any moment and is constant for the object during its
lifeBme.
type(object) – Returns the data type of an object
eval(string) – parses and evaluates the string as a Python expression
open() – Opens a file and returns the file object
Modules
Python Modules
• A module is a logical organization of Python code.
• Modules have a namespace containing objects (functions, types, etc.) that implement the
functionality of the module.
• A module can be accessed from another module importing it.
• A Python code file (.py file) containing a set of functions can be imported as a module.
• Python provides built-in modules like math, random etc.

Importing Modules

Importing module with its name

import module-name

Example:
import math
math.fabs(2.1) # math is a name in the local scope
math.floor(2.1)

Importing module assigning a different name in the local scope

import module-name as local-name

Example:
import mysql.connector as sql # sql is a name in local scope for mysql.connector

sql.connect(......) # will work


mysql.connector.connect(......) # NameError as 'mysql' is undefined

Importing specific names from a module to the local scope

from module-name import name-list

Example:
from math import fabs, floor
fabs(2.1)
floor(2.1)
math.fabs(2.1) # will cause NameError
ceil(2.1) # will cause NameError

Importing all names from a module to the local scope

from module-name import *

Example:
from math import * #import all public names (not starting with underscore)
fabs(2.1)
floor(2.1)
Importing specific names from a module with different names in the local scope

from module-name import name1 as local-name1, name2 as local-name2, ...

Example:
from math import fabs as myabs, floor
myabs(2.1) # myabs is the name in local name for math.fabs
floor(2.1) # floor has the same name in local scope
fabs(2.1) # will cause NameError

Build-in Modules
math — Mathematical functions (as per C language standard)

FuncZon Remarks
math.fabs(x) Return absolute value. Return value is always float and does not
support complex.

Built-in funcBon abs() accepts complex and returns integer or float


based on the argument

math.floor(x) Largest integer less than or equal to x. math.floor(-10.5) will be -11.


Returns integer.

math.ceil(x) Smallest integer greater than or equal to x. math.ceil(-10.5) will be -


10. Returns integer.
math.trunc(x) Removes fracBonal part. Returns integer.

math.sqrt(x) Square root

math.pow(x, y) Converts arguments to float and does not support complex.

Built-in funcBon pow() and ** operator accepts complex and returns


integer/float/complex based on the argument

math.sin(x) Trigonometric funcBons. Arguments should be in radians.


math.cos(x) If the input is in degrees, use the below conversion.
math.tan(x)
radians = degrees * math.pi / 180

math.pi ProperBes that return the values of mathemaBcal constants.


math.e π = 3.141592…
e = 2.718281…
random — Functions to generate pseudo-random numbers

FuncZon Remarks
random.randrange(stop) Returns a random integer in the range.
random.randrange(start, stop)
random.randrange(start, stop, step)

random.randint(a, b) Return a random integer N such that


a <= N <= b.
same as randrange(a, b+1).

random.random() Returns a random floaBng-point number X such that


0.0 <= X < 1.0

Other random funcBons internally use this funcBon.

statistics — Statistical functions

FuncZon Remarks
statistics.mean(iterable) Mean

statistics.median(iterable) Median

statistics.mode(iterable) If there are mulBple modes, the one occurring first in the
data is returned.

staBsBcs.mode([1,4,4,3,2,2,5]) gives 4

statistics.multimode(iterable) Returns all modes as a list.

staBsBcs.mulBmode([1,4,4,3,2,2,5]) gives [4, 2]

User Defined Functions


Defining and Calling Functions

def funcBon-name (parameter-definiBons):


statements-block

Example:

# function definition
def midvalue(a, b):
mid = (a+b)/2
return mid

# calling the function


m = midvalue(5, 7.5)
a and b are the parameters
5 and 7.5 are the arguments

Parameters (or Formal Parameters) are names defined by a funcBon to receive values from the caller
of the funcBon.

Arguments (or Actual Parameters) are values given to the parameters at the Bme of a funcBon call.

Omi{ng the return statement or a return statement without specifying a return value is equivalent
to return None.

Returning mulBple values as a tuple

return expr1, expr2, …

is same as

return (expr1, expr2, …)

The parameters defined as above can take both posiBonal arguments and keyword arguments. With
the above definiBon, all of the below funcBon calls do the same thing.

midvalue(5, 7.5)
midvalue(a = 5, b = 7.5)
midvalue(b = 7.5, a = 5)
midvalue(5, b = 7.5)

In a funcBon call:

• A posiBonal argument cannot come aper a keyword argument.


midvalue(b=7.5, 5) will cause a SyntaxError

• A parameter cannot have both posiBonal argument and keyword argument.


midvalue(5, b=7.5, a=3) will cause a TypeError

• The order of the keyword arguments does not mater.


midvalue(a=5, b=7.5) is same as midvalue(b=7.5, a=5)
Parameters with default value

# function containing parameters with default value


def calc(a, b, c = 10, d = 20):
result = (a+b+c+d)/2
return result

• In the parameters list, parameters with default values can come only aper ALL the
parameters without default value.
calc(a, b=10, c, d=20) will cause a SyntaxError

With the above definiBon. FuncBon calls need not contain arguments for parameters c and d.

Examples:
r = calc(2, 3) # no argument for c and d
r = calc(2, 3, 4) # no argument for d
r = calc(2, 3, d=5) # no argument for c
r = calc(d=3, b=4, a=2) # no argument for c

Note: It is possible to define posiBonal-only or keyword-only parameters. It is also possible to make


the funcBon receive excess posiBonal parameters (as a tuple) and excess keyword parameters (as a
dicBonary)

Scope of Names

• Names can be defined at the module (i.e., file) level or at the funcBon level.

• A global scope holds all the names defined at a module level

• A local scope holds all the names defined at a funcZon level

• Names inside funcBons implicitly considered to be in funcBon’s local scope:


o A variable that gets assigned to within a funcBon
o Names for which del is called (treated as local but del statement is not treated as
assignment)

This default behaviour can be changed using the global statement within the funcBon

• Other names inside funcBons implicitly considered to be in funcBon’s local scope:


(a) FuncBon parameters
(b) Variables used as target in for statement
(c) Variables used as target in except clause
(d) Variables used as target in with statement
(e) Names imported with import statement
(f) Names of nested funcBons – def
(g) Names of classes defined within the funcBon – class

These names CANNOT be used in global statement to change the behaviour. CPython
implementaBon at present does not enforce any of these rules except (a). But can change in
future.

• If a variable is used inside a funcBon without assignment, the global scope variable is used if
available else it is an error.

• The process of associaBng a name to a parBcular scope is called binding.

• A name can refer to a variable, funcBon or module – all of them use the same binding
process.

global statement

global name1, name2, …

Names listed in a global statement must not textually precede the global statement (within the
funcBon).
Examples:

def fun1():
var2 = 20 #local
print(var1) #global (NameError if not defined in global scope)
print(var2) #local

def fun2():
var1 = 20 #local
print(var1) #local

def fun3():
global var1
var1 = 20 #global
print(var1) #global

def fun4():
global var1, var2
var1 = 20 #global
var2 = 30 #global
print(var1, var2) #global

var1 = 10 #global
fun1()
print(var1) # prints 10

var1 = 10 #global
fun2()
print(var1) # prints 10

var1 = 10 #global
fun3()
print(var1) # prints 20

var1 = 10 #global
fun4()
print(var1, var2) # prints 20 30

Within a funcBon code block, the global declaraBon of a name should come before the name is used.

def funA():
print(var1) # SyntaxError - use before global declaration
global var1

Note: This produces a syntax error as the check is done at compile Bme.

Deleting Names

Names can be deleted from the scope using the del statement.

def fun():
global var1, var2

print(var1) # prints 10 on both calls

print(var2) # On 1st call - prints 20


# On 2nd call - NameError

var1 = 10
var2 = 20
fun() # 1st call

del var2
fun() # 2nd call

def fun():
var1 = 10
print(var1) # prints 10
del var1
print(var1) # UnboundLocalError

The excepBon target assigned by the except clause is automaBcally deleted when the except block
completes.

excp = 10

try:
10 / 0
except Exception as excp:
print(excp) # prints division by zero

print(excp) # NameError

Data Structure
Concepts

• Data Structure is an organization of data that makes storage and operations on the
data have the required efficiency in terms of speed and memory usage.

• Examples: array, linked list, hash table, stack etc.

• The data structure used depends on the kind of operations to be performed on the
data.
o Array is suitable if the access is mostly sequential and inserts/deletes are
infrequent.
o Linked list is suitable if the inserts/deletes are frequent but it takes more
space than array.

• Primitive data types are a set of basic data types from which data structure types
are constructed.
• Primitive data types in Python.
o Numeric types (int, float, complex & bool)
o String – It is actually a sequence of characters. It is considered primitive as
there is no character data type (like in C/C++)

• Built-in data structures available in Python.


o list – sequential storage of elements (array).
o dictionary – hash table storage.
o set – implements set concept internally using hashing.
o tuple – immutable sequence.

Note: In Python, data structures that organize data in a linear order (with zero-based
index) are called sequences. String, list and tuple are sequence data types.

Stack

A stack is a linear data structure in which all the insertion and deletion of data values are
done at one end only.

• Stacks follow LIFO (Last In First Out) rule.

• Push operation – adding an element to the stack at the top.

• Pop operation – removing an element from the top.

• Overflow error – When stack has a set maximum capacity,


Overflow error occurs when the stack capacity is full and a push
operation is attempted.

• In Python, stack can be efficiently implemented using list.

Applications of Stack
• Expression Evaluation: It is used to evaluate prefix, postfix and infix expressions.
• Expression Conversion: It can be used to convert one form of expression (prefix,
postfix or infix) to one another.
• Syntax Parsing: Many compilers use a stack for parsing the syntax of expressions.
• Backtracking: It can be used for back traversal of steps in a problem solution.
• Parenthesis Checking: Stack is used to check the proper opening and closing of
parenthesis.
• String Reversal: It can be used to reverse a string.
• Function Call: Stack is used to keep information about the active functions or
subroutines.

Implementation of stack using list


def push(stack, element):
stack.append(element)

def pop(stack):
return stack.pop() # IndexError if stack is empty

def top(stack): # top() can also be called peek()


if len(stack) > 0: # same as if stack:
n = len(stack)
return stack[n-1] # same as return stack[-1]
else:
return None

def isempty(stack):
return len(stack) < 1 # same as return not stack

def size(stack):
return len(stack)

def display(stack):
n = len(stack)
for i in range(n-1,-1,-1):
print(stack[i])

File Handling
Types of Files
Test Files

• A text file is a file containing stream of ASCII or Unicode characters.


• They can use any encoding scheme like ANSI or UTF-8. Non-ASCII characters will consume
more than one byte.
• They are human readable

• End of line

o The default newline markers in Python are '\n' '\r' and '\r\n'.

o The operating system end-of-line (EOL) marker can be accessed using os.linesep. In
Windows, '\r\n' is the EOL. In Linux, '\n' is the EOL.
o Automatic newline translations in file read/write operations.
§ When writing, '\n' is translated to os.linesep
§ When reading, all the following are translated to '\n'
'\r'
'\r\n'
os.linesep

Regular Text Files


• Common extension: .txt
• Contains text that does not follow any specific format

Delimited Text Files


• Common extension: .csv
• Each line has multiple fields with fixed width or separated by spaces, commas or tabs.
• In case of Comma Separated Vector (CSV) format, comma is used as the field separator.

Text Data Files with Structure


• Commonly used extensions are .xml, .json, .html, .css
• The follow a standard schema (structure)
• Specialized software modules needed to parse them

Source Code Files


• Commonly used extensions are .py, .sql, .c
• Contain source code written in a programming language
• Compilers and interpreters read these files

Binary Files

• Contain a stream of bytes.


• Binary files store data in binary form, without encoding them into text.
• They are not human readable.

Opening Files

file_obj = open(file_path, mode, encoding=None, newline=None)

file_path can be any of the following.


• File name without path – the file will be in the current working directory
• Relative path – the path should be relative to the current working directory
• Absolute path

encoding can be specified only for text files. If it is not specified, it depends on the platform. Using
'utf-8' is recommended.
File opening modes:

Mode Description Initial Offset Position


r Read-only mode (default mode) Beginning of file

If file does not exist – FileNotFoundError

w Write-only mode. Beginning of file

If file does not exist – it is created


If file exists – it is truncated

x Write-only mode. Beginning of file

If file does not exist – it is created


If file exists – FileExistsError (same as w except for this)

a Append-only mode. All writes done at end of file End of file


irrespective of the current file pointer.

If file does not exist – it is created


If file exists – Append starts from the current end of file

• If + is added to the mode, both read and write operations are supported.

• If b is added to the mode, the file is opened in binary mode, else it is opened in text mode. t
can be added for text mode, which is anyway the default.

• The file mode should include one (and only one) of r w x a.

• The modes can be in any order: +br r+b br+ are all same.

newline argument can have any of the following values

newline argument When reading When writing


None Universal newlines mode '\n' translated to os.linesep

Line Endings: '\n' '\r' '\r\n'

Line endings translated to '\n'

'' Line Endings: '\n' '\r' '\r\n' No translation

No translation

'\n' Line Ending: newline-arg '\n' translated to newline-arg


'\r'
'\r\n' No translation
Closing Files

An opened file needs to be closed after performing the required read/write operations. File object’s
close method needs to be called to close the file.

file_obj.close()

Note: Data written by successful write operations may not be saved completely to the disk if the
program ends without calling close(). This can happen even when even the program exits
successfully. Using with statement is highly recommend.

File Pointer

Example file containing 8 bytes ABCDEFGH

Offsets à 0 1 2 3 4 5 6 7 8
A B C D E F G H

• A file pointer that stores the current offset position in the file is maintained by the file
object.

• All file read and write operations are performed at the position of the file pointer. In the
above example, if the current offset is 3, read of single byte will give D.

• The file pointer position can be retrieved using the tell() method and can be set using the
seek() method.

file_obj.tell()

Returns the file pointer offset.

file_obj.seek(relative-offset, whence)

Sets the file pointer offset and returns the new absolute position.

whence can be any one of the following (defined in os module)

• os.SEEK_SET – relative-offset is relative to the start of the file. It should be non-negative.

• os.SEEK_CUR – relative-offset is relative to the current offset position. It can be positive or


negative.

• os.SEEK_END – relative-offset is relative to end of the file. Negative relative-offset counts


backwards from end of the file. Positive relative-offset move beyond the end of file.

The following restrictions apply for text files (not for binary files)

• For SEEK_CUR and SEEK_END, only zero is supported as offset


• For SEEK_SET, only zero or offsets returned earlier by tell() are supported.
• Any other combination will have unexpected behaviour
Seeking beyond the start of the file will cause an error.
Reading and Writing Text Files

Function Description
file_obj.read(size= -1) Read and return at most size characters.

EOF condition: Empty string returned when size


argument is not zero.

file_obj.readline(size= -1) Reads and returns a single line. Newline character


(if read from file) is included in the returned string.

Return at most size characters.

EOF condition: Empty string returned when size


argument is not zero.

file_obj.readlines() Reads and returns the rest of the file as list of lines.
Newline character is included in the returned
strings.

EOF condition: Empty list returned when.

file_obj.readlines(hint=-1) Same as readlines() but limits the number of lines


based on the hint argument.

No more lines will be read if the total characters of


all lines so far exceeds hint.

EOF condition: Empty list returned when.

file_obj.write(string) Writes the string to the file.

Returns the number of characters written.

file_obj.writelines(iterable Writes the strings to the file. No newline characters


of strings) are automatically written.

Returns None
file_obj.flush() Flush the write buffers of the stream if applicable.
This does nothing for read-only and non-blocking
streams.

Returns None

Example:

names = ['John\n', 'Tom\n', 'Mary\n']

# writing
with open('names.txt', 'w') as file:
for name in names:
file.write(name)

# Reading
with open('names.txt', 'r') as file:
name = file.readline()
while name:
print(name.rstrip())
name = file.readline()

# writing with writelines


with open('names.txt', 'w') as file:
file.writelines(names)

# Reading with readlines


with open('names.txt', 'r') as file:
for name in file.readlines():
print(name.rstrip())

Reading and Writing Binary Files

All the methods shown above wok for binary as well but with the following differences:

• Deals with byte and bytes instead of character and strings. All size arguments are in bytes
instead of characters.

• Line ending is always a single '\n'. Cannot be specified as any other value.

Exceptions

• open() with 'r' mode can cause FileNotFoundError


• open() with 'x' mode can cause FileExistsError
• Other file operation errors cause OSError
• Accessing file object after closing causes ValueError

Note that EOFError is not raised. When no data is available, read operations return empty value.

with Statement

Using the with statement, file objects can be automatically cleaned up without explicitly calling
close()

with open (…) as file_obj:


# statement block performing file operations on file_obj

When the control leaves the statement block, file_obj.close() is automatically called even in case of
exceptions.

Rule of thumb: Always use with statement.


Only certain objects can be used in a with statement. Apart from file objects, MySQL connections
and cursors can also be used in a with statement.

Using objects that do not support use in with statement will cause an error.

Standard input, output and error files


The sys module defines text streams sys.stdin, sys.stdout and sys.stderr for console I/O.
Pickle Module

The pickle module implements serialization and de-serialization a Python object structure.

Serialization (or Pickling) – Conversion of an object hierarchy into a byte stream


De-serialization (or Unpickling) – Conversion of a byte stream back into an object hierarchy

Pickle does Python-specific serialization/deserialization – producer and consumer should both be


Python programs.

Function Description Exceptions


pickle.dump(obj, file_obj) The object obj is converted to binary pickle.PicklingError
stream and written to file_obj.
when an unpicklable object is
file_obj should be opened in binary encountered
mode and should be writable.

pickle.load(file_obj) Reads the binary representation EOFError on end of file


from the file and returns the
reconstituted object. pickle.UnpicklingError

file_obj should be opened in binary for unpickling problem.


mode and should be readable.
Can also raise other exceptions
including (but not limited to)
AttributeError, ImportError, and
IndexError.

Example:

import os
import pickle

students = [
('John', 88, 55, 76),
('Tom', 81, 54, 66),
('Mary', 83, 65, 71)
]

# Pickling
def writeall():
with open('student.dat', 'wb') as file:
for student in students:
pickle.dump(student, file)
# Unpickling
def readall():
with open('student.dat', 'rb') as file:
while True:
try:
student = pickle.load(file)
except EOFError:
break
print(student)

# Search
def find(name):

with open('student.dat', 'rb') as file:

found = False

while not found:


try:
student = pickle.load(file)
except EOFError:
break
found = student[0] == name

if found:
return student

# Update
def update(name, marks):

with open('student.dat', 'rb+') as file:

found = False

while not found:


offset = file.tell()
try:
student = pickle.load(file)
except EOFError:
break
found = student[0] == name

if found:
student = (student[0], marks[0], marks[1], marks[2])
file.seek(offset, os.SEEK_SET)
pickle.dump(student, file)
return student

writeall()

readall()

student = find('Tom')
print('Found:', student)
student = update('Tom', (93, 75, 81)) # Line-A
print('Updated:', student)

readall() # Line-B

The update approach shown above works because the size of the binary representation of
the updated object exactly matches the original object. If sizes do not match, the data will
get corrupted.

For example, if 81 is changed to 810 in Line-A, there will be an UnpicklingError in Line-B –


the error occurs when an attempt is made to read the object that comes next to the
updated object.

This issue does not arise when adding a new object at the end of the file (i.e., using append
file mode)

CSV module

• The csv module implements reading and writing text files containing tabular data.
• Each line in the text file corresponds to one row of data.
• Each row consists of one or more columns separated by a delimiter (comma is the default
delimiter)

Writer object

Writer object performs write operations.

Creating a writer object:

csv.writer(file_obj)

file_obj should be opened in text mode with newline='' and should be writable.

Writer methods:
• writer.writerow(iterable) – writes one row of data
• writer.writerows(iterable of iterable) – writes multiple rows of data

Note: The argument of csv.writer() can be any object that has a write() method.

Reader object

Reader object performs write operations.

Creating a reader object:

csv.reader(file_obj)

file_obj should be opened in text mode with newline='' and should be readable.
Note: The argument of csv.reader() can also be an iterator of strings.

Data is read by iterating the reader object as below.

for data in reader:


# code block to process the data

Example:

import csv

rows = [
['John', 'No 5, 1st Street', 9999999999],
['Tom', 'No 51, 21st "A" Street', 8888888888],
['Mary', 'No 15, 3rd Street']
]

# Writing CSV
def writeall():
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(rows[0])
writer.writerow(rows[1])
writer.writerow(rows[2])

# Writing CSV with writerows


def writeall2():
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)

# Reading CSV
def readall():
with open('students.csv', 'r', newline='') as file:
reader = csv.reader(file)
for row in reader:
print(row)

The csv file created will have the following contents:

John,"No 5, 1st Street",9999999999


Tom,"No 51, 21st ""A"" Street",8888888888
Mary,"No 15, 3rd Street"

Note that escape sequences are used as needed.

The reader returns each row as a lists of strings (no automatic data types conversions):

['John', 'No 5, 1st Street', '9999999999']


['Tom', 'No 51, 21st "A" Street', '8888888888']
['Mary', 'No 15, 3rd Street']
For using a delimiter other than comma, delimiter argument needs to be added when creating the
writer and the reader.

reader = csv.reader(file, delimiter=':')


writer = csv.writer(file, delimiter=':')
Built-in print() function output formats

Data Type Code Output Remarks


int print(1234) 1234 Same format always
print(-1234) -1234
float print(5.0) 5.0 See below this table for
print(5.5000) 5.5 details.
print(0.0001000001) 0.0001000001
print(0.00009999) 9.999e-05 Note: In the last three
print(1e-4) 0.0001 examples, the numbers
print(1e-5) 1e-05 we are trying to print
print(1e15) 1000000000000000.0 are different but the
print(1e16) 1e+16 output is same.
print(float("INFINITY")) inf
print(float("-INF")) -inf
print(float("NAN")) nan
print(-0.0) -0.0
print(9876543210_12345_1.0) 9876543210123452.0
print(9876543210_12345_2.0) 9876543210123452.0
print(9876543210_12345_3.0) 9876543210123452.0

complex print(5+0j) (5+0j) Real and Imaginary


print(5e15+7j) (5000000000000000+7j) part printed as floats
print(5e16+7e16j) (5e+16+7e+16j)
No .0 suffix.

bool print(5 > 4) True


print(5 < 4) False

None print(None) None

str print('abc') abc The second last line is


print('aaaaa(n)\nbb1') aaaaa(n) shown as it would
print('aaaaa(rn)\r\nbb2') bb1 appear in consoles
print('aaaaa(nr)\n\rbb3') aaaaa(rn) where '\r' overwrites
print('aaaaa(r)\rbb4') bb2 current line.
print('xyz') aaaaa(nr)
bb3 The behaviour of '\r'
bb4aa(r) may vary for different
xyz consoles.

list print([2, 3, 5+4j]) [2, 3, (5+4j)] Newline and other


print([2, 3, 'abc']) [2, 3, 'abc'] control characters are
print([2, "Tom's book"]) [2, "Tom's book"] escaped.
print([2, 'It\'s \"ok"!']) [2, 'It\'s "ok"!']
print([2, 3, 'aa\nbb']) [2, 3, 'aa\nbb'] Quotes are escaped
print([2, 3, 'aa\rbb']) [2, 3, 'aa\rbb'] when needed.
print([2, 3, 'aa\r\nbb']) [2, 3, 'aa\r\nbb']
print([2, 3, 'aa\n\rbb']) [2, 3, 'aa\n\rbb'] Single quote preferred
print([]) [] for enclosing strings.
tuple print((2,3)) (2, 3) Same as list except that
print(tuple()) () ( ) is used instead of [ ]

set print({2, 3}) {2, 3} Same as list except that


print(set()) set() { } is used instead of [ ]

set() is printed for


empty set to
distinguish from
dictionary

range print(range(5)) range(0, 5) start and stop always


print(range(0,5)) range(0, 5) printed. step printed if
print(range(0,5,1)) range(0, 5) it is not 1.
print(range(0,5,2)) range(0, 5, 2)
print(range(-5,10,-1)) range(-5, 10, -1)

dict print({'a':2, 'b':5+4j}) {'a': 2, 'b': (5+4j)}


print({'a':2, 'b':'abc'}) {'a': 2, 'b': 'abc'}
print({}) {}

dict d = {1:5, 2:6} dict_keys([1, 2])


properties print(d.keys()) dict_values([5, 6])
print(d.values()) dict_items([(1, 5), (2, 6)])
print(d.items())

Exception try: my err The message of the


objects raise Exception('my err') division by zero exception is printed.
except Exception as ex:
print(ex)

try:
10 / 0
except Exception as ex:
print(ex)

bytes print(b"A B") b'A B'


print(b"A'B") b"A'B"
print(b"A'B\"C") b'A\'B"C'
print(b"A\"B") b'A"B'
print(b"A\\B") b'A\\B'
print(b"A\t\n\rB") b'A\t\n\rB'
print(b"A\a\b\f\vB") b'A\x07\x08\x0c\x0bB'

type print(int) <class 'int'>


print(type(5)) <class 'int'>
print(type(int)) <class 'type'>
print(ZeroDivisionError) <class 'ZeroDivisionError'>
Float printing format

• For regular (i.e., non-exponent) format, .0 is suffixed if there is no fractional part. This is
done to distinguish floats from integers.

• Other rules as per CPython implementation:

• Format
o Exponent vs regular format – regular format if exponent is in the range -4 to 15
(both inclusive) else exponent formal. The same rule can be expressed as:
Regular format will
§ Not start with 0.0000
§ Not have larger than 16-digit integer part.
o Exponent will have at least 2 digits and + sign is printed for exponent.
o No trailing zeros, except for the .0 suffix to differentiate from integers.

• Significant Digits
o If the number has 15 or less significant digits, matching significant digits printed.
o If the number has more than 15 significant digits, the digits beyond the 15th
significant digit may not be printed as expected. This is because the number is
close to the precision limit of floating-point.

Note: sys.float_info.dig gives the maximum number of significant decimal digits that can be
represented in a float without loss of precision. In CPython, sys.float_info.dig is 15. This is likely to be
the case in most Python implementations as most of them use 8-byte IEEE floating-point format.

For exam purpose – Assume that the output is rounded to 16 significant digits and trailing zeros are
removed.
Example:
print(10 / 5) prints 2.0
print(1 / 625) prints 0.0016
print(20_000_000_000 / 3) prints 6666666666.666667

You might also like