Python Cheatsheet
Python Cheatsheet
WEEK 1
print(“Hello World”)
2. Variables are used to store values. The values can be number, text etc.
3. a = 10 #int
n = 10.3 #float
s = “Student” #str
t = True #bool
4. input() method is used to take input from user in the form of string
d = bool(0) #False
e = bool(10) #True
f = bool(-10) #True
g = bool(‘India’) #True
i = bool(‘’) #False
7. Arithmetic operators
- -> Subtraction
/ -> Division
== -> Equal
9. Logical operators
and -> Returns True if both LHS and RHS are true
10.Strings
s1 = ‘Hello’
Or
s2 = “World”
s1[0] -> H
s1[-1] -> o
12.String slicing
14.String length
len(s1) -> 5
4
5
WEEK 2
1. Comments: A section of our program that is ignored by the compiler(to increase the readability of the code).
A multiline
Comment ‘’’
2. Dynamic Typing: The type of a variable in Python is determined by the value it stores and changes with it.
-> message = 10
3. Multiple Assignments
5. Shorthand Operators
6. in operator - Tells whether something is inside/part of the other thing (similar to the English definition)
“ IT “ in “ IIT Madras “ -> True # searches for the string “IT” in “IIT Madras”
8. Escape Characters - To insert characters that are illegal in a string, use an escape character.
An escape character is a backslash \ followed by the character you want to insert.
print(‘It’s Raining’) -> Error #All Characters after second ‘ (single quote) are not considered as a part of the string (s Raining)
S = ‘ ‘ ‘ A multiline
String ‘ ‘ ‘
print(S) -> A multiline
String
10. In Python, variable names must start with a letter (a-z, A-Z) or an underscore (_), and can be followed by letters, digits (0-9), or underscores. They are case-sensitive and
cannot use Python-reserved keywords. Variable name cannot start with any digit (0-9)
Syntax:
if (condition):
Statement 1 # indentation is used to show that the Statement 1 is inside the if statement, If the condition is True, Statement 1 is executed
elif (condition):
Statement 2 # If the previous condition is False but this condition is true, Statement 2 is executed, elif block is optional
elif (condition):
Statement 3 # If the previous two conditions are False but this condition is true, Statement 3 is executed. There may be an unlimited number of elif blocks.
else:
Statement # If all the previous conditions are False, the statement under the else block is executed. Else block is optional
Example 1:
if(3<5):
print(“3 is less than 5”) -> This print statement is executed because the condition inside if is True.
Example 2:
if(3>5):
elif(5>3):
print(“5 is greater than 3”) # ’elif` has the same syntax as `if`, can follow an `if` any number of times, and runs only if its condition is true and all previous conditions are false.
else:
print(“Equal!!!”) # else doesn't have a condition and will execute if all the above statements are false
10
Flowchart:
1. import math -> Imports the entire math library and accessing it needs “math” keyword, eg math.sqrt(2)
2. from math import * -> Imports the entire math library but “math” keyword is not needed eg. sqrt(2) will work.
“*“ represents “all” , Instead, we can give one or more function names.
3. from math import sqrt -> Import only the “sqrt” function from the entire math library
4. from math import sqrt as s -> Now sqrt function is stored in the variable named “s”
WEEK 3
■ while loop
Python While Loop executes a block of statements repeatedly until a given condition is satisfied.
Syntax: Flowchart:
while condition:
Statement (s)
Note: The condition written beside the while keyword can be placed within
round brackets. It is optional in Python.
Output: 5
factorial of 5 is: 120
The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. For example, the factorial of 5 (5!) is 5×4×3×2×1=120.
This code uses a while loop to determine the factorial of a given integer by the user. If the number is not positive it shows “Invalid Input”, otherwise, it calculates the factorial. The counter, ‘i’,
initialises to 1 and the ‘result’ variable factorial to 1. The while loop continues as long as ‘i’ is less than or equal to ‘n’. Inside the loop, the factorial is updated by multiplying it with ‘i’, and then ‘i’ is
incremented by 1. This process repeats until ‘i’ exceeds ‘n’, at which point the loop exits. The computed factorial is printed by the code at the end.
12
In Python, a for loop is used to iterate over sequences (that is either a string, a list, a tuple, a dictionary, a set, or a string) or any iterable object.
Syntax: Flowchart:
# statements
Let's assume to iterate over a string and print each character on a new line.
s = "IITM"
for char in s:
print(char)
Output: I
I
T
M
General Syntax:
Other syntaxes:
Example:
n = int(input())
if (n < 0):
print("Invalid Input")
else:
factorial = 1
for i in range(1, n+1):
factorial = factorial * i
This code calculates the factorial of a non-negative integer n entered by the user. First, it checks if 'n' is less than 0; if so, it prints "Invalid Input" because the factorial is not defined for negative
numbers. If 'n' is non-negative, it initializes the variable 'factorial' to 1. The for loop [for i in range(1, n+1):] then iterates from 1 to 'n' (inclusive). In each iteration, the loop multiplies 'factorial' by the
current value of 'i', progressively calculating the factorial. Finally, the code prints the result.
14
It is used when the number of iterations is known. It is used when the number of iterations is not known.
It has a built-in loop control variable. There is no built-in loop control variable.
■ Nested loop
Nested loops mean loops inside a loop. For example, while loop inside the for loop, for loop inside the for loop, etc.
s = "python"
length = len(s)
for i in range(length):
for j in range(i+1, length):
print(s[i], s[j])
Output:
15
p y
pt
ph
po
pn
yt
yh
yo
yn
th
to
tn
ho
hn
on
■ break: The break statement, a loop control statement, in Python, terminates the current loop in which it is present..
Output: play
The for loop goes through each character in the string, and an if statement checks if the current character is 'i'. If it is, the break statement exits the loop immediately, stopping any further printing. If
the character is not 'i', it is printed.
■ continue: Continue is also a loop control statement that forces to execute the next iteration of the loop skipping the remaining statements of the loop.
s = "seeing"
for char in s:
if char == 'e':
continue
print(char, end="")
16
Output: sing
The for loop goes through each character in the string, and an if statement checks if the current character is 'e'. If it is, the continue statement skips the rest of the loop immediately. If the character
is not 'e', it is printed.
■ pass: pass statement simply does nothing. The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. Pass
statements can also be used for writing empty loops. Pass is also used for empty control statements, functions, and classes.
Example:
s = "seeing"
for char in s:
if char == 'e':
pass
print(char, end="")
Output: seeing
When the loop encounters the character 'e', the pass statement is executed, which does nothing and allows the loop to continue to the next character. The print statement prints each character on
the same line without adding a new line. As a result, the output is "seeing", including the 'e' characters, because pass does not alter the flow of the loop.
■ Infinite Loop
An infinite loop in Python is a loop that continues to execute indefinitely, or until it is explicitly stopped. Sometimes it occurs unintentionally due to improper updating loop counter.
Example:
n, i = 10, 1
while (i <= n):
print(i)
In the above example, we want to print the first 5 whole numbers but forget to increment the loop counter inside the loop. It will print an unlimited number of 1
17
■ Formatted Printing
❖ sep parameter in print(): The separator between the arguments to print() function in Python is space by default which can be modified and made to any character using the ‘sep’ parameter.
❖ end parameter in print(): By default Python‘s print() function ends with a newline(\n) which can be modified and made to any character using the ‘end’ parameter.
print("Hello", "python", end = ".", sep = ", ") Output: Hello, python.
x=5
print(f"value of x is {x}") Output: value of x is 5
print(f"value of x is {x:5d}") Output: value of x is 5
# Above print statement prints the value of the variable x with a field width of 5 characters, right-aligned. 'd' represents decimal number, 'f' represents float
pi = 22/7
print(f"value of pi is {pi}") Output: value of pi is 3.142857142857143
print(f"value of pi is {pi:.3f}") Output: value of pi is 3.143
# If we want a float number to nth decimal place we’ve to write '.nf'
print(f"value of pi is {pi:8.3f}") Output: value of pi is 3.143
x, pi = 5, 22/7
pi = 22/7
WEEK 4
List Methods: *click on method names for detailed description of each method.
Method Description
list.append(x) Adds an element to the end of the list.
Equivalent to: a[len(a):] = [x]
list.extend(itera Extend the list by appending all the elements of the iterable.
ble) Equivalent to: a[len(a):] = iterable
list.pop(i) Removes the item from index `i` and returns it. If no index
specified then -1 is taken as default value for `i`.
list.count(x) Return number of times `x` appeared in list. (0 if `x` not in list)
● in operator -> (Syntax: item in list): Returns True if item is present in list else False.
● Join Method: Concatenates elements of a list of strings to a string with a user-defined `seperator_string` in between.
Syntax: seperator_string.join(iterable)
● Tuple Methods:
● Any immutable and hashable objects can be used as elements of sets. Eg. int, tuple, string.
● Mutable objects like lists, sets and dictionaries cannot be used as elements of list.
● Note -> item = (1, 2, [3, 4], 5) Here a is a tuple but still non-hashable as it contains a list which is mutable. Hence `item` can’t be an element of a set.
Set Operations:
Methods Description
● `in` operator checks the presence of an element quickly inside a set, as set uses hashing to find values faster compared to other collections.
25
Comprehensions
Syntax:
Eg.
a = int(input()) -> a = int(input())
if a%2 == 0: if a%2 == 0: print(‘even’)
print(‘even’)
Eg.
a = int(input())
if a%2 == 0: ->
print(‘even’)
else:
print(‘odd’)
Eg.
for i in range(5): -> for i in range(5): print(i)
print(i)
Syntax:
Eg.
i = 0 i = 0
while i<5: -> while i<5: print(i); i += 1
print(i)
i += 1
27
List Comprehensions
● Mapping a list:
Syntax:
Eg. Create a new list whose elements are square of that of L1.
L = [ ]
for x in iterable:
if condition:
L.append(f(x))
else:
L.append(g(x))
Eg. Create a new list by doubling the elements at even indices and squaring the elements at odd indices.
L = [ ]
for i in range(len(L1)):
if i%2 == 0:
L.append(L1[i]**2)
else:
L.append(L1[i]*2)
Functions
■ Function in Python
A Function is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code
repeatedly for different inputs, we can do the function calls to reuse the code in it repeatedly.
Syntax:
Calling a function:
1. When the function greet() is called, the program's control transfers to the function definition.
3. The control of the program jumps to the next statement after the function call.
➢ Built-in function: These are Standard functions in Python that are available to use. Example: abs(), len(), max(), min(), etc.
➢ Library functions: The functions that can be accessed by importing a library. Eg. math.sqrt(), random.choice(), etc.
➢ User-defined function: We can create our own functions based on our requirements.
❖ Arguments: Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma.
Example:
def greet(name):
print("Hello,", name)
❖ Default Arguments: A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.
Example:
The add function is defined with two parameters: x and y, where y has a default value of 100. When add(10, 10) is called, it explicitly passes values for both x and y, resulting in x + y which
computes to 20. When add(10) is called without specifying y, the default value of y=100 is used, resulting in x + y which computes to 110.
❖ Keyword Arguments: The idea is to allow the caller to specify the argument name with values so that the caller does not need to remember the order of parameters.
❖ Positional Arguments: We can pass the value without mentioning the parameter name but at that time the positions of the passed values matter. During the function call the first value is
assigned to the first parameter and the second value is assigned to the second parameter and so on. By changing the position, or if you forget the order of the positions, function gives
unexpected result.
Example:
return x - y
The sub() function is defined to subtract y from x. Python allows function arguments to be passed by position or by keyword. In the given examples:
add(x=30, y=10) explicitly specifies the values for x and y, resulting in 30 - 10, which evaluates to 20.
add(y=10, x=30) uses keyword arguments, where the order of x and y is reversed compared to the function definition but still results in 30 - 10, also evaluating to 20 because of keyword arguments.
add(30, 10) and add(10, 30) both pass arguments by position. In the first case, the function computes 30 - 10, resulting in 20, but in the second case, the function returns 10 - 30, i.e., -20. As the
argument name is not mentioned, it considers the first value as x and the second value as y.
❖ Returning multiple values: Python functions can return multiple values using one return statement. All values that should be returned are listed after the return keyword and are separated by
commas.
WEEK 5
Dictionary in Python
■ Basics of Dictionary
● Dictionaries in Python is a data structure that stores values in key: value format.
● A dictionary can be created by placing a sequence of elements within curly {} braces, separated by a ‘comma’.
● A dictionary can also be created by the built-in function dict().
Example:
Python
# creating an empty dictionary
Dict = {}
Dict = dict()
● Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated.
● Dictionary keys are case-sensitive, the same name but different cases of Key will be treated distinctly.
Example:
Python
Example:
Python
Dict = {} # Empty Dictionary
Dict[0] = 'Hello'
Dict[1] = 2
Dict['Value_set'] = 2, 3, 4
print(Dict)
Example:
Python
Dict = {1: 'Hello', 'name': 'Python', 3: 'World'}
■ Nested Dictionary
A dictionary can be stored as the value of another dictionary.
Example:
Python
Dict = {1: 'IIT', 2: 'Madras',
3: {'A': 'Welcome', 'B': 'To', 'C': 'Python'}
}
print(Dict)
# {1: 'IIT', 2: 'Madras', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Python'}}
■ Dictionary Methods
Method Description
get(key, default = value) Returns the value of the specified key. If the key is not
present in the dictionary it returns the default value if any
default value is passed
Python
d = {1: '001', 2: '010', 3: '011'}
print(d.items())
# dict_items([(1, '001'), (2, '010'), (3, '011')])
print(d.keys())
# dict_keys([1, 2, 3])
print(d.values())
# dict_values(['001', '010', '011'])
print(d.pop(1)) # 001
print(d) # {2: '010', 3: '011'}
35
Python
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
dot_product = 0
for i in range(len(x)):
dot_product += x[i]*y[i]
print(dot_product) # 70
Let, A be a matrix of dimension R1 x C1. Here R1 represents the row of A and C1 represents the column of A. It can be denoted as A [R1 X C1].
Let A [R1 X C1] and B [R2 X C2] are matrices. The multiplication of matrices can be done if and only if C1 = R2 and the resultant matrix becomes Z [R1 X C2].
Python
def matrix_multiplication(X, Y):
# row and columns of matrix X
r1, c1 = len(X), len(X[0])
# matrix multiplication
# iterate through rows of X
for i in range(r1):
# iterate through columns of Y
for j in range(c2):
# iterate through rows of Y
for k in range(r2):
result[i][j] += X[i][k] * Y[k][j]
return result
# 3x3 matrix
X = [
[12,7,3],
[4 ,5,6],
[7 ,8,9]
]
# 3x4 matrix
Y = [
[5,8,1,2],
[6,7,3,0],
[4,5,9,1]
]
result = matrix_multiplication(X,Y)
for r in result:
print(r)
# output
# [114, 160, 60, 27]
# [74, 97, 73, 14]
# [119, 157, 112, 23]
38
Python Local variable: Local variables are initialised within a function and are unique to that function. It cannot be accessed outside of the function.
Python
def f():
s = "I love Python" # local variable
print(s)
f()
print(s) # this line throws an error as
# local variable cannot be accessed outside of the function
# Output
# I love Python
# NameError: name 's' is not defined
Python Global variables: Global variables are the ones that are defined and declared outside any function and are not specified to any function. They can be used by any part of the program.
Python
def f():
print(s)
# global scope
s = "I love Python"
f()
# Output
# I love Python
If a global variable is reinitialized inside a function. It is considered as a local variable of that function and if any modification is done on that variable, the value of the global variable will remain
unaffected. For example:
Python
def f():
s = "Hello World"
print(s)
39
# global scope
s = "I love Python"
f()
print(s)
# Output
# Hello World
# I love Python
To modify the value of the global variable we need to use the global keyword.
Python
def f():
global s
s = "Hello World"
print(s)
# global scope
s = "I love Python"
f()
print(s)
# Output
# Hello World
# Hello World
Iterator
In Python, an iterator is an object used to iterate over iterable objects such as lists, tuples, dictionaries, and sets. An object is called iterable if we can get an iterator from it or loop over it.
Python
iter_list = iter(['I', 'Love', 'Python'])
print(next(iter_list)) # I
print(next(iter_list)) # Love
print(next(iter_list)) # Python
Generators
Python has a generator that allows you to create your iterator function. A generator is somewhat of a function that returns an iterator object with a succession of values rather than a single item. A
yield statement, rather than a return statement, is used in a generator function.
The difference is that, although a return statement terminates a function completely, a yield statement pauses the function while storing all of its states and then continues from there on subsequent
calls.
Python
def power(limit):
x = 0
while x<limit:
yield x*x
yield x*x*x
x += 1
a = power(5)
print(next(a), next(a)) # 0 0
print(next(a), next(a)) # 1 1
41
print(next(a)) # 4
print(next(a)) # 8
print(next(a), next(a)) # 9 27
Lambda Functions in Python are anonymous functions, implying they don't have a name. The def keyword is needed to create a typical function in Python, as we already know.
Example:
Python
cube = lambda y: y*y*y
print("cube:", cube(5)) # cube: 125
■ enumerate() Function
The enumerate() function adds a counter to an iterable and returns it as an enumerate object (iterator with index and the value).
Example:
Python
l1 = ["eat", "sleep", "repeat"]
s1 = "code"
■ zip() Function
The zip() function takes iterables (can be zero or more), aggregates them in a tuple, and returns it.
Example:
Python
languages = ['Java', 'Python', 'JavaScript']
versions = [14, 3, 6]
■ map() Function
map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Example:
Python
def square(n):
return n*n
numbers = (1, 2, 3, 4)
result = map(square, numbers)
print(result) # Output: <map object at 0x7f722da129e8>
print(set(result)) # Output: {16, 1, 4, 9}
num1 = [1, 2, 3]
num2 = [10, 20, 40]
■ filter() Function
The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.
Example:
Python
def check_even(number):
return number % 2 == 0
# converting to list
print(list(even_numbers_iterator)) # Output: [2, 4, 6, 8, 10]
WEEK 8
■ Open a File
Python
# syntax to open a file
file = open(file_path, mode)
Mode Description
r open an existing file for a read operation. If the file does not exist, it will
throw an error.
w open an existing file for a write operation. If the file already contains
some data, then it will be overridden but if the file is not present then it
creates the file as well.
a open an existing file for append operation. It won’t override existing data.
If the file is not present then it creates the file.
r+ To read and write data into the file. This mode does not override the
existing data, but you can modify the data starting from the beginning of
the file.
w+ To write and read data. It overwrites the previous file if one exists, it will
truncate the file to zero length or create a file if it does not exist.
a+ To append and read data from the file. It won’t override existing data.
Unset
Hello
How are you?
Hope you are enjoying Python.
Python
file = open('example.txt', 'r')
# Output
'''
Hello
'''
Here you can notice there is an extra line after each of the lines. Because in the file system, there is a '\n' character that gets included when we press enter to go to the new line to write.
Example 2: Here we are going to read a file and store all the lines in a list format.
Python
file = open('example.txt', 'r')
file.close()
# output
# ['Hello\n', 'How are you?\n', 'Hope you are enjoying Python.\n']
Here you can observe the '\n' character present at the end of the lines.
Example 3: Here we are going to read one line at a time.
Python
file = open('example.txt', 'r')
print(first_line.strip())
# to get rid of that extra '\n' character strip() method is used
second_line = file.readline()
print(second_line.strip())
file.close()
# output
'''
Hello
How are you?
Hope you are enjoying Python.
'''
Example 4: we will extract a string that contains all characters in the Python file then we can use the read() method.
Python
file = open('example.txt', 'r')
print(file.read())
file.close()
# output
'''
Hello
How are you?
Hope you are enjoying Python.
'''
seek() method is used to change the position of the File Handle to a given specific position. The file handle is like a cursor, which defines where the data has to be read or written in the file.
Unset
Parameters:
The reference point is selected by the from_what argument. It accepts three values:
0: sets the reference point at the beginning of the file
1: sets the reference point at the current file position
2: sets the reference point at the end of the file
By default from_what argument is set to 0.
Python
# for this case assume the 'example.txt' contains the following line
# Code is like humor. When you have to explain it, it’s bad.
f = open("example.txt", "r")
print(f.readline())
f.close()
# Output
'''
20
When you have to explain it, it’s bad.
'''
Python
file = open('example.txt', 'w')
file.close()
write() method overrides the existing file if exists otherwise creates a new file. You can observe two lines added into a single line, if you want to add it in a different line we have to mention the '\n'
character whenever you want to break a line.
Python
file = open('example.txt', 'w')
file.close()
'''
Python
file = open('example.txt', 'w')
file.close()
Item 2 Item 3
Item 4
'''
Python
file = open('example.txt', 'a')
file.close()
'''
Hello
How are you?
Hope you are enjoying Python.
a new line will be added
'''
Caesar Cipher
Python
import string
encrypt_msg = ''
for char in s:
if char in lower_case:
encrypt_msg += lower_case[(lower_case.index(char) + shift) % 26]
elif char in upper_case:
50
return encrypt_msg
This Python code defines a function `encrypt` that performs a Caesar Cipher encryption on a given string `s` with a specified `shift` value. The function first creates lists of lowercase and uppercase
alphabet letters. It then initializes an empty string `encrypt_msg` to store the encrypted message. As it iterates through each character in the input string, it checks if the character is a lowercase or
uppercase letter. If so, it shifts the character by the specified `shift` value within the bounds of the alphabet and appends the shifted character to `encrypt_msg`. If the character is not a letter, it
appends the character as is. Finally, it returns the encrypted message. The example given shifts each letter in "Hello, King Caesar!!!" by 3 positions, resulting in "Khoor, Lqqj Fdhvdu!!!".
Python
import string
decrypt_msg = ''
for char in s:
if char in lower_case:
decrypt_msg += lower_case[(lower_case.index(char) - shift) % 26]
elif char in upper_case:
decrypt_msg += upper_case[(upper_case.index(char) - shift) % 26]
else:
decrypt_msg += char
return decrypt_msg
This Python code defines a function `decrypt` that performs a Caesar Cipher decryption on a given string `s` with a specified `shift` value. The function creates lists of lowercase and uppercase
alphabet letters and initializes an empty string `decrypt_msg` to store the decrypted message. As it iterates through each character in the input string, it checks if the character is a lowercase or
uppercase letter. If so, it shifts the character backwards by the specified `shift` value within the bounds of the alphabet and appends the shifted character to `decrypt_msg`. If the character is not a
letter, it appends the character as is. Finally, it returns the decrypted message. The example given shifts each letter in "Khoor, Nlqj Fdhvdu!!!!!!" backwards by 3 positions, resulting in "Hello, King
Caesar!!!!!!".
WEEK 9
Recursion
Process of calling a function within itself.
Illustration of recursion:
■ Fibonacci Sequence:
52
Series where each number is obtained by adding its two preceding numbers, starting with 0 followed by 1.
# recursive case
return fibo(n-1) + fibo(n-2)
def recursive_sort(L):
if L == [ ]: # Base Case
return L
# Recursive Case
return [mini] + recursive_sort(L)
Binary Search
An efficient method to search the presence of an element in a sorted array.
Working:
If the list becomes empty and we couldn’t find the element, we return `Element not found.`
if L[mid] == x:
return 'Element Found.'
elif L[mid] > x: # If `x` is less than middle element, then we'll check the left half of the list.
L = L[:mid]
elif L[mid] < x: # If `x` is greater than middle element, then we'll check the right half of the list.
L = L[mid+1:]
if not(L): # Base case for if element not in list (ie. list becomes empty).
return 'Element Not Found'
elif L[mid] > x: # Recursive Case (Checking the left part when `x` < middle element)
return recursive_binary_search(L[:mid], x)
elif L[mid] < x: # Recursive Case (Checking the right part when `x` > middle element)
return recursive_binary_search(L[mid+1:], x)
WEEK 10
Exception Handling in Python
When a Python program meets an error, it stops the execution of the rest of the program. An error in Python might be either an error in the syntax of an expression or a Python exception.
When a Python code comes across a condition it can't handle, it raises an exception. An object in Python that describes an error is called an exception.
When a Python code throws an exception, it has two options: handle the exception immediately or stop and quit. In Python, we catch exceptions and handle them using try and except code blocks.
Python
try:
# code that may cause exception
except:
# code to run when exception occurs
Example: Here we are trying to access the array element whose index is out of bound and handle the corresponding exception.
Python
a = [1, 2, 3]
try:
print ("Fourth element = %d" %(a[3]))
except:
print ("An error occurred")
# Output
'''
An error occurred
'''
As the exception is dealt here by the try…exception block, the output will be shown instead of abruptly stopping the program by showing an error message.
For each try block, there can be zero or more except blocks. Multiple except blocks allow us to handle each exception differently. Please be aware that only one handler will be run at a time.
● Generic syntax:
Python
try:
56
# statement(s)
except Exception1:
# statement(s)
except Exception2:
# statement(s)
except:
# ifnone of the above cases is matched
Example 1:
Python
try:
print(5/0)
except IndexError:
print("Index Out of Bound.")
except ZeroDivisionError:
print("Denominator cannot be 0.")
except:
print("Some other exception occurred")
# Output
'''
Denominator cannot be 0.
'''
The code attempts to divide 5 by 0, which raises a ZeroDivisionError. The try block is used to catch exceptions, and the except block handles specific errors. Since the error is a ZeroDivisionError, the
corresponding except block is triggered, printing "Denominator cannot be 0." If any other exception occurs, it will be caught by the last generic except block, but in this case, it's not needed as the
specific error has already been handled.
Example 2:
Python
try:
print(l)
except IndexError:
print("Index Out of Bound.")
except ZeroDivisionError:
print("Denominator cannot be 0.")
57
# you can write the following line instead of just writing except
# to view the error message associated with the Exception
except Exception as e:
print(e)
# Output
'''
name 'l' is not defined
'''
The code tries to print the value of the variable l, which is not defined, leading to a NameError. The try block is used to handle exceptions, and the generic except Exception as e block catches the
NameError, printing the error message associated with it. The other specific except blocks for IndexError and ZeroDivisionError are not triggered since they don't match the raised exception.
Example 1:
Python
try:
print(5//0)
except ZeroDivisionError:
print("Can't divide by zero")
else:
print("This line is only gets executed when there is no exception")
finally:
print('This line is always executed')
# Output
'''
Can't divide by zero
This is always executed
'''
58
The code attempts to perform integer division by zero (5//0), which raises a ZeroDivisionError. The except block catches this error and prints "Can't divide by zero." The else block, which would
execute if no exception occurred, is skipped. The finally block is then executed regardless of whether an exception was raised, printing "This line is always executed."
Example 2:
Python
try:
print(5//2)
except ZeroDivisionError:
print("Can't divide by zero")
else:
print("This line is only gets executed when there is no exception")
finally:
print('This line is always executed')
# Output
'''
2
This line is only gets executed when there is no exception
This is always executed
'''
The code performs integer division ('5//2'), which successfully results in '2'. Since no exception is raised, the 'else' block executes, printing "This line is only gets executed when there is no exception."
Finally, the 'finally' block executes, printing "This line is always executed," ensuring that it runs regardless of the outcome in the 'try' block.
In Python, there are several built-in Python exceptions that can be raised when an error occurs during the execution of a program. Here are some of the most common types of exceptions in Python:
● SyntaxError: This exception is raised when the interpreter encounters a syntax error in the code, such as a misspelled keyword, a missing colon, or an unbalanced parenthesis.
● TypeError: This exception is raised when an operation or function is applied to an object of the wrong type, such as adding a string to an integer.
● NameError: This exception is raised when a variable or function name is not found in the current scope.
● IndexError: This exception is raised when an index is out of range for a list, tuple, or other sequence types.
● KeyError: This exception is raised when a key is not found in a dictionary.
● ValueError: This exception is raised when a function or method is called with an invalid argument or input, such as trying to convert a string to an integer when the string does not represent a
valid integer.
● AttributeError: This exception is raised when an attribute or method is not found on an object, such as trying to access a non-existent attribute of a class instance.
● IOError: This exception is raised when an I/O operation, such as reading or writing a file, fails due to an input/output error.
59
● ZeroDivisionError: This exception is raised when an attempt is made to divide a number by zero.
● ImportError: This exception is raised when an import statement fails to find or load a module.
● FileNotFoundError: This exception is raised when the given file is not present in the current directory.
❖ Raising Exception
The raise statement allows the programmer to force a specific exception to occur. It specifies the exception that should be raised.
Example:
Python
x = 5
if x > 3:
raise Exception("Value is greater than 3")
# Output
'''
Value is greater than 3
'''
The code checks if the variable x is greater than 3. Since x is 5, which satisfies the condition, an Exception is manually raised with the message "Value is greater than 3," terminating the program with
this error message.
60
A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. The class creates a user-defined data structure,
which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.
# creating an object
obj1 = ClassName()
obj2 = ClassName()
❖ Access attributes of Objects: We use the ‘.’ (dot) notation to access the attributes of a class.
❖ Methods: Objects can also contain methods. Methods in objects are functions that belong to the object.
❖ __init__() method: This method is known as the constructor or object initializer because it defines and sets the initial values for the object’s attributes. It is executed at the time of Object
creation. It runs as soon as an object of a class is instantiated.
Example:
Python
class Person:
# constructor
def __init__(self, name, age):
self.name = name
self.age = age
# methods
def display(self):
print(self.name, self.age)
# Output
'''
John
John 36
'''
The code defines a Person class with a constructor (__init__) that initializes name and age attributes when an object is created. The class also has a display method that prints the name and age of
the person. An object p1 is created with the name "John" and age 36. The name attribute of p1 is accessed and printed, resulting in "John". Then, the display method is called, which prints "John 36".
❖ self parameter: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class. It does not have to be named self , you can call it
whatever you like, but it has to be the first parameter of any function in the class.
❖ __str__() Method: The __str__() function controls what should be returned when the class object is represented as a string. If the __str__() function is not set, the string representation of the
object is returned.
Python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"Name: {self.name}, age: {self.age}"
p1 = Person("John", 36)
print(p1)
# Output
'''
Name: John, age: 36
'''
The code defines a 'Person' class with a constructor ('__init__') that initializes the 'name' and 'age' attributes. It also defines a '__str__' method, which returns a formatted string when an instance of
'Person' is printed. When the object 'p1' is created with the name "John" and age 36, and 'print(p1)' is called, the '__str__' method is invoked, resulting in the output "Name: John, age: 36."
62
Instance variables are for data, unique to each instance and class variables are for attributes and methods shared by all instances of the class. Instance variables are variables whose value is
assigned inside a constructor or method with self, whereas class variables are variables whose value is assigned in the class.
Example:
Python
class Student:
# class variable
count = 0
def __str__(self):
return f"Name: {self.name}, age: {self.age}"
s1 = Student("John", 18)
print(s1)
print("Student count:", Student.count)
s2 = Student("Smith", 19)
s3 = Student("Alex", 17)
print(s2)
print(s3)
print("Student count:", Student.count)
# output
'''
Name: John, age: 18
Student count: 1
Name: Smith, age: 19
Name: Alex, age: 17
Student count: 3
'''
In the 'Student' class example, 'count' is a class variable shared by all instances of the class, used to track the total number of 'Student' objects created. Each time a new 'Student' instance is
initialized, 'count' is incremented, reflecting the total number of students. In contrast, 'name' and 'age' are instance variables specific to each individual 'Student' object, storing unique attributes for
each instance. While 'count' maintains a global state across all instances, 'name' and 'age' hold data specific to the object they belong to.
63
Inheritance in Python
● Inheritance allows you to inherit the properties of a class, i.e., parent class to another, i.e., child class.
● It provides the reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
● It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
❖ General Syntax
Python
Class ParentClass:
{Body}
Class ChildClass(BaseClass):
{Body}
Example:
Python
class Person():
def Display(self):
print("From Person Class")
class Employee(Person):
def Print(self):
print("From Employee Class")
per = Person()
per.Display()
emp = Employee()
emp.Print()
emp.Display()
# output
'''
From Person Class
From Employee Class
From Person Class
'''
The code demonstrates inheritance in Python. The 'Person' class has a method 'Display' that prints "From Person Class." The 'Employee' class inherits from 'Person' and adds its own method 'Print'
that prints "From Employee Class." An instance 'per' of 'Person' is created and calls the 'Display' method. Then, an instance 'emp' of 'Employee' is created, which calls both its own 'Print' method and
the inherited 'Display' method from 'Person', demonstrating how 'Employee' can access methods from its parent class. The output reflects these method calls.
64
❖ Method Overriding: Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is
already provided by one of its superclasses or parent classes. When a method in a subclass has the same name, the same parameters or signature, and the same return type (or sub-type) as a
method in its superclass, then the method in the subclass is said to override the method in the superclass.
❖ super() function: The super() function is a built-in function that returns the objects that represent the parent class. It allows to access the parent class’s methods and attributes in the child
class.
Syntax:
Python
# use parent class attribute
super().attributeName
# use parent class method
super().methodName1() # without parameter
super().methodName2(parameters) # with parameter
## Alternate way
# use parent class attribute
ParentClassName.attributeName
# use parent class method
ParentClassName.methodName1(self) # without parameter
ParentClassName.methodName2(self, parameters) # with parameter
Example 1:
Python
class Person():
def Display(self):
print("From Person Class")
class Employee(Person):
def Display(self):
print("From Employee Class")
per = Person()
per.Display()
emp = Employee()
emp.Display()
# output
'''
From Person Class
From Employee Class
'''
65
In this example, the 'Employee' class inherits from the 'Person' class. Both classes have a 'Display' method, but the 'Employee' class overrides the 'Display' method from 'Person'. When 'per.Display()'
is called on a 'Person' object, it executes the 'Display' method from 'Person', printing "From Person Class." When 'emp.Display()' is called on an 'Employee' object, it executes the overridden 'Display'
method in 'Employee', printing "From Employee Class." This demonstrates that the subclass method takes precedence when called on an instance of the subclass.
Example 2:
Python
class Person():
def Display(self):
print("From Person Class")
class Employee(Person):
def Display(self):
print("From Employee Class")
super().Display()
# Person.Display(self) # alternate way
per = Person()
per.Display()
emp = Employee()
emp.Display()
# output
'''
From Person Class
From Employee Class
From Person Class
'''
In the example, the 'Employee' class overrides the 'Display' method of the 'Person' class with its own version. When 'emp.Display()' is called, it first prints "From Employee Class" from the overridden
method, then uses 'super().Display()' to call the 'Display' method from the 'Person' class, which prints "From Person Class." This allows 'Employee' to extend the functionality of 'Person' while still
using the parent class's method.
❖ __init__() function:
When the __init__() function is added in the child class, the child class will no longer inherit the parent's __init__() function. To keep the inheritance of the parent's __init__() function, add a call to the
parent's __init__() function.
Example:
Python
class Person():
def __init__(self, name, id):
66
self.name = name
self.id = id
def Display(self):
print("Name:", self.name, "ID:", self.id)
class Employee(Person):
def __init__(self, name, id, salary):
super().__init__(name, id)
self.salary = salary
def Display(self):
print("Name:", self.name, "ID:", self.id, "Salary:", self.salary)
# output
'''
Name: Dan ID: 123 Age: Salary: 10000
Name: Dan ID: 123 Age: Salary: 15000
'''
The code demonstrates inheritance and method overriding, with 'Employee' as a subclass of 'Person'. The 'Person' class has an '__init__' method to initialize 'name' and 'id', and a 'Display' method to
print them. The 'Employee' class overrides the '__init__' method to include 'salary' and uses 'super().__init__(name, id)' to call the parent class's constructor. It also overrides the 'Display' method to
print 'name', 'id', and 'salary'. When an 'Employee' object 'emp' is created and its 'Display' method is called, it prints the initial salary. After updating 'emp.salary', calling 'Display' again reflects the
updated salary in the output.
In definition, private variables would be those that can only be seen and accessed by members of the class to which they belong, not by members of any other class. When the programme runs,
these variables are utilized to access the values to keep the information secret from other classes. Even the object of that class cannot access this private variable directly. It is only accessible via
any method. In Python, it is accomplished using two underscores before the name of the attributes or the method.
Example 1:
Python
class Person():
67
def Display(self):
print("Name:", self.name, "ID:", self.__id)
# output
'''
Name: Nobita ID: 1
Name: Nobita ID: 1
Name: Nobita ID: 3
'''
Example 1:
Python
class Person():
68
def Display(self):
print("Name:", self.name, "ID:", self.__id)
def showID(self):
return self.__id
class Employee(Person):
def __init__(self, name, id, salary):
super().__init__(name, id)
self.salary = salary
def Display(self):
print("Name:", self.name)
print("Salary:", self.salary)
# output
'''
Name: Nobita ID: 1
Name: Nobita ID: 1
Name: Nobita ID: 3
'''
❖ Types of Inheritance
Python
class Parent:
pass
class Child(Parent):
pass
2. Multilevel Inheritance: A child class inherits from a parent class, which in turn inherits from another parent class.
Syntax:
Python
class Grandparent:
pass
class Parent(Grandparent):
pass
class Child(Parent):
pass
3. Hierarchical Inheritance: Multiple child classes inherit from the same parent class.
Syntax:
Python
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
pass
4. Multiple Inheritance: A child class inherits from more than one parent class.
Syntax:
Python
class Parent1:
pass
class Parent2:
pass
70
Python
class Parent1:
pass
class Parent2:
pass
class GrandChild1(Child):
pass
class GrandChild2(Child):
pass