12.1 Course Summary
12.1 Course Summary
Omkar Joshi
Department of Computer Science and Engineering Course Instructor
Indian Institute of Technology Ropar IITM Online Degree Programme
Week 1 Week 2 Week 3 Week 4 Week 5 Week 6
1. print() 1. Strings 1. while loop 1. Lists 1. Functions 1. Lists
2. Variables 2. Types of 2. for loop 2. Nested lists 2. Types of 2. Tuples
3. input() quotes and 3. Formatted 3. Obvious sort functions 3. Dictionaries
4. Data types Escape printing 4. Matrix 3. Types of 4. Sets
5. Operators characters 4. Nested loops operations function
6. Expressions 3. if-elif-else 5. break, arguments
4. Types of continue, 4. Scope of
import pass variable
statements
Course summary 1
Datatypes Operators
In Python every entity is Symbol /
an object including all Operators Name / Description
Keyword
datatypes
+ Addition
- Subtraction
Dictionary Set Boolean
Numeric Sequence * Multiplication
dict() set() bool() Arithmetic / Division
% Modulus
Integer String
// Floor division
int() str() ** Exponential
== Double equal to
Float List != Not equal to
float() list() > Greater than
Comparison
< Less than
Complex Tuple >= Greater than equal to
complex() tuple() <= Less than equal to
and Returns True if both statements are true
Logical or Returns False if one of the statements are True
Variables not Reverse the result
No need to declare variables in Python. It is created when the value is in Returns True if the value is present in the sequence
Membership
assigned to it first time. Python allows dynamic typing. Variable name not in Returns True if the value is not present in the sequence
is any sequence of characters a-z, A-Z, 0-9 and underscore but not is Returns True if both variables point to the same object
starting with numbers. Variables are case sensitive. Identity Returns True if both variable do not point to the same
is not
object
Course summary 2
Formatted printing Types of import statements
It allows programmer to take more control over printing output rather
than simply printing space separated values. 1. import math
1. f – string: It will import the math library
name = input()
print(f'Hi, {name}!') 2. from math import *
It will import the entire contents of math library
2. format():
name = input() 3. from math import pi
print('Hi, {}!'.format(name)) It will import only variable pi from math library
Course summary 3
Conditional statements
These conditional statements are used when some kind of decision if – elif – else blocks:
making is required. Comparison operators are most useful when we use If all the conditions given in if and/or elif evaluates to be False then
these conditional statements. Python executes else block.
if block: if a > b:
The goal of this statement is to check if the given condition is True or print('a is greater than b')
False. If it is True then Python will execute the following indented lines elif a < b:
of code. print('a is less than b')
else:
if a > b:
print('a and b are equal')
print('a is greater than b')
Inline if – elif – else (Ternary operator):
if – elif blocks: The above code can be written in a single line as well.
elif stands for else if. This is Python’s way of saying, if the first
condition is False then it will check the next condition.
print('a is greater than b' if a > b else
'a is less than b' if a < b else 'a and b
if a > b:
are equal')
print('a is greater than b')
elif a < b:
Nested conditional statements:
print('a is less than b')
Any of the above mentioned conditional blocks can be written inside
any other conditional blocks and such kind of structure is referred as
nested if – else.
Course summary 4
Loops
while loop: Nested loops:
It enables you to execute set of statements as long as the given Any of the above mentioned loops (while and 2 versions of for) can be
condition is True. written inside any of these loops and such kind of structure is referred
i = 0 as nested iterations/loops.
while i < 10:
print(i) Loop control statements:
i += 1 Loops are used to automate repetitive statements. But sometimes there
This code will print numbers from 0 to 9. may arise a condition where we may want to terminate the loop, skip
an iteration or ignore that condition. In such conditions we use loop
for loop: control statements like break, continue and pass respectively.
Course summary 5
In-built functions How to define a function? How to call a functions
print(): Prints argument(s) on to the console def add(x, y): result = add(10, 20)
input(): Takes input from the console as a string return x + y
type(): Returns the type of an object
len(): Returns the length of an object Types of function arguments
max(): Returns the largest value in the iterable 1. Positional arguments: Mapping of arguments and parameters
min(): Returns the smallest value in the iterable happens as per the sequence
sum(): Sums the elements in an iterator def myFunction(x, y, z):
sorted(): Returns the sorted list return x + y - z
iter(): Returns an iterator object result = myFunction(10, 20, 30)
next(): Returns the next element in the iterator object
enumerate(): Returns an enumerate object by adding counter to an 2. Keyword arguments: Mapping of arguments and parameters is
iterable given explicitly
zip(): Returns an iterator after coupling two or more iterators
def myFunction(z, y, x):
map(): Returns the iterator after applying specific function on each
element in it
return x + y - z
filter(): Returns an iterator after applying specific filtering function result = myFunction(x = 10, z = 30, y = 20)
Course summary 6
Property List Tuple Dictionary Set
Video link Video link Video link Video link
Notation [] () {‘Key’: ‘Value’} {}
Creation list() tuple() dict() set()
Mutability Mutable Immutable Mutable Mutable
Type of elements Keys: Hashable
Any Any Hashable
which can be stored Values: Any
Order of elements Ordered Ordered Unordered* Unordered
Keys: Not allowed
Duplicate elements Allowed Allowed Not allowed
Values: Allowed
Keys: Add, Delete
Operations Add, Update, Delete None Values: Add, Update, Add, Delete
Delete
Indexing, Slicing, Indexing, Slicing,
Operations Iteration Iteration
Iteration Iteration
Sorting Possible Not possible Possible Not possible
*Python 3.6 and earlier. Dictionaries are ordered as per Python 3.7 and above.
Course summary 7
Recursive function Exception handling
When a function calls itself in its definition then it is called as Errors which occur during execution of the Python program are termed
recursive function as exceptions. Exceptions are different from syntax errors. Python
provides try – except blocks in order to handle such exceptions.
def add_N_numbers(n): try:
if n == 1: print(a / b)
return n print(myDictionary['xyz'])
else: except ZeroDivisionError:
return n + add_N_numbers(n - 1) print('Denominator cannot be zero')
result = add_N_numbers(5) except KeyError:
print('This key is not there in the
dictionary')
lambda function
It is an anonymous function which can have any number of arguments
The above code is syntactically correct and will execute successfully if
but can execute only one expression.
value of variable b is non-zero and ‘xyz’ is a key in myDictionary. But
f = lambda a, b, c: a + b - c at the same time it will throw an exception if either variable b is zero or
result = f(10, 20, 5) myDictionary does not have the key ‘xyz’.
Complete list of in-built exceptions.
List comprehension finally block:
It is an optimized way to create a list in a single line.
It is a special type of code block which always executes after successful
myList = [x ** 2 for x in range(10) execution as well as after termination due to execution. Therefore it is
if x % 2 == 0] used to deallocate the system resources used in the program, e.g. file
pointers.
Course summary 8
File handling
We store our entire information on computer systems using files. Hence, How to read a file?
there has to be some way to open/read/write files using Python. There are three major functions through which we can read files.
f.read(): It reads the specified number of bytes from the file.
How to open/create a file? The default/optional argument is -1 which indicated
It is done using open() function which takes to parameters, first is file the entire file content.
name with its extension and second is mode for opening file. f.readline(): It reads the file one line at a time.
f.readlines(): It reads the entire file as a list of strings representing
f = open('abc.txt', 'r') individual lines.
Course summary 9
Object Oriented Programming
Python is an object oriented programming language. Hence, every entity How to implement inheritance?
in Python is an object with its associated attributes and methods. class Person:
def __init__(self, name, age):
How to create class? self.name = name
class myClass: self.age = age
def __init__(self): def display(self):
pass print(self.name, self.age)
How to create object?
class Student(Person):
obj = myClass()
def __init__(self, name, age, marks):
How to add attributes and methods to class? super().__init__(name, age)
class Student: self.marks = marks
def __init__(self, name, age): def display(self):
self.name = name super().display()
self.age = age print(self.marks)
def display(self):
print(self.name, self.age) s1 = Student('ABC', 10, 90)
s1.display()
s1 = Student('ABC', 10)
What are the types of inheritance?
s1.display()
Simple, Hierarchical, Multiple, Multilevel and Hybrid.
Course summary 10
Programming, Data
Transition and Structures and Algorithms
using Python
Use of Python Diploma in
Modern Application
Development – I
Programming Modern Application
Development – II
Programming concept in
Computational Programming Java
Thinking in Python Machine Learning
Foundations
Machine Learning
Diploma in Techniques
Data Science Machine Learning Practice
Course summary 11