Python Cheatsheet
Python Cheatsheet
Quick Reference
WEEK 1
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
5. type() method is used to get datatype of a variable or value
7. Arithmetic operators
+ -> Addition (int or float) , Concatenation (str)
- -> Subtraction
* -> Multiplication (int or float) , Repetition (str)
/ -> Division
// -> Floor Division (Integer division)
% -> Modulus operator (remainder after division)
** -> Exponentiation (power)
9. Logical operators
and -> Returns True if both LHS and RHS are true
or -> Returns False if both LHS and RHS are false
not -> Returns negation of given operand
10.Strings
s1 = ‘Hello’
Or
s2 = “World”
12.String slicing
s1[0:3] -> Hel
14.String length
len() method gives length of a string.
len(s1) -> 5
WEEK 2
-> message = 10
3. Multiple Assignments
Assign multiple values or the same value to multiple variables
5. Shorthand Operators
“ IT “ in “ IIT Madras “ -> True # searches for the string “IT” in “IIT Madras”
“ ads “ in “ IIT Madras “ -> False
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)
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):
print(“3 is less than 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
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:
# body of while loop
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.
Syntax: Flowchart:
for var in iterable:
# 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:
for var in range(start, stop, step)
Other syntaxes:
for var in range(start, stop)
Example:
n = int(input())
if (n < 0):
print("Invalid Input")
else:
factorial = 1
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.
■ Ideal loop option based on problem statement
■ Nested loop
Nested loops mean loops inside a loop. For example, while loop inside the for loop, for loop
inside the for loop, etc.
Example: Possible unique pair in a string
s = "python"
length = len(s)
for i in range(length):
for j in range(i+1, length):
print(s[i], s[j])
Output:
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="")
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
■ 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.
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.insert(i, x) Insert item `x` at index `i`.
list.extend(iterable) Extend the list by appending all the elements
of the iterable.
Equivalent to: a[len(a):] = iterable
list.remove(x) Removes first occurrence of element `x`
from the list.
Raises ValueError if `x` not in list.
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.clear() Removes all elements from list.
Equivalent to: del a[:]
list.copy() Return a shallow copy of the list.
Equivalent to: a[:]
list.index(x) Return the index of first occurrence of `x`.
Raises ValueError if `x` not in list.
list.count(x) Return number of times `x` appeared in list.
(0 if `x` not in list)
list.sort(reverse = True/False) Sort items of list in place, both numerically
and alphabetically.
● Descending order if `reverse = True`
● Ascending order if `reverse = False`
(default parameter)
list.reverse() Reverse the items of list in place.
Popular List Functions:
equal.
[1, 2, 3] == [1, 2, 3] -> True
[1, 2] == [1, 2, 3, 4] -> False
● in operator -> (Syntax: item in list): Returns True if item is present in list else False.
Syntax: seperator_string.join(iterable)
● Tuple Methods:
Method Description Example
● 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:
● Let’s take set1 = {1, 2, 3, 4} and set2 = {1, 3, 5}
Operation Expression Example
Union set1 | set2 {1, 2, 3, 4, 5}
Intersection set1 & set2 {1, 3}
Set Difference set1 - set2 {2, 4}
Symmetric Difference set1 ^ set2 {2, 4, 5}
Subset set1 <= set2 False
(Is set1 subset of set2?) #eg. {1, 2, 3} <= {1, 2, 3} : True
● Few Functions Used With Sets: len, sum, max, min, sorted
● `in` operator checks the presence of an element quickly inside a set, as set uses
hashing to find values faster compared to other collections.
Quick Summary of Collections
Comprehensions
● Using a single line `if` statement:
Syntax:
if condition: -> if condition: block
block
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.
i = 0 i = 0
while i<5: -> while i<5: print(i); i += 1
p ( )
print(i)
i += 1
List Comprehensions
● Mapping a list:
Syntax:
L = [ ] -> L = [f(x) for x in iterable]
for x in iterable:
L.append(f(x))
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.
❖ 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)
Example:
❖ 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:
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.
Example:
● Dictionary keys are case-sensitive, the same name but different cases of Key will be
treated distinctly.
Example:
● While adding a value, if the key-value already exists, the value gets updated
otherwise a new Key with the value is added to the Dictionary.
● Updating an existing value in a Dictionary can be done by using the built-in update()
method. Nested key values can also be added to an existing Dictionary.
Example:
Dict[0] = 'Hello'
Dict[1] = 2
Dict['Value_set'] = 2, 3, 4
print(Dict)
● The items of the dictionary can be deleted by using the del keyword.
Example:
■ Nested Dictionary
A dictionary can be stored as the value of another dictionary.
Example:
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'}
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].
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]
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.
def f():
print(s)
# global scope
s = "I love Python"
f()
# Output
# I love Python
# 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.
def f():
global s
s = "Hello World"
print(s)
# global scope
s = "I love Python"
f()
print(s)
# Output
# Hello World
# Hello World
over it.
● next() function is used to call the next element in the iterable object.
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.
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
print(next(a)) # 4
print(next(a)) # 8
print(next(a), next(a)) # 9 27
■ Python Lambda Function
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:
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).
● start (optional) - enumerate() starts counting from this number. If start is omitted, 0
is taken as start.
Example:
■ zip() Function
The zip() function takes iterables (can be zero or more), aggregates them in a tuple, and
returns it.
Example:
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:
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]
Example:
def check_even(number):
return number % 2 == 0
# converting to list
print(list(even_numbers_iterator)) # Output: [2, 4, 6, 8, 10]
WEEK 8
# 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.
file = open('example.txt', 'r')
# readlines() method reads all the lines of the file and
# returns in a list format where every line is an item of the list
l = file.readlines()
print(l)
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.
file = open('example.txt', 'r')
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.
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.
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.
# 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.
'''
■ Working in Write Mode
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.
file = open('example.txt', 'w')
file.close()
'''
file.close()
file.close()
'''
Hello
How are you?
Hope you are enjoying Python.
a new line will be added
'''
Caesar Cipher
■ Encrypt the message
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:
encrypt_msg += upper_case[(upper_case.index(char) + shift) % 26]
else:
encrypt_msg += char
return encrypt_msg
print(encrypt("Hello, King Caesar!!!", 3))
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!!!".
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:
Series where each number is obtained by adding its two
preceding numbers, starting with 0 followed by 1.
Recursive code for getting nth term of fibonacci series:
def fibo(n):
if n == 0: # base case
return 0
if n == 1: # base case
return 1
# recursive case
return fibo(n-1) + fibo(n-2)
# Recursive Case
return [mini] + recursive_sort(L)
Binary Search
An efficient method to search the presence of an element in a
sorted array.
Working:
1. Compare your target `x` with middle element.
2. If `x` == middle element we return `Element found`.
3. Else if it is less than middle element, then `x` can only lie
in the left(smaller) half subarray. So, we repeat the
algorithm again in the left part.
4. Else if it is greater than middle element, we repeat the
algorithm in the right part.
If the list becomes empty and we couldn’t find the element, we
return `Element not found.`
An Illustration to check presence of `8` in a sorted list:
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:]
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)
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.
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.
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:
try:
# statement(s)
except Exception1:
# statement(s)
except Exception2:
# statement(s)
except:
# ifnone of the above cases is matched
Example 1:
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:
try:
print(l)
except IndexError:
print("Index Out of Bound.")
except ZeroDivisionError:
print("Denominator cannot be 0.")
# 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.
❖ There are two optional blocks, i.e., the else block and finally block, associated with
the try block.
❖ The else comes after the except block(s). Only when the try clause fails to throw an
exception does the code go on to the else block. The finally block comes at the end.
❖ The final block always executes after the normal termination of the try block or after
the try block terminates due to some exception.
Example 1:
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
'''
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:
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.
● IndexError: This exception is raised when an index is out of range for a list, tuple, or
other sequence types.
● 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.
● IOError: This exception is raised when an I/O operation, such as reading or writing a
file, fails due to an input/output error.
● 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:
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.
Class & Object in Python
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.
Example:
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.
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."
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:
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.
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
Class ParentClass:
{Body}
Class ChildClass(BaseClass):
{Body}
Example:
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.
❖ 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:
# 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:
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
'''
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:
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:
class Person():
def __init__(self, name, id):
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:
class Person():
def __init__(self, name, id):
self.name = name
self.__id = id # making the variable private
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:
class Person():
def __init__(self, name, id):
self.name = name
self.__id = id # making the variable private
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)
❖ Types of Inheritance
class Child(Parent):
pass
2. Multilevel Inheritance: A child class inherits from a parent class, which in turn inherits
from another parent class.
Syntax:
class Grandparent:
pass
class Parent(Grandparent):
pass
class Child(Parent):
pass
3. Hierarchical Inheritance: Multiple child classes inherit from the same parent class.
Syntax:
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:
class Parent1:
pass
class Parent2:
pass
class Parent2:
pass
class GrandChild1(Child):
pass
class GrandChild2(Child):
pass