Python Crash Course — Part 2
With Code Implementation…

You can find Python Crash Course — Part 1 —
This is the part 2 of Python Crash Course.
Some of the other best Series —
How to solve any System Design Question ( approach that you can take)?
100 days : Your Data Science and Machine Learning Degree Series with projects
Complete Data Visualization and Pre-processing Series with projects
Projects Videos —
All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).
Subscribe today!
System Design Case Studies — In Depth
Design Instagram
Design Messenger App
Design Twitter
Design URL Shortener
Design Dropbox
Mega Compilation : Solved System Design Case studies
Tech Newsletter —
If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :
Introduction to Functions in Python
In python, functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it, define interfaces and save a lot of time
- A function can be called multiple times to provide modularity and reusability to the python program
- We can easily track a large python program easily when it is divided into multiple functions
- There are mainly two types of functions —
User-define functions — Defined by the user to perform the specific task
Built-in functions — Functions which are pre-defined
- You can define the function using the def keyword
- Arguments is the information that’s passed into the function
- The return statement is used to return the value. A function can have only one return
- Once created, we can call the function using the function name followed by the parentheses
Example :
def my_function(parameters):
print(“My first Function”)
return expression
#calling the function
my_function()
Private Variables in Python
In python, a variable is a named location used to store or hold the value/data in the memory.
- When we create a variable inside a function, it is local by default
- We create Private Variables by using underscore _ before a named prefix
- This is quite helpful for letting subclasses override methods without breaking intraclass method calls
There are three ways private variables can be implemented :
_Single Leading Underscores
__Double Leading Underscores
__Double leading and Double trailing underscores__
- __name__ is a special built-in variable which points to the name of the current module
Example :
_program
__var
__len__
Implementation —
# Private Variable
#Single underscore (_)
class test:
def __init__(self,num):
self._num= num#_privatemethod private method
def _numfunc(self):
print("Hello")obj=test(156)
#_ attributes can be accessed as normal variables
obj._numfunc()Implementation —
# Private Variable
#Double Underscore (__)class test:
def __init__(self,num):
self.__num= num
def Print(self):
print("__num = {}".format(self.__num))obj=test(156)Implementation —
# Private Variable
#Trailing underscore(n_)class Test:
def __init__(self,c):
#To avoid clash with python keyword
self.num_= numGlobal and Non Local Variables in Python
When we create a variable inside a function, it is local by default. When we define a variable outside of a function, it is global by default
- Nonlocal variables are used in nested functions whose local scope is not defined
- Global variables are those variables which are defined and declared outside a function and we can use them inside the function or we can use global keyword to make a variable global inside a function
Example :
global variable_name
Implementation —
#Local Variabledef test():
l = "local_variable"
return lvar=test()
print(var)Output —
local_variableImplementation —
#Global Variablevar = 10def test():
var = 20
print("local variable x:", var)val=test()
print("global variable x:", var)Output —
local variable x: 20
global variable x: 10Implementation —
#Non Local Variabledef test():
var = "local_variable"def test_one():
nonlocal var
var = "non_local_variable"
print("Non Local value:", var)
test_one()
print("Outer value:", var)
test()First Class functions in Python
In python, a function in Python is First Class Function, if it supports all of the properties of a First Class object such as —
It is an instance of an Object type
Pass First Class Function as argument of some other functions
Return Functions from other function
Store Functions in lists, sets or some other data structures
Functions can be stored as variable
- In Python, a function can be assigned as variable which is used without function parentheses
- Functions are objects you can pass them as arguments to other functions
- First-class functions allow you to pass around behavior and abstract
- Closure functions — Functions are be nested and they can carry forward parent function’s state with them
Example :
def func1(func):
var = func(‘Hello World’)
print(var)
Implementation —
#Implementation 2
def outer(a): # Outer functiondef inner(b): # Inner function
return b+10
return inner(a)a = 10
var = outer(a)
print(var)Output —
20
__import__() function
In python, the inbuilt __import__() function helps to import modules in runtime
Syntax —
__import__(name, globals, locals, fromlist, level)
- name : Name of the module to import.
- globals : Dictionary of global names used to determine how to interpret the name in a package context.
- locals : Dictionary of local names names used to determine how to interpret the name in a package context.
- fromlist : The fromlist gives the names of objects or submodules that should be imported from the module given by name.
- level : level specifies whether to use absolute or relative imports.
- To import a module by name, use importlib.import_module()
Example :
_var = __import__(‘spam.ham’, globals(), locals(), [], -1)
Implementation —
#implementation
#fabs() method is defined in the math module which returns the #absolute value of a number
math_score = __import__('math', globals(), locals(), [], 0)
print(math_score.fabs(-17.4))Output —
17.4Tuple Unpacking with Python Functions
In python, tuples are immutable data types. Python offers a very powerful tuple assignment tool that maps right hand side arguments into left hand side arguments i.e mapping is known as unpacking of a tuple of values into a normal variable.
- During the unpacking of tuple, the total number of variables on the left-hand side should be equivalent to the total number of values in given tuple
- It uses a special syntax to pass optional arguments (*args) for tuple unpacking
Example :
days = (“sunday”, “monday”, “tuesday”,”wednesday”,”thursday”)
(day1, day2, *day) = days
Implementation —
def result(a, b):
return a + b
# function with normal variables
print (result(100, 200))
# A tuple is created
c = (100, 300)
# Tuple is passed
# function unpacked them
print (result(*c))Output —
300 400
Static Variables and Methods in Python
In Python, Static variables are the variables that belong to the class and not to objects.
- Static variables are shared amongst objects of the class
- Python allows providing same variable name for a class/static variable and an instance variable
One of the best article I read on Functions by
Static Method -
- In Python, Static methods are the methods which are bound to the class rather than an object of the class
- Static Methods are called using the class name and not the objects of the class
- Static methods are bound to the class
- There are two ways of defining a static method:
@staticmethod
staticmethod()
Example :
class class_name:
@staticmethod
def function_name(parameters):
print(var)
Implementation —
class test:
static_variable = 25 # Access through classprint(test.static_variable) # prints 5# Access through an instance
ins = test()
print(ins.static_variable) # still 5# Change within an instance
ins.static_variable = 14
print(ins.static_variable)
print(test.static_variable)Output —
25 25 14 25
Implementation —
# Static Method : Use @staticmethod
class sample_shape:
@staticmethod
def msgg(msg):
print(msg)
print("Triangles")
sample_shape.msgg("Welcome to sample shape class")Output —
Welcome to sample shape class
TrianglesLambda Functions in Python
In python, Lambda is used to create small anonymous functions using “lambda” keyword and can be used wherever function objects are needed.It can any number of arguments but only one expression
Syntax :
lambda argument(s): expression
- It can be used inside another function
- In python normal functions are defined using the def keyword, anonymous functions are defined using the lambda keyword
- Whenever we require a nameless function for a short period of time, we use lambda functions
Example :
var = lambda x: x * 5
Implementation —
#A lambda function that adds 10 to the number passed in as an #argument, and print the result
x = lambda a, b, c : a * b + c
print(x(5, 6, 8))Output —
38
Map and Filter Functions in Python
In python, Map allows you to process and transform the items of the iterables or collections without using a for loop.
- In Python, the map() function applies the given function to each item of a given iterable construct (i.e lists, tuples etc) and returns a map object
Syntax —
map(function, iterable)
- In python, filter() function returns an iterator when the items are filtered through a function to test if the item is true or not
Syntax —
filter(function, iterable)
where function is the to be run for each item in the iterable
iterable is the iterable to be filtered
Example :
result = map(lambda x: x+x, numbers)
var = filter(function_name, sequence)
Implementation —
#map
numbers = [1,2,3,4,5]
strings = ['s', 't', 'x', 'y', 'z']mapped_list = list(map(lambda x, y: (x, y), strings, numbers))print(mapped_list)Output —
[('s', 1), ('t', 2), ('x', 3), ('y', 4), ('z', 5)]Implementation —
#map implementation
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday']mod_days = list(map(str.swapcase, days))print(mod_days)Output —
['sUNDAY', 'mONDAY', 'tUESDAY', 'wEDNESDAY']Implementation —
#Filter functionmarks = [95, 40, 68, 95, 67, 61, 88, 23, 38, 92]def stud(score):
return score < 33fail_list = list(filter(stud, marks))
print(fail_list)Output —
[23]All the Complete System Design Series Parts —
6. Networking, How Browsers work, Content Network Delivery ( CDN)
Github —
Magic Methods in Python
In Python, Magic methods in Python are the special methods that start and end with the double underscores
- Magic methods are not meant to be invoked directly by you, but the invocation happens internally from the class once certain action is performed
- Examples for magic methods are: __new__, __repr__, __init__, __add__, __len__, __del__ etc. The __init__ method used for initialization is invoked without any call
- Use the dir() function to see the number of magic methods inherited by a class
- The advantage of using Python’s magic methods is that they provide a simple way to make objects behave like built-in types
- Magic methods can be used to emulate the behavior of built-in types of user-defined objects. Therefore, whenever you find yourself trying to manipulate a user-defined object’s output in a Python class, then use magic methods.
Example :
v = 4
v.__add__(2)
Implementation —
# __Del__ methodfrom os.path import joinclass FileObject:def __init__(self, file_path='~', file_name='test.txt'):
self.file = open(join(file_path, file_name), 'rt')def __del__(self):
self.file.close()
del self.fileImplementation —
# __repr__ methodclass String:
def __init__(self, string):
self.string = stringdef __repr__(self):
return 'Object: {}'.format(self.string)Inheritance and Polymorphism in Python
- In Python, Inheritance and Polymorphism are very powerful and important concept
- Using inheritance you can use or inherit all the data fields and methods available in the parent class
- On top of it, you can add you own methods and data fields
- Python allows multiple inheritance i.e you can inherit from multiple classes
- Inheritance provides a way to write better organized code and re-use the code
One of the best article I read on class inheritance by Erdem Isbilen
Syntax —
class ParentClass:
Body of parent class
class DerivedClass(ParentClass):
Body of derived class
- In Python, Polymorphism allows us to define methods in the child class with the same name as defined in their parent class
Example —
class X:
def sample(self):
print(“sample() method from class X”)
class Y(X):
def sample(self):
print(“sample() method from class Y”)
Implementation —
# Inheritanceclass Vehicle:def __init__(self, name, color):
self.__name = name
self.__color = colordef getColor(self):
return self.__colordef setColor(self, color):
self.__color = colordef get_Name(self):
return self.__nameclass Bike(Vehicle):def __init__(self, name, color, model):
super().__init__(name, color) # call parent class
self.__model = modeldef get_details(self):
return self.get_Name() + self.__model + " in " +
self.getColor() + " color"b_obj = Bike("Cziar", "red", "TK720")
print(b_obj.get_details())
print(b_obj.get_Name())Output —
Cziar TK720 in red color
CziarImplementation —
# Polymorphismfrom math import piclass Shape:
def __init__(self, name):
self.name = namedef area(self):
passclass Sqr(Shape):
def __init__(self, length):
super().__init__("Square")
self.length = lengthdef area(self):
return self.length**2class Circle(Shape):
def __init__(self, radius):
super().__init__("Circle")
self.radius = radiusdef area(self):
return pi*self.radius**2a = Square(6)
b = Circle(10)
print(a.area())
print(b.area())Output —
36
314.1592653589793Errors and Exception Handling in Python
In Python, an error can be a syntax error or an exception.
When the parser detects an incorrect statement, Syntax errors occur.
- Exceptions errors are raised when an external event occurs which in some way changes the normal flow of the program
- Exception error occurs whenever syntactically correct python code results in an error
- Python comes with various built-in exceptions as well as the user can create user-defined exceptions
- Garbage collection is the memory management feature i.e a process of cleaning shared computer memory
Some of python’s built in exceptions —
IndexError : When the wrong index of a list is retrieved
ImportError : When an imported module is not found
KeyError : When the key of the dictionary is not found
NameError: When the variable is not defined
MemoryError : When a program run out of memory
TypeError : When a function and operation is applied in an incorrect type
AssertionError : When assert statement fails
AttributeError : When an attribute assignment is failed
Try and Except in Python
In Python, exceptions can be handled using a try statement
- The block of code which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause
- In case no exception has occurred, the except block is skipped and program normal flow continues
- A try clause can have any number of except clauses to handle different exceptions but only one will be executed in case the exception occurs
- We can also raise exceptions using the raise keyword
- The try statement in Python can have an optional finally clause which executes regardless of the result of the try- and except blocks
Example :
try:
print(a)
except:
print(“Something went wrong”)
finally:
print(“Exit”)
Implementation —
# try, except, finallytry:
print(1 / 0)
except:
print("Error occurred")finally:
print("Exit")Output —
Error occurred
ExitUser-defined Exceptions
In Python, user can create his own error by creating a new exception class
- Exceptions need to be derived from the Exception class, either directly or indirectly
- Exceptions errors are raised when an external event occurs which in some way changes the normal flow of the program
- User defined exceptions can be implemented by raising an exception explicitly, by using assert statement or by defining custom classes for user defined exceptions
- Use assert statement to implement constraints on the program. When, the condition given in assert statement is not met, the program gives AssertionError in output
- You can raise an existing exception by using the raise keyword and the name of the exception
- To create a custom exception class and define an error message, you need to derive the errors from the Exception class directly
- When creating a module that can raise several distinct errors, a common practice is to create a base class for exceptions defined by that module, and subclass that to create specific exception classes for different error conditions, this is called Hierarchical custom exceptions
Example —
class class_name(Exception)
Implementation —
class Error(Exception):
passclass TooSmallValueError(Error):
passnumber = 100while True:
try:
num = int(input("Enter a number: "))
if num < number:
raise TooSmallValueError
break
except TooSmallValueError:
print("Value too small")Output —
Enter a number: 40
Value too smallGarbage Collection in Python
In Python, Garbage collection is the memory management feature i.e a process of cleaning shared computer memory which is currently being put to use by a running program when that program no longer needs that memory and can be used other programs
- In python, Garbage collection works automatically. Hence, python provides with good memory management and prevents the wastage of memory
- In python, forcible garbage collection can be done by calling collect() function of the gc module
- In python, when there is no reference left to the object in that case it is automatically destroyed by the Garbage collector of python and __del__() method is executed
Example :
import gc
gc.collect()
Implementation —
#manual garbage collectionimport sys, gcdef test():
list = [18, 19, 20,34,78]
list.append(list)def main():
print("Garbage Creation")
for i in range(5):
test()print("Collecting..")
n = gc.collect()
print("Unreachable objects collected by GC:", n)
print("Uncollectable garbage list:", gc.garbage)if __name__ == "__main__":
main()
sys.exit()Output —
Garbage Creation
Collecting..
Unreachable objects collected by GC: 33Python Debugger
Debugging is the process of locating and solving the errors in the program. In python, pdb which is a part of Python’s standard library is used to debug the code
- pdb module internally makes used of bdb and cmd modules
- It supports setting breakpoints and single stepping at the source line level, inspection of stack frames, source code listing etc
Syntax —
import pdb
pdb.set_trace()
- To set the breakpoints, there is a built-in function called breakpoint()
Implementation —
import pdb
def multiply(a, b):
answer = a * b
return answer
pdb.set_trace()
a = int(input("Enter first number : "))
b = int(input("Enter second number : "))
sum = multiply(a, b)Decorators in Python
In Python, a decorator is any callable Python object that is used to modify a function or a class. It takes a function, adds some functionality, and returns it.
- Decorators are a very powerful and useful tool in Python since it allows programmers to modify/control the behavior of function or class.
- In Decorators, functions are passed as an argument into another function and then called inside the wrapper function.
- Decorators are usually called before the definition of a function you want to decorate.
There are two different kinds of decorators in Python:
Function decorators
Class decorators
- When using Multiple Decorators to a single function, the decorators will be applied in the order they’ve been called
- By recalling that decorator function, we can re-use the decorator
Implementation —
#Decoratorsdef test_decorator(func):
def function_wrapper(x):
print("Before calling" + func.__name__)
res = func(x)
print(res)
print("After calling" + func.__name__)
return function_wrapper@test_decorator
def sqr(n):
return n**2
sqr(20)Output —
Before callingsqr
400
After callingsqrImplementation —
# Multiple Decoratorsdef lowercase_decorator(function):
def wrapper():
func= function()
make_lowercase = func.lower()
return make_lowercase
return wrapperdef split_string(function):
def wrapper():
func= function()
split_string =func.split()
return split_string
return wrapper@split_string
@lowercase_decoratordef test_func():
return 'MOTHER OF DRAGONS'
test_func()Output —
['mother', 'of', 'dragons']Memoization using Decorators
In Python, memoization is a technique which allows you to optimize a Python function by caching its output based on the parameters you supply to it.
- Once you memoize a function, it will only compute its output once for each set of parameters you call it with. Every call after the first will be quickly retrieved from a cache.
- If you want to speed up the parts in your program that are expensive, memoization can be a great technique to use.
One of the best article I read about Decorators by Hensle Joseph
There are three approaches to Memoization —
Using global
Using objects
Using default parameter
Using a Callable Class
Implementation —
#fibonacci series using Memoization using decoratorsdef memoization_func(t):
dict_one = {}
def h(z):
if z not in dict_one:
dict_one[z] = t(z)
return dict_one[z]
return h
@memoization_func
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)print(fib(20))Output —
6765Defaultdict
In python, a dictionary is a container that holds key-value pairs. Keys must be unique, immutable objects
- If you try to access or modify keys that don’t exist in the dictionary, it raise a KeyError and break up your code execution. To tackle this issue, Python defaultdict type, a dictionary-like class is used
- If you try to access or modify a missing key, then defaultdict will automatically create the key and generate a default value for it
- A defaultdict will never raise a KeyError
- Any key that does not exist gets the value returned by the default factory
- Hence, whenever you need a dictionary, and each element’s value should start with a default value, use a defaultdict
Syntax —
from collections import defaultdict
demo = defaultdict(int)
Implementation —
from collections import defaultdict
default_dict_var = defaultdict(list)
for i in range(10):
default_dict_var[i].append(i)
print(default_dict_var)Output —
defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4], 5: [5], 6: [6], 7: [7], 8: [8], 9: [9]})OrderedDict
In python, OrderedDict is one of the high performance container datatypes and a subclass of dict object. It maintains the order in which the keys are inserted. In case of deletion or re-insertion of the key, the order is maintained and used when creating an iterator
- It’s a dictionary subclass that remembers the order in which its contents are added
- When the value of a specified key is changed, the ordering of keys will not change for the OrderedDict
- If an item is overwritten in the OrderedDict, it’s position is maintained
- OrderedDict popitem removes the items in FIFO order
- The reversed() function can be used with OrderedDict to iterate elements in the reverse order
- OrderedDict has a move_to_end() method to efficiently reposition an element to an endpoint
Example —
from collections import OrderedDict
my_dict = {‘Sunday’: 0, ‘Monday’: 1, ‘tuesday’: 2}
# creating ordered dict
ordered_dict = OrderedDict(my_dict)
Generators in Python
In Python, Generator functions act just like regular functions with just one difference that they use the Python yield keyword instead of return . A generator function is a function that returns an iterator A generator expression is an expression that also returns an iterator
- Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop.
- A return statement terminates a function entirely but a yield statement pauses the function saving all its states and later continues from there on successive calls.
- Generator expressions can be used as the function arguments. Just like list comprehensions, generator expressions allow you to quickly create a generator object within minutes with just a few lines of code.
- The major difference between a list comprehension and a generator expression is that a list comprehension produces the entire list while the generator expression produces one item at a time as lazy evaluation. For this reason, compared to a list comprehension, a generator expression is much more memory efficient
Example —
def generator():
yield “x”
yield “y”
for i in generator():
print(i)
Implementation —
def test_sequence():
num = 0
while num<10:
yield num
num += 1
for i in test_sequence():
print(i, end=",")Output —
0,1,2,3,4,5,6,7,8,9,Implementation —
# Python generator with Loop#Reverse a string
def reverse_str(test_str):
length = len(test_str)
for i in range(length - 1, -1, -1):
yield test_str[i]
for char in reverse_str("Trojan"):
print(char,end =" ")Output —
n a j o r TImplementation —
# Generator Expression
# Initialize the list
test_list = [1, 3, 6, 10]
# list comprehension
list_comprehension = [x**3 for x in test_list]
# generator expression
test_generator = (x**3 for x in test_list)
print(list_comprehension)
print(type(test_generator))
print(tuple(test_generator))Output —
[1, 27, 216, 1000]
<class 'generator'>
(1, 27, 216, 1000)Coroutine in Python
- Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed
- Because coroutines can pause and resume execution context, they’re well suited to concurrent processing
- Coroutines are a special type of function that yield control over to the caller, but does not end its context in the process, instead maintaining it in an idle state
- Using coroutines the yield directive can also be used on the right-hand side of an = operator to signify it will accept a value at that point in time.
Example —
def func():
print(“My first Coroutine”)
while True:
var = (yield)
print(var)
coroutine = func()
next(coroutine)
Implementation —
def func():
print("My first Coroutine")
while True:
var = (yield)
print(var)
coroutine = func()
next(coroutine)Output —
My first CoroutineKeep Learning and coding :) Peace out :)
Advanced SQL Series
Day 2 : SQL Basics, Query Structure, Built In functions Conditions
Day 4 : Set Theory Operations, Stored Procedures and CASE statements in SQL
Day 6 : Subqueries, Group by, order by and Having clauses in SQL and Analytical Functions
Day 7 : Window Functions, Grouping Sets and Constraints in SQL
Day 8 : BigQuery Basics, SELECT, FROM, WHERE and Date and Extract in BigQuery
Day 9 : Common Expression Table, UNNEST Clause, SQL vs NoSQL Databases
Day 10 : Triggers, Pivot and Cursors in SQL
Day 14 : MySQL in Depth
Day 15 : PostgreSQL inDepth
Anyways, For Day 15 of 15 days of Advanced SQL, we will cover —
PostgreSQL inDepth
Github for Advanced SQL that you can follow —
All the projects, data structures, algorithms, system design, Data Science and ML, Data Engineering, MLOps and Deep Learning videos will be published on our youtube channel ( just launched).
Subscribe today!
System Design Case Studies — In Depth
Complete Data Structures and Algorithm Series
Github —





