Python Crash Course — Part 1
With Code Implementation…

Intro to Python
Python is a high-level, most widely used multi-purpose, easy to read programming language.
It is —
- Interpreted − Python is processed at runtime by the interpreter. i.e you do not need to compile your program before executing it
- Interactive − You can interact with the interpreter directly to write your programs
- Portable — It can run on wide range of hardware Platforms
- Object-Oriented − Python supports Object-Oriented style and Procedural Paradigms
- Used as a scripting language or can be compiled to byte-code for building large applications
- It has simple structure, and a clearly defined syntax
- Known as Beginner’s Language − It’s a great language for the beginner-level programmers
- Source code is comparatively easy-to-maintain
- Provides very high-level dynamic data types and supports dynamic type checking.
- Has a huge collection of standard library
Popular applications for Python:
- Web Development
- Data Science — including machine learning, data analysis, and data visualization
- Scripting
- Game Development
- CAD Applications
- Embedded Applications
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!
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 :
Python Data Types
Data Types represent the kind of value variables can hold and what all operations can be performed on a particular data.

Implementation —
#Int data Typevar = 5
print(var)
print(type(var))Output —
5
<class 'int'>Implementation —
#Float data typevar2 = 0.1234
print(var2)
print(type(var2))Output —
0.1234
<class 'float'>Implementation —
#Complex numbers data typex = 2j
print(x)
print(type(x))Output —
2j
<class 'complex'>Implementation —
#String Data Typestr_one = "Hello World"
print(str_one)
print(type(str_one))Output —
Hello World
<class 'str'>Implementation —
#list Data typelist1 = ["car","bike"]
print(list1)
print(type(list1))Output —
['car', 'bike']
<class 'list'>Implementation —
#Tuple Data Typetup= ("car","bike","bus")
print(tup)
print(type(tup))Output —
('car', 'bike', 'bus')
<class 'tuple'>Implementation —
#Dictionary Data typedict_one = {"Name": "Steve", "Location": "NewYork"}
print(dict_one)
print(type(dict_one))Output —
{'Name': 'Steve', 'Location': 'NewYork'}
<class 'dict'>Implementation —
#Set Data typeset_one = set({"Hello","world","Hello"})
print(set_one)
print(type(set_one))Output —
{'world', 'Hello'}
<class 'set'>Implementation —
#Frozen Setf_one = frozenset({"Hello","World","Hello"})
print(f_one)
print(type(f_one))Output —
frozenset({'Hello', 'World'})
<class 'frozenset'>Implementation —
#Boolean Data Typeb = True
print(b)
print(type(b))Output —
True
<class 'bool'>Implementation —
#Byte Data Typebyte_one = b"World"
print(type(byte_one))
print(byte_one)Output —
<class 'bytes'>
b'World'Python Strings
Strings are arrays of bytes representing unicode characters.
- Strings in python are surrounded by either single quotation marks, or double quotation marks.
‘today’ is the same as “today”.
Example :
var1 = ‘Hello’
var2 = “Complete Python Course”
- You can assign a multiline string to a variable by using three quotes.
Implementation —
#String
str1 = "Welcome to complete Python Course"
str2 = 'Welcome to the complete Python Course'
str3 = """This is a
multiline
String"""Output —
Welcome to complete Python Course
Welcome to the complete Python Course
This is a
multiline
StringIndexing and Slicing with Strings —
- In python, Indexing is used to access individual characters of a string
- Square brackets are used to access the character of the string using Index
- Index starts with 0
- -1 refers to the last character, -2 refers to the second last character and so on
Example:
var[3]
var[-2]
- In python, Slicing is used to access a range of characters in the string
- Slicing operator colon (:) is used
Example:
var[1:4]
var[:4]
Implementation —
# Indexing : String starts with 0th Indexs= "Captain America"
print(s[4])Output —
aImplementation —
#Slicing : slice(start, stop, step), it returns a sliced object containing elements in the given ranges= "Captain America"
print(s[1:5:2])Output —
atImplementation —
#Reverseprint(s[::-1])Output —
aciremA niatpaCString Methods
- istitle() : Checks for Titlecased String
- isupper() : returns if all characters are uppercase characters
- join() : Returns a Concatenated String
- ljust() : returns left-justified string of given width
- lower() : returns lowercased string
- lstrip() : Removes Leading Characters
- maketrans() : returns a translation table
- replace() : Replaces Substring Inside
- rfind() : Returns the Highest Index of Substring
- rjust() : returns right-justified string of given width
- rstrip() : Removes Trailing Characters
- split() : Splits String from Left
- splitlines() : Splits String at Line Boundaries
- startswith() : Checks if String Starts with the Specified String
- strip() : Removes Both Leading and Trailing Characters
- swapcase() : swap uppercase characters to lowercase; vice versa
- title() : Returns a Title Cased String
- translate() : returns mapped charactered string
- upper() : returns uppercased string
- zfill() : Returns a Copy of The String Padded With Zeros
- capitalize() : Converts first character to Capital Letter
- casefold() : converts to case folded strings
- center(): Pads string with specified character
- count() : returns occurrences of substring in string
- encode() : returns encoded string of given string
- endswith(): Checks if String Ends with the Specified Suffix
- expandtabs() : Replaces Tab character With Spaces
- find(): Returns the index of first occurrence of substring
- format(): formats string into nicer output
- format_map() : Formats the String Using Dictionary
- index(): Returns Index of Substring
- isalnum(): Checks Alphanumeric Character
- isalpha() : Checks if All Characters are Alphabets
- isdecimal() : Checks Decimal Characters
- isdigit() : Checks Digit Characters
- isidentifier() : Checks for Valid Identifier
- islower() : Checks if all Alphabets in a String are Lowercase
- isnumeric() : Checks Numeric Characters
- isprintable() : Checks Printable Character
- isspace() : Checks Whitespace Characters
Implementation —
#capitalize() method : Returns a copy of the string with its first #character capitalized and the rest lowercaseda = "complete python course"
print(a.capitalize())Output —
Complete python courseImplementation —
#centre(width[, fillchar]) : Returns the string centered in a #string of length widtha = "Python"
b = a.center(10, "*")
print(b)Output —
**Python**Implementation —
# casefold() method : Returns a casefolded copy of the string. #Casefolded strings may be used for caseless matchinga = "PYTHON"
print(a.casefold())Output —
pythonImplementation —
# count(sub[, start[, end]]) : Returns the number of non-overlapping #occurrences of substring (sub) in the range [start, end]a = "Welcome to complete Python Course"print(a.count("c"))
print(a.count("o"))
print(a.count("Python"))Output —
2 5 1
Implementation —
# endswith(suffix[, start[, end]]) : Returns True if the string ends #with the specified suffix, otherwise it returns Falsea = "Watermelon"
print(a.endswith("s"))
print(a.endswith("melon"))Output —
False
TrueImplementation —
# find(sub[, start[, end]]) : Returns the lowest index in the string # where substring sub is found within the slice s[start:end]a = "Exercise"
print(a.find("r"))
print(a.find("e"))Output —
3 2
Implementation —
# index(sub[, start[, end]]) : Similar to find function, except that # it raises a ValueError when the substring is not founda = "Continent"
print(a.index("i"))
print(a.index("C"))
print(a.index("nent"))Output —
4 0 5
Implementation —
# isalnum() : Returns True if all characters in the string are #alphanumeric, else returns Falsec = "456"
d = "$*%!!**"
print(c.isalnum())
print(d.isalnum())Output —
True
FalseImplementation —
# isalpha() : Returns True if all characters in the string are #alphabetic, else returns Falsec = "456"
d = "Python"
print(c.isalpha())
print(d.isalpha())Output —
False
TrueImplementation —
# isdecimal() : Returns True if all characters in the string are #decimal characters, else returns Falsec = u"\u00B10"
x = "10"
print(c.isdecimal())
print(x.isdecimal())Output —
False
TrueImplementation —
# isdigit() : Returns True if all characters in the string are #digits, else returns Falsec = "4567"
d = "1.65"
print(c.isdigit())
print(d.isdigit())Output —
True
FalseImplementation —
# join(iterable) : Returns a string which is the concatenation of #the strings in iterable.
# A TypeError will be raised if there are any non-string values in #iterablea = ","
print(a.join("CD"))Output —
C,DImplementation —
# partition(sep) : Splits the string at the first occurrence of sep, #and returns a 3-tuple containing the part before the separator, the #separator itself, and the part after the separatora = "Complete.Python-course"print(a.partition("-"))
print(a.partition("."))Output —
('Complete.Python', '-', 'course')
('Complete', '.', 'Python-course')Implementation —
# split(sep=None, maxsplit=-1) : Returns a list of the words in the #string,using sep as the delimiter strip.If maxsplit is given,at #most maxsplit splits are done.If maxsplit is not specified or -1, #then there is no limit on the number of splits.a = "Welcome,,Friends,"
print(a.split(","))Output —
['Welcome', '', 'Friends', '']Implementation —
# strip([chars]) : Returns a copy of the string with leading and #trailing characters removed. The chars argument is a string #specifying the set of characters to be removeda = "***Python***"
print(a.strip("*"))Output —
PythonImplementation —
# swapcase() : Returns a copy of the string with uppercase #characters converted to lowercase and vice versa
a = "Hi Homies"
print(a.swapcase())Output —
hI hOMIESImplementation —
# zfill(width) : Returns a copy of the string left filled with ASCII #0 digits to make a string of length widtha = "-124"
print(a.zfill(6))Output —
-00124Implementation —
# lstrip([chars]) : Return a copy of the string with leading #characters removed.The chars argument is a string specifying the #set of characters to be removed.a = "*****Python-----"
print(a.lstrip("*"))Output —
Python-----Implementation —
# rindex(sub[, start[, end]]) : Just like rfind() but raises #ValueError when the substring sub is not founda = "Hi World"print(a.rindex("d"))
print(a.rindex("W"))Output —
7 3
Python F Strings
- Python F-String are used to embed python expressions inside string literals for formatting, using minimal syntax.
- It’s an expression that’s evaluated at the run time.
- They have the f prefix and use {} brackets to evaluate values.
- f-strings are faster than %-formatting and str.format()
Syntax —
f “string_variable”
- In order to format and output an expression in the formatted way, you should use curly braces {}
Implementation —
def max_no(x,y):
return x if x>y else y
f_no= 12
s_no =25
print(f'Max of {f_no} and {s_no} is {max(f_no,s_no)}')Output —
Max of 12 and 25 is 25Implementation —
from decimal import Decimal
width = 4
round_point = 2
value = Decimal('12.39065')
print(f'result:{value:{width}.{round_point}}')Output —
result: 12Operators in Python
- In python, operators are used to perform operations on variables and values
Arithmetic operators : +, — , *, /, //, %, **
Logical operators : and, or, not
Identity operators : is, is not
Membership operators : in , not in
Bitwise operators : &, |, ^,~, << , >>
Assignment operators : =, +=, -=, *=,/= , %=, //=, **=, &=, |=, ^=, >>=, <<=
Comparison operators : ==, !=, > , <, >=, <=
- Ternary operators are operators that evaluate things based on a condition being true or false
Syntax : [true] if [expression] else [false]
- Operator overloading can be implemented in Python
Example —
a & b
a >> 2
a is not b
‘b’ in list1
Implementation —
#Arithmatic Operatorsx=10
y=4#Addition
print("Addition:",x+y)#Subtraction
print("Subtraction:",x-y)#Multiply
print("Multiply: ",x*y)#Division
print("Division:",x/y)#Modulus
print("Modulus:",x%y)#Floor Division
print("Floor Division:",x//y)#Exponent
print("Exponent:",x**y)Output —
Addition: 14
Subtraction: 6
Multiply: 40
Division: 2.5
Modulus: 2
Floor Division: 2
Exponent: 10000Implementation —
#Comparison Operator : Result is either True or Falsex=5
y=3#Greater than
print("Greater than:",x>y)#Less than
print("Greater than:",x<y)#Greater than equal to
print("Greater than equal to:",x>=y)#less than equal to
print("Less than:",x<=y)#Not equal to
print("Not equal to:",x!=y)#Equal to
print("Equal to:",x==y)Output —
Greater than: True
Greater than: False
Greater than equal to: True
Less than: False
Not equal to: True
Equal to: FalseImplementation —
#Logical Operators : and, or, not [Result is either True or False]x= True
y= False#And
print("And result:",(x and y))#Or
print("Or result:",(x or y))#Not
print("Not result:",(not y))Output —
And result: False
Or result: True
Not result: TrueImplementation —
# Bitwise operatorsx = 1001
y = 1010#And
print("And result:",(x & y))#Or
print("Or result:",(x | y))#Not
print("Not result:",(~y))#Xor
print("XOR result:",(x^y))#Bitwise right shift
print("Bitwise right shift result:",(x>>2))#Bitwise left shift
print("Bitwise left shift result:",(x<<2))Output —
And result: 992
Or result: 1019
Not result: -1011
XOR result: 27
Bitwise right shift result: 250
Bitwise left shift result: 4004Implementation —
# Assignment operators : used in Python to assign values to variablesx=5
x+=5
x-=2
x*=2
x**=2Implementation —
# Identity Operator : is and is not are the identity operators in Pythonx=5
y=5
z='a'
print("Is operator result:", (x is y))
print("Not is operator result:", (y is not z))Output —
Is operator result: True
Not is operator result: TrueImplementation —
#Membership operator : in operatorx = 'Python Course'
print('y' in x)
print('a' in x)Output —
True
FalseChaining Comparison Operators with Logical Operators
- In python, in order to check more than two conditions, we implement chaining where two or more operators are chained together as shown in the example below
if x < y < z :
{…..}
- In accordance with associativity and precedence in Python, all comparison operations have the same priority. Resultant values of Comparisons yield boolean values such as either True or False
- When chaining the comparison operators, the sequence can be arbitrary.
Example —
x > y <= c is equivalent to x > y and y <= c
Implementation —
#Chaining Comparison operators with Logical operatorsa, b, c, d, e, f, g = 10, 15, 2, 1, 45, 25, 19
e1 = a <= b < c > d < e is not f is g
e2 = a is d < f is cprint(e1)
print(e2)Output —
False
FalsePython Lists
- One of the most versatile data type in Python, Lists are used to store multiple items ( homogeneous or non-homogeneous) in a single variable.
- Place the items inside the square brackets[]
- Items can be of any data type
- Lists are defined as objects with the data type ‘list’
- Items are ordered, changeable, and allow duplicate values
- list() constructor can be used when creating a new list
- To access values in lists, use the square brackets for slicing along with the index to obtain item value available at a particular index
- Items inside list are indexed, the first item has index [0], the second item has index [1] etc
Example —
var=[1, 2 , ‘car’, ‘sunday’ , 3.14]
empty_list = []
items = list((“apple”, “banana”, “grapes”))
Implementation —
#Create a Listlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
print(list_one)Output —
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday']Implementation —
#List Lengthprint(len(list_one))Output —
5
Implementation —
# List with different data typeslist_two = ['abc',67,True,3.14,"female"]
print(list_two)Output —
['abc', 67, True, 3.14, 'female']Implementation —
#type() with List
print(type(list_two))Output —
<class 'list'>Implementation —
#list() constructor to make a Listlist_cons = list(("hello","World","Beautiful","Day"))
print(list_cons)Output —
['hello', 'World', 'Beautiful', 'Day']Implementation —
# nested listlist_nest= ["hello",[8,4,6],['World']]
print(list_nest)Output —
['hello', [8, 4, 6], ['World']]Implementation —
#slice lists in Python : Use the slicing operator :(colon)list_one = ["sunday","monday","tuesday","wednesday","thursday"]
print(list_one[1:4])Output —
['monday', 'tuesday', 'wednesday']Implementation —
#Add/Change List Elements : use the assignment operator = to change #an itemlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one[3] = 'friday'
print(list_one)Output —
['sunday', 'monday', 'tuesday', 'friday', 'thursday']Implementation —
# Appending and Extending lists in Python : Use the append() or #extend() methodlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one.append('friday')
print(list_one)#extendlist_one.extend(['saturday'])
print(list_one)Output —
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday']
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']Implementation —
# Concatenating and repeat lists : use + operator to concate two #lists and use * operator to repeat listslist_one = ["sunday","monday","tuesday","wednesday","thursday"]
print(list_one + [0,1,2,3,4])#repeat operation
print(['a','b']*2)Output —
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 0, 1, 2, 3, 4]
['a', 'b', 'a', 'b']Implementation —
# Delete/Remove List Elements : delete one or more items or entire list using the keyword deldel list_one[2]
print(list_one)#remove method : remove the given item or pop() method to remove an item at the given index locationlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one.remove("tuesday")
print(list_one)#pop methodlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one.pop(2)
print("Pop result:", list_one)Output —
['sunday', 'monday', 'thursday']
['sunday', 'monday', 'wednesday', 'thursday']
Pop result: ['sunday', 'monday', 'wednesday', 'thursday']Implementation —
# index() method : Returns the index of the first matched itemlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
print(list_one.index("tuesday"))Output —
2
Implementation —
# sort() method: Sort items in a list in ascending orderlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one.sort()
print(list_one)Output —
['monday', 'sunday', 'thursday', 'tuesday', 'wednesday']Implementation —
# reverse() : Reverse the order of items in the listlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one.reverse()
print(list_one)Output —
['thursday', 'wednesday', 'tuesday', 'monday', 'sunday']Implementation —
# copy(): Returns a shallow copy of the listlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_two = list_one.copy()
print(list_two)Output —
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday']Implementation —
#Membership : check if an item exists in a list or not, using the keyword inlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
print('tuesday' in list_one)Output —
TrueImplementation —
# insert() method : insert item at a desired locationlist_one = ["sunday","monday","tuesday","wednesday","thursday"]
list_one.insert(2,'friday')
print(list_one)Output —
['sunday', 'monday', 'friday', 'tuesday', 'wednesday', 'thursday']All the Complete System Design Series Parts —
6. Networking, How Browsers work, Content Network Delivery ( CDN)
Github —
List Comprehensions
- In python, list comprehensions are used to create a new list based on the values of an existing list in the most elegant and shortest way.
- List comprehension consists of an expression followed by for statement inside square [] brackets.
One of the best article I read for Python Data Structures by Jiahui Wang
Example :
new_list = [x for x in list1 if “a” in x]
Implementation —
sqr = [2**x for x in range(20)]
print(sqr)Output —
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288]Python Dictionaries
- In python, dictionary is an unordered collection of data values in which data values are stored in key:value pairs
- Created by placing sequence of elements within curly {} braces, separated by ‘ , ‘
- Values can be of any datatype and can be duplicated, whereas keys are immutable and can’t be repeated
- Can be created by the built-in function dict()
- Dictionaries are defined as objects with the data type ‘dict
- dict() constructor can be used when creating a new dict
- To access values in dict, use the keys
- Key Value format makes dictionary one of the most optimized and efficient data type in Python
Example —
var={ ‘first_day’: ‘sunday’ , ‘second_day’: ‘monday’, ‘third_day’: ‘tuesday’}
empty_dict = {}
class dict(**kwarg)
Implementation —
#Create a Dictionary#empty dictionary
dict_emp = {}#dict with items
dict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(dict_one)Output —
{0: 'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}Implementation —
#Accessing Elements from Dictionary : Using keys or get() methoddict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(dict_one[1])#get() method
print(dict_one.get(2))Output —
monday
tuesdayImplementation —
# Length of Dictionary
dict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(len(dict_one))Output —
5
Implementation —
#Changing and Adding Dictionary elements: add new items or change the value of existing items using an = operatordict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}#change element
dict_one[2] = 'friday'
print("After changing the element", dict_one)#Add element
dict_one[5] = 'saturday'
print("After adding the element :", dict_one)Output —
After changing the element {0: 'sunday', 1: 'monday', 2: 'friday', 3: 'wednesday', 4: 'thursday'}
After adding the element : {0: 'sunday', 1: 'monday', 2: 'friday', 3: 'wednesday', 4: 'thursday', 5: 'saturday'}Implementation —
#Removing elements from Dictionary : Use the pop() or popitem() #methoddict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(dict_one.pop(2))#popitem : remove an arbitrary item and return (key,value)print(dict_one.popitem())Output —
tuesday
(4, 'thursday')Implementation —
# remove all items : using clear methoddict_one.clear()
print(dict_one)Output —
{}Implementation —
#fromkeys(seq[, t]): Returns a new dictionary with keys from seq and value equal to tsubjects = {}.fromkeys(['Computer Science','Space Science','Math','English'])
print(subjects)Output —
{'Computer Science': None, 'Space Science': None, 'Math': None, 'English': None}Implementation —
#items() method : displays a list of dictionary's (key, value) tuple pairsdict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(dict_one.items())Output —
dict_items([(0, 'sunday'), (1, 'monday'), (2, 'tuesday'), (3, 'wednesday'), (4, 'thursday')])Implementation —
#keys() method : displays a list of all the keys in the dictionarydict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(dict_one.keys())Output —
dict_keys([0, 1, 2, 3, 4])Implementation —
#values() method : displays a list of all the values in the dictionary
dict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(dict_one.values())Output —
dict_values(['sunday', 'monday', 'tuesday', 'wednesday', 'thursday'])Implementation —
#setdefault() method : returns the value of a key. If not there, it inserts key with a value to the dictionary
dict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
element = dict_one.setdefault(3)
print(element)#If key not present
element = dict_one.setdefault(6)
print("If key is not present:", dict_one.items())Output —
wednesday
If key is not present: dict_items([(0, 'sunday'), (1, 'monday'), (2, 'tuesday'), (3, 'wednesday'), (4, 'thursday'), (6, None)])Implementation —
#Nested Dictionaries
people = {"subject": {0:"Maths",1:"English",3:"Science"},
"marks": {0:42,1:36,2: 78},
"Age": {0:12,1:34,2:19}
}
#print(people["marks"][0])
print(people["Age"][0])Output —
42
Implementation —
#sorted(): Return a new sorted list of keys in the dictionary
dict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(sorted(dict_one))Output —
[0, 1, 2, 3, 4]Implementation —
#Iterate through dictionaydict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
for i in dict_one.items():
print(i)Output —
(0, 'sunday')
(1, 'monday')
(2, 'tuesday')
(3, 'wednesday')
(4, 'thursday')Implementation —
# Dictionary Comprehensioncubes = {x: x*x*x for x in range(10)}
print(cubes)Output —
{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729}Implementation —
# update() method : updates the dictionary with the elements from
# another dictionary object or from any other key/value pairsdict1 ={0:"zero",4:"four",5:"five"}
dict2={2:"two"}
# updates the value of key 2
dict1.update(dict2)
print(dict1)Output —
{0: 'zero', 4: 'four', 5: 'five', 2: 'two'}Implementation —
# Membership Test : check if a key is in a dictionary or not using #the keyword indict_one = {0:'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday'}
print(0 in dict_one.keys())Output —
TruePython Tuples
- In python, a tuple is a collection of objects which is ordered and immutable
- Created by placing sequence of elements within round () braces, separated by ‘ , ‘
- Values can be of any datatype
- Concatenation of tuples can be done by the use of ‘+’ operator
- Tuples are just like lists except that the tuples are immutable i.e cannot be changed
Example —
var= (4, ‘Hello’, 5, ‘World’)
empty_tuple = ()
Implementation —
# Tuple with itemstup= (10,"Hello",3.14,"a")
print(tup)
print(type(tup))Output —
(10, 'Hello', 3.14, 'a')
<class 'tuple'>Implementation —
# Negative Indexing : index of -1 refers to the last item, -2 to the #second last item and so ontup = (10,"Hello",3.14,"a")
print(tup[-2])#Reverse the tuple
print(tup[::-1])Output —
3.14
('a', 3.14, 'Hello', 10)Implementation —
#concatenation and repeat in Tuples#concatenation using + operator
tup = (10,"Hello",3.14,"a")
print(tup + (50,60))#repeat using * operator
print(tup * 2)Output —
(10, 'Hello', 3.14, 'a', 50, 60)
(10, 'Hello', 3.14, 'a', 10, 'Hello', 3.14, 'a')Implementation —
#membership : check if an item exists in a tuple or not, using the keyword intup = (10,"Hello",3.14,"a")
print(10 in tup)
print("World" in tup)Output —
True
FalseImplementation —
#Iterate through Tuple : use for loop to iterate through each item #in a tupletup = (10,"Hello",3.14,"a")
for i in tup:
print(i)Output —
10
Hello
3.14
aImplementation —
#Nested Tuplenest_tup = ((10,"Hello",3.14,"a"), (70,(8,"Mike")))
print(nest_tup)a,b = nest_tup
print(a)
print(b)Output —
((10, 'Hello', 3.14, 'a'), (70, (8, 'Mike')))
(10, 'Hello', 3.14, 'a')
(70, (8, 'Mike'))Implementation —
#Enumerate : use enumerate functiontup = (10,"Hello",3.14,"a")
for i in enumerate(tup):
print(i)Output —
(0, 10)
(1, 'Hello')
(2, 3.14)
(3, 'a')Python Sets
- In python, a set is a collection of objects which is both unindexed and unordered
- Sets make sure that there are no duplicate elements in the items sequence
- Created by using the built-in set() function with an iterable object by placing the items inside curly {} braces, separated by ‘,’
- Items can be added to the set by using built-in add() function
- Items can be accessed by looping through the set using loops or using ‘in’ keyword
- Items can be removed from the set by using built-in remove()
Example —
var= set([“Hello”, “People”, “Hello”])
Implementation —
#Create Set
set_one = {10, 20, 30, 40}
print(set_one)#Create set from list using set()
set_two = set([10, 20, 30, 40, 30, 20])
print(set_two)Output —
{40, 10, 20, 30}
{40, 10, 20, 30}Implementation —
# Removing elements : Use the methods discard(), pop() and remove()
#set_one is {100, 70, 40, 10, 80, 20, 60, 30}
#discard() method
set_one.discard(100)
print("After discard:",set_one)#remove() method
set_one.remove(40)
print("After removing element :", set_one)#pop() method
set_one.pop()
print("After removing element :", set_one)Output —
After discard: {70, 40, 10, 80, 20, 60, 30}
After removing element : {70, 10, 80, 20, 60, 30}
After removing element : {10, 80, 20, 60, 30}Implementation —
#Set operations
X = {10, 20, 30, 40, 50}
Y = {40, 50, 60, 70, 80}
Z = {20, 30, 100, 50, 10}#Union : Union of X, Y, Z is a set of all elements from all three #sets using | operator or union() method
print("Set Union:", X|Y|Z)#Intersection :Intersection of X, Y, Z is a set of all elements from #all three sets using & operator or intersection()
print("Set Intersection:", X&Y&Z)#Difference : Difference of X, Y is a set of all elements from both #sets using - operator or difference()
print("Set Difference:", X-Y)# Symmetric Difference : Symmetric Difference of X, Y, Z is a set of #all elements from all three sets using ^ operator or #symmetric_difference()
print("Set Symmetric Difference:", X^Y^Z)Output —
Set Union: {100, 70, 40, 10, 80, 50, 20, 60, 30}
Set Intersection: {50}
Set Difference: {10, 20, 30}
Set Symmetric Difference: {80, 50, 100, 70, 60}Implementation —
#enumerate : Returns an enumerate object which contains the index #and value for all the items of the set as a pairset_one = {10, 20, 30, 40, 50, 30}
for i in enumerate(set_one):
print(i)Output —
(0, 40)
(1, 10)
(2, 50)
(3, 20)
(4, 30)Implementation —
#Frozenset : set which has the characteristics of a set, but its elements cannot be changed once assignedX = frozenset([10, 20, 30, 40, 50])
Y = frozenset([40, 50, 60, 70, 80])print(X.union(Y))Output —
frozenset({70, 40, 10, 80, 50, 20, 60, 30})Loops in Python
If, Elif and Else Statements —
- In python, If, Elif and Else statements are used to facilitate decision making i.e when we want to execute a code only if a certain condition is satisfied.
- In the example below, the program evaluates the test expression and will execute statement(s) only if the test expression is True. If the test expression1 is False, then it checks elif test expression 2 and if that’s also False, then at last it goes to else and executed else statement.
if test_expression1:
statement(s)
elif test_expression2:
statements(s)
else :
statement
- Python interprets non-zero values as True. None and 0 are interpreted as False.
- The if block can have only one else block but there can be multiple elif blocks.
While loops —
- In python, while loop is used to traverse/iterate over a block of code as long as the test condition is true
- In the example below, test_expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. The loop continues till the test condition becomes False.
while test_expression:
Do this
Example :
while i <= 10:
sum = sum — i
i = i+1
For Loops and Range function —
- In python, for loop is used to traverse/iterate over a sequence (list, tuple, string etc)
- In the example below, i is the variable that takes the value of the item inside the sequence on each iteration. Loop continues until we reach the last item in the sequence
for i in sequence:
Do this
- Range ( range()) is used to generate sequence of numbers where the syntax of range is
range(start, stop, step_size)
Example :
for i in range(len(list1)):
print(“The element is “, list1[i])
Implementation —
price = 200if price > 120:
print("Price is greater than 120")
elif price == 120:
print("Price is 120")
elif price < 120:
print("Price is less than 120")
else:
print("Exit")Output —
Price is greater than 120Implementation —
# If, Elif and Else one liner
x = 200
var = {x < 190: "Condition one satisfied",
x != 200: "Condition two satisfied"}.get(True, "Condition third satisfied")
print(var)Output —
Condition third satisfiedImplementation —
#while with else statement
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")Output —
1
2
3
4
5
i is no longer less than 6Implementation —
# for loop with range functiondays =['sunday','monday','tuesday']
for i in range(len(days)):
print("Today is", days[i])Output —
Today is sunday
Today is monday
Today is tuesdayBreak and Continue Statement
- In python, we can use break statement when we want to terminate the current loop without checking test condition
- Once terminated using the break statement, the control of the program goes to the statement immediately after the body of the loop
Example :
for value in list1:
if value == “t”:
break
print(value)
- In python, we can use continue statement when we want to skip the rest of the code for the current loop iteration
Example :
for value in list1:
if value == “t”:
continue
print(value)
Implementation —
#break statement
count = 0
while True:
print(count)
count += 1
if count >= 10:
breakprint('exit')Output —
0
1
2
3
4
5
6
7
8
9
exitImplementation —
#Continue statement
for x in range(15):
if x % 2 == 0:
continue
print(x)Output —
1 3 5 7 9 11 13
Input Output in Python
- In python, there are two inbuilt functions to read the input from the user
raw_input ( ) function : reads one line from user input and returns it as a string
input ( ) function : Similar to raw_input, except it evaluates the user expression
- For multiple user inputs, we can use —
split() method
List comprehension
- In python, output is using the print() function
- String literals in print() statement are used to format the output
\n : Add blank new line
“” : To print an empty line.
- end keyword is used to print specific content at the end of the execution of the print() function
Example —
num= input(“Enter the number: “)
str = raw_input(“Enter your input: “)
print(“Python” , end = ‘**’)
One of the best article I read for Python Data Structures by Eyal Trabelsi
Implementation —
# inputnum = int(input('Enter a number: '))Output —
Enter a number: 50Implementation —
num = 5
print('The value of num is', num)
print("The value is %d" %num)Output —
The value of num is 5
The value is 5Python Object Oriented Programming
- Python is a multi-paradigm programming language and supports Object Oriented programming. In Python everything is a object. An object has two characteristics : Attributes and Behavior
- Principles of object-oriented programming system are —
Class
Object
Method
Inheritance
Polymorphism
Encapsulation
- Class and constructor — It’s a blueprint for the object. In python we use the class keyword to define class. Class constructor is used to assign the values to the data members of the class when an object of the class is created.
- Object — It’s an instantiation of a class.
- Method — It’s a function that is associated with an object
- Inheritance — Specifies that the child object acquires all the properties and behaviors of the parent object.
- Polymorphism — Refers to functions having the same names but carrying different functionalities.
- Encapsulation — To prevents data from direct modification, we can restrict access to methods and variables in python
Attributes and Class in Python
- In Python, a class is blueprint of the object
- To define a class, we use the keyword “class” following the class name and semicolon —
class class_name:
Body of the class
- Object — It’s an instantiation of a class. The object instance contains real data or value
To create an instance :
obj1 = class_name()
- Class constructor — to assign the values to the data members of the class when an object of the class is created, we use constructor. The __init__() method is called constructor method
class class_name:
def __init__(self, parameters):
self.param1 = parameters
- Instance attributes refer to the attributes inside the constructor method. Class attributes refer to the attributes outside the constructor method
- Method — It’s a function that is associated with an object, used to describe the behavior of the objects
def method_name(self)
Implementation —
#Class implementation
class cat:def __init__(self, cat_name, cat_breed):
self.name = cat_name
self.age = cat_breedImplementation —
#Class attribute and Instance Attributeclass emp:
x = 10 #class attribute
def __init__(self):
self.name = 'Steve'
self.salary = 10000
def display(self):
print(self.name)
print(self.salary)
obj_emp = emp()
print("Dictionary conversion:", vars(obj_emp))Output —
Dictionary conversion: {'name': 'Steve', 'salary': 10000}Here are some of the most important and useful Python tricks that every developer should know:
- List comprehensions: A concise and efficient way to create a list in one line of code.
- Generators and yield: Allows creation of iterators, which can be used to efficiently process large amounts of data.
- Lambda functions: Anonymous functions that can be used to create small, throw-away functions for short, one-time use.
- Decorators: A way to modify or extend the behavior of a function or class without changing its source code.
- Context managers: A way to manage resources such as file handles, database connections, and other similar resources.
- The “with” statement: A convenient way to use context managers in a more readable way.
- String formatting: Allows you to insert values into a string in a more readable and concise way.
- The “for-else” loop: A way to use the else clause in a for loop to execute code only if the loop finishes without encountering a break statement.
- Zip and enumerate: Two built-in functions that can be used to iterate over multiple lists in parallel or to keep track of the index while iterating over a list.
- Map, filter, and reduce: Higher-order functions that allow you to perform operations on a list of values in a more functional and efficient way.
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 —





