Swapnil Saurav

PYTHON DECEMBER 2023

DAY 1 - INSTALLATION

”’
Python Installation link:
https://www.digitalocean.com/community/tutorials/install-python-windows-10

Python is an interpreted language
”’
print(5*4,end=” and “);  # will evaluate
print(‘5*4’) #will print as it is
print(“5*6”);
print(“5*6=”,\n+str(5*6)) # functions have arguments- they are separated by ,
print(“20”+“30”,20+30,20,30)
print(“5*6=”+str(5*6))

# This isn’t right!
print(“This isn’t right!”)
# He asked,”What’s your name”?
print(”’He asked,”What’s your name”?”’)
print(“””He asked,”What’s your name”?”””)
print(‘This isn\’t right!’)
print(“He asked,\”What\’s your name\”?”)

# \ – is called as ESCAPE SEQUENCE
# \ will add or remove power from you
print(\\n is used for newline in Python”)
print(\\\\n will result in \\n”)
print(   r”\\n will result in \n”      # regular expression
print(“HELLO”);print(“HI”)

## datatypes
#numeric: integer (int), float (float), complex (complex)
#text: string (str) – ‘   ”   ”’    “””
#boolean: boolean(bool) – True and False
x = 1275 # let x = 5
y = 6
print(x+y)
print(type(x))

 

 

 

 

# basic data types:
var1 = 5
print(type(var1)) #<class ‘int’>

var1 = 5.0
print(type(var1)) #<class ‘float’>

var1 = “5.0”
print(type(var1)) #<class ‘str’>

var1 = “””5.0″””
print(type(var1)) #<class ‘str’>

var1 = True
print(type(var1)) #<class ‘bool’>

var1 = 5j
print(type(var1)) #<class ‘complex’>

length = 100
breadth = 15
area = length * breadth
peri = 2*(length + breadth)
print(“Area of a rectangle with length”,length,“and breadth”,breadth,“is”,area,“and perimeter is”,peri)
# f-string
print(f”Area of a rectangle with length {length} and breadth {breadth} is {area} and perimeter is {peri})
print(f”Area of a rectangle with length {length} and breadth {breadth} is {area} and perimeter is {peri})

# float value
tot_items= 77
tot_price = 367
price_item =tot_price/tot_items
print(f”Cost of each item when total price paid is {tot_price} for {tot_items} items is {price_item:.1f} currency”)

”’
Assignment submission process:
1. Create Google drive folder: share with the instructor
2. within this folder – add your .py files
”’
”’
Assignment 1:
1. Write a program to calculate area and circumference of a circle and display info in a formatted manner
2. WAP to calculate area and perimeter of a square
3. WAP to calculate simple interest to be paid when principle amount, rate of interest and time is given
4. WAP to take degree celcius as input and give Fahrenheit output
”’

name, country,position=“Virat”,“India”,“Opening”
print(f”Player {name:<10} plays for {country:>12} as a/an {position:^15} in the cricket.”)
name, country,position=“Mangwaba”,“Zimbabwe”,“Wicket-keeper”
print(f”Player {name:<10} plays for {country:>12} as a/an {position:^15} in the cricket.”)

# operators: arithematic operators
# -5 * -5 = 25
print(5j * 5j) # -25 +0j
val1, val2 = 10,3
print(val1 + val2)
print(val1 – val2)
print(val1 * val2)
print(val1 / val2) #3.333 – always be float

print(val1 % val2) # modulo (%) – remainder
print(val1 // val2) #integer division (non decimal)
print(val1 ** val2) # power() 10**3 = 10*10*10

# comparison operators


# complex numbers are square root negative numbers
# square root of 25 -> 5
# square root of -25? 25 * -1 = 5j
# Comparison operators – compare the values
# asking, is …
# your output is always a bool value – True or False
val1,val2,val3 = 20,20,10
print(val1 > val2) #val1 greater than val2 ?
print(val1 >= val2)
print(val1 > val3) #val1 greater than val3 ?
print(val1 >= val3) # True
print(“Second set:”)
print(val1 < val2) #F
print(val1 <= val2) #T
print(val1 < val3) #F
print(val1 <= val3) #F
print(“third set:”)
print(val1 == val2) # T
print(val2==val3) # F

print(val1 != val2) # F
print(val2!=val3) # T
”’
a = 5 # assign value 5 to the variable a
a ==5 # is the value of a 5?
a!=5 # is value of a not equal to 5 ?
”’
## Logical operators: and or not
”’
Committment: I am going to cover Python and SQL in this course

Actual 1: I covered Python and SQL
Actual 2: I covered SQL
Actual 3: I covered Python

Committment 2: I am going to cover Python or SQL in this course

Actual 1: I covered Python and SQL
Actual 2: I covered SQL
Actual 3: I covered Python
”’
#logical operators takes bool values as input and also output is another bool
print(True and True ) # T
print(False and True ) #F
print(True and False ) #F
print(False and False ) #F
print(“OR:”)
print(True or True ) # T
print(False or True ) #T
print(True or False ) #T
print(False or False ) #F
print(“NOT”)
print(not True)
print(not False)
val1,val2,val3 = 20,20,10
print(val1 > val2 and val1 >= val2 or val1 > val3 and val1 >= val3 or val1 < val2 and val2!=val3)
# F and T or T and T or F and T
# F or T or F
# T
# Self Practice: output is True – solve it manually
print(val1 <= val2 or val1 < val3 and val1 <= val3 and val1 == val2 or val2==val3 or val1 != val2)

# Bitwise operator : & | >> <<
print(bin(50)) #bin() convert into binary numbers
# 50 = 0b 110010
print(int(0b110010)) #int() will convert into decimal number
print(oct(50)) # Octal number system: 0o62
print(hex(50)) #hexadecimal: 0x32

# Assignments (3 programs) – refer below
# bitwise operators
num1 = 50 # 0b 110010
num2 = 25 # 0b 011001
print(bin(50))
print(bin(25))
”’
110010
011001
111011 (|)
010000 (&)
”’
print(int(0b111011)) #bitwise | result = 59
print(int(0b10000)) #bitwise & result = 16
print(50&25)
print(50|25)
”’
Left Shift:
110010 << 1 = 1100100
Right Shift:
110010 >> 1 = 11001
”’
print(50<<2) # 50*2*2 : 110010 0 0
print(int(0b11001000))
print(50>>2) # 50 /2 /2
print(int(0b1100))
# input() – to read values from the user
a = input(‘Enter the value for length:’)
print(a)
print(type(a))
a = int(a)
print(type(a))
# int(), float(), str(), complex(), bool()
b = int(input(“Enter the value for breadth:”))
area = a*b
print(“Area of the rectangle is”,area)
total_marks = 150

if total_marks>=200:
print(“Congratulations! You have passed the exam”)
print(“You have 7 days to reserve your admission”)
else:
print(“Sorry, You have not cleared the exam”)
print(“Try again after 3 months”)

print(“Thank you”)
#
marks = 75
”’
>=85: Grade A
>=75: B
>=60: C
>=50: D
<50: E
”’
if marks>=85:
print(“Grade A”)
elif marks>=75:
print(“Grade B”)
elif marks>=60:
print(“Grade C”)
elif marks>=50:
print(“Grade D”)
else:
print(“Grade E”)

print(“Done”)
###.
marks = 85
”’
>=85: Grade A
>=75: B
>=60: C
>=50: D
<50: E
”’
if marks>=85:
print(“Grade A”)

if marks>=75 and marks<85:
print(“Grade B”)
if marks>=60 and marks<75:
print(“Grade C”)
if marks>=50 and marks<60:
print(“Grade D”)
if marks<50:
print(“Grade E”)

print(“Done”)
### NEST IF
marks = 98.0001
”’
>=85: Grade A
>=75: B
>=60: C
>=50: D
<50: E
>90: award them with medal
”’
if marks>=85:
print(“Grade A”)
if marks >= 90:
print(“You win the medal”)
if marks>98:
print(“Your photo will be on the wall of fame”)
elif marks>=75:
print(“Grade B”)
elif marks>=60:
print(“Grade C”)
elif marks>=50:
print(“Grade D”)
else:
print(“Grade E”)

print(“Done”)
”’
Practice basic programs from here:
https://www.scribd.com/document/669472691/Flowchart-and-C-Programs
”’

# check if a number is odd or even
num1 = int(input(“Enter the number: “))
if num1<0:
print(“Its neither Odd or Even”)
else:
if num1%2==0:
print(“Its Even”)
else:
print(“Its Odd”)

## check the greater of the given two numbers:

num1, num2 = 20,20
if num1>num2:
print(f”{num1} is greater than {num2})
elif num2>num1:
print(f”{num2} is greater than {num1})
else:
print(“They are equal”)

## check the greater of the given three numbers:

num1, num2,num3 = 29,49,29
if num1>num2: # n1 > n2
if num1>num3:
print(f”{num1} is greater”)
else:
print(f”{num3} is greater”)
else: # n2 is greater or equal to
if num2 > num3:
print(f”{num2} is greater”)
else:
print(f”{num3} is greater”)
##

#enter 3 sides of a triangle and check if they are:
#equilateral, isoceles, scalene, right angled triangle
side1,side2,side3 = 90,60,30
if side1==side2:
if side1 == side3:
print(“Equilateral”)
else:
print(“Isoceles”)
else:
if side1==side3:
print(“Isoceles”)
else:
if side2==side3:
print(“Isoceles”)
else:
print(“Scalene”)

#modify the above code to handle Right Angled triangle logic
# loops –
# FOR : know how many times you need to repeat
# WHILE : dont know how many times but you have the condition
# range(start, stop,step): starts with start, goes upto stop (not including)
# step: each time value is increasesd by step
# range(10,34,6): 10, 16, 22, 28
# range(start, stop) : default step is 1
# range(10,17): 10,11,12,13,14,15,16
# range(stop): default start is zero, default step is 1
# range(5): 0,1,2,3,4

# generate values from 1 to 10
for counter in range(1,11): # 1,2,3…10
print(counter,end=“, “)
print()
print(“Thank You”)

# generate first 10 odd numbers
for odd_num in range(1,11,2): # 1,2,3…10
print(odd_num,end=“, “)
print()
print(“———-“)
for counter in range(10):
print(2*counter+1,end=“, “)
print()
print(“———-“)
# generate even numbers till 50
for even_num in range(0,50,2): # 1,2,3…10
print(even_num,end=“, “)
print()
##############
# WHILE: is always followed by a condition and only if the condition is true, u get in
# WAP to print hello till user says so
user = “y”
while user==“y”:
print(“Hello”)
user = input(“Enter y to continue or anyother key to stop: “)
##

print(“method 2”)

while True:
user = input(“Enter y to continue or anyother key to stop: “)
if user!=“y”:
break
print(“Hello”)

print(“Thank you”)
count = int(input(“How many times you want to print: “))
while count >0:
print(“Hello”)
count-=1 #count = count-1
# For loops
”’
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
”’
n=5
for j in range(n):
for i in range(n):
print(“*”,end=” “)
print()

”’
*
* *
* * *
* * * *
* * * * *
”’
n=5
num_stars=1
for j in range(n):
for i in range(num_stars):
print(“*”,end=” “)
print()
num_stars+=1

#
n=5
for j in range(n):
for i in range(j+1):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for j in range(n):
for i in range(n-j):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for j in range(n):
for k in range(j):
print(“”, end=” “)
for i in range(n-j):
print(“*”,end=” “)
print()

””
Practice Program:
*
* *
* * *
* * * *
* * * * *
”’
”’
Multiplication table:
1 * 1 = 1 2 * 1 = 2 … 10 * 1 = 10
1 * 2 = 2 2 * 2 = 4

10 * 10 = 100
”’

for mul in range(1,11):
for tab in range(1,11):
print(f”{tab:<2}* {mul:<2}= {tab*mul:<2},end=” “)
print()

”’
Print prime numbers between 5000 and 10,000

10 – prime or not
2
10%2==0 => not a prime
3
4
”’
for num in range(5000,10000):
isPrime = True
for i in range(2,num//2+1):
if num%i==0:
isPrime = False
break
if isPrime:
print(num,end=“, “)
”’
num = 11
isPrime =T
i in range(2,6)
isPrime =F
”’
# WAP to create a menu option to perform arithmetic operations
”’
Before you use while loop, decide:
1. Should the loop run atleast once (Exit Controlled), or
2. Should we check the condition even before running the loop (Entry controlled)
”’
# method 1: Exit controlled
while True:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“Your Option: “)
print(“1. Add”)
print(“2. Subtract”)
print(“3. Multiply”)
print(“4. Divide”)
print(“5. Exit”)
ch = input(“Enter your choice: “)
if ch==“1”:
print(“Addition = “,num1 + num2)
elif ch==“2”:
print(“Difference = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “,num1 / num2)
elif ch==“5”:
break
else:
print(“Invalid Option”)

#
# method 2: Exit controlled
ch = “1”
while ch!=“5”:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“Your Option: “)
print(“1. Add”)
print(“2. Subtract”)
print(“3. Multiply”)
print(“4. Divide”)
print(“5. Exit”)
ch = input(“Enter your choice: “)
if ch==“1”:
print(“Addition = “,num1 + num2)
elif ch==“2”:
print(“Difference = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “,num1 / num2)
elif ch==“5”:
print(“Exiting now…”)
else:
print(“Invalid Option”)
##
# method 3: Entry controlled
choice = input(“Enter Yes to perform arithmetic operations: “)
while choice.lower() ==“yes”:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“Your Option: “)
print(“1. Add”)
print(“2. Subtract”)
print(“3. Multiply”)
print(“4. Divide”)
print(“5. Exit”)
ch = input(“Enter your choice: “)
if ch==“1”:
print(“Addition = “,num1 + num2)
elif ch==“2”:
print(“Difference = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “,num1 / num2)
elif ch==“5”:
choice =“no”
print(“Exiting now…”)
else:
print(“Invalid Option”)

#
# Generate odd numbers from 1 till user wants to continue
num1 = 1
while True:
print(num1)
num1+=2
ch=input(“Enter y to generate next number or anyother key to stop: “)
if ch!=‘y’:
break
# Generate fibonacci numbers from 1 till user wants to continue
num1 = 0
num2 = 1
while True:
num3 =num1 +num2
print(num3)
num1,num2 = num2,num3
ch=input(“Enter y to generate next number or anyother key to stop: “)
if ch!=‘y’:
break
# Generate fibonacci numbers from 1 till user wants to continue

print(“Hit Enter key to continue or anyother key to stop! “)
num1 = 0
num2 = 1
while True:
num3 =num1 +num2
print(num3,end=“”)
num1,num2 = num2,num3
ch=input()
if ch!=:
break
import random
print(random.random())
print(random.randint(100,1000))

from random import randint
print(randint(100,1000))

# guess the number game – computer (has the number) v human (attempting)
from random import randint

num = randint(1,100)
attempt=0
while True:
guess = int(input(“Guess the number (1-100): “))
if guess<1 or guess>100:
print(“Invalid attempt!!!”)
continue

attempt+=1 #attempt=attempt+1
if guess ==num:
print(f”Congratulations! You got it right in {attempt} attempts.”)
break
elif guess < num:
print(“Sorry, that’s incorrect. Please try again with a higher number!”)
else:
print(“Sorry, that’s incorrect. Please try again with a lower number!”)

### ###
# guess the number game – computer (has the number) v computer (attempting)
from random import randint
start,stop = 1,100
num = randint(1,100)
attempt=0
while True:
#guess = int(input(“Guess the number (1-100): “))
guess = randint(start,stop)
if guess<1 or guess>100:
print(“Invalid attempt!!!”)
continue

attempt+=1 #attempt=attempt+1
if guess ==num:
print(f”Congratulations! You got it right in {attempt} attempts.”)
break
elif guess < num:
print(f”Sorry, {guess} that’s incorrect. Please try again with a higher number!”)
start=guess+1
else:
print(f”Sorry, {guess} that’s incorrect. Please try again with a lower number!”)
stop=guess-1

##
# guess the number game – computer (has the number) v computer (attempting)
from random import randint
total_attempts = 0
for i in range(10000):
start,stop = 1,100
num = randint(1,100)
attempt=0
while True:
#guess = int(input(“Guess the number (1-100): “))
guess = randint(start,stop)
if guess<1 or guess>100:
print(“Invalid attempt!!!”)
continue

attempt+=1 #attempt=attempt+1
if guess ==num:
print(f”Congratulations! You got it right in {attempt} attempts.”)
total_attempts+=attempt
break
elif guess < num:
print(f”Sorry, {guess} that’s incorrect. Please try again with a higher number!”)
start=guess+1
else:
print(f”Sorry, {guess} that’s incorrect. Please try again with a lower number!”)
stop=guess-1

print(“========================================”)
print(“Average number of attempts = “,total_attempts/10000)
print(“========================================”)
”’
Multi line
text of
comments
which can go into
multiple lines
”’
# Strings
str1 = ‘Hello’
str2 = “Hello there”
print(type(str1), type(str2))
str3 = ”’How are you?
Where are you from?
Where do you want to go?”’
str4 = “””I am fine
I live here
I am going there”””
print(type(str3), type(str4))
print(str3)
print(str4)
# one line of comment
”’
Multi line
text of
comments
which can go into
multiple lines
”’

# what’s your name?
print(‘what\’s your name?’)

# counting in Python starts from zero
str1 = ‘Hello there how are you?’
print(“Number of characters in str1 is”,len(str1))
print(“First character: “,str1[0], str1[-len(str1)])
print(“Second character: “,str1[1])
print(“Last character: “,str1[len(str1)-1])
print(“Last character: “,str1[-1])
print(“Second Last character: “,str1[-2])
print(“5th 6th 7th char: “,str1[4:7])
print(“First 4 char: “,str1[0:4],str1[:4])
print(“first 3 alternate char: “,str1[1:5:2])
print(“last 3 characters:”,str1[-3:])
print(“last 4 but one characters:”,str1[-4:-1])
print(str1[5:1:-1])

txt1 = “HiiH”
txt2=txt1[-1::-1] #reversing the text
print(txt2)
txt2=str1[-1:-7:-1] #reversing the text
print(txt2)
if txt2 == txt1:
print(“Its palindrome”)
else:
print(“Its not a palindrome”)
var1 = 5
#print(var1[0]) # ‘int’ object is not subscriptable

# add two strings
print(“Hello”+“, “+“How are you?”)
print(“Hello”,“How are you?”)
print((“Hello”+” “)*5)
print(“* “*5)
# for loop – using strings
str1 = “hello”
for i in str1:
print(i)

for i in range(len(str1)):
print(i, str1[i])

print(type(str1)) # <class ‘str’>
str2 = “HOW Are You?”
up_count, lo_count,sp_count = 0,0,0
for i in str2:
if i.islower():
lo_count+=1
if i.isupper():
up_count+=1
if i.isspace():
sp_count+=1

print(f”Number of spaces={sp_count}, uppercase letters={up_count} and lower case letters={lo_count})

#input values:
val1 = input(“Enter a number: “)
if val1.isdigit():
val1 = int(val1)
print(val1 * 5)
else:
print(“Invalid value”)

str3 = “123af ds”
print(str3.isalnum())

#
str1 =“How are You”
# docs.python.org
help(str.isascii)

help(help)


str1 = “HOw are YOU today?”
print(str1.upper())
print(str1.lower())
print(str1.title())
#str1 = str1.title()
# strings are immutable – you cant edit
#str1[3] = “A” #TypeError: ‘str’ object does not support item assignment
str1= str1[0:3]+“A”+str1[4:]
print(str1)
cnt = str1.lower().count(‘o’)
print(cnt)
cnt = str1.count(‘O’,3,15) # x,start,end
print(cnt)
# Strings – method
str1 = “Hello how are you doing today”
var1 = str1.split()
print(“Var 1 =”,var1)
var2 = str1.split(‘o’)
print(“Var 2 =”,var2)
str2 = “1,|Sachin,|Mumbai,|Cricket”
var3 = str2.split(‘,|’)
print(var3)
str11 = ” “.join(var1)
print(“Str11 = “,str11)
str11 = “”.join(var2)
print(“Str11 = “,str11)
str11= “–“.join(var3)
print(“Str11 = “,str11)
# Strings – method
str1 = “Hello how are you doing today”
str2 = str1.replace(‘o’,‘ooo’)
print(str2)
cnt = str1.count(‘z’)
print(“Number of z in the str1 =”,cnt)
find_cnt = str1.find(‘ow’)
if find_cnt==-1:
print(“Given substring is not in the main string”)
else:
print(“Substring in the str1 found at =”,find_cnt)

find_cnt = str1.find(‘o’,5,6)
print(“Substring in the str1 found at =”,find_cnt)

str2 = str1.replace(‘z’,‘ooo’,3)
print(str2)

################
## LIST = Linear Ordered Mutable Collection
l1 = [55, ‘Hello’,False,45.9,[2,4,6]]
print(“type of l1 = “,type(l1))
print(“Number of members in the list=”,len(l1))
print(l1[0],l1[4],l1[-1])
print(“type of l1 =”,type(l1[0]))
print(“type of l1 =”,type(l1[-1]))
l2 = l1[-1]
print(l2[0], l1[-1][0], type(l1[-1][0]))
l1[0] = 95
print(“L1 =”,l1)
## LIST = Linear Ordered Mutable Collection
l1 = [55, ‘Hello’,False,45.9,[2,4,6]]

for member in l1:
print(member)

print(l1+l1)
print(l1*2)

print(“count = “,l1.count(False))
print(“count = “,l1.count(‘Hello’))
# remove second last member – pop takes position
l1.pop(-2)
print(“L1 after Pop: “,l1)
l1.pop(-2)
print(“L1 after Pop: “,l1)
# delete the element – remove takes value
cnt = l1.count(‘Helloo’)
if cnt>0:
l1.remove(‘Helloo’)
print(“L1 after Remove: “,l1)
else:
print(“‘Helloo’ not in the list”)

# Collections – Lists – linear mutable ordered collection
l1 = [10,50,90,20,90]
# add and remove members
l1.append(25) #append will add at the end
l1.append(45)
print(“L1 after append: “,l1)
#insert takes position and the value to add
l1.insert(2,35)
l1.insert(2,65)
print(“L1 after insert: “,l1)
l1.remove(35) #takes value to delete
l1.remove(90)
print(“L1 after remove: “,l1)
cnt_90 = l1.count(90)
print(“Number of 90s: “,cnt_90)
l1.pop(2) #index at which you want to delete
print(“L1 after pop: “,l1)

# Collections – Lists – linear mutable ordered collection
l1 = [10,50,90,20,90]
l2 = l1.copy() #shallow – photocopy
l3 = l1 # deepcopy – same list with two names
print(“1. L1 = “,l1)
print(“1. L2 = “,l2)
print(“1. L3 = “,l3)
l1.append(11)
l2.append(22)
l3.append(33)
print(“2. L1 = “,l1)
print(“2. L2 = “,l2)
print(“2. L3 = “,l3)
print(“Index of 90:”,l1.index(90,3,7))

# Extend: l1 = l1+l2
l2=[1,2,3]
l1.extend(l2)
print(“L1 after extend:”,l1)
l1.reverse()
print(“L1 after reverse: “,l1)
l1.sort() #sort in ascending order
print(“L1 after sort: “,l1)
l1.sort(reverse=True) #sort in descending order
print(“L1 after reverse sort: “,l1)
l1.clear()
print(“L1 after clear: “,l1)

######## question from Vivek: ###########
l1 = [9,5,7,2]
target = 12
l2=l1.copy()
l2.sort() #[2,5,7,19]
for i in range(len(l2)-1):
if l2[i]+l2[i+1] == target
#l1.index(l2[i]), l1.index(l2[i+1])
break
else:
> target: stop
<target: check with i+1 with i+2
# Tuple – linear ordered immutable collection
l1 = [2,4,6,8]
print(l1, type(l1))
t1 = (2,4,6,8,2,4,6,2)
print(t1, type(t1))
l1[1] = 14
print(“L1 = “,l1)

#t1[1] = 14 TypeError: ‘tuple’ object does not support item assignment

print(“Index of 2 =”,t1.index(2))
print(“Count of 2 =”,t1.count(2))
print(t1, type(t1))
t1=list(t1)
t1[1] = 14
t1 = tuple(t1)
print(t1, type(t1))
for i in t1:
print(i)


t2 = (3,6,9) #packing
a,b,c = t2 #unpacking
print(a,type(a),b,type(b),c,type(c))

t3 = ()
print(“type of t3=”,type(t3))

t4 = (“Hello”,4)
print(“type of t4=”,type(t4))

# (“Hello” + “World”)*3 -> “Hello” + “World”*3
###############
# Dictionary: unordered mutable collection
# pairs of key:value
d1 = {0:3,1:6,2:9}
print(“type = “,type(d1))
print(d1[1])

basic_health= {“Name”:“Sachin”,
“Weight”:156,
“Age”:42,
23:“NY”}

print(basic_health[“Name”])

patients =[{“Name”:“Sachin”,“Weight”:156,“Age”:42,23:“NY”},
{“Name”:“Virat”,“Weight”:126,“Age”:38,23:“NY”},
{“Name”:“Rohit”,“Weight”:176,“Age”:24,23:“NY”},
{“Name”:“Kapil”,“Weight”:196,“Age”:62,23:“NY”}]

print(patients[1][“Weight”])
basic_health= {“Name”:“Sachin”,
“Weight”:2,
“Age”:2,
“Age”:10,
23:“NY”,
“Age”:15}

print(basic_health.keys())
print(basic_health.values())
print(basic_health.items())

# Dictionary
”’
WAP to input marks of three students in three subjects

marks = {‘Sachin’: [78, 87, 69], ‘Kapil’: [59, 79, 49], ‘Virat’: [88, 68, 78]}
”’
students = [‘Sachin’,‘Kapil’,‘Virat’]
subjects = [‘Maths’,‘Science’,‘English’]
marks = {}
#marks_list = []
num_students, num_subjects = 3,3

for i in range(num_students):
marks_list = []
for j in range(num_subjects):
m = int(input(“Enter the marks in subject ” + subjects[j]+” : “))
marks_list.append(m)
temp = {students[i]:marks_list}
marks.update(temp)
#marks_list.clear()

print(“Marks entered are: “,marks)

# Dictionary
”’
WAP to input marks of three students in three subjects.
calculate total and average of marks for all the 3 students
find who is the highest scorer in total and also for each subject

marks = {‘Sachin’: [78, 87, 69], ‘Kapil’: [59, 79, 49], ‘Virat’: [88, 68, 78]}
”’
students = [‘Sachin’, ‘Kapil’, ‘Virat’]
subjects = [‘Maths’, ‘Science’, ‘English’]
marks = {‘Sachin’: [78, 87, 69], ‘Kapil’: [59, 79, 49], ‘Virat’: [88, 68, 78]}
topper = {‘Total’: –1, ‘Name’: []}
subject_highest = [-1, –1, –1]

num_students, num_subjects = 3, 3
for i in range(num_students):
tot, avg = 0, 0
key = students[i]
for j in range(num_subjects):
tot = tot + marks[key][j]
# checking the highest values for each subject
# …

avg = tot / 3
print(f”Total marks obtained by {students[i]} is {tot} and average is {avg:.1f})
# check highest total
if tot >= topper[‘Total’]:
topper[‘Total’] = tot
topper[‘Name’].append(key)

print(f”{topper[‘Name’]} has topped the class with total marks of {topper[‘Total’]})
# Dictionary
”’
WAP to input marks of three students in three subjects.
calculate total and average of marks for all the 3 students
find who is the highest scorer in total and also for each subject

marks = {‘Sachin’: [78, 87, 69], ‘Kapil’: [59, 79, 49], ‘Virat’: [88, 68, 78]}
”’
students = [‘Sachin’,‘Kapil’,‘Virat’]
subjects = [‘Maths’,‘Science’,‘English’]
marks = {‘Sachin’: [78, 87, 69], ‘Kapil’: [59, 79, 49], ‘Virat’: [88, 68, 78]}
topper = {‘Total’:-1, ‘Name’:[]}
subject_highest = [-1,-1,-1]

num_students, num_subjects = 3,3
for i in range(num_students):
tot,avg = 0,0
key = students[i]
for j in range(num_subjects):
tot = tot + marks[key][j]
#checking the highest values for each subject
if marks[key][j] > subject_highest[j]:
subject_highest[j] = marks[key][j]

avg = tot / 3
print(f”Total marks obtained by {students[i]} is {tot} and average is {avg:.1f})
# check highest total
if tot >=topper[‘Total’]:
topper[‘Total’] = tot
topper[‘Name’].append(key)

print(f”{topper[‘Name’]} has topped the class with total marks of {topper[‘Total’]})
print(f”Highest marks for subjects {subjects} is {subject_highest})

marks = {‘Sachin’: [78, 87, 69], ‘Kapil’: [59, 79, 49], ‘Virat’: [88, 68, 78]}

# deep & shallow copy
marks2 = marks
marks3 = marks.copy()
print(“before update:”)
print(“Marks = “,marks)
print(“Marks2 = “,marks2)
print(“Marks3 = “,marks3)
marks2.pop(‘Kapil’)
marks.update({‘Mahi’:[71,91,81]})
print(“after update:”)
print(“Marks = “,marks)
print(“Marks2 = “,marks2)
print(“Marks3 = “,marks3)

###########################
## SETS
# SETS
l1 = [‘Apple’,‘Apple’,‘Apple’,‘Apple’,‘Apple’]
print(“Values in L1 = “,len(l1))

s1 = {‘Apple’,‘Apple’,‘Apple’,‘Apple’,‘Apple’}
print(type(s1))
print(“Values in S1 = “,len(s1))
# property 1: removes duplicate values

s1 = {‘Apple’,‘Banana’,‘Orange’,‘Grapes’,‘Mango’}
s2 = {‘Grapes’,‘Mango’, ‘Guava’,‘Pine apple’,‘Cherry’}
# property 2: order doesnt matter

print(“union – total how many values”)
print(s1|s2)
print(s1.union(s2))
print(“Intersection – common values between the sets”)
print(s1 & s2)
print(s1.intersection(s2))
print(“Difference (minus) – u remove set of values from another set”)
print(s1 – s2)
print(s1.difference(s2))
print(s2 – s1)
print(s2.difference(s1))

print(“Symmetric difference”)
print(s1 ^ s2)
print(s1.symmetric_difference(s2))
##
s1 = {1,2,3,4,5,6}
s2 = {4,5,6}
print(s1.isdisjoint(s2))
print(s1.issuperset(s2))

# sets, lists, tuples -> they are convertible in each others form
l1 = [‘Apple’,‘Apple’,‘Apple’,‘Apple’,‘Apple’]
l1 = list(set(l1))
print(l1)
s1 = {4,2,3}
print(s1)
# Functions

def smile():
txt=”’ A smile, a curve that sets all right,
Lighting days and brightening the night.
In its warmth, hearts find their flight,
A silent whisper of pure delight.”’
print(txt)

smile()

smile()

smile()

#==================

# function to calculate gross pay
def calc_grosspay():
basic_salary = 5000
hra = 0.1 * basic_salary
da = 0.4 * basic_salary
gross_pay = basic_salary + hra + da
print(“Your gross pay is”,gross_pay)

def calc_grosspay_withreturn():
basic_salary = 5000
hra = 0.1 * basic_salary
da = 0.4 * basic_salary
gross_pay = basic_salary + hra + da
return gross_pay

def calc_grosspay_return_input(basic_salary):
hra = 0.1 * basic_salary
da = 0.4 * basic_salary
gross_pay = basic_salary + hra + da
return gross_pay


bp_list = [3900,5000,6500,9000]
gp_1 = calc_grosspay_return_input(bp_list[3])
print(“Gross Pay for this month is”,gp_1)

gp = calc_grosspay_withreturn()
print(“Total gross pay for ABC is”,gp)
gp_list=[]
gp_list.append(gp)
calc_grosspay()
# Functions
HOUSENO = 55
def myfunc1():
#x = 51
global x
print(“1 Value of x =”,x)
print(“My House No =”, HOUSENO)
x = 51
print(“2 Value of x =”, x)


def myfunc2(a,b):
print(“======== MYFUNC2 ========”)
print(f”a = {a} and b = {b})
print(“Sum of a and b = “,a+b)

def myfunc3(a=5,b=3.14):
print(“======== MYFUNC3 ========”)
print(f”a = {a} and b = {b})
print(“Sum of a and b = “,a+b)

def myfunc4(a,b):
print(“======== MYFUNC4 ========”)
print(f”a = {a} and b = {b})
print(“Sum of a and b = “,a+b)


def myfunc5(a,*b):
print(“a = “,a)
print(“b = “, b)

def myfunc6(a,*b, **c):
print(“a = “,a)
print(“b = “, b)
print(“c = “, c)

# default arguments (there is a default value added)
myfunc3(10,20)
myfunc3(10)
myfunc3()
# required positional arguments
myfunc2(10,20)

x = 5
myfunc1()
print(“Value of x =”,x)
# non-positional = keyword arguments
myfunc4(b=22, a=33)

# variable length arguments
myfunc5(10,20,30,40)
myfunc5(10)
myfunc5(10, 20)
myfunc6(10, 20,“hello”,name=“Sachin”,runs=3000,city=“Mumbai”)
# function to check prime numbers
”’
10 = 2 to 5
7 = 2,
9 = 2,3
”’

def gen_prime(num):
”’
This function takes a parameter and checks if its a prime number or not
:param num: number (int)
:return: True/False (True for prime number)
”’
isPrime = True
for i in range(2,num//2):
if num%i ==0:
isPrime = False
break
return isPrime


if __name__ ==“__main__”:
num = 11
print(num,” : “,gen_prime(num))
num = 100
print(num,” : “,gen_prime(num))

# generate prime numbers between given range
start,end = 1000, 5000
for i in range(start,end):
check = gen_prime(i)
if check:
print(i,end=“, “)


# doc string: multi line comment added at the beginning of the function
help(gen_prime)
#import infy_apr as ia
from infy_apr import gen_prime

def product_val(n1,n2):
return n1 * n2

if __name__==“__main__”:
num1 = 1
num2 = 3
print(“Sum of two numbers is”,num2+num1)
# generate prime numbers between 50K to 50.5K
for i in range(50000,50500):
check = gen_prime(i)
if check:
print(i,end=“, “)
# recursive functions
”’
O -> L1 (20) -> L1(19) -> L1(18) … -> 1 -> 1
”’
def sayhi(n):
if n>0:
print(“Hello”)
sayhi(n-1)
else:
return 1

sayhi(20)

”’
Factorial of a number:
5! = 5 * 4 * 3 * 2 * 1
”’

def facto(n):
if n<=1:
return 1
else:
return n * facto(n-1)

result = facto(5)
print(“Factorial is”,result)

#############
def f1():
def f2():
print(“I am in f2 which is inside f1”)

print(“first line of f1”)
f2()
print(“second line of f1”)

def calculate(n1,n2,op):
def plus(n1,n2):
return n1 + n2
def diff(n1,n2):
return n1-n2
if op==“+”:
output = plus(n1,n2)
if op==“-“:
output = diff(n1, n2)
return output

res = calculate(5,10,“+”)
print(“1. Result = “,res)
res = calculate(5,10,“-“)
print(“2. Result = “,res)

####################

def plus(n1,n2):
return n1 + n2
def diff(n1,n2):
return n1-n2
def calculate(n1,n2,func):
output = func(n1,n2)
return output

res = calculate(5,10,plus)
print(“1. Result = “,res)
res = calculate(5,10,diff)
print(“2. Result = “,res)
###############

# in-built functions()
# user defined functions()
# anonymous / one line /lambda

def myfunc1(a,b):
return a**b
#above myfunc1() can also be written as:
myfunc2 = lambda a,b: a**b
print(“5 to power of 4 is”,myfunc2(5,4))

”’
map: apply same logic on all the values of the list: multiply all the values by 76
filter: filter out values in a list based on a condition: remove -ve values
reduce: reduce multiple values in a list to a single value
”’
# a= 11, b = 12, c = 13…
calc = 0
list1 = [‘a’,‘b’,‘c’,‘d’]
word = input()
for i in word:
calc = calc+list1.index(i) + 11 # 11 + 13+14
print(calc)

# recursive functions
”’
O -> L1 (20) -> L1(19) -> L1(18) … -> 1 -> 1
”’
def sayhi(n):
if n>0:
print(“Hello”)
sayhi(n-1)
else:
return 1

sayhi(20)

”’
Factorial of a number:
5! = 5 * 4 * 3 * 2 * 1
”’

def facto(n):
if n<=1:
return 1
else:
return n * facto(n-1)

result = facto(5)
print(“Factorial is”,result)

#############
def f1():
def f2():
print(“I am in f2 which is inside f1”)

print(“first line of f1”)
f2()
print(“second line of f1”)

def calculate(n1,n2,op):
def plus(n1,n2):
return n1 + n2
def diff(n1,n2):
return n1-n2
if op==“+”:
output = plus(n1,n2)
if op==“-“:
output = diff(n1, n2)
return output

res = calculate(5,10,“+”)
print(“1. Result = “,res)
res = calculate(5,10,“-“)
print(“2. Result = “,res)

####################

def plus(n1,n2):
return n1 + n2
def diff(n1,n2):
return n1-n2
def calculate(n1,n2,func):
output = func(n1,n2)
return output

res = calculate(5,10,plus)
print(“1. Result = “,res)
res = calculate(5,10,diff)
print(“2. Result = “,res)
###############

# in-built functions()
# user defined functions()
# anonymous / one line /lambda

def myfunc1(a,b):
return a**b
#above myfunc1() can also be written as:
myfunc2 = lambda a,b: a**b
print(“5 to power of 4 is”,myfunc2(5,4))

”’
map: apply same logic on all the values of the list: multiply all the values by 76
filter: filter out values in a list based on a condition: remove -ve values
reduce: reduce multiple values in a list to a single value
”’
# a= 11, b = 12, c = 13…
calc = 0
list1 = [‘a’,‘b’,‘c’,‘d’]
word = input()
for i in word:
calc = calc+list1.index(i) + 11 # 11 + 13+14
print(calc)

”’
map: apply same logic on all the values of the list: multiply all the values by 76
filter: filter out values in a list based on a condition: remove -ve values
reduce: reduce multiple values in a list to a single value
”’
value_usd = [12.15,34.20,13,8,9,12,45,87,56,78,54,34]
value_inr = []
# 1 usd = 78 inr
for v in value_usd:
value_inr.append(v*78)
print(“Value in INR: “,value_inr)

value_inr =list(map(lambda x: 78*x,value_usd))
print(“Value in INR: “,value_inr)

# filter: filter out the values
new_list=[12,7,0,-5,-6,15,18,21,-44,-90,-34,56,43,12,7,0,-5,-6,15,18,21,-44,-90,-34,56,43]
output_list = list(filter(lambda x: x>=0,new_list))
print(“Filtered: “,output_list)

output_list = list(filter(lambda x: x%3==0 and x>=0,new_list))
print(“Filtered: “,output_list)

# reduce
import functools as ft
#from functools import reduce
new_list=[12,7,0,-5,-6,15,18,21,-44,-90,-34,56,43]
val = ft.reduce(lambda x,y:x+y,new_list)
print(“Value after reduce = “,val)
”’
x+y => [12,7,0,-5,-6,15,18,21,-44,-90,-34,56,43]
12+7
19+0
19+ -5
14 + -6
8+15
”’
abc = lambda x,y:x+y
def abc(x,y):
return x+y

##################################

## class & objects
”’
car – class
number of wheels – 4, color, make

driving
parking
”’

class Book:
number_of_books = 0

def reading(self):
print(“I am reading a book”)

b1 = Book() #creating object of class Book
b2 = Book()
b3 = Book()
b4 = Book()
print(b1.number_of_books)
b1.reading()
”’
class level variables and methods
object level variables and methods
”’
”’
__init__() : will automatically called when object is created
”’
class Book:
book_count = 0 # class level variable

def __init__(self,title): # object level method
self.title=title # object level variable
total = 0 #normal variable
Book.book_count+=1
@classmethod
def output(cls):
print(“Total book now available = “, Book.book_count)

b1 = Book(“Python Programming”)
b2 = Book(“SQL Programming”)
b3 = Book(“”)
print(type(b1))

#############
print(“Printing book_count: “)
print(b1.book_count)
print(b2.book_count)
print(b3.book_count)
print(Book.book_count)
print(“Printing output:”)
b1.output()
b2.output()
b3.output()
Book.output()
print(“Printing Title”)
print(“B1 title: “, b1.title)
print(“B2 title: “, b2.title)
print(“B3 title: “, b3.title)
#print(Book.title) AttributeError: type object ‘Book’ has no attribute ‘title’

##############
class MyMathOp:

def __init__(self,a,b):
self.n1 = a
self.n2 = b

def add_numbers(self):
self.total = self.n1 + self.n2

def subtract_numbers(self):
self.subtract = self.n1 – self.n2

def check_prime(self):
# check if n1 is prime or not
self.checkPrime = True
for i in range(2, self.n1//2+1):
if self.n1 % i==0:
self.checkPrime=False

m1 = MyMathOp(15,10)
print(m1.n1)
m1.check_prime()
print(m1.checkPrime)
”’
Encapsulation: information hiding – creating class
Abstraction: implementation hiding
Inheritance: inheritance properties from another class
Polymorphism: having multiple forms

”’

class Shape:
def __init__(self,s1=0,s2=0,s3=0,s4=0):
self.s1 = s1
self.s2 = s2
self.s3 = s3
self.s4 = s4
self.area = –1
self.surfacearea = –1

def print_val(self):
if self.s1>0:
print(“Side 1 = “,self.s1)
if self.s2>0:
print(“Side 2 = “,self.s2)
if self.s3>0:
print(“Side 3 = “,self.s3)
if self.s4>0:
print(“My Side 4 = “,self.s4)

def myarea(self):
print(“Area is not implemented!”)

”’
def mysurfacearea(self):
print(“Suraface area is not implemented!”)
”’
class Rectangle(Shape):
def __init__(self,s1,s2):
Shape.__init__(self,s1,s2)

def myarea(self):
print(“Area is”,self.s1*self.s2)

class Circle(Shape):
def __init__(self,s1):
Shape.__init__(self,s1)

def myarea(self):
print(“Area is”,3.14*self.s1*self.s2)


r1 = Rectangle(34,45)
r1.print_val()
r1.myarea()
c1 = Circle(12)
c1.print_val()
c1.myarea()
”’
Encapsulation: information hiding – creating class
Abstraction: implementation hiding
Inheritance: inheritance properties from another class
Polymorphism: having multiple forms

”’

class Shape:
def __init__(self,s1=0,s2=0,s3=0,s4=0):
self.s1 = s1
self.s2 = s2
self.s3 = s3
self.s4 = s4
self.area = –1
self.surfacearea = –1

def print_val(self):
if self.s1>0:
print(“Side 1 = “,self.s1)
if self.s2>0:
print(“Side 2 = “,self.s2)
if self.s3>0:
print(“Side 3 = “,self.s3)
if self.s4>0:
print(“My Side 4 = “,self.s4)

def myarea(self):
print(“Area is not implemented!”)

def myarea(self,s1):
pass

def myarea(self,s1,s2):
pass

def mysurfacearea(self):
print(“Suraface area is not implemented!”)

def dummy1(self): #public member
print(“Shape.Dummy1”)

def _dummy2(self): # protected
print(“Shape.Dummy2”)
def __dummy3(self): # protected
print(“Shape.Dummy2”)

def dummy4(self):
Shape.__dummy3(self)
class Rectangle(Shape):
def __init__(self,s1,s2):
Shape.__init__(self,s1,s2)

def myarea(self):
print(“Area is”,self.s1*self.s2)

class Circle(Shape):
def __init__(self,s1):
Shape.__init__(self,s1)

def myarea(self):
print(“Area is”,3.14*self.s1*self.s2)

class Cuboid(Rectangle):
def something(self):
print(“In Cuboid”)

class AnotherShape:
def test1(self):
print(“AnotherShape.test1”)
Shape.dummy1(self)
Shape._dummy2(self)
#Shape.__dummy3(self)


r1 = Rectangle(34,45)
r1.print_val()
r1.myarea()
c1 = Circle(12)
c1.print_val()
c1.myarea()

s1 = Shape()
#s1.myarea()
#s1.myarea(10)
#s1.area(10,20)

as1 = AnotherShape()
as1.test1()
”’
public: anyone can call public members of a class
protected (_var): (concept exists but practically it doesnt exist) – behaves like public
concept: only the derived class call
private (__var): available only within the given class
”’
#s1.__dummy3()
#r1.__dummy3()
s1.dummy4()
# Exception Handling – Errors
# syntax error
print(“hello”)

# logical error

# runtime errors – exceptions
a = 50
try:
b = int(input(“Enter the denominator: “))
except ValueError:
print(“You have provided invalid value for B, changing the value to 1”)
b = 1

try:
print(a/b) # ZeroDivisionError
print(“A by B is”,a/b)
except ZeroDivisionError:
print(“Sorry, we cant perform the analysis as denominator is zero”)

print(“thank you”)

################
a = 50
b = input(“Enter the denominator: “)
try:
print(“A by B is”, a / int(b)) # ZeroDivisionError & ValueError

except ValueError:
print(“You have provided invalid value for B, changing the value to 1”)
b = 1

except ZeroDivisionError:
print(“Sorry, we cant perform the analysis as denominator is zero”)

except Exception:
print(“An error has occurred, hence skipping this section”)

else:
print(“So we got the answer now!”)

finally:
print(“Not sure if there was an error but we made it through”)
print(“thank you”)

# File handling
”’
Working with Text files:
1. read: read(), readline(), readlines()
2. write: write(), writelines()
3. append

Modes: r,r+, w, w+, a, a+

Accessing the file:
1. Absolute path:
2. Relative path:
”’
path=“C:/Folder1/Folder2/txt1.txt”
path=“C:\\Folder1\\Folder2\\txt1.txt”
path=“ptxt1.txt”

content=”’Twinkle twinkle little star
How I wonder what you are
Up above the world so high
like a diamond in the sky
”’

file_obj = open(path,“a+”)

file_obj.write(content)

file_obj.seek(0) # go to the beginning of the content

read_cnt = file_obj.read()
file_obj.close()

print(read_cnt)

#################

path=“ptxt1.txt”

file_obj = open(path,“r”)

read_cnt = file_obj.read()

print(“============”)
print(read_cnt)
file_obj.seek(0)
read_cnt = file_obj.read(10)
print(“============”)
print(read_cnt)

read_cnt = file_obj.readline()
print(“============”)
print(read_cnt)
read_cnt = file_obj.readline(10000)
print(“============”)
print(read_cnt)
file_obj.close()

################

path=“ptxt1.txt”

file_obj = open(path,“r+”)

file_cnt = file_obj.readlines()

print(file_cnt)
file_obj.close()

file_obj = open(path,“w”)
write_nt = [‘Hello how are you?\n,‘I am fine\n,‘Where are you going\n,‘sipdfjisdjisdjf\n]
file_obj.writelines(write_nt)
file_obj.close()

PART 2: DATA SCIENCE NOV 2023

# NUMPY
# pip install numpy
import numpy as np
nums = range(16)
nums = np.reshape(nums,(8,2))
print(nums)
nums = np.reshape(nums,(4,4))
print(nums)
print(“Shape: Rows = “,nums.shape[0], “and columns = “,nums.shape[1])
# indexing
print(nums[1,2], nums[-3,-2])
print(nums[1]) # 2nd row
print(nums[:,1]) # : rows from 0th to (n-1)th
print(nums[-1], nums[:,-2], nums[-1,-2])

# to give your own set of values, you need to provide in terms of list
l1 = [[1,5,7],[2,4,9],[1,1,3],[3,3,2]]
# array is a function to convert list into numpy
mat1 = np.array(l1)
print(mat1)

print(np.zeros((3,3)))
print(np.ones((3,3)))
print(np.full((5,7),2.0))
print(np.full((5,7),9))

# eye – identity matrix: square matrix with 1 on its main diagonal
mat1 = np.eye(5)
print(mat1)

# NUMPY
import numpy as np
# to give your own set of values, you need to provide in terms of list
l1 = [[1,5,7],[2,4,9],[1,1,3],[3,3,2]]
# array is a function to convert list into numpy
mat1 = np.array(l1) # 4 * 3 – shape
print(mat1)
l2 = [[2,3,4],[2,1,2],[5,2,3],[3,2,2]]
# array is a function to convert list into numpy
mat2 = np.array(l2)
print(mat2)

# Matrices operations
print(mat1 + mat2)
print(np.add(mat1, mat2))

print(mat1 – mat2)
print(np.subtract(mat1, mat2))

print(mat1 * mat2)
print(np.multiply(mat1, mat2))

print(mat1 / mat2)
print(np.divide(mat1, mat2))

# actual matrix multiplication is done using matmul()
l3 = [[2,3,4],[2,1,2],[5,2,3]]
# array is a function to convert list into numpy
mat3 = np.array(l3)
print(mat3)
print(“Matrix Multiplication”)
print(np.matmul(mat1, mat3))
print(mat1 @ mat3)
## calculating determinant

l4 = [[1,3,5],[1,3,1],[2,3,4]]
mat5 = np.array(l4)
det_mat5 = np.linalg.det(mat5)
print(“Determinant of matrix 5 is”,det_mat5)
print(“Inverse of matrix 5 is: \n,np.linalg.inv(mat5))

”’
Linear Algebra Equation:
x1 + 5×2 = 7
-2×1 – 7×2 = -5

x1 = -8, x2= 3,
”’
coeff_mat = np.array([[1,5],[-2,-7]])
#var_mat = np.array([[x1],[x2]])
result_mat = np.array([[7],[-5]])
# equation here is coeff_mat * var_mat = result_mat [eg: 5 * x = 10]
# which is, var_mat = coeff_mat inv * result_mat
det_coeff_mat = np.linalg.det(coeff_mat)
if det_coeff_mat !=0:
var_mat = np.linalg.inv(coeff_mat) @ result_mat
print(“X1 = “,var_mat[0,0])
print(“X2 = “,var_mat[1,0])
else:
print(“Solution is not possible”)

# # scipy = scientific python
# pip install scipy
”’
#Inequality = OPTIMIZATION or MAXIMIZATION / MINIMIZATION PROBLEM
Computer Parts Assembly:
Laptops & Desktops
profit: 1000, 600
objective: either maximize profit or minimize cost

constraints:
1. Demand: 500, 600
2. Parts: Memory card: 5000 cards available
3. Manpower: 25000 minutes


”’

”’
Optimization using Scipy
let’s assume d = desktop, n = notebooks

Constraints:
1. d + n <= 10000
2. 2d + n <= 15000
3. 3d + 4n <= 25000

profit: 1000 d + 750 n => maximize
-1000d – 750 n =>minimize

”’
import numpy as np
from scipy.optimize import minimize, linprog
d = 1
n = 1
profit_d = 1000
profit_n = 750
profit = d * profit_d + n * profit_n
obj = [-profit_d, -profit_n]
lhs_con = [[1,1],[2,1],[3,4]]
rhs_con = [10000, 15000, 25000]

boundary = [(0, float(“inf”)), # boundary condition for # of desktops
(10, 200000)] # we just added some limit for notebooks
opt = linprog(c=obj, A_ub=lhs_con, b_ub=rhs_con, bounds=boundary, method=“revised simplex”)
print(opt)
if opt.success:
print(f”Number of desktops = {opt.x[0]} and number of laptops = {opt.x[1]})
print(“Maximum profit that can be generated = “,-1 * opt.fun)
else:
print(“Solution can not be generated”)

### ### ### PANDAS
# Pandas – dataframe which resembles Table structure
# pip install pandas
import pandas as pd
df1 = pd.DataFrame()
print(df1)
print(type(df1))

# fruit production
data = [[“Apple”, 15000, 11000,6000],
[“Banana”, 18000,22000,29000],
[“Mango”, 2, 900, 19000],
[“Guava”, 19000,11000,25000]]

fruit_production = pd.DataFrame(data)
print(fruit_production)
print(“Slicing 1:\n)
print(fruit_production.iloc[1:3,2:]) #based on index
print(“Slicing 2:\n)
print(fruit_production.loc[1:3,2:]) #based on title(names)

fruit_production = pd.DataFrame(data,
columns=[“Fruits”,“January”,“February”,“March”])
print(fruit_production)

fruit_production = pd.DataFrame(data,
columns=[“Fruits”,“January”,“February”,“March”],
index=[“Fruit 1”,“Fruit 2”,“Fruit 3”,“Fruit 4”])
print(fruit_production)

## dataframe.loc() dataframe.iloc()

print(“Slicing 1:\n)
print(fruit_production.iloc[1:3,2:]) #based on index
print(“Slicing 2:\n)
print(fruit_production.loc[[“Fruit 2”, “Fruit 3”],[“February”,“March”]]) #based on title(names)

### ###

# pandas
# pip install pandas
import pandas as pd
l1 = [10,20,30,40,50]
l1 = [[“Sachin”,101,20000,“BATSMAN”],[“Kapil”,501,12000,“BOWLER”],
[“Sunil”,12,21000,“BATSMAN”],[“Zaheer”,725,2000,“BOWLER”]]
df1 = pd.DataFrame(l1,columns=[“Player”,“Wickets”,“Runs”,“Type”],
index=[“Player 1”,“Player 2”,“Player 3”,“Player 4”])
print(df1)

d1 = {‘Apple’:[12000,11000,13000],
‘Banana’: [17000,18000,19000],
‘Mango’:[11000,13000,15000]}
df2 = pd.DataFrame(d1)
print(df2)

# creating dataframe from list of dictionary
data1 = [{“Guava”:9000, “Oranges”: 5000},
{“Guava”:8000, “Oranges”: 7000},
{“Guava”:10000, “Oranges”: 6000}]
df3 = pd.DataFrame(data1)
print(df3)

print(df3.iloc[0,:]) #first row and all column values
print(df3.iloc[:,0])

print(df2.iloc[:,0:2])
print(df2.iloc[[0,2],[0,2]])

#
print(df2.loc[[0,2],[“Apple”,“Mango”]])
print(df1.loc[[“Player 1”,“Player 4”],[“Player”,“Runs”]])

df2.iloc[2,0] = 14000
print(df2)
print(“========= DF1 =============”)
df1[‘Avg’] = df1[‘Runs’] / df1[“Wickets”]
print(df1)
print(“Reading data from DF1: “)
df4 = df1[df1.Player !=‘Sachin’] #filter where clause
print(\n\n New dataset without Sachin: \n, df4)
df1 = df1.drop(“Player”,axis=1) # axis default is 0
# unlike pop() and del – drop() returns a new dataframe
print(df1)


print(“Average Wickets of all the players = “,df1[‘Wickets’].mean())
print(“Average Wickets of players by type = \n\n,df1.groupby(‘Type’).mean())
# axis = 0 refers to rows
# axis = 1 refers to columns

print(\n\nDropping columns from DF1: “)
del df1[‘Wickets’] #dropping column Wickets using del
print(df1)

df1.pop(‘Runs’) #dropping column using pop
print(df1)
#

import pandas as pd

ud_df = pd.read_csv(“D:/datasets/gitdataset/user_device.csv”)
print(ud_df) # 272 rows x 6 columns
print(“Rows: “,ud_df.shape[0])
print(“Columns: “,ud_df.shape[1])

print(ud_df.tail(1))
print(ud_df.head(1))

use_df = pd.read_csv(“D:/datasets/gitdataset/user_usage.csv”)
print(use_df) # 240 rows x 4 columns

result_df = pd.merge(use_df[[‘use_id’,‘monthly_mb’,‘outgoing_sms_per_month’,
‘outgoing_mins_per_month’]], ud_df,
on=‘use_id’)
print(result_df) # [159 rows x 9 columns] = ud_df: 159 + 113, use_df = 159 + 81

result_df = pd.merge(use_df[[‘use_id’,‘monthly_mb’,‘outgoing_sms_per_month’,
‘outgoing_mins_per_month’]], ud_df,
on=‘use_id’, how=‘outer’)
print(result_df)

result_df = pd.merge(use_df[[‘use_id’,‘monthly_mb’,‘outgoing_sms_per_month’,
‘outgoing_mins_per_month’]], ud_df,
on=‘use_id’, how=‘left’)
print(result_df)

result_df = pd.merge(use_df[[‘use_id’,‘monthly_mb’,‘outgoing_sms_per_month’,
‘outgoing_mins_per_month’]], ud_df,
on=‘use_id’, how=‘right’)
print(result_df)

## Working with Pandas – Example ##
import pandas as pd
import numpy as np
df = pd.read_csv(“D:/datasets/gitdataset/hotel_bookings.csv”)
print(df.shape)
print(df.dtypes)
”’
numeric – int, float
categorical – 1) Nominal – there is no order 2) Ordinal – here order is imp
”’
df_numeric = df.select_dtypes(include=[np.number])
print(df_numeric)

df_object= df.select_dtypes(exclude=[np.number])
print(df_object) # categorical and date columns

print(df.columns)
for col in df.columns:
missing = np.mean(df[col].isnull())
if missing >0:
print(f”{col}{missing})

”’
Phases:
1. Business objective
2. Collect the relevant data
3. Preprocessing – making data ready for use
a. Handle missing values
b. Feature scaling – scale the values in the column to similar range
c. Outliers / data correction
d. handling categorical data:
i. Encode the data to convert text to number
East = 0, North = 1, South = 2, West = 3
ii. Column Transform into multple columns
iii. Delete any one column
4. EDA- Exploratory Data Analysis: to understand the data
5. MODEL BUILDING – Divide the train and test


”’
import pandas as pd
df = pd.read_csv(“https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/1_Data_PreProcessing.csv”)
print(df)

Phases:
1. Business objective
2. Collect the relevant data
3. Preprocessing – making data ready for use
a. Handle missing values
b. Feature scaling – scale the values in the column to similar range
c. Outliers / data correction
d. handling categorical data:
i. Encode the data to convert text to number
East = 0, North = 1, South = 2, West = 3
ii. Column Transform into multple columns
iii. Delete any one column
4. EDA- Exploratory Data Analysis: to understand the data
5. MODEL BUILDING –
a. Divide the train and test
b. Run the model
6. EVALUATE THE MODEL:
a. Measure the performance of each algorithm on the test data
b. Metric to compare: based on Regression (MSE, RMSE, R square) or
classification (confusion matrix -accuracy, sensitivity..)
c. select the best performing model
7. DEPLOY THE BEST PERFORMING MODEL

Hypothesis test:
1. Null Hypothesis (H0): starting statement (objective)
Alternate Hypethesis (H1): Alternate of H0

Z or T test:
Chi square test: both are categorical

e.g. North zone: 50 WIN 5 LOSS – p = 0.005

# simple (single value) v composite (specifies range)
# two tailed test v one tailed test [H0: mean = 0,
H1 Left Tailed: mean <0
H1 Right Tailed: mean >0
# level of significance:
alpha value: confidence interval – 95%
p value: p value <0.05 – we reject Null Hypothesis

import pandas as pd
df = pd.read_csv(“https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/1_Data_PreProcessing.csv”)
X = df.iloc[:,:3].values
y = df.iloc[:,3].values
#print(“X: \n”)
#print(X)
#print(“Y: \n”)
#print(y)

# scikit-learn package to perform ML
# install the package by: pip install scikit-learn
# but when you import, its sklearn

# Complete tutorial on sklearn:
# https://scikit-learn.org/stable/

# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=‘mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)

# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0])
print(X)

import pandas as pd
df = pd.read_csv(“https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/1_Data_PreProcessing.csv”)
X = df.iloc[:,:3].values
y = df.iloc[:,3].values
#print(“X: \n”)
#print(X)
#print(“Y: \n”)
#print(y)

# scikit-learn package to perform ML
# install the package by: pip install scikit-learn
# but when you import, its sklearn

# Complete tutorial on sklearn:
# https://scikit-learn.org/stable/

# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=‘mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)

# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[0])],remainder=‘passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
#print(X)

# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
print(X_train)
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)

”’
Regression: Output (Marks) is a continous variable
Algorithm: Simple (as it has only 1 X column) Linear (assuming that dataset is linear) Regression
X – independent variable(s)
Y – dependent variable
”’
import pandas as pd
import matplotlib.pyplot as plt
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/2_Marks_Data.csv”
df = pd.read_csv(link)
X = df.iloc[:,:1].values
y = df.iloc[:,1].values

”’
# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=’mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)

# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[0])],remainder=’passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
#print(X)
”’

# EDA – Exploratory Data Analysis
plt.scatter(x=df[‘Hours’],y=df[‘Marks’])
plt.show()
”’
Scatter plots – shows relationship between X and Y variables. You can have:
1. Positive correlation:
2. Negative correlation:
3. No Correlation
4. Correlation: 0 to +/- 1
5. Correlation value: 0 to +/- 0.5 : no correlation
6. Strong correlation value will be closer to +/- 1
7. Equation: straight line => y = mx + c
”’
# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=100)
print(X_train)

”’
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)
”’

## RUN THE MODEL
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
# fit – train the model
regressor.fit(X_train, y_train)
print(f”M/Coefficient/Slope = {regressor.coef_} and the Constant = {regressor.intercept_})

# y = 7.5709072 X + 20.1999196152844
# M/Coefficient/Slope = [7.49202113] and the Constant = 21.593606679699406

y_pred = regressor.predict(X_test)
result_df =pd.DataFrame({‘Actual’: y_test, ‘Predicted’: y_pred})
print(result_df)

# Analyze the output
”’
Regression: Output (Marks) is a continous variable
Algorithm: Simple (as it has only 1 X column) Linear (assuming that dataset is linear) Regression
X – independent variable(s)
Y – dependent variable
”’
import pandas as pd
import matplotlib.pyplot as plt
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/2_Marks_Data.csv”
df = pd.read_csv(link)
X = df.iloc[:,:1].values
y = df.iloc[:,1].values

”’
# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=’mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)

# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[0])],remainder=’passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
#print(X)
”’

# EDA – Exploratory Data Analysis
plt.scatter(x=df[‘Hours’],y=df[‘Marks’])
plt.show()
”’
Scatter plots – shows relationship between X and Y variables. You can have:
1. Positive correlation:
2. Negative correlation:
3. No Correlation
4. Correlation: 0 to +/- 1
5. Correlation value: 0 to +/- 0.5 : no correlation
6. Strong correlation value will be closer to +/- 1
7. Equation: straight line => y = mx + c
”’
# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=100)
print(X_train)

”’
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)
”’

## RUN THE MODEL
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
# fit – train the model
regressor.fit(X_train, y_train)
print(f”M/Coefficient/Slope = {regressor.coef_} and the Constant = {regressor.intercept_})

# y = 7.5709072 X + 20.1999196152844
# M/Coefficient/Slope = [7.49202113] and the Constant = 21.593606679699406

y_pred = regressor.predict(X_test)
result_df =pd.DataFrame({‘Actual’: y_test, ‘Predicted’: y_pred})
print(result_df)

# Analyze the output
from sklearn import metrics
mse = metrics.mean_squared_error(y_true=y_test, y_pred=y_pred)
print(“Root Mean Squared Error (Variance) = “,mse**0.5)
mae = metrics.mean_absolute_error(y_true=y_test, y_pred=y_pred)
print(“Mean Absolute Error = “,mae)
print(“R Square is (Variance)”,metrics.r2_score(y_test, y_pred))

## Bias is based on training data
y_pred_tr = regressor.predict(X_train)
mse = metrics.mean_squared_error(y_true=y_train, y_pred=y_pred_tr)
print(“Root Mean Squared Error (Bias) = “,mse**0.5)
print(“R Square is (Bias)”,metrics.r2_score(y_train, y_pred_tr))
## Bias v Variance

import pandas as pd
import matplotlib.pyplot as plt
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/3_Startups.csv”
df = pd.read_csv(link)
print(df.describe())
X = df.iloc[:,:4].values
y = df.iloc[:,4].values

”’
# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=’mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)
”’
# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,3] = lc.fit_transform(X[:,3])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[3])],remainder=‘passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
print(X)


# EDA – Exploratory Data Analysis
plt.scatter(x=df[‘Administration’],y=df[‘Profit’])
plt.show()
plt.scatter(x=df[‘R&D Spend’],y=df[‘Profit’])
plt.show()
plt.scatter(x=df[‘Marketing Spend’],y=df[‘Profit’])
plt.show()

# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=100)
print(X_train)

”’
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)
”’


## RUN THE MODEL
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
# fit – train the model
regressor.fit(X_train, y_train)
print(f”M/Coefficient/Slope = {regressor.coef_} and the Constant = {regressor.intercept_})

# y = -3791.2 x Florida -3090.1 x California + 0.82 R&D – 0.05 Admin + 0.022 Marketing+ 56650


y_pred = regressor.predict(X_test)
result_df =pd.DataFrame({‘Actual’: y_test, ‘Predicted’: y_pred})
print(result_df)

# Analyze the output
from sklearn import metrics
mse = metrics.mean_squared_error(y_true=y_test, y_pred=y_pred)
print(“Root Mean Squared Error (Variance) = “,mse**0.5)
mae = metrics.mean_absolute_error(y_true=y_test, y_pred=y_pred)
print(“Mean Absolute Error = “,mae)
print(“R Square is (Variance)”,metrics.r2_score(y_test, y_pred))

## Bias is based on training data
y_pred_tr = regressor.predict(X_train)
mse = metrics.mean_squared_error(y_true=y_train, y_pred=y_pred_tr)
print(“Root Mean Squared Error (Bias) = “,mse**0.5)
print(“R Square is (Bias)”,metrics.r2_score(y_train, y_pred_tr))

”’
Case 1: All the columns are taken into account:
Mean Absolute Error = 8696.887641252619
R Square is (Variance) 0.884599945166969
Root Mean Squared Error (Bias) = 7562.5657508560125
R Square is (Bias) 0.9624157828452926
”’
## Testing

import statsmodels.api as sm
import numpy as np
X = np.array(X, dtype=float)
print(“Y:\n,y)
summ1 = sm.OLS(y,X).fit().summary()
print(“Summary of All X \n—————-\n:”,summ1)

import pandas as pd
import matplotlib.pyplot as plt
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/3_Startups.csv”
df = pd.read_csv(link)
print(df.describe())
X = df.iloc[:,:4].values
y = df.iloc[:,4].values

”’
# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=’mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)
”’
# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,3] = lc.fit_transform(X[:,3])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[3])],remainder=‘passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
print(X)

”’
After doing Backward elemination method we realized that all the state columns
are not significantly impacting the analysis hence removing those 2 columns too.
”’
X = X[:,2:] # after backward elemination

# EDA – Exploratory Data Analysis
plt.scatter(x=df[‘Administration’],y=df[‘Profit’])
plt.show()
plt.scatter(x=df[‘R&D Spend’],y=df[‘Profit’])
plt.show()
plt.scatter(x=df[‘Marketing Spend’],y=df[‘Profit’])
plt.show()

# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=100)
print(X_train)

”’
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)
”’


## RUN THE MODEL
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
# fit – train the model
regressor.fit(X_train, y_train)
print(f”M/Coefficient/Slope = {regressor.coef_} and the Constant = {regressor.intercept_})

# y = -3791.2 x Florida -3090.1 x California + 0.82 R&D – 0.05 Admin + 0.022 Marketing+ 56650


y_pred = regressor.predict(X_test)
result_df =pd.DataFrame({‘Actual’: y_test, ‘Predicted’: y_pred})
print(result_df)

# Analyze the output
from sklearn import metrics
mse = metrics.mean_squared_error(y_true=y_test, y_pred=y_pred)
print(“Root Mean Squared Error (Variance) = “,mse**0.5)
mae = metrics.mean_absolute_error(y_true=y_test, y_pred=y_pred)
print(“Mean Absolute Error = “,mae)
print(“R Square is (Variance)”,metrics.r2_score(y_test, y_pred))

## Bias is based on training data
y_pred_tr = regressor.predict(X_train)
mse = metrics.mean_squared_error(y_true=y_train, y_pred=y_pred_tr)
print(“Root Mean Squared Error (Bias) = “,mse**0.5)
print(“R Square is (Bias)”,metrics.r2_score(y_train, y_pred_tr))

”’
Case 1: All the columns are taken into account:
Mean Absolute Error = 8696.887641252619
R Square is (Variance) 0.884599945166969
Root Mean Squared Error (Bias) = 7562.5657508560125
R Square is (Bias) 0.9624157828452926
”’
## Testing

import statsmodels.api as sm
import numpy as np
X = np.array(X, dtype=float)
#X = X[:,[2,3,4]]
print(“Y:\n,y)
summ1 = sm.OLS(y,X).fit().summary()
print(“Summary of All X \n—————-\n:”,summ1)

## Test for linearity
# 1. All features (X) should be correlated to Y
# 2. Multicollinearity: Within X there should not be any correlation,
# if its there then take any one for the analysis

import pandas as pd
import matplotlib.pyplot as plt
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/4_Position_Salaries.csv”
df = pd.read_csv(link)
print(df.describe())
X = df.iloc[:,1:2].values
y = df.iloc[:,2].values

”’
# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=’mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)
”’
”’
# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,3] = lc.fit_transform(X[:,3])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[3])],remainder=’passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
print(X)

”’
”’
After doing Backward elemination method we realized that all the state columns
are not significantly impacting the analysis hence removing those 2 columns too.

X = X[:,2:] # after backward elemination
”’
”’
# EDA – Exploratory Data Analysis
plt.scatter(x=df[‘Level’],y=df[‘Salary’])
plt.show()
”’

# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=100)
print(X_train)

from sklearn.linear_model import LinearRegression
from sklearn import metrics
”’
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)
”’
”’
#Since dataset is too small, lets take entire data for training
X_train, y_train = X,y
X_test, y_test = X,y
”’
”’
## RUN THE MODEL

regressor = LinearRegression()
# fit – train the model
regressor.fit(X_train, y_train)
print(f”M/Coefficient/Slope = {regressor.coef_} and the Constant = {regressor.intercept_}”)

# y =
y_pred = regressor.predict(X_test)
result_df =pd.DataFrame({‘Actual’: y_test, ‘Predicted’: y_pred})
print(result_df)

# Analyze the output

mse = metrics.mean_squared_error(y_true=y_test, y_pred=y_pred)
print(“Root Mean Squared Error (Variance) = “,mse**0.5)
mae = metrics.mean_absolute_error(y_true=y_test, y_pred=y_pred)
print(“Mean Absolute Error = “,mae)
print(“R Square is (Variance)”,metrics.r2_score(y_test, y_pred))

## Bias is based on training data
y_pred_tr = regressor.predict(X_train)
mse = metrics.mean_squared_error(y_true=y_train, y_pred=y_pred_tr)
print(“Root Mean Squared Error (Bias) = “,mse**0.5)
print(“R Square is (Bias)”,metrics.r2_score(y_train, y_pred_tr))

# Plotting the data for output
plt.scatter(x=df[‘Level’],y=df[‘Salary’])
plt.plot(X,y_pred)
plt.xlabel(“Level”)
plt.ylabel(“Salary”)
plt.show()
”’

# 3. Model – Polynomial regression analysis
# y = C + m1 * X + m2 * x square
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline

for i in range(1,10):
#prepare the parameters
parameters = [(‘polynomial’, PolynomialFeatures(degree=i)),(‘modal’,LinearRegression())]
pipe = Pipeline(parameters)
pipe.fit(X_train,y_train)
y_pred = pipe.predict(X)
## Bias is based on training data
y_pred_tr = pipe.predict(X_train)
mse = metrics.mean_squared_error(y_true=y_train, y_pred=y_pred_tr)
rmse_tr = mse ** 0.5
print(“Root Mean Squared Error (Bias) = “,rmse_tr)
print(“R Square is (Bias)”,metrics.r2_score(y_train, y_pred_tr))

## Variance is based on validation data
y_pred_tt = pipe.predict(X_test)
mse = metrics.mean_squared_error(y_true=y_test, y_pred=y_pred_tt)
rmse_tt = mse ** 0.5
print(“Root Mean Squared Error (Variance) = “, rmse_tt)
print(“R Square is (Variance)”, metrics.r2_score(y_test, y_pred_tt))
print(“Difference Between variance and bias = “,rmse_tt – rmse_tr)
# Plotting the data for output
plt.scatter(x=df[‘Level’],y=df[‘Salary’])
plt.plot(X,y_pred)
plt.title(“Polynomial Analysis degree =”+str(i))
plt.xlabel(“Level”)
plt.ylabel(“Salary”)
plt.show()

import pandas as pd
import matplotlib.pyplot as plt
#link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/4_Position_Salaries.csv”
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/3_Startups.csv”
df = pd.read_csv(link)
print(df.describe())
X = df.iloc[:,0:4].values
y = df.iloc[:,4].values

”’
# 1. Replace the missing values with mean value
from sklearn.impute import SimpleImputer
import numpy as np
imputer = SimpleImputer(missing_values=np.nan, strategy=’mean’)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
#print(X)
”’

# 2. Handling categorical values
# encoding
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
lc = LabelEncoder()
X[:,3] = lc.fit_transform(X[:,3])

from sklearn.compose import ColumnTransformer
transform = ColumnTransformer([(‘one_hot_encoder’, OneHotEncoder(),[3])],remainder=‘passthrough’)
X=transform.fit_transform(X)
X = X[:,1:] # dropped one column
print(X)


”’
After doing Backward elemination method we realized that all the state columns
are not significantly impacting the analysis hence removing those 2 columns too.

X = X[:,2:] # after backward elemination
”’
”’
# EDA – Exploratory Data Analysis
plt.scatter(x=df[‘Level’],y=df[‘Salary’])
plt.show()
”’

# 3. splitting it into train and test test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=100)
print(X_train)

from sklearn.linear_model import LinearRegression
from sklearn import metrics
”’
# 4. Scaling / Normalization
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train[:,3:])
X_test = scale.fit_transform(X_test[:,3:])
print(X_train)
”’
”’
#Since dataset is too small, lets take entire data for training
X_train, y_train = X,y
X_test, y_test = X,y
”’

## RUN THE MODEL – Support Vector Machine Regressor (SVR)
from sklearn.svm import SVR
#regressor = SVR(kernel=’linear’)
#regressor = SVR(kernel=’poly’,degree=2,C=10)
# Assignment – Best value for gamma: 0.01 to 1 (0.05)
regressor = SVR(kernel=“rbf”,gamma=0.1,C=10)
# fit – train the model
regressor.fit(X_train, y_train)


# y =
y_pred = regressor.predict(X_test)
result_df =pd.DataFrame({‘Actual’: y_test, ‘Predicted’: y_pred})
print(result_df)

# Analyze the output

mse = metrics.mean_squared_error(y_true=y_test, y_pred=y_pred)
print(“Root Mean Squared Error (Variance) = “,mse**0.5)
mae = metrics.mean_absolute_error(y_true=y_test, y_pred=y_pred)
print(“Mean Absolute Error = “,mae)
print(“R Square is (Variance)”,metrics.r2_score(y_test, y_pred))

## Bias is based on training data
y_pred_tr = regressor.predict(X_train)
mse = metrics.mean_squared_error(y_true=y_train, y_pred=y_pred_tr)
print(“Root Mean Squared Error (Bias) = “,mse**0.5)
print(“R Square is (Bias)”,metrics.r2_score(y_train, y_pred_tr))


# Plotting the data for output
plt.scatter(X_train[:,2],y_pred_tr)
#plt.plot(X_train[:,2],y_pred_tr)
plt.show()

#Decision Tree & Random Forest
import pandas as pd
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/3_Startups.csv”
link = “D:\\datasets\\3_Startups.csv”
df = pd.read_csv(link)
print(df)

#X = df.iloc[:,:4].values
X = df.iloc[:,:1].values
y = df.iloc[:,:-1].values
from sklearn.model_selection import train_test_split
X_train, X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=100)

from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor()
regressor.fit(X_train,y_train)
y_pred = regressor.predict(X_test)

# Baging, Boosting, Ensemble
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators=10)
regressor.fit(X_train,y_train)
y_pred = regressor.predict(X_test)

## Assignment these algorithms and check the RMSE and R square values

# Ridge Lasso Elasticnet
import pandas as pd
link=“https://raw.githubusercontent.com/swapnilsaurav/Dataset/master/student_scores_multi.csv”
df = pd.read_csv(link)
print(df)
X = df.iloc[:,0:3].values
y = df.iloc[:,3].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.85, random_state=100)

from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
lr_ridge = Ridge(alpha=0.8)
lr_ridge.fit(X_train,y_train)
y_ridge_pred = lr_ridge.predict(X_test)

from sklearn.metrics import r2_score
r2_ridge_test = r2_score(y_test, y_ridge_pred)

y_ridge_pred_tr = lr_ridge.predict(X_train)
r2_ridge_train = r2_score(y_train, y_ridge_pred_tr)
print(f”Ridge Regression: Train R2 = {r2_ridge_train} and Test R2={r2_

# Classifications algorithm: supervised algo which predicts the class
”’
classifier: algorithm that we develop
model: training and predicting the outcome
features: the input data (columns)
target: class that we need to predict
classification: binary (2 class outcome) or multiclass (more than 2 classes)

Steps to run the model:
1. get the data
2. preprocess the data
3. eda
4. train the model
5. predict the model
6. evaluate the model

”’
#1. Logistic regression
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/5_Ads_Success.csv”
import pandas as pd
df = pd.read_csv(link)
X = df.iloc[:,1:4].values
y = df.iloc[:,4].values

from sklearn.preprocessing import LabelEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0] )

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=100)

# Scaling as Age and Salary are in different range of values
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

## Build the model
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)

# visualize the outcome
X_train = X_train[:,1:]
X_test = X_test[:,1:]
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import numpy as np
x_set, y_set = X_train, y_train
X1,X2 = np.meshgrid(np.arange(start = x_set[:,0].min()-1, stop=x_set[:,0].max()+1, step=0.01),
np.arange(start = x_set[:,1].min()-1, stop=x_set[:,1].max()+1, step=0.01))
plt.contourf(X1,X2,classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),
cmap=ListedColormap((‘red’,‘green’)))
plt.show()

https://designrr.page/?id=155238&token=545210681&type=FP&h=7849

# Classifications algorithm: supervised algo which predicts the class
”’
classifier: algorithm that we develop
model: training and predicting the outcome
features: the input data (columns)
target: class that we need to predict
classification: binary (2 class outcome) or multiclass (more than 2 classes)

Steps to run the model:
1. get the data
2. preprocess the data
3. eda
4. train the model
5. predict the model
6. evaluate the model

”’
#1. Logistic regression
link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/5_Ads_Success.csv”
link = “D:\\datasets\\5_Ads_Success.csv”
import pandas as pd
df = pd.read_csv(link)
X = df.iloc[:,1:4].values
y = df.iloc[:,4].values

from sklearn.preprocessing import LabelEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0] )

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=100)

# Scaling as Age and Salary are in different range of values
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

## Build the model
”’
## LOGISTIC REGRESSION
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
”’
from sklearn.svm import SVC
”’
## Support Vector Machine – Classifier
classifier = SVC(kernel=’linear’)

classifier = SVC(kernel=’rbf’,gamma=100, C=100)
”’
from sklearn.neighbors import KNeighborsClassifier
## Refer types of distances:
# https://designrr.page/?id=200944&token=2785938662&type=FP&h=7229

classifier = KNeighborsClassifier(n_neighbors=9, metric=‘minkowski’)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)

# visualize the outcome
X_train = X_train[:,1:]
X_test = X_test[:,1:]
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import numpy as np
x_set, y_set = X_train, y_train
X1,X2 = np.meshgrid(np.arange(start = x_set[:,0].min()-1, stop=x_set[:,0].max()+1, step=0.01),
np.arange(start = x_set[:,1].min()-1, stop=x_set[:,1].max()+1, step=0.01))
plt.contourf(X1,X2,classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),
cmap=ListedColormap((‘red’,‘green’)))

#Now we will plot training data
for i, j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set==j,0],
x_set[y_set==j,1], color=ListedColormap((“red”,“green”))(i),
label=j)
plt.show()

## Model Evaluation using Confusion Matrix
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(“Confusion Matrix: \n,cm)
cr = classification_report(y_test, y_pred)
accs = accuracy_score(y_test, y_pred)
print(“classification_report: \n,cr)
print(“accuracy_score: “,accs)

import sklearn.tree

link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/5_Ads_Success.csv”
link = “D:\\datasets\\5_Ads_Success.csv”
import pandas as pd
df = pd.read_csv(link)
X = df.iloc[:,1:4].values
y = df.iloc[:,4].values

from sklearn.preprocessing import LabelEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0] )

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=100)

# Scaling as Age and Salary are in different range of values
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

## Build the model
”’
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion=”gini”)
”’
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators=39, criterion=“gini”)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)

# visualize the outcome
X_train = X_train[:,1:]
X_test = X_test[:,1:]
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import numpy as np
x_set, y_set = X_train, y_train
X1,X2 = np.meshgrid(np.arange(start = x_set[:,0].min()-1, stop=x_set[:,0].max()+1, step=0.01),
np.arange(start = x_set[:,1].min()-1, stop=x_set[:,1].max()+1, step=0.01))
plt.contourf(X1,X2,classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),
cmap=ListedColormap((‘red’,‘green’)))

#Now we will plot training data
for i, j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set==j,0],
x_set[y_set==j,1], color=ListedColormap((“red”,“green”))(i),
label=j)
plt.show()

## Model Evaluation using Confusion Matrix
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(“Confusion Matrix: \n,cm)
cr = classification_report(y_test, y_pred)
accs = accuracy_score(y_test, y_pred)
print(“classification_report: \n,cr)
print(“accuracy_score: “,accs)

”’
# Show decision tree created

output = sklearn.tree.export_text(classifier)
print(output)
# visualize the tree
fig = plt.figure(figsize=(40,60))
tree_plot = sklearn.tree.plot_tree(classifier)
plt.show()
”’

”’
In Ensemble Algorithms – we run multiple algorithms to improve the performance
of a given business objective:
1. Boosting: When you run same algorithm – Input varies based on weights
2. Bagging: When you run same algorithm – average of all
3. Stacking: Over different algorithms – average of all
”’

import sklearn.tree

link = “https://raw.githubusercontent.com/swapnilsaurav/MachineLearning/master/5_Ads_Success.csv”
link = “D:\\datasets\\5_Ads_Success.csv”
import pandas as pd
df = pd.read_csv(link)
X = df.iloc[:,1:4].values
y = df.iloc[:,4].values

from sklearn.preprocessing import LabelEncoder
lc = LabelEncoder()
X[:,0] = lc.fit_transform(X[:,0] )

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=100)

# Scaling as Age and Salary are in different range of values
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

## Build the model
”’
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion=”gini”)

from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators=39, criterion=”gini”)
”’
from sklearn.ensemble import AdaBoostClassifier
classifier = AdaBoostClassifier(n_estimators=7)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)

# visualize the outcome
X_train = X_train[:,1:]
X_test = X_test[:,1:]
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import numpy as np
x_set, y_set = X_train, y_train
X1,X2 = np.meshgrid(np.arange(start = x_set[:,0].min()-1, stop=x_set[:,0].max()+1, step=0.01),
np.arange(start = x_set[:,1].min()-1, stop=x_set[:,1].max()+1, step=0.01))
plt.contourf(X1,X2,classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),
cmap=ListedColormap((‘red’,‘green’)))

#Now we will plot training data
for i, j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set==j,0],
x_set[y_set==j,1], color=ListedColormap((“red”,“green”))(i),
label=j)
plt.show()

## Model Evaluation using Confusion Matrix
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(“Confusion Matrix: \n,cm)
cr = classification_report(y_test, y_pred)
accs = accuracy_score(y_test, y_pred)
print(“classification_report: \n,cr)
print(“accuracy_score: “,accs)

”’
# Show decision tree created

output = sklearn.tree.export_text(classifier)
print(output)
# visualize the tree
fig = plt.figure(figsize=(40,60))
tree_plot = sklearn.tree.plot_tree(classifier)
plt.show()
”’

”’
In Ensemble Algorithms – we run multiple algorithms to improve the performance
of a given business objective:
1. Boosting: When you run same algorithm – Input varies based on weights
2. Bagging: When you run same algorithm – average of all
3. Stacking: Over different algorithms – average of all
”’

https://designrr.page/?id=36743&token=2022711066&type=FP&h=3547

 

from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

 

X,y = make_blobs(n_samples=300, n_features=3, centers=4)
plt.scatter(X[:,0], X[:,1])
plt.show()

 

from sklearn.cluster import KMeans
km = KMeans(n_clusters=5, init=“random”,max_iter=100)
y_cluster =km.fit_predict(X)

 

plt.scatter(X[y_cluster==0,0],X[y_cluster==0,1],c=“blue”,label=“Cluster A”)
plt.scatter(X[y_cluster==1,0],X[y_cluster==1,1],c=“red”,label=“Cluster B”)
plt.scatter(X[y_cluster==2,0],X[y_cluster==2,1],c=“green”,label=“Cluster C”)
plt.scatter(X[y_cluster==3,0],X[y_cluster==3,1],c=“black”,label=“Cluster D”)
plt.scatter(X[y_cluster==4,0],X[y_cluster==4,1],c=“orange”,label=“Cluster E”)
plt.show()

 

distortion = []
max_centers = 30
for i in range(1,max_centers):
km = KMeans(n_clusters=i, init=“random”, max_iter=100)
y_cluster = km.fit(X)
distortion.append(km.inertia_)

 

print(“Distortion:\n,distortion)
plt.plot(range(1,max_centers),distortion,marker=“o”)
plt.show()

 

import pandas as pd
import matplotlib.pyplot as plt
link = “D:\\Datasets\\USArrests.csv”
df = pd.read_csv(link)
#print(df)
X = df.iloc[:,1:]
from sklearn.preprocessing import normalize
data = normalize(X)
data = pd.DataFrame(data)
print(data)

## plotting dendogram
import scipy.cluster.hierarchy as sch
dendo = sch.dendrogram(sch.linkage(data, method=‘ward’))
plt.axhline(y=0.7,color=“red”)
plt.show()

link = “D:\\datasets\\Market_Basket_Optimisation.csv”
import pandas as pd
df = pd.read_csv(link)
print(df)
from apyori import apriori
transactions = []
for i in range(len(df)):
if i%100==0:
print(“I = “,i)
transactions.append([str(df.values[i,j]) for j in range(20)])

## remove nan from the list
print(“Transactions:\n,transactions)

association_algo = apriori(transactions, min_confidence=0.2, min_support=0.02, min_lift=2)
print(“Association = “,list(association_algo))

”’
Time Series Forecasting – ARIMA method

1. Read and visualize the data
2. Stationary series
3. Optimal parameters
4. Build the model
5. Prediction
”’
import pandas as pd
#Step 1: read the data
link = “D:\\datasets\\gitdataset\\AirPassengers.csv”
air_passengers = pd.read_csv(link)

”’
#Step 2: visualize the data
import plotly.express as pe
fig = pe.line(air_passengers,x=”Month”,y=”#Passengers”)
fig.show()
”’
# Cleaning the data
from datetime import datetime
air_passengers[‘Month’] = pd.to_datetime(air_passengers[‘Month’])
air_passengers.set_index(‘Month’,inplace=True)

#converting to time series data
import numpy as np
ts_log = np.log(air_passengers[‘#Passengers’])
#creating rolling period – 12 months
import matplotlib.pyplot as plt
”’
moving_avg = ts_log.rolling(12).mean
plt.plot(ts_log)
plt.plot(moving_avg)
plt.show()
”’
#Step 3: Decomposition into: trend, seasonality, error ( or residual or noise)
”’
Additive decomposition: linear combination of above 3 factors:
Y(t) =T(t) + S(t) + E(t)

Multiplicative decomposition: product of 3 factors:
Y(t) =T(t) * S(t) * E(t)
”’
from statsmodels.tsa.seasonal import seasonal_decompose
decomposed = seasonal_decompose(ts_log,model=“multiplicative”)
decomposed.plot()
plt.show()

# Step 4: Stationary test
”’
To make Time series analysis, the TS should be stationary.
A time series is said to be stationary if its statistical properties
(mean, variance, autocorrelation) doesnt change by a large value
over a period of time.
Types of tests:
1. Augmented Dickey Fuller test (ADH Test)
2. Kwiatkowski Phillips Schnidt Shin (KPSS) test
3. Phillips Perron (PP) Test

Null Hypothesis: The time series is not stationary
Alternate Hypothesis: Time series is stationary
If p >0.05 we reject Null Hypothesis
”’
from statsmodels.tsa.stattools import adfuller
result = adfuller(air_passengers[‘#Passengers’])
print(“ADF Stats: \n,result[0])
print(“p value = “,result[1])
”’
To reject Null hypothesis, result[0] less than 5% critical region value
and p > 0.05
”’

# Run the model
”’
ARIMA model: Auto-Regressive Integrative Moving Average
AR: p predicts the current value
I: d integrative by removing trend and seasonality component from previous period
MA: q represents Moving Average

AIC- Akaike’s Information Criterion (AIC) – helps to find optimal p,d,q values
BIC – Bayesian Information Criterion (BIC) – alternative to AIC
”’
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plot_acf(air_passengers[‘#Passengers’].diff().dropna())
plot_pacf(air_passengers[‘#Passengers’].diff().dropna())
plt.show()
”’
How to read above graph:
To find q (MA), we look at the Autocorrelation graph and see where there is a drastic change:
here, its at 1, so q = 1 (or 2 as at 2, it goes to -ve)

To find p (AR) – sharp drop in Partial Autocorrelation graph:
here, its at 1, so p = 1 (or 2 as at 2, it goes to -ve)

for d (I) – we need to try with multiple values
intially we will take as 1

”’
”’
Time Series Forecasting – ARIMA method

1. Read and visualize the data
2. Stationary series
3. Optimal parameters
4. Build the model
5. Prediction
”’
import pandas as pd
#Step 1: read the data
link = “D:\\datasets\\gitdataset\\AirPassengers.csv”
air_passengers = pd.read_csv(link)

”’
#Step 2: visualize the data
import plotly.express as pe
fig = pe.line(air_passengers,x=”Month”,y=”#Passengers”)
fig.show()
”’
# Cleaning the data
from datetime import datetime
air_passengers[‘Month’] = pd.to_datetime(air_passengers[‘Month’])
air_passengers.set_index(‘Month’,inplace=True)

#converting to time series data
import numpy as np
ts_log = np.log(air_passengers[‘#Passengers’])
#creating rolling period – 12 months
import matplotlib.pyplot as plt
”’
moving_avg = ts_log.rolling(12).mean
plt.plot(ts_log)
plt.plot(moving_avg)
plt.show()
”’
#Step 3: Decomposition into: trend, seasonality, error ( or residual or noise)
”’
Additive decomposition: linear combination of above 3 factors:
Y(t) =T(t) + S(t) + E(t)

Multiplicative decomposition: product of 3 factors:
Y(t) =T(t) * S(t) * E(t)
”’
from statsmodels.tsa.seasonal import seasonal_decompose
decomposed = seasonal_decompose(ts_log,model=“multiplicative”)
decomposed.plot()
plt.show()

# Step 4: Stationary test
”’
To make Time series analysis, the TS should be stationary.
A time series is said to be stationary if its statistical properties
(mean, variance, autocorrelation) doesnt change by a large value
over a period of time.
Types of tests:
1. Augmented Dickey Fuller test (ADH Test)
2. Kwiatkowski Phillips Schnidt Shin (KPSS) test
3. Phillips Perron (PP) Test

Null Hypothesis: The time series is not stationary
Alternate Hypothesis: Time series is stationary
If p >0.05 we reject Null Hypothesis
”’
from statsmodels.tsa.stattools import adfuller
result = adfuller(air_passengers[‘#Passengers’])
print(“ADF Stats: \n,result[0])
print(“p value = “,result[1])
”’
To reject Null hypothesis, result[0] less than 5% critical region value
and p > 0.05
”’

# Run the model
”’
ARIMA model: Auto-Regressive Integrative Moving Average
AR: p predicts the current value
I: d integrative by removing trend and seasonality component from previous period
MA: q represents Moving Average

AIC- Akaike’s Information Criterion (AIC) – helps to find optimal p,d,q values
BIC – Bayesian Information Criterion (BIC) – alternative to AIC
”’
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plot_acf(air_passengers[‘#Passengers’].diff().dropna())
plot_pacf(air_passengers[‘#Passengers’].diff().dropna())
plt.show()
”’
How to read above graph:
To find q (MA), we look at the Autocorrelation graph and see where there is a drastic change:
here, its at 1, so q = 1 (or 2 as at 2, it goes to -ve)

To find p (AR) – sharp drop in Partial Autocorrelation graph:
here, its at 1, so p = 1 (or 2 as at 2, it goes to -ve)

for d (I) – we need to try with multiple values
intially we will take as 1

”’
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(air_passengers[‘#Passengers’], order=(1,1,1))
result = model.fit()
plt.plot(air_passengers[‘#Passengers’])
plt.plot(result.fittedvalues)
plt.show()
print(“ARIMA Model Summary”)
print(result.summary())

model = ARIMA(air_passengers[‘#Passengers’], order=(4,1,4))
result = model.fit()
plt.plot(air_passengers[‘#Passengers’])
plt.plot(result.fittedvalues)
plt.show()
print(“ARIMA Model Summary”)
print(result.summary())

# Prediction using ARIMA model
air_passengers[‘Forecasted’] = result.predict(start=120,end=246)
air_passengers[[‘#Passengers’,‘Forecasted’]].plot()
plt.show()

# predict using SARIMAX Model
import statsmodels.api as sm
model = sm.tsa.statespace.SARIMAX(air_passengers[‘#Passengers’],order=(7,1,1), seasonal_order=(1,1,1,12))
result = model.fit()
air_passengers[‘Forecast_SARIMAX’] = result.predict(start=120,end=246)
air_passengers[[‘#Passengers’,‘Forecast_SARIMAX’]].plot()
plt.show()

https://drive.google.com/drive/folders/1Xe3HftLxL1T6HsEBUfjq_zXANjTnr6Cz?usp=drive_link

”’
NLP – Natural Language Processing – analysing review comment to understand
reasons for positive and negative ratings.
concepts like: unigram, bigram, trigram

Steps we generally perform with NLP data:
1. Convert into lowercase
2. decompose (non unicode to unicode)
3. removing accent: encode the content to ascii values
4. tokenization: will break sentence to words
5. Stop words: not important words for analysis
6. Lemmetization (done only on English words): convert the words into dictionary words
7. N-grams: set of one word (unigram), two words (bigram), three words (trigrams)
8. Plot the graph based on the number of occurrences and Evaluate
”’
”’
cardboard mousepad. Going worth price! Not bad
”’

link=“https://raw.githubusercontent.com/swapnilsaurav/OnlineRetail/master/order_reviews.csv”
import pandas as pd
import unicodedata
import nltk
import matplotlib.pyplot as plt
df = pd.read_csv(link)
print(list(df.columns))
”’
[‘review_id’, ‘order_id’, ‘review_score’, ‘review_comment_title’,
‘review_comment_message’, ‘review_creation_date’, ‘review_answer_timestamp’]
”’
df[‘review_creation_date’] = pd.to_datetime(df[‘review_creation_date’])

df[‘review_answer_timestamp’] = pd.to_datetime(df[‘review_answer_timestamp’])

# data preprocessing – making data ready for analysis
reviews_df = df[df[‘review_comment_message’].notnull()].copy()
#print(reviews_df)
”’
Write a function to perform basic preprocessing steps
”’
def basic_preprocessing(text):
txt_pp = text.lower()
print(txt_pp)
#remove accent

# applying basic preprocessing:
reviews_df[‘review_comment_message’] = \
reviews_df[‘review_comment_message’].apply(basic_preprocessing)

”’
NLP – Natural Language Processing – analysing review comment to understand
reasons for positive and negative ratings.
concepts like: unigram, bigram, trigram

Steps we generally perform with NLP data:
1. Convert into lowercase
2. decompose (non unicode to unicode)
3. removing accent: encode the content to ascii values
4. tokenization: will break sentence to words
5. Stop words: not important words for analysis
6. Lemmetization (done only on English words): convert the words into dictionary words
7. N-grams: set of one word (unigram), two words (bigram), three words (trigrams)
8. Plot the graph based on the number of occurrences and Evaluate
”’
”’
cardboard mousepad. Going worth price! Not bad
”’

link=“D:/datasets/OnlineRetail/order_reviews.csv”
import pandas as pd
import unicodedata
import nltk
import matplotlib.pyplot as plt
df = pd.read_csv(link)
print(list(df.columns))
”’
[‘review_id’, ‘order_id’, ‘review_score’, ‘review_comment_title’,
‘review_comment_message’, ‘review_creation_date’, ‘review_answer_timestamp’]
”’
#df[‘review_creation_date’] = pd.to_datetime(df[‘review_creation_date’])
#df[‘review_answer_timestamp’] = pd.to_datetime(df[‘review_answer_timestamp’])

# data preprocessing – making data ready for analysis
reviews_df = df[df[‘review_comment_message’].notnull()].copy()
#print(reviews_df)

# remove accents
def remove_accent(text):
return unicodedata.normalize(‘NFKD’,text).encode(‘ascii’,errors=‘ignore’).decode(‘utf-8’)
#STOP WORDS LIST:
STOP_WORDS = set(remove_accent(w) for w in nltk.corpus.stopwords.words(‘portuguese’))

”’
Write a function to perform basic preprocessing steps
”’
def basic_preprocessing(text):
#converting to lower case
txt_pp = text.lower()
#print(txt_pp)

#remove the accent
#txt_pp = unicodedata.normalize(‘NFKD’,txt_pp).encode(‘ascii’,errors=’ignore’).decode(‘utf-8’)
txt_pp =remove_accent(txt_pp)
#print(txt_pp)
#tokenize
txt_token = nltk.tokenize.word_tokenize(txt_pp)
#print(txt_token)

# removing stop words
txt_token = (w for w in txt_token if w not in STOP_WORDS and w.isalpha())
return txt_token

# applying basic preprocessing:
reviews_df[‘review_comment_words’] = \
reviews_df[‘review_comment_message’].apply(basic_preprocessing)

#get positive reviews – all 5 ratings in review_score
reviews_5 = reviews_df[reviews_df[‘review_score’]==5]

#get negative reviews – all 1 ratings
reviews_1 = reviews_df[reviews_df[‘review_score’]==1]

## write a function to creaet unigram, bigram, trigram
def create_ngrams(words):
unigram,bigrams,trigram = [],[],[]
for comment in words:
unigram.extend(comment)
bigrams.extend(.join(bigram) for bigram in nltk.bigrams(comment))
trigram.extend(‘ ‘.join(trigram) for trigram in nltk.trigrams(comment))
return unigram,bigrams,trigram

#create ngrams for rating 5 and rating 1
uni_5, bi_5, tri_5 = create_ngrams(reviews_5[‘review_comment_words’])
print(uni_5)
print(‘””””””””””””””””””‘)
print(bi_5)
print(” =========================================”)
print(tri_5)

uni_1, bi_1, tri_1 = create_ngrams(reviews_1[‘review_comment_words’])
#print(uni_5)

# distribution plot
def plot_dist(words, color):
nltk.FreqDist(words).plot()

”’
NLP – Natural Language Processing – analysing review comment to understand
reasons for positive and negative ratings.
concepts like: unigram, bigram, trigram

Steps we generally perform with NLP data:
1. Convert into lowercase
2. decompose (non unicode to unicode)
3. removing accent: encode the content to ascii values
4. tokenization: will break sentence to words
5. Stop words: not important words for analysis
6. Lemmetization (done only on English words): convert the words into dictionary words
7. N-grams: set of one word (unigram), two words (bigram), three words (trigrams)
8. Plot the graph based on the number of occurrences and Evaluate
”’
”’
cardboard mousepad. Going worth price! Not bad
”’

link=“D:/datasets/OnlineRetail/order_reviews.csv”
import pandas as pd
import unicodedata
import nltk
import matplotlib.pyplot as plt
df = pd.read_csv(link)
print(list(df.columns))
”’
[‘review_id’, ‘order_id’, ‘review_score’, ‘review_comment_title’,
‘review_comment_message’, ‘review_creation_date’, ‘review_answer_timestamp’]
”’
#df[‘review_creation_date’] = pd.to_datetime(df[‘review_creation_date’])
#df[‘review_answer_timestamp’] = pd.to_datetime(df[‘review_answer_timestamp’])

# data preprocessing – making data ready for analysis
reviews_df = df[df[‘review_comment_message’].notnull()].copy()
#print(reviews_df)

# remove accents
def remove_accent(text):
return unicodedata.normalize(‘NFKD’,text).encode(‘ascii’,errors=‘ignore’).decode(‘utf-8’)
#STOP WORDS LIST:
STOP_WORDS = set(remove_accent(w) for w in nltk.corpus.stopwords.words(‘portuguese’))

”’
Write a function to perform basic preprocessing steps
”’
def basic_preprocessing(text):
#converting to lower case
txt_pp = text.lower()
#print(txt_pp)

#remove the accent
#txt_pp = unicodedata.normalize(‘NFKD’,txt_pp).encode(‘ascii’,errors=’ignore’).decode(‘utf-8’)
txt_pp =remove_accent(txt_pp)
#print(txt_pp)
#tokenize
txt_token = nltk.tokenize.word_tokenize(txt_pp)
#print(txt_token)

# removing stop words
txt_token = tuple(w for w in txt_token if w not in STOP_WORDS and w.isalpha())
return txt_token



## write a function to creaet unigram, bigram, trigram
def create_ngrams(words):
unigrams,bigrams,trigrams = [],[],[]
for comment in words:
unigrams.extend(comment)
bigrams.extend(‘ ‘.join(bigram) for bigram in nltk.bigrams(comment))
trigrams.extend(‘ ‘.join(trigram) for trigram in nltk.trigrams(comment))


return unigrams, bigrams, trigrams


# applying basic preprocessing:
reviews_df[‘review_comment_words’] = \
reviews_df[‘review_comment_message’].apply(basic_preprocessing)

#get positive reviews – all 5 ratings in review_score
reviews_5 = reviews_df[reviews_df[‘review_score’]==5]

#get negative reviews – all 1 ratings
reviews_1 = reviews_df[reviews_df[‘review_score’]==1]
#create ngrams for rating 5 and rating 1
uni_5, bi_5, tri_5 = create_ngrams(reviews_5[‘review_comment_words’])
print(uni_5)
print(bi_5)
print(tri_5)

# Assignment: perform similar tasks for reviews that are negative (review score = 1)
#uni_1, bi_1, tri_1 = create_ngrams(reviews_1[‘review_comment_words’])
#print(uni_5)

# distribution plot
def plot_dist(words, color):
nltk.FreqDist(words).plot(20,cumulative=False, color=color)

plot_dist(tri_5, “red”)

#NLP – Natural Language processing:
# sentiments: Positive, Neutral, Negative
#
”’
we will use nltk library for NLP:
pip install nltk
”’
import nltk
#1. Convert into lowercase
text = “Product is great but I amn’t liking the colors as they are worst”
text = text.lower()

”’
2. Tokenize the content: break it into words or sentences
”’
text1 = text.split()
#using nltk
from nltk.tokenize import sent_tokenize,word_tokenize
text = word_tokenize(text)
#print(“Text =\n”,text)
#print(“Text =\n”,text1)

”’
3. Removing Stop words: Words which are not significant
for your analysis. E.g. an, a, the, is, are
”’
my_stopwords = [‘is’,‘i’,‘the’]
text1 = text
for in text1:
    
if in my_stopwords:
        text.remove(w)
print(“Text after my stopwords:”,text1)

nltk.download(
“stopwords”)
from nltk.corpus import stopwords
nltk_eng_stopwords = 
set(stopwords.words(“english”))
#print(“NLTK list of stop words in English: “,nltk_eng_stopwords)
”’
Just for example: we see the word but in the STOP WORDS but
we want to include it, then we need to remove the word from the set
”’
# removing but from the NLTK stop words
nltk_eng_stopwords.remove(‘but’)

for in text:
    
if in nltk_eng_stopwords:
        text.remove(w)
print(“Text after NLTK stopwords:”,text)

”’
4. Stemming: changing the word to its root
eg: {help: [help, helped, helping, helper]}

One of the method is Porter stemmer
”’
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
text = [stemmer.stem(w) 
for in text]
”’ above line is like below:
t_list=[]
for w in text:
    a = stemmer.stem(w)
    t_list.append(a)
”’
print(“Text after Stemming:”,text)
”’
5. Part of Speech Tagging (POS Tagging)
grammatical word which deals with the roles they place
like – 8 parts of speeches – noun, verb, …

Reference: https://www.educba.com/nltk-pos-tag/
POS Tagging will give Tags like

CC: It is the conjunction of coordinating
CD: It is a digit of cardinal
DT: It is the determiner
EX: Existential
FW: It is a foreign word
IN: Preposition and conjunction
JJ: Adjective
JJR and JJS: Adjective and superlative
LS: List marker
MD: Modal
NN: Singular noun
NNS, NNP, NNPS: Proper and plural noun
PDT: Predeterminer
WRB: Adverb of wh
WP$: Possessive wh
WP: Pronoun of wh
WDT: Determiner of wp
VBZ: Verb
VBP, VBN, VBG, VBD, VB: Forms of verbs
UH: Interjection
TO: To go
RP: Particle
RBS, RB, RBR: Adverb
PRP, PRP$: Pronoun personal and professional

But to perform this, we need to download any one tagger:
e.g. averaged_perceptron_tagger
nltk.download(‘averaged_perceptron_tagger’)
”’
nltk.download(‘averaged_perceptron_tagger’)

import nltk
from nltk.tag import DefaultTagger
py_tag = DefaultTagger (
‘NN’)
tag_eg1 = py_tag.tag ([
‘Example’‘tag’])
print(tag_eg1)

#txt = “Example of nltk pos tag list”
#txt = [‘product’, ‘great’, ‘but’, “not”, ‘like’, ‘color’]
#txt = word_tokenize(txt)
#txt = [‘Example’,’of’,’nltk’,’pos’,’tag’,’list’]
pos_txt = nltk.pos_tag(text)
print(“POS Tagging:”, pos_txt)

”’
6. Lemmetising
takes a word to its core meaning
We need to download:  wordnet
”’
nltk.download(‘wordnet’)
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
print(“Very good = “,lemmatizer.lemmatize(“very good”))
print(“Halves = “,lemmatizer.lemmatize(“halves”))

text = 
“Product is great but I amn’t liking the colors as they are worst”
text = word_tokenize(text)
text = [lemmatizer.lemmatize(w) 
for in text]
print(“Text after Lemmatizer: “,text)


# Sentiment analysis – read the sentiments of each sentence
”’
If you need more data for your analysis, this is a good source:
https://github.com/pycaret/pycaret/tree/master/datasets

We will use Amazon.csv for this program

”’
import pandas as pd
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.sentiment.vader import SentimentIntensityAnalyzer

link = “https://raw.githubusercontent.com/pycaret/pycaret/master/datasets/amazon.csv”
df = pd.read_csv(link)
print(df)

#Let’s create a function to perform all the preprocessing steps
# of a nlp analysis
def preprocess_nlp(text):
#tokenise
#print(“0”)
text = text.lower() #lowercase
#print(“1”)
text = word_tokenize(text) #tokenize
#print(“2”)
text = [w for w in text if w not in stopwords.words(“english”)]
#lemmatize
#print(“3”)
lemm = WordNetLemmatizer()
#print(“4”)
text = [lemm.lemmatize(w) for w in text]
#print(“5”)
# now join all the words as we are predicting on each line of text
text_out = ‘ ‘.join(text)
#print(“6”)
return text_out

# import Resource vader_lexicon
import nltk
nltk.download(‘vader_lexicon’)


df[‘reviewText’] = df[‘reviewText’].apply(preprocess_nlp)
print(df)

# NLTK Sentiment Analyzer
# we will now define a function get_sentiment() which will return
# 1 for positive and 0 for non-positive
analyzer = SentimentIntensityAnalyzer()
def get_sentiment(text):
score = analyzer.polarity_scores(text)
sentiment = 1 if score[‘pos’] > 0 else 0
return sentiment

df[‘sentiment’] = df[‘reviewText’].apply(get_sentiment)

print(“Dataframe after analyzing the sentiments: \n,df)

#confusion matrix
from sklearn.metrics import confusion_matrix
print(“Confusion matrix:\n,confusion_matrix(df[‘Positive’],df[‘sentiment’]))

”’ RESULT

Confusion matrix:
[[ 1131 3636]
[ 576 14657]]
Accuracy: (1131 + 14657) / (1131 + 14657 + 576 + 3636) = 15788/20000 = 78.94%
”’
# Visualization
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
plt.hist(data, bins=30, histtype=‘stepfilled’, color=“red”)
plt.title(“Histogram Display”)
plt.xlabel(“Marks”)
plt.ylabel(“Number of Students”)
plt.show()
# Analyzing Hotel Bookings data
# https://github.com/swapnilsaurav/Dataset/blob/master/hotel_bookings.csv
link=“https://raw.githubusercontent.com/swapnilsaurav/Dataset/master/hotel_bookings.csv”
import pandas as pd
df = pd.read_csv(link)
#print(“Shape of the data: “,df.shape)
#print(“Data types of the columns:”,df.dtypes)
import numpy as np
df_numeric = df.select_dtypes(include=[np.number])
#print(df_numeric)
numeric_cols = df_numeric.columns.values
#print(“Numeric column names: “,numeric_cols)
df_nonnumeric = df.select_dtypes(exclude=[np.number])
#print(df_nonnumeric)
nonnumeric_cols = df_nonnumeric.columns.values
#print(“Non Numeric column names: “,nonnumeric_cols)

####
#preprocessing the data
import seaborn as sns
import matplotlib.pyplot as plt
colors = [“#091AEA”,“#EA5E09”]
cols = df.columns
sns.heatmap(df[cols].isnull(), cmap=sns.color_palette(colors))
plt.show()

cols_to_drop = []
for col in cols:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
#print(f”{col} -> {pct_miss}”)
cols_to_drop.append(col) #column list to drop

# remove column since it has more than 80% missing value
df = df.drop(cols_to_drop, axis=1)

for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
print(f”{col} -> {pct_miss})
# check for rows to see the missing values
missing = df[col].isnull()
num_missing = np.sum(missing)
if num_missing >0:
df[f’{col}_ismissing’] = missing
print(f”Created Missing Indicator for {cols})

### keeping track of the missing values
ismissing_cols = [col for col in df.columns if ‘_ismissing’ in col]
df[‘num_missing’] = df[ismissing_cols].sum(axis=1)
print(df[‘num_missing’])

# drop rows with > 12 missing values
ind_missing = df[df[‘num_missing’] > 12].index
df = df.drop(ind_missing,axis=0) # ROWS DROPPED

#count for missing values
for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >0:
print(f”{col} -> {pct_miss})

”’
Still we are left with following missing values:
children -> 2.0498257606219004
babies -> 11.311318858061922
meal -> 11.467129071170085
country -> 0.40879238707947996
deposit_type -> 8.232810615199035
agent -> 13.687005763302507
”’

# Analyzing Hotel Bookings data
# https://github.com/swapnilsaurav/Dataset/blob/master/hotel_bookings.csv
link=“https://raw.githubusercontent.com/swapnilsaurav/Dataset/master/hotel_bookings.csv”
import pandas as pd
df = pd.read_csv(link)
#print(“Shape of the data: “,df.shape)
#print(“Data types of the columns:”,df.dtypes)
import numpy as np
df_numeric = df.select_dtypes(include=[np.number])
#print(df_numeric)
numeric_cols = df_numeric.columns.values
print(“Numeric column names: “,numeric_cols)
df_nonnumeric = df.select_dtypes(exclude=[np.number])
#print(df_nonnumeric)
nonnumeric_cols = df_nonnumeric.columns.values
print(“Non Numeric column names: “,nonnumeric_cols)

####
#preprocessing the data
import seaborn as sns
import matplotlib.pyplot as plt
colors = [“#091AEA”,“#EA5E09”]
cols = df.columns
sns.heatmap(df[cols].isnull(), cmap=sns.color_palette(colors))
plt.show()

cols_to_drop = []
for col in cols:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
#print(f”{col} -> {pct_miss}”)
cols_to_drop.append(col) #column list to drop

# remove column since it has more than 80% missing value
df = df.drop(cols_to_drop, axis=1)

for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
print(f”{col} -> {pct_miss})
# check for rows to see the missing values
missing = df[col].isnull()
num_missing = np.sum(missing)
if num_missing >0:
df[f’{col}_ismissing’] = missing
#print(f”Created Missing Indicator for {cols}”)

### keeping track of the missing values
ismissing_cols = [col for col in df.columns if ‘_ismissing’ in col]
df[‘num_missing’] = df[ismissing_cols].sum(axis=1)
print(df[‘num_missing’])

# drop rows with > 12 missing values
ind_missing = df[df[‘num_missing’] > 12].index
df = df.drop(ind_missing,axis=0) # ROWS DROPPED

#count for missing values
for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >0:
print(f”{col} -> {pct_miss})

”’
Still we are left with following missing values:
children -> 2.0498257606219004 # numeric
babies -> 11.311318858061922 #numeric
meal -> 11.467129071170085 # non-numeric
country -> 0.40879238707947996 # non-numeric
deposit_type -> 8.232810615199035 # non-numeric
agent -> 13.687005763302507 #numeric
”’
#HANDLING NUMERIC MISSING VALUES
df_numeric = df.select_dtypes(include=[np.number])
for col in df_numeric.columns.values:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss > 0:
med = df[col].median()
df[col] = df[col].fillna(med)

#HANDLING non-NUMERIC MISSING VALUES
df_nonnumeric = df.select_dtypes(exclude=[np.number])
for col in df_nonnumeric.columns.values:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss > 0:
mode = df[col].describe()[‘top’]
df[col] = df[col].fillna(mode)


print(“#count for missing values”)
for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >0:
print(f”{col} -> {pct_miss})

#drop duplicate values
print(“Shape before dropping duplicates: “,df.shape)
df = df.drop(‘id’,axis=1).drop_duplicates()
print(“Shape after dropping duplicates: “,df.shape)

 

 

 

DATABRICKS LEARNING
# We will implement Stack operations using List
# Last in First Out

class MyStack:
def __init__(self):
self.mystack = []

def add_stack(self,val):
#adding member
self.mystack.append(val)

def remove_stack(self):
#remove from the list
self.mystack.pop()

def print_stack(self):
print(“Values in the list are: \n,self.mystack)

stack1 = MyStack()
stack1.add_stack(40)
stack1.add_stack(10)
stack1.add_stack(30)
stack1.add_stack(60)
stack1.add_stack(20)
stack1.print_stack()
stack1.remove_stack()
stack1.print_stack()
stack1.remove_stack()
stack1.print_stack()
PYTHON NOV 2023
”’
print() – displays the content on the screen
functions have () after the name

python commands are case sensitive- Print is not same as print
”’

# idgjdsigjfigj
# comments mean that you are asking computer to ignore them

print(5)
print(5+3)
print(‘5+3’)
print(“5+3”)
print(‘5+2*3=’,5+2*3,“and 4*3=”,4*3)
print(“Hello How are you?”);print(‘Hello How are you?’)
# print always starts from a new line
# escape sequence: \n (newline) \t for tab spaces
print(“How are you doing? \nWhere are you \tgoing?”);
# What’s your name?
print(“What’s your name?”)
# He asked me,”What’s your name?”
print(“He asked me,\”What’s your name?\”,end=\n)

# He asked me,\”What’s your name?\”
print(“He asked me,\\\”What’s your name?\\\”,end=\n)
print(“Hello”,end=” – “)
print(“How are you?”)

print(“Basic data types in Python”)
# numeric – int (integer)- -99, -4,0,5,888: no decimal values
marks1 = 43

marks2 = 87
print(“Marks1 =”,marks1)
marks1 = 99
print(marks1)
# function: type() – it gives the datatype
print(type(marks1)) #<class ‘int’>

marks = 87.0 # <class ‘float’>
print(type(marks))

# complex: square root of -1: j
calc = 3j * 4j
print(calc) # 12 j-square = -12 + 0j
print(‘Data type of calc = ‘,type(calc))

# int float complex
a = –55
print(type(a))
a = –55.0
print(type(a))
a = –55j
print(type(a))

# str – string – text
print(“HELLO”)
name=“Sachin”
print(name)
print(“type = “,type(name))
name=‘Virat kohli leads \nbangalore team in IPL’
print(name)
print(“type = “,type(name))

name=”’Rohit is the captain
of Indian team
He opens in the ODIs”’
print(name)
print(“type = “,type(name))

name=“””Rohit led the Indian team
in 2023 ODI World cup and
reached finals”””
print(name)
print(“type = “,type(name))

#5th data type – Bool boolean – 2 values: True and False
val1 = True # False
print(type(val1))

# Formatting the print statement
quantity = 12
price = 39
total = quantity * price
print(“Total cost of”,quantity,“books which costs per copy Rs”,price,“will be Rs”,total)
# f – string is used to format the output
print(f”Total cost of {quantity} books which costs per copy Rs {price} will be Rs {total})

# f-string is used to format float values as well
quantity, total = 12, 231.35
price = total/quantity
print(f”Total cost of {quantity} books which costs per copy Rs {price:.1f} will be Rs {total})

# f-string for string values
name,country,title=“Rohit”,“India”,“Captain”
print(f”Player {name:<12} plays for {country:^10} and is the {title:>15} of the team”)
name,country,title=“Mangbwabe”,“Zimbabwe”,“Wicket-keeper”
print(f”Player {name:<12} plays for {country:^10} and is the {title:>15} of the team”)

### INPUT
## to take input from the user
## input can take no or at max 1 parameter
inp_val = int(input(“Enter first number: “))
print(inp_val)
print(“Datatype of input=”,type(inp_val))
inp_val2 = int(input(“Enter second number: “))
print(“Sum of two numbers=”,inp_val+inp_val2)

## change below programs to accept the values from the user using input

# 1. write a program to calculate area and perimeter of a rectangle
l=50
b=20
area = l*b
peri = 2*(l+b)
print(f”Area and perimeter of a rectangle with length {l} and breadth {b} is {area} and {peri} respectively”)
# 2. write a program to calculate area and perimeter of a square
#### Assignment ##
# 3. write a program to calculate volume and surface area of a cone
#### Assignment ##
# 4. write a program to calculate volume and surface area of a cylinder
#### Assignment ##
# 5. write a program to calculate area and circumference of a circle
r=50
pi = 3.12
area = pi*r**2
cir = 2*pi*r
print(f”Area and circumference of a circle with radius {r} is {area} and {cir} respectively”)
# input() – read input from the user
num1 = int(input(“Enter first number:”))
print(“type = “,type(num1))
num2 = int(input(“Enter second number:”))
print(“Sum is “,num1+num2)

# calculate area and perimeter for a rectangle
length=float(input(“Enter length of the rectangle:”))
breadth=float(input(“Enter breadth of the rectangle:”))
perimeter = (length+breadth)*2
print(“Perimeter of the rectangle is”,perimeter)

# int() -to convert to int
#similarly you can use float(), str() bool() complex()
# operators:
# Arithmatic operators: + – * / ** // % (modulo – remainder)
num1 = 11 #assignment operator = we are assigning value 11 to num1
num2 = 3
print(num1 + num2)
print(num1 – num2)
print(num1 * num2)
print(num1 / num2)
print(num1 ** num2) #power
print(num1 // num2) #integer division
print(num1 % num2) # remainder

## relational operators (comparision)
## > >= < <= == != (is it?)
## output is always bool (True or False)
num1,num2,num3 = 11,9,11
print(“Relational : “, num1 > num2) # T
print(“Relational : “, num1 >= num3) # T
print(“Relational : “, num1 < num2) # F
print(“Relational : “, num1 <= num3) # T
print(“Relational : “, num1 == num2) # F
print(“Relational : “, num1 == num3) # T
print(“Relational : “, num1 != num2) # T
print(“Relational : “, num1 != num3) # F
print(“Relational : “, num1 > num3) # F
print(“Relational : “, num1 < num3) # F

# Logical operators: and or not
# input and output are both bool values
”’
Prediction 1: Rohit and Ishan will open the batting
Prediction 2: Rohit or Ishan will open the batting
Actual: Rohit and Gill opened the batting
Prediction 1 False
Prediction 2 True

Truth Table: AND (*)
T and T = T
T and F = F
F and T = F
F and F = F

OR (+)
T or T = T
T or F = T
F or T = T
F or F = F

not T = F
not F = T
”’
num1,num2,num3 = 11,9,11
print( not(num1 > num2 and num1 >= num3 or num1 < num2 or num1 <= num3 and num1 == num2
and num1 == num3 or num1 != num2 or num1 != num3 and num1 > num3 or num1 < num3))
# T and T or F or T and F and T or T or F and F or F
# T or F or F or T or F or F
# T
# int to binary and vice-versa
num1 = 34
print(“Binary of num1=”,bin(34))
num2 = 0b100010
print(“Integer of num2=”,int(num2))
print(oct(34)) # 0o42
print(hex(34)) # 0x22

#Bitwise: & (bitwise and) | (bitwise or) >> (right shift) << (left shift)
num1 = 23 #0b10111
num2 = 31 #0b11111
print(bin(num1),“and”,bin(num2))
”’
bitwise &
10111
11111
——–
10111
”’
print(int(0b10111)) # 23
print(“23 & 31 = “,23 & 31) # 23

”’
bitwise |
10111
11111
——–
11111
”’
print(“23 | 31 = “,23 | 31) # 31

”’
THTO
54320
”’
print(“23 << 2:”,23 << 2) # 92
”’
1011100 << 2
”’
print(int(0b1011100))

print(“23 << 2:”,23 >> 2) # 5
”’
101
”’
print(int(0b101))

# conditions
”’
display message after checking if the student has passed or failed the exam
condition is avg >= 40 to pass

if command checks the condition is Python
syntax:
if condition :
# perform things when the condition is true


Title
* sub
o ss
i.
ii.
”’
avg =82
if avg >=40:
print(“Congratulations!”)
print(“You’ve passed!”)

print(“Thank you”)
”’
Check avg and print Pass or Fail
”’
avg = 19
if avg >=40:
print(“Pass”)
else:
print(“Fail”)
# IF – condition – will always result into True or False

num1 = 71.000000001
num2 = 71
# if num1 is greater than num2 then I want to print How are you? otherwise do nothing
if num1 > num2:
print(“How are you?”)
print(“Where are you going?”)

print(“Thank you”)

# if num1 is greater than num2 then I want to print How are you? otherwise print Do nothing
if num1 > num2:
print(“How are you?”)
print(“Where are you going?”)
else:
print(“Do Nothing”)

”’
Input a number from the user and check if its +ve, -ve or zero
”’
val = int(input(“Enter a number: “))
print(“Type of data =”,type(val))

# IF – ELIF – ELSE
if val==0: # == is to check the equality
print(“Its Zero”)
elif val <= 0:
print(“Its -ve number”)
else:
print(“Its +ve number”)

if val==0:
print(“Its Zero”)
if val<=0:
print(“Its -ve number”)
if val>=0:
print(“Its +ve number”)

”’
Write a program to take 2 inputs from the user and check if the first
number is greater, smaller or equal to the second one
”’
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
if num1 > num2:
print(num1,“is greater than”,num2)
elif num1 < num2:
print(num1,“is less than”,num2)
else:
print(num1, “and”, num2,“are equal”)

”’
WAP to take marks in 5 subjects as input, calculate total and average
and assign grade based on below condition:
a. avg 85 – Grade A
b. avg 70-85 – Grade B
c. avg 60-70 – Grade C
d. avg 50-60 – Grade D
e. avg 40 -50 – Grade E
f. avg <40 – Grade F
”’
marks1 = float(input(“Enter the marks in subject 1: “))
marks2 = float(input(“Enter the marks in subject 2: “))
marks3 = float(input(“Enter the marks in subject 3: “))
marks4 = float(input(“Enter the marks in subject 4: “))
marks5 = float(input(“Enter the marks in subject 5: “))
total = marks1 + marks5 + marks4 + marks3 + marks2
avg = total / 5
print(f”Total marks is {total:.2f} and average is {avg:.2f})
if avg>=85:
print(“Grade A”)
elif avg>=70:
print(“Grade B”)
elif avg>=60:
print(“Grade C”)
elif avg>=50:
print(“Grade D”)
elif avg>=40:
print(“Grade E”)
else:
print(“Grade F”)

”’
Let’s write a program to read length and breadth from the user
check if its square or rectangle and calculate area and perimeter
”’
length = int(input(‘Enter the length: ‘))
breadth = int(input(‘Enter the breadth: ‘))
#and & or are logical operator which connects you conditonal statements
# and: both the statements need to be true for True else its false
# or: both the statements need to be false for False else its True
if length>0 and breadth >0:
print(“Rectangle and Square both possible”)
if length==breadth:
print(“Square”)
print(f”Area is {length**2} and the perimeter is {4*length})
else:
print(“Rectangle”)
print(f”Area is {length * breadth} and the perimeter is {2 * (length+breadth)})
else:
print(“Neither Rectangle nor Square possible”)
”’
check if a number is positive, negative or zero
if the number is -ve, find the square root
if number is positive, check if its 2 digit or not
if 2 digits then interchange the values
otherwise, check if its divisible by 15,
”’

num1 = int(input(“Enter a number: “))
if num1<0:
print(“This is negative”)
print(f”Square root of {num1} is {num1**0.5})
elif num1==0:
print(“This is zero”)
else:
print(“This is positive”)
if num1>9 and num1<100:
#interchange the values: eg 35 = 53
# divide number by 10 =
d = num1 // 10
r = num1 % 10
new_num1 = r*10+d
print(f”{num1} is now made into {new_num1})

else:
if num1 % 15==0: # % mod – will give you remainder
print(“Number is divisible by 15”)
else:
print(“Number is not divisible by 15”)

#LOOPS – repeat the give block of code multiple times
# when you know exactly how many times to run – for
# repeatition is done based on a certain condition – while

# range(start,end,increment)- generates range of values from start upto end
# by increasing each element ny increment
# range(6,18,3): 6,9,12, 15
# range(start,end): increment is default 1
# range(15,19): 15,16,17,18
# range(end): start = 0, increment = 1
# range(6): 0, 1, 2, 3, 4, 5
#print(), input(), type(), int(),str(),complex(),bool(), float()

for var in range(6,18,3):
print(“Hello from the loop!”)
print(“Value of var is”,var)

for count in range(15,19):
print(“Hello from the loop2!”)
print(“Value of var is”,count)

for count in range(4):
print(“Hello from the loop3!”)
print(“Value of var is”,count)

###
for i in range(5):
print(“*”,end=” “)
print()
for i in range(1,101):
print(i,end=“, “)
print()
”’
Generate odd numbers between 1 and 30
”’
for i in range(1,30,2):
print(i,end=“, “)
print()
”’
Generate first 10 even numbers
”’
start = 0
for i in range(10):
print(start,end=“, “)
start=start+2

print()
# for loop examples
”’
Print all the numbers between 1 and 1000 which is perfectly divisible by 19 and 51
”’
start,end = 1, 10001
num1,num2 = 19,51
for n in range(start,end):
if n%num1==0 and n%num2==0:
print(n,end=“, “)
print()
”’
Generate prime numbers between 10000 and 50000
”’
start,end = 40000, 42000
for n in range(start,end):
isPrime = True
for num in range(2,n//2+1):
if n %num==0:
isPrime = False
break
if isPrime:
print(n,end=“, “)

”’
Print different * patterns
”’
for i in range(5):
print(“*”)

”’
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
”’
for j in range(5):
for i in range(5):
print(“*”,end=” “)
print()

”’
*
* *
* * *
* * * *
* * * * *
”’

for j in range(5):
for i in range(1+j):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’

for j in range(5):
for i in range(5-j):
print(“*”,end=” “)
print()
”’
* * * * *
* * * *
* * *
* *
*
”’

for j in range(5):
for i in range(j):
print(” “,end=“”)
for i in range(5-j):
print(“*”,end=” “)
print()

”’
Assignment:
*
* *
* * *
* * * *
* * * * *

Solve assignments from the website
”’
## WHILE Loop
”’
WAP to print hello till user says no
”’
while True:
print(“HELLO 1”)
usr_inp=input(“Enter N to stop: “)
if usr_inp.lower()==“n”:
break
print(“====”)
usr_inp=input(“Enter N to stop: “)
while usr_inp.lower() !=‘n’:
print(“HELLO 2”)
usr_inp = input(“Enter N to stop: “)
”’
A company offers dearness allowance (DA) of 40% of basic pay and house
rent allowance (HRA) of 10% of basic pay. Input basic pay of an employee,
calculate his/her DA, HRA and Gross pay (Gross = Basic Pay + DA+ HRA).
a. Modify the above scenario, such that the DA and HRA
percentages are also given as inputs.
b. Update the program such that the program uses a user-defined
function for calculating the Gross pay. The function takes Basic pay,
DA percentage and HRA percentage as inputs and returns the gross pay.
”’
#Case 1
basic_pay = int(input(“Enter your basic pay:”))
da = basic_pay *0.4
hra = basic_pay*0.1
gross_pay = basic_pay + da + hra
print(“Your gross pay for this month is Rs”,gross_pay)

#Case 2
basic_pay = int(input(“Enter your basic pay:”))
da = int(input(“Enter the dearness allowance (%): “))
da = da/100
hra = int(input(“Enter the House rent allowance (%): “))
hra = hra/100
gross_pay = basic_pay + basic_pay*da + basic_pay*hra
print(“Your gross pay for this month is Rs”,gross_pay)
#case 3

# defining a user defined function (udf)
# input taken by the function – passing the value
# and anything returned from the function – function returns the output
def calc_gross_pay(bp,da,hra=10):
hra = hra / 100
da = da / 100
gross_pay = bp + bp * da + bp * hra
return gross_pay


basic_pay = int(input(“Enter your basic pay:”))
da = int(input(“Enter the dearness allowance (%): “))
hra = int(input(“Enter the House rent allowance (%): “))

result = calc_gross_pay(basic_pay,da,hra)
print(“Your gross pay for this month is Rs”,result)

result = calc_gross_pay(basic_pay,da)
print(“Your gross pay with default hra for this month is Rs”,result)

result = calc_gross_pay(da=da,bp=basic_pay,hra=hra)
print(“Your gross pay with non-positional for this month is Rs”,result)

# required positional arguments
# default (non-required)

”’
You have a monthly income of Rs 1100. Your monthly outgoings are as follows.
• Rent – Rs.500
• Food – Rs.300
• Electricity – Rs.40
• Phone – Rs 60
• Cable TV – Rs 30.
Calculate the Monthly Expenses and the remainder (what’s left over each month).
a. Modify the above program by inputting the income as well as values
for expenses and calculate Monthly expense.
b. Include a function to check whether you will have savings or you
have to borrow money based on the monthly income and total expenses.
The function should print an appropriate message for each case.
”’
#case 1
income = 1100
Rent=500
Food=300
Electricity=40
Phone=60
Cable=30
expenses = Rent+Food+Electricity+Phone+Cable
remainder = income-expenses
print(“Your expenses for this month is”,expenses)
print(“You remainder for this month is”,remainder)

#case 2
income = int(input(“Enter your Income:”))
Rent= int(input(“Enter your rent:”))
Food= int(input(“Enter your food expenses:”))
Electricity= int(input(“Enter your Electricity charges:”))
Phone= int(input(“Enter your Phone expenses:”))
Cable= int(input(“Enter your Cable TV expenses:”))
expenses = Rent+Food+Electricity+Phone+Cable
remainder = income-expenses
print(“Your expenses for this month is”,expenses)
print(“You remainder for this month is”,remainder)


# case 3
def check_remainder(income,expenses):
remainder = income-expenses
if remainder<0:
print(f”You need to borrow Rs {remainder} for this month”)
elif remainder>0:
print(f”You have a savings of Rs {remainder} for this month”)
else:
print(“This month you neither have savings nor need to borrow any money”)

income = int(input(“Enter your Income:”))
Rent= int(input(“Enter your rent:”))
Food= int(input(“Enter your food expenses:”))
Electricity= int(input(“Enter your Electricity charges:”))
Phone= int(input(“Enter your Phone expenses:”))
Cable= int(input(“Enter your Cable TV expenses:”))
expenses = Rent+Food+Electricity+Phone+Cable
check_remainder(income,expenses)


########## PRACTICE #################

# defining a user defined function (udf)
# input taken by the function – passing the value
# and anything returned from the function – function returns the output
def calc_gross_pay(n1,n2):
print(“Hi, I am in calc_gross_pay_function”)
total = n1 + n2
#print(total)
return total


val1 = 100
val2 = 150
ret_val = calc_gross_pay(val1,val2) #calling the function pass the value
print(“Value returned from the function is”,ret_val)
val1 = 10
val2 = 50
result = calc_gross_pay(val1,val2) #calling the function pass the value
print(“Value returned from the function is”,result)


# Guessing the number game: Computer v Human
# computer will think of the number and we will guess it
import random
num1 = random.randint(1,100)
attempts = 0
fouls = 0
while True:
guess = int(input(“Guess the number between 1 and 100: “))
if guess<1 or guess>100:
print(“Your guess is outside the valid number range! “,end=” “)
if fouls==0:
print(“This is your first foul, so you can continue but another foul will make you lose.”)
else:
print(“This is your second foul, sorry you lose.”)
break
fouls+=1
continue
attempts+=1
if num1 == guess:
print(f”You guessed it right in {attempts} attempts!”)
break
elif num1 > guess:
print(“Sorry! Its Incorrect! Guess a higher number”)
else:
print(“Sorry! Its Incorrect! Guess a lower number”)

#############
# Guessing the number game: Computer v Computer
# computer will think of the number and it will only guess it
import random
import time # date, datetime

start = time.time()
### finding average attempts of running this program
total_attempts = 0
for i in range(10000):
num1 = random.randint(1,100)
attempts = 0
fouls = 0
low,high=1,100
while True:
guess = random.randint(low,high)
if guess<1 or guess>100:
print(“Your guess is outside the valid number range! “,end=” “)
if fouls==0:
print(“This is your first foul, so you can continue but another foul will make you lose.”)
else:
print(“This is your second foul, sorry you lose.”)
break
fouls+=1
continue
attempts+=1
if num1 == guess:
print(f”You guessed it right in {attempts} attempts!”)
total_attempts+=attempts # a+=b => a = a+b ; a/=c => a =a/c
break
elif num1 > guess:
print(“Sorry! Its Incorrect! Guess a higher number”)
low=guess+1

else:
print(“Sorry! Its Incorrect! Guess a lower number”)
high=guess-1
end = time.time()
print(f”On average this program has taken {total_attempts/10000:.1f} attempts”)
print(f”Total time taken by the program to run 10000 times is {end-start} seconds”)

#############
# LIST
# collections: list, tuple, sets, dictionary, numpy, pandas
l1 = [10,20,“30”,False,“Hello”,[1,3,5]]
print(“Type of the variable = “,type(l1))
print(“Size/Length of the list = “,len(l1))
# read the values of a list:
print(l1[0])

# LIST: ordered mutable linear collection
list1 = [34,“Hello”,[2,3,4], True, False, 45]

#indexing – forward
print(“First value – “,list1[0])
print(“third value – “,list1[2])
list1[0] = 55.5
print(list1)
# backward indexing – right to left
print(“Last element – indexed as -1: “,list1[-1])
print(“First value – “,list1[-3])
print(“1,3,5 values – “,list1[0],list1[2],list1[4])
#
print(“First to third values – “,list1[0:3],list1[:3])
print(“First to third values – “,list1[0:5:2])
print(“First to last values – “,list1[:])
print(“last three values – “,list1[-3:])

list2 = [3,4,5]
list3 = list1 + list2
print(list3)
list4 = list2*3
print(“List4 = “,list4)

## using list in a for loop
for counter in list1:
print(“HELLO : “,counter,“has a data type of”,type(counter))


## Properties of a list
l1 = [2,3,4]
l1.pop() #pop without index will remove last element from the list
print(“1. l1 = “,l1)
l1.pop(0) #pop will remove the element at the given index
print(“2. l1 = “,l1)
l1.append(5) #append always adds the value at the end of the list
print(“3. l1 = “,l1)
l1.append(1)
print(“4. l1 = “,l1)
l1.append(8)
print(“5. l1 = “,l1)
l1.sort() #default it sorts in ascending
print(“6. l1 = “,l1)
l1.sort(reverse=True) #will sort in descending
print(“6. l1 = “,l1)
## creating duplicate list
l2 = l1 #deep copy – both variables point to the same data
l3 = l1.copy() # shallow copy – you create a different copy
print(“11 L1 = “,l1)
print(“11 L2 = “,l2)
print(“11 L3 = “,l3)
l1.append(33)
l2.append(43)
l3.append(53)
l1.append(3)
print(“12 L1 = “,l1)
print(“12 L2 = “,l2)
print(“12 L3 = “,l3)
# (value, start, stop) – whats the index of the value between start and stop
# start =0, stop default is -1
print(“Index of 3: “,l1.index(3,3,10))
# REMEMBER: Index will throw error when value not the in list
# count() will do the count and its used exactly like index
num= l1.count(3)
print(“Number of 3 in the list is”,num)
l1_dup = l1[3:11]
num = l1_dup.count(3)
# above 2 statements can be clubbed as one shown below:
num = l1[3:11].count(3)
print(“Number of 3 in the given range is”,num)
print(“List before reverse is: “,l1)
l1.reverse()
print(“List after reverse is: “,l1)

# + will perform: c = a+ b
#extend will be like a = a+b
list4 = [11,22,33]
l1.extend(list4)
print(“L1 after extend: “,l1)

#pop takes index – remove takes value to remove/delete from the list
l1.remove(3)
cnt = l1.count(18)
if cnt>0:
l1.remove(18)
print(“1. After remove: “,l1)
#append() will always add at the end, insert takes the position also along with the values
# first it takes index, then the value to add
l1.insert(2,32)
print(“1. INSERT 1=”,l1)
l1.insert(2,42)
print(“2. INSERT 2=”,l1)
l1.clear() # will clear the data from the list
print(“99 List1 = “,l1)
# I want to input marks of 5 students in 5 subjects

students_marks = []

for j in range(5):

all_marks=[]
for i in range(5):
marks = int(input(“Enter the marks in subject “+str(i+1)+“: “))
all_marks.append(marks)
print(f”Marks obtained by student {j+1}: {all_marks})
students_marks.append(all_marks)
print(“Marks obtained by students are:\n,students_marks)

students_marks=[[66, 55, 77, 88, 99], [45, 65, 76, 78, 98],
[90, 80, 45, 55, 55], [54, 64, 74, 84, 94],
[34, 53, 99, 66, 76]]
subjects = [“Maths”,“Stats”,“Physics”,“Programming”,“SQL”]
for k in range(len(students_marks)):
total = sum(students_marks[k])
print(f”Total marks obtained by student {k+1} is {total} and average “
f”is {total/len(students_marks)})
max_marks = max(students_marks[k])
print(f”Highest marks obtained by student {k + 1} is {max_marks}
f”in subject {subjects[students_marks[k].index(max_marks)]})


# TUPLE: linear ordered immutable collection

# tuple declared using ()
t1 = ()
print(“Type of t1 = “,type(t1))
t1 = (“hello”,) # (5+3)*2 =
print(t1)
print(“Type of t1 = “,type(t1))

t1 = (5,4,6,9,1)
print(t1)
print(“Type of t1 = “,type(t1))
# indexing is exactly same as list
#t1[0]=8 – ‘tuple’ object does not support item assignment

for i in t1:
print(“from tuple: “,i)

t1=list(t1) # converting tuple to list
t1=tuple(t1) #converting list to tuple

############
## STRING – str
###########
# there is no difference between declaring string using ‘ or ” quotes
# and there is no difference between ”’ and “”” strings
# ‘ or ” declares only 1 line of text but ”’ and “”” can be used
# to declare multi line of text
str1 = “hello”
#str1[0]=”H” – ‘str’ object does not support item assignment
# strings are immutable
# strings are same as list or tuple
# 0 to n-1 indexing and -1 to -n indexing

str2 = ‘hi there’

str3 = “””How are you?
Where are you?
What are you doing?”””

str4 = ”’I am fine
I am here
I am doing nnothing”’
print(type(str1), type(str2),type(str3), type(str4))
print(“Str1 \n————“)
print(str1)
print(“Str2 \n————“)
print(str2)
print(“Str3 \n————“)
print(str3)
print(“Str4 \n————“)
print(str4)

str11=str1.upper()
print(str1,str11)
str22 = “Hello ” + “There ” * 2
print(“Str22 = “,str22)
# str are used in for loop exactly same way as list or tuple
for i in str1:
print(“STR = “,i)
# Strings – in python
str1 = “HELLO”
str3 = ”’How Are YoU?”’
str2 = “123456”
# methods with is…() – is it … ?
print(“isupper: “,str1.isupper())
print(“islower: “,str3.islower())
print(“istitle: “,str3.istitle())
print(“isnumeric: “,str2.isnumeric())
print(“”,str2.isalnum())
print(“title: “,str3.title())
print(“lower: “,str3.lower())
print(“upper: “,str3.upper())

str3 = ”’How Are YoU?”’
print(“startswith: “,str3.startswith(“H”))
print(“endswith: “,str3.endswith(“?”))
usname = input(“Enter your username (only text and numbers allowed: “)
if usname.isalnum():
print(“Username accepted”)
else:
print(“Invalid username!”)
num1 = input(“Enter length: “)
if num1.isnumeric():
num1 = int(num1)
else:
print(“Invalid number”)

str4=“abcdefghijklmnopqrstuvwxyz”
# I want to check if the starting
# character is A and ending is Z
if str4.upper().startswith(“A”) and str4.upper().endswith(“Z”):
print(“Your condition is true”)
else:
print(“Incorrect condition”)

while True:
inp = input(“Enter Yes to stop and any key to continue: “)
if inp.title()==“Yes”:
break



str5 = “Enter Yes to stop and any key to continue: “
str_words = str5.split()
print(str_words)
# join() will take list as input
print(“JOIN: “,” “.join(str_words))
str_hyphen = “-:-“.join(str_words)
print(“New Statement: “,str_hyphen)
# need to split this special text
str_words = str_hyphen.split(“-:-“)
print(“STR HYPHEN: “,str_words)

str1 = “How are you going?”
str1 = str1.replace(“g”,“d”)
print(“1. Str1 = “,str1)

str1 = “How are you going you?”
str1 = str1.replace(“g”,“d”,1)
print(“2. Str1 = “,str1)

# you in str1 or not
# -1 indicates value not found
# positive number indicates first matching index
print(str1)
print(str1.upper().find(“YOU”,9,21))

str1 = ” How are you going you? “
print(str1.strip())
str1 = str1.split()
str1 = ” “.join(str1)
print(str1)

######### DICTIONARY ##########
## mutable unordered collection: pair of key and value (key:value)
dict1 = {1:“Hello”,“Name”:“Sachin”,“Runs”:35000}
print(dict1)
print(dict1[1])
print(dict1[“Runs”])
print(dict1.values())
print(dict1.keys())
print(dict1.items())

# Dictionary: immutable unordered collection
dict1 = {}
print(“Type of dictionary: “,type(dict1))

t_dict ={“Name”:“Sachin”}
dict1.update(t_dict)
print(dict1)

”’
Write a program to store marks of 5 subjects along with names
”’
master_data = {}
for i in range(3):
name=input(“Enter the student’s name: “)
marks = []
for j in range(5):
m1=int(input(“Enter the marks in Subject “+str(j+1)+“: “))
marks.append(m1)
t_dict={name:marks}
master_data.update(t_dict)

#
print(“The details are:\n,master_data)
”’
{‘Sachin’: [56, 76, 39, 76, 54], ‘Virat’: [89, 90, 33, 59, 90], ‘Mahi’: [88, 77, 99, 88, 99]}
”’
data = {‘Sachin’: [56, 76, 39, 76, 54],
‘Virat’: [89, 90, 33, 59, 90],
‘Mahi’: [88, 77, 99, 88, 99]}
print(list(data.keys())[1])

for k in data.keys():
print(k)

# deep & shallow
data2 = data # both will point to the same memory location
data3 = data.copy() # shallow – create photocopy- another dict object

data2.update({‘Rohit’:[66,67,78,77,82]})
print(“Data: “,data)
print(“Data 2: “,data2)
print(“Data 3: “,data3)

#SETS – linear mutable unordered collection

set1 = set({})
print(“Set1 = “,set1)
set1.add(“Apple”)
print(“Set1 = “,set1)

set1 = {1,3,5,7,9}
print(‘0. SET1: ‘,set1)
set2 = {3,4,5,6,7}


# properties of sets
print(“# Union”)
print(set1 | set2)
set3 = set1.union(set2)
print(set3)
print(“#Intersection”)
print(set1 & set2)
set3 = set1.intersection(set2)
print(set3)

print(“# Difference”)
print(set1 – set2)
set3 = set1.difference(set2)
print(set3)
print(set2 – set1)
set3 = set2.difference(set1)
print(set3)
print(“#Symmetric difference”)
print(set1 ^ set2)
set3 = set1.symmetric_difference(set2)
print(set3)

set1.remove(7)
print(“1. Set1:”, set1)
set1.pop()
print(“2. Set1:”, set1)
set1.clear()
print(“3. Set1:”, set1)

### List, Tuple, Set – they can be converted into each other format
l1=[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
l1=list(set(l1))
print(l1)

###########
## Functions: user defined functions
def whatever():
print(“Hello”)
print(“Hello 2”)
print(“hello 3”)

whatever()

## 4 types:
# required positional parameters
# default keyword
# variable length paramets
# Functions
”’
Write a function to check if the number is prime or not
and use this to generate prime numbers
”’
def check_prime(val=53):
”’
This is a user defined function which takes
a value as input and checks if its a prime or not
@Written by Sachin Kohli
:param val:
:return:
”’
isPrime = True
for i in range(2,val//2+1):
if val%i==0:
isPrime = False
break
”’
if isPrime:
print(f”{val} is a prime number”)
else:
print(f”{val} is not a prime number”)
”’
return isPrime
# required positional argument
# default
# keyword

# check if val1 is greater than val2 then subtract
#otherwise add them
#SyntaxError: non-default argument follows default argument
def myfunction(val1, val2=50):
”’

:param val1:
:param val2:
:return:
”’
print(f”input values are: {val1} and {val2})
if val1 > val2:
print(“Subtraction = “,val1-val2)
else:
print(“Addition = “,val1+val2)

# write a function to add all the given numbers
#* against the argument makes it take values as a tuple
#** against the argument makes it take values as a dictionary
def add_all_num(*values, **data):
print(“add_all_num: values Values passed are: “,values)
print(“add_all_num: **data Values passed are: “, data)

if __name__==“__main__”: # current file is running
res = check_prime(41)
myfunction(43,33)
myfunction(val2=10,val1=20) #keywords, use exact same variable name
res = check_prime()
add_all_num(5,6,10,12,15, name=“Sachin”, game=“Cricket”,runs=50000)

”’ generate prime numbers between 10,000 to 15,000”’
for num in range(10000,15001):
res = check_prime(num)
if res:
print(num,end=“, “)
print()

print(“#################”)
print(help(input))
print(“——————–“)

print(input.__doc__)
print(“#################”)
print(check_prime.__doc__)

else:
print(“Thanks for using my program”)
# Class and Objects

class Books:
#functions which are part of a class are called methods
# members of class can be variables and methods
#object level members & class level members
total_books = 0

def __init__(self,title,author,price):
self.title = title
self.author = author
self.cost = price
Books.total_books +=1

def print_info(self):
print(“Title of the book is”,self.title)
print(“Author of the book is”, self.author)
print(“Cost of the book is”, self.cost)

@classmethod
def print_total(cls):
print(“Total books in the library are”,cls.total_books)


class Library:
total_lib = 0

def __init__(self,name,loc,pincode):
self.name = name
self.location = loc
self.pin = pincode

def print_info(self):
print(“Library: “,self.name)
print(f”Location: {self.location}{self.pin})

if __name__==“__main__”:
# CREATE OBJECT OF CLASS BOOKS
book1 = Books(“Python Programming”,“Sachin”,399)
book2 = Books(“SQL Programming”,“Virat”,299)
#creating objects call __init__() automatically
book3 = Books(“Machine Learning”,“Rohit”,499)
#book1.add_info(“Python Programming”,”Sachin”,399)
#book2.add_info(“SQL Programming”,”Virat”,299)
#book3.add_info(“Machine Learning”,”Rohit”,499)
book2.print_info()

Books.print_total()
book1.print_total()
book2.print_total()
book3.print_total()

###############################
###### Another file
###############################
import prog1

b1 = prog1.Books(“Data Analytics”, “Saurav”, 298)
b1.print_info()
l1 = prog1.Library(“ABC International Library”, “Hyderabad”, 500081)
l1.print_info()

”’
Create a class called MyMathOps and add functionalities for
Addition, Subtraction, Power, Multiplication and Division
You should have following methods:
1. init() – to get 2 values
2. calc_add() – perform addition
3. display_add() – to print total
4. calc_sub() – perform addition
5. display_sub() – to print total
6. calc_power() – perform addition
7. display_power() – to print total
8. calc_mul() – perform addition
9. display_mul() – to print total
10. calc_div() – perform addition
11. display_div() – to print total
”’
”’
Properties of class & objects:
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction

#Accessibility: public (var), private (__var), protected (_var)
”’
#magazines
class LibraryContent:
def __init__(self,title,price):
self.title = title
self.cost = price

def __print_data(self):
print(“data from Library Content”)

def print_info(self):
print(“info from Library content”)

def display_something(self):
print(“Do Nothing”)
class Magazines(LibraryContent):
total_mags = 0
def __init__(self,title,issn,price):
LibraryContent.__init__(self, title, price)
self.issn = issn
Books.total_books +=1

def print_info(self):
print(“Title of the book is”,self.title)
print(“ISSN of the book is”, self.issn)
print(“Cost of the book is”, self.cost)

@classmethod
def print_total(cls):
print(“Total books in the library are”,cls.total_books)

class Books(LibraryContent):
#functions which are part of a class are called methods
# members of class can be variables and methods
#object level members & class level members
total_books = 0

def __init__(self,title,author,price):
LibraryContent.__init__(self,title,price)
self.author = author
Books.total_books +=1

def print_info(self):
print(“Title of the book is”,self.title)
print(“Author of the book is”, self.author)
print(“Cost of the book is”, self.cost)

@classmethod
def print_total(cls):
print(“Total books in the library are”,cls.total_books)


class Library:
total_lib = 0

def __init__(self,name,loc,pincode):
self.name = name
self.location = loc
self.pin = pincode

def print_info(self):
print(“Library: “,self.name)
print(f”Location: {self.location}{self.pin})

if __name__==“__main__”:
m1 = Magazines(“International Journal for Robotics”,“247-9988”,19800)
m1.print_info()
b1 = Books(“Python Book”,“Virat”,299)
b1.print_info()
m1.print_info()
#m1.__print_data()
#b1.print_data()
m1.display_something()

############ ANOTHER FILE #################
#Working with files:
# modes: r(read), w(write), a (append)
# r+, w+, a+

filename = “17DEC.txt”
fileobj = open(filename,“a+”) #by default read mode
fileobj.write(”’Twinkle Twinkle little star
How I wonder what you are”’)
fileobj.seek(0)
fileobj.write(”’Twinkle X Twinkle X little star
How I wonder what you are”’)
cont = fileobj.read()
print(cont)
fileobj.close()
stamp_5 = 2
stamp_2 = 2
stamp_1 = 2
total = 51
stamp_1+=3

rest_amount = total – (stamp_5*5 +stamp_2*2+stamp_1*1)
# we need to distribute rest_amount to 5,2,1
more_5 =rest_amount//5
stamp_5 +=more_5
rest_amount = rest_amount%5

more_2 =rest_amount//2
stamp_2 +=more_2
rest_amount = rest_amount%2
stamp_1+=rest_amount

print(f”Rs 5 ={stamp_5}, Rs 2 = {stamp_2}, Rs 1 = {stamp_1})
l1 = [10,40,20,50,30,60]
# l1 = [10,20,30,40,50,60]
”’
5
4
3
2
1
0
”’
#Print – Bubble Sort
l1 = [60,50,40,30,20,10]
for i in range(len(l1)-1):
for j in range(len(l1)-1-i):
if l1[j] > l1[j+1]:
l1[j],l1[j + 1] = l1[j + 1], l1[j]
print(“Sorted L1 = “,l1)

l1 = [10,40,20,50,30,60]
l1 = [60,50,40,30,20,10]
#Print – Selection Sort
for i in range(len(l1)-1):
for j in range(1+i, len(l1)):
if l1[i] > l1[j]:
l1[i],l1[j] = l1[j], l1[i]
print(“Sorted L1 = “,l1)

## if 55 is in the list or not
element = 50
l1 = [10,40,20,50,30,60]
found = False
for i in l1:
if element==i:
found = True
if found:
print(“Element is in the list!”)
else:
print(“Element is not in the list”)
# sequential sort – method
found = False
for i in range(len(l1)):
if element==l1[i]:
found = True
break
if found:
print(“Element is in the list!”)
else:
print(“Element is not in the list”)

# Binary search works on sorted list
L1 = [10, 20, 30, 40, 50, 60]
low,high=0,len(L1)-1

element = 51
found = False
while low<=high:
mid = (low + high) // 2
if L1[mid]==element:
found=True
break
else:
if element > L1[mid]:
low=mid+1
else:
high=mid-1

if found:
print(f”Binary Search: {element} is in the list!”)
else:
print(f”Binary Search: {element} is not in the list”)

############

# Files
filename=“17DEC.txt”
filobj = open(filename, “r+”)
content = filobj.read()
print(“1. =============\n,content)
filobj.seek(0)
content = filobj.read(20)
print(“2. =============\n,content)
content = filobj.read(20)
print(“3. =============\n,content)
filobj.seek(0)
content = filobj.readline()
print(“4. =============\n,content)
content = filobj.readline(5000) #read 5000 characters (in current)
print(“5. =============\n,content)

filobj.seek(0)
content = filobj.readlines()
print(“6. =============\n,content)
filobj.close()

# opening again in write mode
filobj = open(filename, “w”)
content=“””filename=”17DEC.txt”
filobj = open(filename, “r+”)
content = filobj.read()
print(“1. =============\n“,content)
filobj.seek(0)
content = filobj.read(20)
print(“2. =============\n“,content)
content = filobj.read(20)
print(“3. =============\n“,content)
filobj.seek(0)”””
filobj.write(content)

content= [‘filename=”17DEC.txt”\n, ‘filobj = open(filename, “r+”)\n,
‘content = filobj.read()\n, ‘print(“1. =============\n,
‘”,content)\n, ‘filobj.seek(0)\n, ‘content = filobj.read(20)\n,
‘print(“2. =============\n, ‘”,content)\n,
‘content = filobj.read(20)\n, ‘print(“3. =============\n,
‘”,content)\n, ‘filobj.seek(0)’]

filobj.writelines(content)
filobj.close()
”’
two types:
1. OLTP – Online Transaction Processing
2. OLAP – Online Analytical Processing

RDBMS – Relational Database Management System

SQL – Structured Query Language – language of Database
SELECT, INSERT, UPDATE, DELETE
CREATE, DROP

Table: EMPLOYEES
EMPID ENAME EPHONE EEMAIL DEPT DHEAD DCODE DLOC
1 AA 123 aa@aa.com Executive AA E01 NY
2 AB 223 ab@aa.com Executive AA E01 ML

Relationship:
1:1 – All the columns in same table
1:M / M:1 – Put them in 2 tables and connect them using Foreign Key
M:M – Pu them in 2 different tables and connect them using 3rd table

— MYSQL database
https://dev.mysql.com/downloads/mysql/
Download and install 8.0.35
1. Server
2. Client
3. Workbench
Connection requires: you know the server location, username, password, database_name

# Data types in MYSQL:
https://dev.mysql.com/doc/refman/8.0/en/data-types.html


use ouremployees;

create table departments (
DID integer primary key,
dname varchar(20),
dhod varchar(20),
dcode varchar(10));

insert into departments values(101,’PD’,’MR PD’,’234AWER439′);
select * from departments;

select * from employees;

”’
# connect to MYSQL
import pymysql
hostname,dbname,username,password = “localhost”,“ouremployees”,“root”,“learnSQL”
db_con = pymysql.connect(host=hostname,database=dbname, user=username,password=password)

db_cursor = db_con.cursor()


sql1=”’Create table employees(empid integer primary key,
name varchar(30),
phone varchar(10),
did integer,
Foreign key (DID) references Departments(DID))
”’
#db_cursor.execute(sql1)

sql1 = ”’Insert into Employees values(
101, ‘Sachin T’,’3456555′,101)”’
#db_cursor.execute(sql1)
db_con.commit()

sql1=”’Select * from Employees”’
db_cursor.execute(sql1)
results = db_cursor.fetchall()
for data in results:
print(data)
db_con.close()

”’
Two types of stats:
Descriptive stats: Central tendency: mean median mode
Measure of variance: range, variance & standard deviation


MF1 – 15% – 10-20%
MF2 – 15% – -10% – 50%

5 & 6 = 5.5
1 & 10 – 5.5

Inferential stats:
”’

# NUMPY – core scientific library
import numpy as np
vals = range(15)
mat1 = np.reshape(vals,(5,3))
print(mat1)
print(“1: “,mat1[3,1])
print(“2: “,mat1[:5,1])
print(“3: \n,mat1[1:4,1:])
print(“Shape: “,mat1.shape)
print(“Number of rows =”,mat1.shape[0])
print(“Number of columns =”,mat1.shape[1])
”’
2x + 5y = 15
3x + 4y = 20
scipy –
”’

mat2=np.zeros((4,4))
print(“Matrix 2 = \n,mat2)
mat2=np.ones((4,4))
print(“Matrix 2 = \n,mat2)

mat2=np.full((4,4),5.001)
print(“Matrix 2 = \n,mat2)

mat3 = np.random.random((3,3))
print(“Matrix 3 = \n,mat3)

np.random.seed(100)
mat3 = np.random.randint(10,50,(3,3))
print(“Matrix 3 = \n,mat3)
”’
[[18 34 13]
[49 33 25]
[20 40 44]]
”’


np.random.seed(100)
mat3 = np.random.randint(10,20,(3,3))
print(“Matrix 3 = \n,mat3)

mat4 = np.random.randint(15,25,(3,3))
print(“Matrix 4 = \n,mat4)

print(“Matrix addition”)
print(mat3 + mat4)
print(np.add(mat3,mat4))

print(“Matrix subtraction”)
print(mat3 – mat4)
print(np.subtract(mat3,mat4))

print(“Matrix multiplication”)
print(mat3 * mat4)
print(np.multiply(mat3,mat4))

print(“Matrix division”)
print(mat3 / mat4)
print(np.divide(mat3,mat4))

print(“Matmul – Matrix Multiplication”)
# MAT3 (m,n) @ MAT4 (x,y) = RESULT_MAT(m,y) and possible only if
#1. n = x
# (3,3) * (3,3) = (3,3)
# (2,8) * (2,8) => matmul is not possible
# (2,8) * (8,2) = (2,2)
# (8,2) * (2,8) = (8,8)
mat3 = np.random.randint(10,20,(8,2))
print(“Matrix 3 = \n,mat3)

mat4 = np.random.randint(15,25,(2,8))
print(“Matrix 4 = \n,mat4)
print(mat3 @ mat4)
print(np.matmul(mat3,mat4))

# NUMPY – core scientific library
import numpy as np

x = np.arange(0,3*np.pi,0.05)
print(x)
y=np.sin(x)
print(“Sin values of x:”,y)
import matplotlib.pyplot as plt
plt.plot(x,y)
plt.show()


import numpy as np

”’ x=3, y= 7
2x + 5y = 41
3x + 4y = 37
convert into 3 matrices:
1. Coefficient Matrix:
[2 5]
[3 4]

2. Variable Matrix:
[x]
[y]

3. Output Matrix:
[15]
[20]

Coeff Matrix * Variable Matrix = Output
=> Variable = inverse(Coeff) * Output
check if determinant is non-zero and then solve it

”’
coeff = np.array([[2,5],[3,4]])
print(coeff)
output=np.array([[41],[37]])
det_coeff = np.linalg.det(coeff)
print(“Determinant of Coeff Matrix = “,det_coeff)
if det_coeff==0:
print(“Cant find the solution for this problem”)
else:
result = np.linalg.inv(coeff) @ output
print(“Solution=\n,result)

”’
2x+3y = 10 # y=0,x=5; y=0.0001, 3.5, y=2,
4x+6y = 20
”’
November 23 Morning Python
# Compiler
# interpreter – Python, R
# pycharm editor by Jetbrains
print(“HELLO”) #tffjhgjhj
”’
sdfgdfgdf
dfdfgfd
zdfgdfzgzdf
”’
# print – print the content on the screen
print(“5 + 3”)
print(5 + 3)
print(“5 + 3 =”, 5 + 3, “and 6+2 is also”,6+2)
#below we defined a variable called num1 and assgined a value
num1=30
num1 = 90

# there are 5 basic types of data:

# 1. integer (int): -infinity to + infinity no decimal
# 45, -99, 0, 678
num1=30
# type() which gives the type of the data (datatype)
print(type(num1)) #<class ‘int’>

# 2. Float (float) – decimal data
# 45.8, -99.9, 0.0, 678.123456678
num1=30.0
print(type(num1)) #<class ‘float’>
# 3. complex [square root of -1]: 3+2j, -7j

num1=30j
print(type(num1)) #<class ‘complex’>

print(30j* 30j)


# 4. string (str) – text data
# strings are defined using ‘ or ” or ”’ or “””
word1 = ‘hello’
print(type(word1)) #<class ‘str’>
word1 = ‘5’
print(type(word1)) #<class ‘str’>

# 5. boolean (bool) – just 2 values: True and False
b1 = ‘True’
print(type(b1)) #<class ‘str’>
b1 = True
print(type(b1)) #<class ‘bool’>

### ####
print(“Hello”)
a = 45;print(a);print(‘a’)

##
# variables – can have alphabets, numbers and _
# variable name cant start with a number
cost_potato = 35
cost_tomato = 55
qty_potato = 37
total_cost_potato = cost_potato * qty_potato
print(“Cost of potato is Rs”,cost_potato,“/kg so for the”,
qty_potato,“kg cost would be Rs”,total_cost_potato)

# f – string (format string)
print(f”Cost of potato is Rs {cost_potato} /kg so for the “
f”{qty_potato} kg cost would be Rs {total_cost_potato})

# print()
# int, float, bool, complex, str
# f string
print(“Hello”)
print(“Hi”)

# escape sequence: \
print(“Hello \\hi”)
print(\n will generate newline”)
print(\t will give tab spaces”)

print(\\n will generate newline”)
print(\\t will generate tab space”)

# \\n is not generate newline
print(\\\\n is not generate newline”)

# f string
q = 50
p = 25
t = q * p
print(f”The cost of each pen is {p} so for {q} quantities, the total cost will be {t}.”)

t = 200
q = 23
p = t / q
# how to format decimal places using f-string
print(f”The cost of each pen is {p:.2f} so for {q} quantities, the total cost will be {t}.”)

# format your string values
pos,country, name = “Batsman”,“India”, “Virat”
print(f”Player {name:<16} plays for {country:^16} as a {pos:>16} in the team”)
pos,country, name = “Wicket-keeper”,“South Africa”, “Mangabwabe”
print(f”Player {name:<16} plays for {country:^16} as a {pos:>16} in the team”)

### working with variables
# numeric operations available: Arithematic operations
n1, n2 = 10,5
print(n1 + n2)
print(n1 – n2)
print(n1 * n2)
print(n1 / n2)
print(n1 // n2) # integer division
print(31/4)
print(31//4)
print(n1 ** n2) #power of10 to 5
print(n1 % n2) # modulus – mod – remainder
print(31 % 4)

### relational operations
# comparators – comparison operators: > < <= >=, == , !=
# output is bool values
# 6 > 9 : is 6 greater than 9? False
n1,n2,n3 = 10,5, 10
print(n1 > n2) # T
print(n1 < n2) # F
print(n1 >= n2) # T
print(n1 <= n2) # F
print(n1 == n2) # F
print(n1 != n2) # T
print(“checking with n3….”)
print(n1 > n3) # F
print(n1 < n3) # F
print(n1 >= n3) # T
print(n1 <= n3) # T
print(n1 == n3) # T
print(n1 != n3) # F

# Assignment operation =
n1 = 10
print(n1) # value is 10

#logical operators: i/p and o/p both bool
#operators are: and or not
”’
Prediction: Rohit and Gill will open the batting
Actual: Rohit and Kishan opened the batting
Prediction ? False

Prediction: Rohit or Gill will open the batting
Actual: Rohit and Kishan opened the batting
Prediction ? True

AND:
T and T => True and rest everything is False

OR:
F or F => False and rest everything is True

NOT: Not True is false and Not false is true
”’
n1,n2,n3 = 10, 5, 10
print(n1 == n2 and n1 != n2 and n1 > n3 or n1 < n3 or n1 >= n3 and n1 <= n3 or
n1 == n3 and n1 != n3)
# F and T and F or F or T and T or T and F
# F or F or T or F
# T
# BODMAS: AND take priority over OR
## converting one type to another:
# int() str() bool() complex() float()
n1 = 50
print(n1)
print(type(n1))
n2 = str(n1)
print(type(n2))
print(n2)

# input() – is to read the content from the user
# by default, input() will read as string value
val1 = input()
print(val1)
print(type(val1))

#WAP to add two numbers by taking input from the user
a = input(“Enter first number: “)
b = input(“Enter second number: “)
c = int(a) + int(b)
print(“Sum is “,c)

# WAP to read length and breadth of a rectangle from the user
# and display area and perimeter.
len = int(input(“Enter length: “))
breadth = int(input(“Enter breadth: “))
area = len * breadth
peri = 2*(len + breadth)
print(f”Area = {area} and Perimeter = {peri})
# calculate area and circunference of a circle
pi = 3.14
rad = float(input(“Enter the radius of the circle: “))
area = pi*(rad**2)
cir = 2*pi*rad
print(f”A circle with radius {rad:.1f} has an area of {area:.2f} and circunference of {cir:.2f})


#program to calculate total and average of marks obtained in 5 subjects
sub1 = int(input(“Enter the marks in subject 1: “))
sub2 = int(input(“Enter the marks in subject 2: “))
sub3 = int(input(“Enter the marks in subject 3: “))
sub4 = int(input(“Enter the marks in subject 4: “))
sub5 = int(input(“Enter the marks in subject 5: “))
total = sub1 + sub2 + sub3 + sub4 + sub5
avg = total / 5
print(“Total marks obtained is”,total,“with an average of”,avg)

# WAP to indicate number of each value of currency note you will pay
# based on the total demand
”’
Currency notes available: 500, 100, 50, 20, 10, 5, 2, 1
Total bill = 537
500 – 1
20 – 1
10 – 1
5 – 1
2 – 1
”’
five00, one00,fifty,twenty, ten,five,two,one = 0,0,0,0,0,0,0,0
total_amount= int(input(“Enter total bill amount = “))
five00 = total_amount // 500
total_amount = total_amount % 500
one00 = total_amount // 100
total_amount = total_amount % 100
fifty = total_amount // 50
total_amount = total_amount % 50
twenty = total_amount // 20
total_amount = total_amount % 20
ten = total_amount // 10
total_amount = total_amount % 10
five = total_amount // 5
total_amount = total_amount % 5
two = total_amount // 2
total_amount = total_amount % 2
one = total_amount
print(“Total currency that would be paid:”)
print(f”500s = {five00}, 100s = {one00}, 50s ={fifty},20s = {twenty}, “
f”10s = {ten},5s = {five},2s ={two},1s={one})
# Conditions: if command is used to check for the conditions
# if command is followed by condition (conditional operator) and if
# the condition result in true it will get into If block

num1 = 9
q = int(input(“Enter the quantity: “))
if q > 0:
print(“Quantity accepted”)
print(“Sales confirmed”)
print(“Thank You”)
#but lets say you want to have alternate condition, that means when
# its True then print True part and when its not then you want to
#print false part
if q > 0: #True then go to below
print(“Given Quantity accepted”)
else: # if condition is false then comes here
print(“Quantity rejected”)

##
num = 0
if num>0:
print(“Number is positive”)
else:
print(“Number is not positive”)

# WAP to indicate number of each value of currency note you will pay
# based on the total demand
”’
Currency notes available: 500, 100, 50, 20, 10, 5, 2, 1
Total bill = 537
500 – 1
20 – 1
10 – 1
5 – 1
2 – 1
”’
five00, one00,fifty,twenty, ten,five,two,one = 0,0,0,0,0,0,0,0
total_amount= int(input(“Enter total bill amount = “))
five00 = total_amount // 500
total_amount = total_amount % 500
one00 = total_amount // 100
total_amount = total_amount % 100
fifty = total_amount // 50
total_amount = total_amount % 50
twenty = total_amount // 20
total_amount = total_amount % 20
ten = total_amount // 10
total_amount = total_amount % 10
five = total_amount // 5
total_amount = total_amount % 5
two = total_amount // 2
total_amount = total_amount % 2
one = total_amount
print(“Total currency that would be paid:”)
if five00>0:
print(f”500s = {five00})
if one00>0:
print(f”100s = {one00})
if fifty>0:
print(f”50s ={fifty})
if twenty>0:
print(f”20s = {twenty})
if ten>0:
print(f”10s = {ten})

if five>0:
print(f”5s = {five})
if two>0:
print(f”2s ={two})
if one>0:
print(f”1s={one})

#program to calculate total and average of marks obtained in 5 subjects
sub1 = int(input(“Enter the marks in subject 1: “))
sub2 = int(input(“Enter the marks in subject 2: “))
sub3 = int(input(“Enter the marks in subject 3: “))
sub4 = int(input(“Enter the marks in subject 4: “))
sub5 = int(input(“Enter the marks in subject 5: “))
total = sub1 + sub2 + sub3 + sub4 + sub5
avg = total / 5
print(“Total marks obtained is”,total,“with an average of”,avg)

# we need to check if the student has passed or failed
# avg > 50 – pass otherwise fail
if avg>=50:
print(“Student has passed”)
else:
print(“Student has failed”)
# check if a number is positive or negative or neither
num = int(input(“Enter the number: “))
if num > 0:
print(“Its positive”)
elif num < 0:
print(“Its negative”)
else:
print(“Its neither – its zero”)

#program to calculate total and average of marks obtained in 5 subjects
sub1 = int(input(“Enter the marks in subject 1: “))
sub2 = int(input(“Enter the marks in subject 2: “))
sub3 = int(input(“Enter the marks in subject 3: “))
sub4 = int(input(“Enter the marks in subject 4: “))
sub5 = int(input(“Enter the marks in subject 5: “))
total = sub1 + sub2 + sub3 + sub4 + sub5
avg = total / 5
print(“Total marks obtained is”,total,“with an average of”,avg)

# we need to check if the student has passed or failed
# avg > 50 – pass otherwise fail
if avg>=50:
print(“Student has passed”)
else:
print(“Student has failed”)

# Grading of the student:
”’
avg >= 90 : Grade A
avg >= 80 : Grade B
avg >= 70: Grade C
avg >= 60: Grade D
avg >= 50: Grade E
avg <50: Grade F
”’
if avg >= 90 :
print(“Grade A”)
elif avg >= 80 :
print(“Grade B”)
elif avg >= 70:
print(“Grade C”)
elif avg >= 60:
print(“Grade D”)
elif avg >= 50:
print(“Grade E”)
else:
print(“Grade F”)

# input a number and check if the number is odd or even
num = int(input(“Enter the number: “))
if num<0:
print(“Invalid number!”)
elif num%2==0:
print(“Even number”)
else:
print(“Odd Number”)

# input a number and check if the number is odd or even
# example of nested condition
num = int(input(“Enter the number: “))
if num>0:
if num%2==0:
print(“Even number”)
else:
print(“Odd Number”)

#program to calculate total and average of marks obtained in 5 subjects
sub1 = int(input(“Enter the marks in subject 1: “))
sub2 = int(input(“Enter the marks in subject 2: “))
sub3 = int(input(“Enter the marks in subject 3: “))
sub4 = int(input(“Enter the marks in subject 4: “))
sub5 = int(input(“Enter the marks in subject 5: “))
total = sub1 + sub2 + sub3 + sub4 + sub5
avg = total / 5
print(“Total marks obtained is”,total,“with an average of”,avg)
# we need to check if the student has passed or failed
# avg > 50 – pass otherwise fail
# and also assign Grades
if avg>=50:
print(“Student has passed”)
if avg >= 90:
print(“Grade A”)
if avg>95:
print(“You win President’s Medal”)
if avg >98:
print(“You win State Governor’s Award!”)
elif avg >= 80:
print(“Grade B”)
elif avg >= 70:
print(“Grade C”)
elif avg >= 60:
print(“Grade D”)
else:
print(“Grade E”)
else:
print(“Student has failed”)
print(“Grade F”)

########## ###########
## LOOPS
########## ###########
# Loops : repeatition
# hello 10 times
# for loop: used when we know how many times to repeat
# while loop: used when we know when to repeat and when not to
# range() works with if
# range(=start, <end, =step): start = 5, end =11, step=2:
# 5, 7, 9,
# range(=start, <end): step default = 1
# range(5,11): 5,6,7,8,9,10

# range(<end): step default = 1, start default = 0
# range(5): 0,1,2,3,4
for i in range(5,11,2):
print(i,“hello 1”)

for i in range(5,10):
print(i,“hello 2”)

for i in range(5):
print(i,“hello”)
# Loops – repeatitive tasks
# for loop and while loop

# generate numbers from 1 to 20
for i in range(1,21):
print(i,end=“, “)
print()
# generate first 10 odd numbers
for i in range(10):
print(i*2+1,end=“, “)
print()
# generate even numbers between 10 and 20
for i in range(10,21,2):
print(i,end=“, “)
print()
#multiplication table of 8 upto 10 multiples
num = 8
for i in range(1,11):
print(f”{num} * {i} = {num*i})

#multiplication table of 1 to 10 upto 10 multiples
for num in range(1,11):
for i in range(1,11):
print(f”{num} * {i} = {i*num})

#multiplication table of 1 to 10 upto 10 multiples
# print them side by side
”’
1×1=1 2×1=2 … 10×1=10
1×2=2 2×2=2

1×10=10
”’
for num in range(1,11):
for i in range(1,11):
print(f”{i} * {num} = {num*i},end=\t)
print()

print(“Thank you”)
######### #######
## More examples of For Loop
####### #######
for i in range(5):
print(“*”,end=” “)
print()
print(\n#print square pattern of stars”)
num = 5
for j in range(num):
for i in range(num):
print(“*”,end=” “)
print()

print(\n#right angled triangle pattern”)
num = 5
for j in range(num): #tracking rows
for i in range(j+1): #column
print(“*”,end=” “)
print()

print(\n#inverted right angled triangle pattern”)
num = 5
for j in range(num): #tracking rows
for i in range(num-j): #column
print(“*”,end=” “)
print()

print(\n#Isoceles triangle pattern”)
”’
* * * * *
* * * *
* * *
* *
*
”’
num = 5
for j in range(num): #tracking rows
for i in range(j): #column
print(” “,end=“”)
for k in range(num-j): #column
print(“*”,end=” “)
print()

print(\nASSIGNMENT: Inverted Isoceles triangle pattern”)
”’
*
* *
* * *
* * * *
* * * * *
”’
# Write your code here

# Calcualte total of 5 subjects marks
total = 0
for i in range(5):
m1 = int(input(“Marks in subject “+ str(i+1)+ “: “))
total += m1 #total = total + m1
print(total)
# Loops – While loop: we know the condition when to start/stop
# generate numbers from 1 to 20
i=1
while i<21:
print(i,end=“, “)
i=i+1
print()
# generate first 10 odd numbers
i = 0
while i <10:
print(i*2+1,end=“, “)
i+=1
print()
# generate even numbers between 10 and 20
i=10
while i<21:
print(i,end=“, “)
i+=2
print()

## ## ##
# Calcualte total of 5 subjects marks for given number of students
cont = 1
while cont ==1:
total = 0
for i in range(5):
m1 = int(input(“Marks in subject “+ str(i+1)+ “: “))
total += m1 #total = total + m1
print(total)
inp = input(“Do you want to add more students (y for yes/anyother key to stop):”)
if inp!=“y”:
cont = 0

## rewriting above program
while True:
total = 0
for i in range(5):
m1 = int(input(“Marks in subject “+ str(i+1)+ “: “))
total += m1 #total = total + m1

print(total)
inp = input(“Do you want to add more students (y for yes/anyother key to stop):”)
if inp!=“y”:
break
”’
break: it will throw you out of the loop
”’
”’
Create a set of Menu options for a Library
”’
while True:
print(“Menu:”)
print(“1. Add books to the library”)
print(“2. Remove books from the library”)
print(“3. Issue the book to the member”)
print(“4. Take the book from the member”)
print(“5. Quit”)
choice = input(“Enter your option:”)
if choice==“1”:
print(“Adding books to the library”)
elif choice==“2”:
print(“Removing books from the library”)
elif choice==“3”:
print(“Issuing the book to the member”)
elif choice==“4”:
print(“Taking the book from the member”)
elif choice==“5”:
break
else:
print(“Invalid option! try again…”)

”’
Based on this program, create a Menu option for performing basic
arithematic operations like + – * / // ** %
”’
”’
Develop a guessing number Game

”’
import random
num1 = random.randint(1,100)
attempts = 0
while True:
guess = int(input(“Guess the number (1-100): “))
if guess >100 or guess < 1:
print(“INVALID NUMBER!”)
continue
attempts+=1
if num1 ==guess:
print(f”Good job! You guessed it correctly in {attempts} attempts.”)
break
elif num1 < guess:
print(“Sorry! Try guessing lower number”)
else:
print(“Sorry! Try guessing higher number”)

# # # # # #
”’
Develop a guessing number Game

”’
import random
num1 = random.randint(1,100)
attempts = 0
low,high = 1,100
while True:
#lets make computer guess the number
guess = random.randint(low,high)
if guess >100 or guess < 1:
print(“INVALID NUMBER!”)
continue
attempts+=1
if num1 ==guess:
print(f”Good job! You guessed {guess} correctly in {attempts} attempts.”)
break
elif num1 < guess:
print(f”Sorry! Try guessing lower number than {guess})
high=guess – 1
else:
print(f”Sorry! Try guessing higher number than {guess})
low= guess + 1

# # # #
# # # # #
# LIST
# # # # #
l1 = [25,45.9,“1”,[29,41]]
print(“Data type of L1 : “,type(l1))
print(“Number of values in L1 : “,len(l1))
print(“Values in the list = “,l1)

l2 = [“Hello”,“How”,“Are”,“You”]
l3 = l1 + l2
print(“L3 = “,l3)
print(“Multiply = “,l2 * 3)

# Indexing
#indexing starts with 0,
print(“4th value from the list: “,l3[3])
l5 = l3[3]
print(l5[1])
print(l3[3][1])
print(“last member of the list = “,l3[len(l3)-1])
print(“last member of the list = “,l3[-1])
print(“first member of the list = “,l3[0])
print(“first member of the list = “,l3[-len(l3)])
print(“First 3 members of the list: “,l3[0:3])
print(“First 3 members of the list: “,l3[:3])
print(“Last 3 members of the list: “,l3[:])
”’
List – linear ordered mutable collection
”’
l1 = [5,15,25,35]
l1[1] = 20 #mutable
print(l1)
print(l1[1:3])
print(l1[:]) #left of : blank -0 index, blank on right indicate – last index
print(“Last 2 members of L1=”,l1[2:4], “or”,l1[2:],“or”,l1[-2:])
var1 = ‘hello’
print(var1[1])
# strings are immutable
#var1[1]=”E” – TypeError: ‘str’ object does not support item assignment

l1 = [100]
print(type(l1))
l1.append(10) # to add members to the list – it will add at the end
l1.append(20)
l1.append(30)
#insert() – also adds but it needs the position
l1.insert(40,1)
l1.insert(1,50)
l1.insert(1,20)
l1.insert(1,20)

print(l1)
#remove(value_to_be_removed) , pop(index_to_be_deleted)
# remove all 20s
#first count the # of 20
num_20 = l1.count(20)
for i in range(num_20):
l1.remove(20)
print(l1)
l1.pop(1)
print(l1)
l1.append(100)
# check if 100 is in the list
count_100 = l1.count(100)
if count_100 >0:
print(“100 is in the list and at position”,l1.index(100))
print(“100 is in the list and at position”, l1.index(100,1,7))
else:
print(“100 is not in the list”)

## write a program to read marks of 5 subjects
# and store the values and display total and avg

marks=[]
for i in range(5):
m = int(input(“Enter marks: “))
marks.append(m)

print(“Total marks = “,sum(marks),“and avg=”,sum(marks)/5)
l2 = [1,2,3,4,5,6,2,3,4]
l2.reverse()
print(“Reverse: “,l2)
l2.sort() #sort in increasing order
print(“Sort: “,l2)
l2.sort(reverse=True) #sorting in decreasing order
print(l2)
####
print(“======= ========”)

l3 = l2 #deep copy – two names of same list
l4 = l2.copy() #shallow copy – photocopy
print(“L2 = “,l2)
print(“L3 = “,l3)
print(“L4 = “,l4)
l2.append(77)
l3.append(88)
l4.append(99)
print(“L2 = “,l2)
print(“L3 = “,l3)
print(“L4 = “,l4)
print(“======= ========”)

l2.clear() # delete all the members from the list
print(“After clear: “,l2)

”’
Stack – First In Last Out:
Assignment – Implement Stack concept using List:
use: while True to create a menu with 4 options:
1. Add to the stack
2. Remove to the stack
3. Print the values from the stack
4. Quit
Start with an empty list and perform addition/deletion
to the list based on user selection
”’
# TUPLE
val1 = (1, “hello”, [3, 4, 5]) # packing
print(“Data type = “, type(val1))
# immutable
l1 = [2, 3, 4]
l1[1] = 33
print(“L1 = “, l1)
# val1[1]=”There” TypeError: ‘tuple’ object does not support item assignment
# so tuples are immutable

# indexing – reading values from tuple is exactly like list
for t in val1:
print(t)

# unpacking:
v1, v2, v3 = val1
print(v1, v2, v3)
print(“Number of values in tuple=”, len(val1))

val2 = (39, 68)
val3 = (14, 98, 254, 658)
if val2 > val3:
print(“Val2 is greater”)
else:
print(“Val3 is greater”)

# converting tuple to list
val1 = list(val1)
val1 = tuple(val1)
#STRINGS
# handling text data
str1 = “HELLO”
str2 = ‘Hi there’
str3 = ”’Hello how are you?
Are doing well?
See you soon”’
str4 = “””I am fine
I am doing well
hope you are doing great too”””
print(str3)

# indexing is very similar to list and tuple
print(str1[0],str1[-1],str1[:])
print(str1+” “+str2)
print(str1*3)
# strings are also (like tuple) immutable
# you cant edit the existing string value
#str1[0]=”Z” TypeError: ‘str’ object does not support item assignment
str1 = “Z”+str1[1:]
print(“New Value = “,str1)

# you can use str in for loop just like List or Tuple
for s in str1:
print(s)

#methods in str class
str1 = “heLLO how aRE YOU?”
# case related – convering to a specific case
print(str1.lower())
print(str1.upper())
print(str1.title())

#checking the existing str value
str1 = “heLLO how aRE YOU?”
str2= “helohowru”
print(str1.isalpha())
print(str2.isalpha())
print(str1.isspace())
print(str2.islower())
print(str2.isalnum())
JUNE 2023 Data Science Course

THIS IS THE 4th and the last part of the complete Data Science with Python course started 3 months ago!!

”’
NLP – Natural Language Processing – analysing review comment to understand
reasons for positive and negative ratings.
concepts like: unigram, bigram, trigram

Steps we generally perform with NLP data:
1. Convert into lowercase
2. decompose (non unicode to unicode)
3. removing accent: encode the content to ascii values
4. tokenization: will break sentence to words
5. Stop words: not important words for analysis
6. Lemmetization (done only on English words): convert the words into dictionary words
7. N-grams: set of one word (unigram), two words (bigram), three words (trigrams)
8. Plot the graph based on the number of occurrences and Evaluate
”’
”’
cardboard mousepad. Going worth price! Not bad
”’

link=“D:/datasets/OnlineRetail/order_reviews.csv”
import pandas as pd
import unicodedata
import nltk
import matplotlib.pyplot as plt
df = pd.read_csv(link)
print(list(df.columns))
”’
[‘review_id’, ‘order_id’, ‘review_score’, ‘review_comment_title’,
‘review_comment_message’, ‘review_creation_date’, ‘review_answer_timestamp’]
”’
#df[‘review_creation_date’] = pd.to_datetime(df[‘review_creation_date’])
#df[‘review_answer_timestamp’] = pd.to_datetime(df[‘review_answer_timestamp’])

# data preprocessing – making data ready for analysis
reviews_df = df[df[‘review_comment_message’].notnull()].copy()
#print(reviews_df)

# remove accents
def remove_accent(text):
return unicodedata.normalize(‘NFKD’,text).encode(‘ascii’,errors=‘ignore’).decode(‘utf-8’)
#STOP WORDS LIST:
STOP_WORDS = set(remove_accent(w) for w in nltk.corpus.stopwords.words(‘portuguese’))

”’
Write a function to perform basic preprocessing steps
”’
def basic_preprocessing(text):
#converting to lower case
txt_pp = text.lower()
#print(txt_pp)

#remove the accent
#txt_pp = unicodedata.normalize(‘NFKD’,txt_pp).encode(‘ascii’,errors=’ignore’).decode(‘utf-8’)
txt_pp =remove_accent(txt_pp)
#print(txt_pp)
#tokenize
txt_token = nltk.tokenize.word_tokenize(txt_pp)
#print(txt_token)

# removing stop words
txt_token = tuple(w for w in txt_token if w not in STOP_WORDS and w.isalpha())
return txt_token



## write a function to creaet unigram, bigram, trigram
def create_ngrams(words):
unigrams,bigrams,trigrams = [],[],[]
for comment in words:
unigrams.extend(comment)
bigrams.extend(‘ ‘.join(bigram) for bigram in nltk.bigrams(comment))
trigrams.extend(‘ ‘.join(trigram) for trigram in nltk.trigrams(comment))


return unigrams, bigrams, trigrams


# applying basic preprocessing:
reviews_df[‘review_comment_words’] = \
reviews_df[‘review_comment_message’].apply(basic_preprocessing)

#get positive reviews – all 5 ratings in review_score
reviews_5 = reviews_df[reviews_df[‘review_score’]==5]

#get negative reviews – all 1 ratings
reviews_1 = reviews_df[reviews_df[‘review_score’]==1]
#create ngrams for rating 5 and rating 1
uni_5, bi_5, tri_5 = create_ngrams(reviews_5[‘review_comment_words’])
print(uni_5)
print(bi_5)
print(tri_5)

# Assignment: perform similar tasks for reviews that are negative (review score = 1)
#uni_1, bi_1, tri_1 = create_ngrams(reviews_1[‘review_comment_words’])
#print(uni_5)

# distribution plot
def plot_dist(words, color):
nltk.FreqDist(words).plot(20,cumulative=False, color=color)

plot_dist(tri_5, “red”)

#NLP – Natural Language processing:
# sentiments: Positive, Neutral, Negative
#
”’
we will use nltk library for NLP:
pip install nltk
”’
import nltk
#1. Convert into lowercase
text = “Product is great but I amn’t liking the colors as they are worst”
text = text.lower()

”’
2. Tokenize the content: break it into words or sentences
”’
text1 = text.split()
#using nltk
from nltk.tokenize import sent_tokenize,word_tokenize
text = word_tokenize(text)
#print(“Text =\n”,text)
#print(“Text =\n”,text1)

”’
3. Removing Stop words: Words which are not significant
for your analysis. E.g. an, a, the, is, are
”’
my_stopwords = [‘is’,‘i’,‘the’]
text1 = text
for w in text1:
   
if w in my_stopwords:
        text.remove(w)
print(“Text after my stopwords:”,text1)

nltk.download(
“stopwords”)
from nltk.corpus import stopwords
nltk_eng_stopwords =
set(stopwords.words(“english”))
#print(“NLTK list of stop words in English: “,nltk_eng_stopwords)
”’
Just for example: we see the word but in the STOP WORDS but
we want to include it, then we need to remove the word from the set
”’
# removing but from the NLTK stop words
nltk_eng_stopwords.remove(‘but’)

for w in text:
   
if w in nltk_eng_stopwords:
        text.remove(w)
print(“Text after NLTK stopwords:”,text)

”’
4. Stemming: changing the word to its root
eg: {help: [help, helped, helping, helper]}

One of the method is Porter stemmer
”’
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
text = [stemmer.stem(w)
for w in text]
”’ above line is like below:
t_list=[]
for w in text:
    a = stemmer.stem(w)
    t_list.append(a)
”’
print(“Text after Stemming:”,text)
”’
5. Part of Speech Tagging (POS Tagging)
grammatical word which deals with the roles they place
like – 8 parts of speeches – noun, verb, …

Reference: https://www.educba.com/nltk-pos-tag/
POS Tagging will give Tags like

CC: It is the conjunction of coordinating
CD: It is a digit of cardinal
DT: It is the determiner
EX: Existential
FW: It is a foreign word
IN: Preposition and conjunction
JJ: Adjective
JJR and JJS: Adjective and superlative
LS: List marker
MD: Modal
NN: Singular noun
NNS, NNP, NNPS: Proper and plural noun
PDT: Predeterminer
WRB: Adverb of wh
WP$: Possessive wh
WP: Pronoun of wh
WDT: Determiner of wp
VBZ: Verb
VBP, VBN, VBG, VBD, VB: Forms of verbs
UH: Interjection
TO: To go
RP: Particle
RBS, RB, RBR: Adverb
PRP, PRP$: Pronoun personal and professional

But to perform this, we need to download any one tagger:
e.g. averaged_perceptron_tagger
nltk.download(‘averaged_perceptron_tagger’)
”’
nltk.download(‘averaged_perceptron_tagger’)

import nltk
from nltk.tag import DefaultTagger
py_tag = DefaultTagger (
‘NN’)
tag_eg1 = py_tag.tag ([
‘Example’, ‘tag’])
print(tag_eg1)

#txt = “Example of nltk pos tag list”
#txt = [‘product’, ‘great’, ‘but’, “not”, ‘like’, ‘color’]
#txt = word_tokenize(txt)
#txt = [‘Example’,’of’,’nltk’,’pos’,’tag’,’list’]
pos_txt = nltk.pos_tag(text)
print(“POS Tagging:”, pos_txt)

”’
6. Lemmetising
takes a word to its core meaning
We need to download:  wordnet
”’
nltk.download(‘wordnet’)
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
print(“Very good = “,lemmatizer.lemmatize(“very good”))
print(“Halves = “,lemmatizer.lemmatize(“halves”))

text =
“Product is great but I amn’t liking the colors as they are worst”
text = word_tokenize(text)
text = [lemmatizer.lemmatize(w)
for w in text]
print(“Text after Lemmatizer: “,text)


# Sentiment analysis – read the sentiments of each sentence
”’
If you need more data for your analysis, this is a good source:
https://github.com/pycaret/pycaret/tree/master/datasets

We will use Amazon.csv for this program

”’
import pandas as pd
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.sentiment.vader import SentimentIntensityAnalyzer

link = “https://raw.githubusercontent.com/pycaret/pycaret/master/datasets/amazon.csv”
df = pd.read_csv(link)
print(df)

#Let’s create a function to perform all the preprocessing steps
# of a nlp analysis
def preprocess_nlp(text):
#tokenise
#print(“0”)
text = text.lower() #lowercase
#print(“1”)
text = word_tokenize(text) #tokenize
#print(“2”)
text = [w for w in text if w not in stopwords.words(“english”)]
#lemmatize
#print(“3”)
lemm = WordNetLemmatizer()
#print(“4”)
text = [lemm.lemmatize(w) for w in text]
#print(“5”)
# now join all the words as we are predicting on each line of text
text_out = ‘ ‘.join(text)
#print(“6”)
return text_out

# import Resource vader_lexicon
import nltk
nltk.download(‘vader_lexicon’)


df[‘reviewText’] = df[‘reviewText’].apply(preprocess_nlp)
print(df)

# NLTK Sentiment Analyzer
# we will now define a function get_sentiment() which will return
# 1 for positive and 0 for non-positive
analyzer = SentimentIntensityAnalyzer()
def get_sentiment(text):
score = analyzer.polarity_scores(text)
sentiment = 1 if score[‘pos’] > 0 else 0
return sentiment

df[‘sentiment’] = df[‘reviewText’].apply(get_sentiment)

print(“Dataframe after analyzing the sentiments: \n,df)

#confusion matrix
from sklearn.metrics import confusion_matrix
print(“Confusion matrix:\n,confusion_matrix(df[‘Positive’],df[‘sentiment’]))

”’ RESULT

Confusion matrix:
[[ 1131 3636]
[ 576 14657]]
Accuracy: (1131 + 14657) / (1131 + 14657 + 576 + 3636) = 15788/20000 = 78.94%
”’

# Visualization
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
plt.hist(data, bins=30, histtype=‘stepfilled’, color=“red”)
plt.title(“Histogram Display”)
plt.xlabel(“Marks”)
plt.ylabel(“Number of Students”)
plt.show()
# Analyzing Hotel Bookings data
# https://github.com/swapnilsaurav/Dataset/blob/master/hotel_bookings.csv
link=“https://raw.githubusercontent.com/swapnilsaurav/Dataset/master/hotel_bookings.csv”
import pandas as pd
df = pd.read_csv(link)
#print(“Shape of the data: “,df.shape)
#print(“Data types of the columns:”,df.dtypes)
import numpy as np
df_numeric = df.select_dtypes(include=[np.number])
#print(df_numeric)
numeric_cols = df_numeric.columns.values
#print(“Numeric column names: “,numeric_cols)
df_nonnumeric = df.select_dtypes(exclude=[np.number])
#print(df_nonnumeric)
nonnumeric_cols = df_nonnumeric.columns.values
#print(“Non Numeric column names: “,nonnumeric_cols)

####
#preprocessing the data
import seaborn as sns
import matplotlib.pyplot as plt
colors = [“#091AEA”,“#EA5E09”]
cols = df.columns
sns.heatmap(df[cols].isnull(), cmap=sns.color_palette(colors))
plt.show()

cols_to_drop = []
for col in cols:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
#print(f”{col} -> {pct_miss}”)
cols_to_drop.append(col) #column list to drop

# remove column since it has more than 80% missing value
df = df.drop(cols_to_drop, axis=1)

for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
print(f”{col} -> {pct_miss})
# check for rows to see the missing values
missing = df[col].isnull()
num_missing = np.sum(missing)
if num_missing >0:
df[f’{col}_ismissing’] = missing
print(f”Created Missing Indicator for {cols})

### keeping track of the missing values
ismissing_cols = [col for col in df.columns if ‘_ismissing’ in col]
df[‘num_missing’] = df[ismissing_cols].sum(axis=1)
print(df[‘num_missing’])

# drop rows with > 12 missing values
ind_missing = df[df[‘num_missing’] > 12].index
df = df.drop(ind_missing,axis=0) # ROWS DROPPED

#count for missing values
for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >0:
print(f”{col} -> {pct_miss})

”’
Still we are left with following missing values:
children -> 2.0498257606219004
babies -> 11.311318858061922
meal -> 11.467129071170085
country -> 0.40879238707947996
deposit_type -> 8.232810615199035
agent -> 13.687005763302507
”’
# Analyzing Hotel Bookings data
# https://github.com/swapnilsaurav/Dataset/blob/master/hotel_bookings.csv
link=“https://raw.githubusercontent.com/swapnilsaurav/Dataset/master/hotel_bookings.csv”
import pandas as pd
df = pd.read_csv(link)
#print(“Shape of the data: “,df.shape)
#print(“Data types of the columns:”,df.dtypes)
import numpy as np
df_numeric = df.select_dtypes(include=[np.number])
#print(df_numeric)
numeric_cols = df_numeric.columns.values
print(“Numeric column names: “,numeric_cols)
df_nonnumeric = df.select_dtypes(exclude=[np.number])
#print(df_nonnumeric)
nonnumeric_cols = df_nonnumeric.columns.values
print(“Non Numeric column names: “,nonnumeric_cols)

####
#preprocessing the data
import seaborn as sns
import matplotlib.pyplot as plt
colors = [“#091AEA”,“#EA5E09”]
cols = df.columns
sns.heatmap(df[cols].isnull(), cmap=sns.color_palette(colors))
plt.show()

cols_to_drop = []
for col in cols:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
#print(f”{col} -> {pct_miss}”)
cols_to_drop.append(col) #column list to drop

# remove column since it has more than 80% missing value
df = df.drop(cols_to_drop, axis=1)

for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >80:
print(f”{col} -> {pct_miss})
# check for rows to see the missing values
missing = df[col].isnull()
num_missing = np.sum(missing)
if num_missing >0:
df[f’{col}_ismissing’] = missing
#print(f”Created Missing Indicator for {cols}”)

### keeping track of the missing values
ismissing_cols = [col for col in df.columns if ‘_ismissing’ in col]
df[‘num_missing’] = df[ismissing_cols].sum(axis=1)
print(df[‘num_missing’])

# drop rows with > 12 missing values
ind_missing = df[df[‘num_missing’] > 12].index
df = df.drop(ind_missing,axis=0) # ROWS DROPPED

#count for missing values
for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >0:
print(f”{col} -> {pct_miss})

”’
Still we are left with following missing values:
children -> 2.0498257606219004 # numeric
babies -> 11.311318858061922 #numeric
meal -> 11.467129071170085 # non-numeric
country -> 0.40879238707947996 # non-numeric
deposit_type -> 8.232810615199035 # non-numeric
agent -> 13.687005763302507 #numeric
”’
#HANDLING NUMERIC MISSING VALUES
df_numeric = df.select_dtypes(include=[np.number])
for col in df_numeric.columns.values:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss > 0:
med = df[col].median()
df[col] = df[col].fillna(med)

#HANDLING non-NUMERIC MISSING VALUES
df_nonnumeric = df.select_dtypes(exclude=[np.number])
for col in df_nonnumeric.columns.values:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss > 0:
mode = df[col].describe()[‘top’]
df[col] = df[col].fillna(mode)


print(“#count for missing values”)
for col in df.columns:
pct_miss = np.mean(df[col].isnull()) * 100
if pct_miss >0:
print(f”{col} -> {pct_miss})

#drop duplicate values
print(“Shape before dropping duplicates: “,df.shape)
df = df.drop(‘id’,axis=1).drop_duplicates()
print(“Shape after dropping duplicates: “,df.shape)

DAY 73: Power BI (Coming Soon)

DAY 74: Tableau (Coming soon)

Thats the end of the course - entire content in presented in 4 blog pages
Learn Python and Data Science – WkDay SEP 2023

This page is a live document and will be updated with content as we make progress

print(“skdjidsjgdfji”)
print(“5+3=”, 5+3)

num1 = 5
num2 = 3
print(num1 + num2)
num1 = 21
print(num1 – num2)

# print() –
print(“5+3=”,5+3,“and 6+4=”,6+4,end=” : “);
# every print() statement has an invisible newline \n
print( “5+5=”,5+5,“and 6+14=”,6+14);

print(“Twinkle Twinkle”, end=” ” ) # sample comment fgjdtjgdhjcghjgh
print(“Little star”)
# indentation is key!
#semicolon ; exist in Python but its not mandatory
print(“Hi there”); print(“How are you?”)
# syntax : grammer of human language

#variables – stores temporary values
var1 = 5
# create a variable by name var1 which currently has the value 5
var2 = 10
var1 = 20
print(var1 + var2)
#data which variables have
# data types- what kind of values a variable has
#basic data types: int (integer – no decimal part): -5, -2,0,5,999…
# type() gives the current data types
print(“Type of var1 = “,type(var1))

# datatype – float: with decimal types: -5.9, -3.8978888,0.0, 5.7
var3 = 20.5
print(“datatype(var3): “, type(var3))

# complex numbers – square root of minus numbers
# square root of -1 is i (in maths) – j (in Python)
# sq root of -16: 16 * -1 = 4j
var4 = 3+4j
print(“Data type of var4 = “,type(var4))
# (3+4j)*(3-4j) = 9 – (-16)= 9+16 = 25 + 0j
print((3+4j)*(34j))
# 3.2 + 2.8 =6.0

# 4th datatype – text type is called – str (string)
var5 = “hello” #string data will always be in quotes: ‘ or “
print(“datatype(var5) =”,type(var5))

# 5th data type = boolean (bool)
# bool = True or False only
var6 = False
print(“Data type of var6 =”,type(var6))
# var6 is an object of class bool – meaning var6 inherits all the
# properties of class bool
var7 = ‘FALSE’

# program is run successfully when exit code is 0
quantity = 17
price = 48
total_cost = quantity * price
print(“Cost of each pen is”,price,“so the total cost of”,quantity,“pens would be”,total_cost)
# format string
print(f”Cost of each pen is {price} so the total cost of {quantity} pens would be {total_cost})
”’
refer to lines 51 to 56, write below programs:
1. WAP to calulcate area and perimeter of a rectangle
2. WAP to calculate area and circunference of a circle
”’

var1 = 5
print(var1)
var1 = “Five”
print(var1)
”’
#about variables
1. variable name should start with a text
2. variable name can have digits and _
”’
cost = 17
quantity = 5
price = cost * quantity
print(“The cost of each pen is”,cost,“so the total cost of”,quantity,“pens will be”,price)
# format string / f-string
print(f”The cost of each pen is {cost} so the total cost of {quantity} pens will be {price})

price = 100
quantity = 33
cost = price / quantity
print(“The cost of each pen is”,cost,“so the total cost of”,quantity,“pens will be”,price)
# format string / f-string
print(f”The cost of each pen is {cost:.1f} so the total cost of {quantity} pens will be {price})

player,country,position = “Kohli”,“India”,“Opener”
print(f”Player {player:<12} plays for the country {country:>10} and is {position:^15} for the team.”)

player,country,position = “Manbwange”,“Zimbabwe”,“Wicket-Keeper”
print(f”Player {player:<12} plays for the country {country:>10} and is {position:^15} for the team.”)
print(f”Player {player:.<12} plays for the country {country:>10} and is {position:X^15} for the team.”)

# input dynamic value from the user
a = input(“Enter a number: “)
print(“You have entered:”,a)
print(f”1. Data type of {a} is {type(a)})
b= 100 #assigning an integer
c=“100” #assigning a string
# input() since it cant predict what value is being entered,
# it assumes that all the input value is a string
a = input(“Enter a number: “)
a= int(a) #explicit conversion of data types
print(f”2. Data type of {a} is {type(a)})

# int(), float(), str(), bool(), complex()

## escape sequeence – \ works only for one character after it appears
## to add or remove super power
print(“We are talking about\nis for new line”)
print(“We are talking about\tis for new line”)
print(“We are talking about\\tis for new line”)
print(“We are talking about\\nis for new line”)

# \\n in python prints \n
print(\\\\n in python prints \\n”)


# Different operators
# Arithematic operators
num1,num2 = 59,10
print(num1 + num2) #addition
print(num1 – num2) # subtraction
print(num1 * num2) # multiplication
print(num1 / num2) #division – 5.0
print(2 ** 3) #power
print(num1 // num2) #integer division
print(num1 % num2) # % mod – remainder

# comparison /relational operators
# > < >= <= ==(is it equal?) !=
#anything as input – output is always bool
num1,num2 = 59,10
print(“Greater”)
print(num1 > num2) #True
print(num1 >= num1) #True
print(num1 > num1) # False
print(“Smaller”)
print(num1 < num2) # False
print(num1 <= num2) # False
print(num1 < num1) # False
print(“Equal”)
print(num1 == num2) # False
print(num1 == num1) # True
print(num1 != num2) # True
print(num1 != num1) # False

# logical operators:
# operators: and or not
#input and output are all bool values
# prediction: Rahul and Rohit will open the batting
# actual: Rohit and Gill opened the batting
# and (*): True and True = True , rest all the 3 options are False
# or (+): False or False = False , rest all the 3 options are True
# prediction: Rahul or Rohit will open the batting
# actual: Rohit and Gill opened the batting
print(num1 > num2 or num1 >= num1 and num1 > num1 or num1 < num2 and num1 <= num2 or num1 < num1
and num1 == num2 or num1 == num1 and num1 != num2 or num1 != num1)
”’
True
”’
print(not True)
print(not False)

#### ####
”’
Condition and iterations
Conditions are handled using if
”’
avg = 39
# if avg >= 40 then say pass otherwise say fail
if avg >=40:
print(“You have passed!”)

if avg >=40:
print(“You have passed!”)
print(“PASSSSSSSSSSSSS”)
else:
print(“You have failed!”)
# conditions in Python
# WAP to find sum, avg of 3 subjects marks and check if pass or fail
”’
Any program is made up of: 3 parts: input, process, output

”’
# 1. input marks in 3 subjects
m1 = int(input(“Enter the marks in subject 1: “))
m2 = int(input(“Enter the marks in subject 2: “))
m3 = int(input(“Enter the marks in subject 3: “))
#process: calculating total and avg
total = m1 + m2 + m3
avg = total /3
if avg >=40:
# output – when if condition becomes True
print(“You have passed”)
else: # when if is False
print(“You have failed”)

#WAP to check if a number is positive or not
num1 = int(input(“Enter a number: “))
if num1 >0:
print(f”{num1} is positive”)
elif num1 <0:
print(f”{num1} is negative”)
else:
print(f”{num1} is neither positive nor negative”)


#WAP to check if a number is odd or even
num1 = int(input(“Enter a number to check if its odd or even: “))
if num1>=0:
print(“Can be odd or even”)

if num1 % 2 == 0:
print(f”{num1} is an even number”)
else:
print(f”{num1} is an odd number”)
else:
print(“Negative numbers are not fit”)

”’
Assignment: input a number and check if its positive or negative.
If positive check if the number is divisible by 2 , 3, 7.
if its divisible by 7 then check if its even or odd.
If its odd check if its greater than 100 or not
”’
”’
Take average and check if pass or fail and
then assign grade based on:
A : avg > 85%
B : avg > 75%
C : avg > 60%
D: avg > 40%
E: avg < 40%
”’
# 1. input marks in 3 subjects
m1 = int(input(“Enter the marks in subject 1: “))
m2 = int(input(“Enter the marks in subject 2: “))
m3 = int(input(“Enter the marks in subject 3: “))
#process: calculating total and avg
total = m1 + m2 + m3
avg = total /3
if avg >=40:
# output – when if condition becomes True
print(“You have passed”)
if avg>=85:
print(“Grade: A”)
elif avg>=75:
print(“Grade: B”)
elif avg>=50:
print(“Grade: C”)
else:
print(“Grade: D”)
else: # when if is False
print(“You have failed”)
print(“Grade: E”)

”’
Loops – execute same block of code more than once
1. For loop: when you know exactly how many times to run the loop
2. While loop: when you dont know exactly how many times
but you know a perticular condition till when to run
”’
# working of range(): generates range of values
”’
range(start,end,increment): range() takes 3 values. Start indicates
the starting value of the series. End indicates last but one value(upto end,
end is not included). increment will increase the start value
range(3,12,3) – 3,6,9

range(start,end): increment is default 1
range(3,8): 3,4,5,6,7

range(end): default start is 0 and increment is 1
range(5): 0,1,2,3,4
”’

for i in range(3,12,3):
print(“Hello and the value of i is”,i)

for i in range(3,8):
print(“Hello there and the value of i is”,i)

for i in range(5):
print(“Hello and the value of i is”,i)
#while will have always have condition, as long as the condition
# is true, while loop will repeat
count = 0
while count < 5:
print(“HELLO”)
count=count+1
ch=“y”
while ch==“y”:
print(“Do you want to continue?”)
ch=input(“Enter y to continue or any other key to stop: “)
# for loop example using range
for i in range(5):
print(“*”,end=” “)

”’
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
”’
for j in range(5):
for i in range(5):
print(“*”,end=” “)
print()

”’
*
* *
* * *
* * * *
* * * * *
”’
for j in range(5):
for i in range(j+1):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for j in range(5):
for i in range(5-j):
print(“*”,end=” “)
print()

”’
*
* *
* * *
* * * *
* * * * *
”’
for j in range(5):
for k in range(5-j-1):
print(” “,end=“”)

for i in range(j+1):
print(“*”,end=” “)
print()
”’
Assignment:

* * * * *
* * * *
* * *
* *
*
”’
# While loop in Python
”’
Write a program to keep checking if a number is odd or
even until user enters a negative number
”’

num1 = int(input(“Enter the number: “))
while num1 >=0:
if num1%2 ==0:
print(“Its even!”)
else:
print(“Its odd!”)
num1 = int(input(“Enter the number: “))

cont = “y”
while cont ==“y”:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“Menu:”)
print(“1. Addition”)
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“5. Quit”)
op = input(“Enter the option from above menu (1 or 2 or 3 or 4 or 5): “)
if op==“1”:
print(“Sum of the given two numbers are: “,num1 + num2)
elif op==“2”:
print(“Difference of the given two numbers are: “,num1 – num2)
elif op==“3”:
print(“Product of the given two numbers are: “,num1 * num2)
elif op==“4”:
print(“Ratio of the given two numbers are: “,num1 / num2)
elif op==“5”:
cont = “n”
else:
print(“You have not given a valid option, start from beginning!”)

”’
Assignment: Modify the above program so that user is asked to enter numbers
only when they choose options 1 to 4.
”’

while True:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“Menu:”)
print(“1. Addition”)
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“5. Quit”)
op = input(“Enter the option from above menu (1 or 2 or 3 or 4 or 5): “)
if op==“1”:
print(“Sum of the given two numbers are: “,num1 + num2)
elif op==“2”:
print(“Difference of the given two numbers are: “,num1 – num2)
elif op==“3”:
print(“Product of the given two numbers are: “,num1 * num2)
elif op==“4”:
print(“Ratio of the given two numbers are: “,num1 / num2)
elif op==“5”:
break
else:
print(“You have not given a valid option, start from beginning!”)

# checking the condition: Entry check & Exit check
”’
WAP to generate Fibonacci series numbers:
0,1,1,2,3,5,8,13….

1. first 5 numbers of the series
2. till user wants
”’
print(“Fibonacci numbers are (using For loop):”)
n = 10
f,s=0,1
for i in range(n):
if i<2:
print(i,end=” , “)
else:
t = s+f # 0,1,
print(t, end=” , “)
f=s
s=t

print(\nFibonacci numbers are (using While loop):”)
print(“Enter any key to stop: “)
f,s=0,1
counter = 1
while True:
if counter ==1:
print(f, end=” , “)
elif counter ==2:
print(s, end=” , “)
else:
t = s+f
print(t, end=” , “)
f=s
s=t

counter += 1 #counter=counter + 1
cont = input()
if len(cont)>0:
break

##
”’
Develop guessing number game:
Computer (giving the number) v Human (Guessing the number)
”’
import random
#random is a module which has functions related to random number generation
num = random.randint(1,100)
attempts = 0
while True:
guess = int(input(“Enter the number to guess (1-100): “))
if guess <1 or guess >100:
print(“Invalid guess!”)
continue
attempts+=1 #attempts= attempts+1
if guess == num:
print(f”Congratulations! You have guessed it correctly in {attempts} attempts!”)
break
elif guess < num:
print(“Try to guess a higher number!”)
else:
print(“Try to guess a lower number!”)

”’
Develop guessing number game:
Human (giving the number) v Computer (Guessing the number)
”’
import random
#random is a module which has functions related to random number generation
num = 55
attempts = 0
low,high = 1,100
while True:
#guess = int(input(“Enter the number to guess (1-100): “))
guess = random.randint(low,high)
if guess <1 or guess >100:
print(“Invalid guess!”)
continue
attempts+=1 #attempts= attempts+1
if guess == num:
print(f”Congratulations! You have guessed it correctly in {attempts} attempts!”)
break
elif guess < num:
print(f”Try to guess a higher number than {guess}!”)
low = guess+1
else:
print(f”Try to guess a lower number than {guess}!”)
high=guess-1

”’
Develop guessing number game:
Computer (giving the number) v Computer (Guessing the number)
”’
import random
#random is a module which has functions related to random number generation
num = random.randint(1,100)
attempts = 0
low,high = 1,100
while True:
#guess = int(input(“Enter the number to guess (1-100): “))
guess = random.randint(low,high)
if guess <1 or guess >100:
print(“Invalid guess!”)
continue
attempts+=1 #attempts= attempts+1
if guess == num:
print(f”Congratulations! You have guessed it correctly in {attempts} attempts!”)
break
elif guess < num:
print(f”Try to guess a higher number than {guess}!”)
low = guess+1
else:
print(f”Try to guess a lower number than {guess}!”)
high=guess-1
# Strings – used for handling text data
str1 = ‘HELLO’ \
‘i’
str2 = “How are you”
str3 = ”’I am fine
I am doing alright
I am super”’
str4 = “””I am here
I am there
I am everywhere”””
print(str1, str2, str3, str4)
print(type(str1),type(str2),type(str3),type(str4))
str5 = “How are”
str6 = “You?”
print(str5 +” “+ str6)
print(“Hello ” + str(5))
print(“HELLO ” * 5)

#for loop
for i in str6:
print(“running the loop: “,i)

# indexing
str7 = “I am fine here how are you there”
print(“Length: “,len(str7))
# position of each character starts from 0
print(str7)
print(“First character of str7 = “,str7[0])
print(“Second character of str7 = “,str7[1])
print(“Last character of str7 = “,str7[ len(str7) – 1 ])
print(“Last character of str7 = “,str7[ – 1 ])
print(“Second last character of str7 = “,str7[ – 2 ])
print(“6th to 9th characters: “,str7[5:9])

# Strings
str1 = “I am fine”
# first 3
print(str1[0:3])
#last 3
print(str1[-3:])
#
print(type(str1))
#methods are the functions defined under a class
# is.. is for asking question
# isalpha() is like asking is the object alphabets
print(“Is alpha: “,str1.isalpha())
str2 = “helothere”
print(“Is alpha: “,str2.isalpha())

num1= input(“Enter a number: “)
if num1.isdigit():
num1 = int(num1)
else:
print(“Invalid number!”)
print(“Str1 isit titlecase:”, str1.istitle())
print(“Str1 convert to title case: “,str1.title())
print(“Str1 is it uppercase: “,str1.isupper())
print(“Str1 is uppercase: “,str1.upper())
print(“Str1 is it lowercase: “,str1.islower())
print(“Str1 is lowercase: “,str1.lower())
str2 = “I am fine I am good how are you am fine?”
print(“count of o: “,str2.count(“am”))
print(“count of o: “,str2.count(“am”,5,15))
print(“Split: “,str2.split(‘am’))
l1 = [‘I ‘, ‘ fine I ‘, ‘ good how are you ‘, ‘ fine?’]
print(“Join: “,“am”.join(l1))
start = 3
if str2.count(“am”,start)>0:
print(“Index: “,str2.index(“am”,start))
else:
print(“Given text not found in str2”)
print(str2.replace(“am”,“AM”))

# votingin india you need to be over 18 yrs and nationality India
nationality=“” #initialize
age = input(“Enter your age:”)
if age.isdigit():
age = int(age)
if age>=18:
#age criteria matched
nationality = input(“Enter you country of nationality:”)
if nationality.lower()==“india”:
print(“You are eligible to vote in India”)
else:
print(“Your nationality doesnt match”)
else:
print(“Your age criteria not matched”)
else:
print(“Not a valid age, hence exiting the program”)

print(“Printing nationality: “,nationality)

# Strings are immutable – you cant edit the value of a string
#nationality[2]=”d” TypeError: ‘str’ object does not support item assignment

# Collections – multiple values in one variable
# list – mutable ordered collection
list1 = [5,10,“Hello”, 6.5, True]
print(type(list1))
print(“first member: “,list1[0])
print(“last member: “,list1[-1])
print(“last 3 member: “,list1[-3:])
print(“first 3 member: “,list1[:3])
print([1,2,3] + [“First”,“Second”,“Third”])
print([1,2,3] * 4)

for i in list1:
print(f”{i} => {type(i)})

for i in range(len(list1)):
print(f”{list1[i]} => {type(list1[i])})

###
list1 = [5,10,“Hello”, 6.5, True]
if list1.count(“hello”) > 0:
idx = list1.index(“hello”)
list1.pop(idx)
else:
print(“hello is not in the list”)

# pop() looks for index, remove() looks for the value
list1.remove(“Hello”)
print(list1)

# add value to the list:
# append() – will add values at the end of the list
# insert() – needs value and also index
list1.append([2,4,6,8])
list1.insert(1,“Sachin Tendulkar”)
print(“After adding: “,list1)
print(list1[-1][2]) # how to access list inside another list

#LIST – 2
# list is an ordered mutable collection
# methods like append(), insert(), remove(), pop(),
#count(), index()
#wap to read marks of 5 students in their 5 subjects
marks = []
for i in range(5):
print((f”Enter the marks for student {i+1} ))
tlist=[]
for j in range(5):
m = int(input(f”Enter the marks in subject {j+1}: “))
tlist.append(m)
marks.append(tlist)

print(“Marks are:”,marks)
”’
[[45, 46, 89, 89, 98], [90, 98, 81, 67, 76], [45, 55, 58, 57, 60],
[5, 4, 5, 7, 8], [3, 6, 8, 9, 9]]
”’

marks=[[45, 46, 89, 89, 98], [90, 98, 81, 67, 76], [45, 55, 58, 57, 60],
[5, 99, 5, 7, 8], [5, 99, 8, 9, 9]]
marks.sort()
print(marks)
marks.sort(reverse=True)
print(marks)
marks.reverse()
print(marks)
l1 = [1,2,3]
l2 = [5,6,7]
l1.extend(l2) # l1 = l1 + l2
print(“L1: “,l1)

# copy
l3=l1 # l3 and l1 point to the same dataset(location in memory)
l4 =l1.copy() #shallow copy – it creates a new copy
print(“1. L1: “,l1)
print(“1. L3: “,l3)
print(“1. L4: “,l4)
l1.append(11)
l1.append(12)
l3.append(13)
l4.append(15)
print(“2. L1: “,l1)
print(“2. L3: “,l3)
print(“2. L4: “,l4)

l1.clear()
print(“L3 = “,l3)

### TUPLES ###
# Tuple – ordered immutable collection
t1 =()
print(type(t1))
t1=(1,2,3,4,1,2,3,1,2,1)
print(“Count = “,t1.count(2))
print(“Index = “,t1.index(2))
t1 = (4,)
print(type(t1))
t1 = (4,5) #packing
print(type(t1))
a,b = t1 #unpacking
print(a,b)
t1 = list(t1) #converting a tuple to list
t1 = tuple(t1) #converting a list to tuple
#accessing tuples are faster than accessing lists

t1=(10,30,20,40)
#iteration (loop) and indexing is exactly like List/String
print(t1[1])

#Dictionary: unordered collection – doesnt have index, instead
# dictionary has key:value pair – as a user you have to give the key
dict1 = {1: “Five”, “Eight”: 80}
print(type(dict1))
print(dict1)
print(dict1[‘Eight’])

marks = {}
for i in range(3):
n = input(f”Enter Student’s name: “)
tlist=[]
for j in range(3):
m = int(input(f”Enter the marks in subject {j+1}: “))
tlist.append(m)

marks.update({n:tlist})

print(“Final list:\n,marks)

”’
{‘S’: [3, 6, 9], ‘R’: [1, 5, 9], ‘P’: [9, 5, 1]}
”’

dict2 = {‘S’: [3, 6, 9], ‘R’: [1, 5, 9], ‘P’: [9, 5, 1]}
print(“All Keys are: “,dict2.keys())
print(“All Values are: “,dict2.values())
# items() will give key and values pairs
print(“Items are: “,dict2.items())

for k,v in dict2.items():
print(f”{k} = {v})

dict3 = dict2.copy() #shallow copy
dict4 = dict2 #deepcopy
dict2.update({‘Q’:{4,6,9}})
print(“Dict3 – copy: “,dict3)
print(“Dict4 – =: “,dict4)

# pop() and popitem() both are used to remove data
#popitem() has no arguments – it will delete on of its own (latest added)
# pop() takes the key and that perticular key:value pair is deleted
dict2.popitem()
print(“2. Dict4 – =: “,dict4)
dict2.pop(“R”)
print(“3. Dict4 – =: “,dict4)
dict2 = {‘S’: [3, 6, 9], ‘R’: [1, 5, 9], ‘P’: [9, 5, 1]}
print(dict2[‘S’])
print(“Get key for the given value: “,dict2.get(“S”)) #another way of getting the value
dict2 = {‘S’: [3, 6, 9], ‘R’: [1, 5, 9], ‘P’: [9, 5, 1]}
x = dict2.setdefault(“T”,{0,0,0})
print(x)
print(dict2)
dict2[“T”] = {10,20,30}
print(dict2)
#SETS – linear unordered mutable collection
set1 = {}
print(type(set1))
set1 = {5,7,8,11, 11,11,8,7}
print(type(set1))
print(“Length = “,len(set1))
print(set1)
# set operations like: Union, Intersection, Difference
set1 = {5,7,8,11}
set2 = {3,4,11,8,7}
print(“union”)
print(set1 | set2)
print(set1.union(set2))
print(“intersection (filter common)”)
print(set1.intersection(set2))
print(set1 & set2)
print(“Difference”)
print(set1.difference(set2))
print(set1 – set2)
print(set2.difference(set1))
print(set2 – set1)
print(“Symmetric difference”)
print(set2.symmetric_difference(set1))
print(set2 ^ set1)

print(set1.isdisjoint(set2))
#set1.copy() – deep copy
set1.clear()
print(“Set 1: “,set1)
# sets, lists and tuples can be converted into each others form
set2 = {3,4,11,8,7}
set2 = list(set2)
set2.append(19)
set2 = set(set2)

## Functions ###
”’
1. inbuilts functions: print(), len(), set(), list() …..
2. user defined functions:
3. one line functions
”’
def list_of_questions():
print(“How are you?”)
print(“Where are you?”)
print(“Are you coming?”)

list_of_questions()
print(“printing list of questions once again!!!!!!”)
list_of_questions()


# function definition: of no input argument
def sum_two_nums():
num1 = 45
num2 = 38
add = num1 + num2
print(“Sum of two numbers is”,add)

# function definition: of taking input argument
def sum_two_nums1(num1, num2):
print(f’num1 = {num1} and num2 = {num2})
add = num1 + num2
print(“Sum of two numbers is”,add)

# function definition: of taking input argumenta
# and returning values
# 1. required positional arguments: both required and positional
def sum_two_nums2(num1, num2):
print(f’num1 = {num1} and num2 = {num2})
add = num1 + num2
#print(“Sum of two numbers is”,add)
return add

# default positional
def sum_two_nums3(num1, num2=9):
print(f’num1 = {num1} and num2 = {num2})
add = num1 + num2
#print(“Sum of two numbers is”,add)
return add


#calling functions
sum_two_nums()
x = 38
y = 67
#calling functions by passing two values
sum_two_nums1(x, y)

#calling functions by passing two values and catching the return value
result = sum_two_nums2(27,37)
print(‘Function has returned ‘,result)

result = sum_two_nums3(27,39)
print(‘Function has returned ‘,result)
result = sum_two_nums3(27)
print(‘Function has returned ‘,result)

# how to make arguments non-positional
# by using keywords while passing the value
result = sum_two_nums3(num2 = 27,num1 = 39)
print(‘Function has returned ‘,result)
# functions
”’
1. required: you have to provide value to this argument
2. positional: takes the value based on the position
3. default (non-required):
4. keyword (non-positional)
”’
def func1(n1,n2):
print(“N1 = “,n1)
print(“N2 = “,n2)
add = n1 + n2
return add

def func2(n1,n2=66): #assigning a default value
print(“N1 = “,n1)
print(“N2 = “,n2)
add = n1 + n2
return add

def func3(n1=-1,n2=0): #assigning a default value
print(“N1 = “,n1)
print(“N2 = “,n2)
add = n1 + n2
return add

def fun4(s1,s2,s3,*s,**info):
print(“Values of S are:”,s)
if len(s)==0:
print(“We are dealing with a triangle”)
elif len(s)==1:
print(“We are dealing with a square or a rectangle”)
elif len(s)==2:
print(“We are dealing with a Pentagon”)
elif len(s)==3:
print(“We are dealing with a Hexagon”)
elif len(s)==4:
print(“We are dealing with a Heptagon”)
elif len(s)==5:
print(“We are dealing with a Octagon”)
elif len(s)>5:
print(“We are dealing with a shape greater than 8 sides”)

print(“INFO = “,info)
if “name” in info.keys():
print(f”Name of the player is {info[‘name’]})
if “game” in info.keys():
print(f”The player plays {info[‘game’]})
if “city” in info.keys():
print(f”The player lives in {info[‘city’]})

# docstring is the multi line comment added in the first line in a function
def checkprime(num):
”’
This is a function that takes parameter and returns
True for Prime and False for non Prime
:param num: value to check
:return: True for Prime and False for Non-Prime
”’
isPrime = True
if num<2:
isPrime = False
elif num>2:
for i in range(2,num//2 + 1):
if num%i==0:
isPrime = False
break
return isPrime

”’
10! = 10*9!
”’
def recurfacto(n):
if n==0:
return 1
return n*recurfacto(n-1)


if __name__ ==“__main__”:

result = func1(10,20)
print(“Total is”,result)
result = func2(10)
print(“Total is”,result)
result = func2(20,50)
print(“Total is”,result)
result = func3(2,5)
print(“Total is”,result)
result = func3()
print(“Total is”,result)

result = func3(n2=6)
print(“Total is”,result)
fun4(1,2,3,4,5,6,7,8, name=“Sachin”,game=“Cricket”,city=“Mumbai”)
fun4(1,2,3, name=“Virat”,game=“Cricket”)
for i in range(10,20):
result = checkprime(i)
if result:
print(f”{i} is Prime”)
else:
print(f”{i} is not Prime”)

# using the same above function to generate prime numbers between 5000 and 6000
print(“Prime numbers are:”)
for i in range(5000,6001):
result = checkprime(i)
if result:
print(i,end=“, “)
print()

result =recurfacto(10)
print(“10 factorial is”,result)
import p1 as MyOwnModule
from p1 import checkprime,fun4

if __name__==“__main__”:
print(“Running PY18.py”)
#printing doc string
print(checkprime.__doc__)
#result = p1.checkprime(97)
MyOwnModule.checkprime(97)
result = checkprime(97)
if result:
print(“97 is prime”)
else:
print(“97 is not a prime”)
a = “50” # creating an object of class str
print(type(a))
b = “hello”
print(type(b))
b.upper()

class Book:
# class members: variables and methods
total_books = 0 #class variable
def __init__(self, title, author): # object method
self.title = title
self.author = author
Book.total_books +=1
def put_data(self): # object method
print(“Title = “,self.title)
print(“Author = “,self.author)

@classmethod
def count_book(cls):
print(“Total books in the shelf is “,cls.total_books)

b1 = Book(“Python”,“Sachin”)# init is called when we create object
print(type(b1))
print(“Total books =”,b1.total_books)
b2 = Book(“Data Science”,“Virat”) # __init__() is called
print(type(b2))
print(“Total books =”,b2.total_books)
#b1.input_data(“Python”,”Sachin”) #calling method
b1.put_data()
#b2.input_data(“Data Science”,”Virat”)
b2.put_data()
print(“Total books =”,b1.total_books)
b2.count_book()
b1.count_book()
Book.count_book()
”’
Properties of class & objects:
1. Inheritance:
2. Polymorphism:
3. Abstraction: hiding implementation detail
4. Encapsulation: hiding information
”’
# program to implement addition, subtraction, multiplication, division
class Super_Op:
def __init__(self):
print(“B2 is initialized”)

def Class_Output1(self):
print(“Output from Super_Op”)
class Math_Op(Super_Op):
def __init__(self,n):
self.n = n
def val_square(self):
return self.n ** 2
def val_squarert(self):
return self.n ** 0.5
def Class_Output1(self):
print(“Output from Math_Op”)
class B2:
def __init__(self):
print(“B2 is initialized”)

def Class_Output1(self):
print(“Output from B2”)
class Arith_Op (B2,Math_Op):

def __init__(self, n1,n2):
Math_Op.__init__(self,n1)
# call Math_Op init to make sure the data is initialized
self.n1 = n1
self.n2 = n2
def Add(self):
return self.n1 + self.n2
def Sub(self):
return self.n1 – self.n2
def Mul(self):
return self.n1 * self.n2
def Div(self):
return self.n1 / self.n2
def Class_Output1(self):
print(“Output from Arith_OP”)

a1 = Arith_Op(20,10)
a2 = Arith_Op(200,100)
a3 = Arith_Op(120,110)
a4 = Arith_Op(520,110)
print(“Addition: “,a1.Add())
print(“Subtraction: “,a2.Sub())
print(“Multiplication: “,a3.Mul())
print(“Division: “,a4.Div())

b1 = Math_Op(50)
print(“Square root: “,b1.val_squarert())
print(“Square: “, b1.val_square())

a1.val_square()
print(“Square root: “,a1.val_squarert())
print(“Square: “, a1.val_square())
a1.Class_Output()
”’
Properties of class & objects:
1. Inheritance:
2. Polymorphism:
3. Abstraction: hiding implementation detail
4. Encapsulation: hiding information: 3 types of accessibility provided are:
4.1: public
4.2: protected
4.3: private
”’
# program to implement addition, subtraction, multiplication, division
class Super_Op:
def __init__(self):
print(“B2 is initialized”)

def Class_Output(self):
print(“Output from Super_Op”)

def _Class_Output2(self): #protected
print(“Output from Super_Op – Protected”)

def __Class_Output2(self): #private
print(“Output from Super_Op – Private”)
class Math_Op(Super_Op):
def __init__(self,n):
self.n = n
def val_square(self):
return self.n ** 2
def val_squarert(self):
return self.n ** 0.5
def Class_Output(self):
print(“Output from Math_Op”)
class B2:
def __init__(self):
print(“B2 is initialized”)

def Class_Output(self):
print(“Output from B2”)
class Arith_Op (B2,Math_Op):

def __init__(self, n1=0,n2=0):
Math_Op.__init__(self,n1)
# call Math_Op init to make sure the data is initialized
self.n1 = n1
self.n2 = n2
def Add(self):
return self.n1 + self.n2
def Sub(self):
return self.n1 – self.n2
def Mul(self):
return self.n1 * self.n2
def Div(self):
return self.n1 / self.n2
def Class_Output1(self):
print(“Output from Arith_OP”)

class testClass:
def testMethod(self):
s1 = Super_Op()
s1.Class_Output()
s1._Class_Output2()
”’ Protected members is being called but technically it shouldnt be
possible to call – this is not yet implemeted in Python”’
#s1.__Class_Output2()
# above method of Super_Op is not accessible.
”’
testClass is no way connected to any of the classes above but still it can
call Super_Op class members. – Public
so, public members can be called by any class.

protected: if you want to make any member protected, you need to add _(single
underscore) before the name. protected members can be called by derived classes only.
BUT THIS IS STRICTLY NOT IMPLEMENTED IN PYTHON!

private: if you want to make any member private, you need to add __(double
underscore) before the name. Private members can be accessed only by the members of
the same class.
”’
t1 = testClass()
t1.testMethod()

a1 = Arith_Op(10,20)

m1 = Math_Op(4)
m1._Class_Output2()
#m1.__Class_Output2() – you cant call since its private

##################
”’
handling external files:
.text, .json .csv

How to handle file reading:
import os
”’

import os
print(“Operating system is use: “,os.name)
# nt platform for windows
# posix platform for Mac, Unix, Linux
# java etc…

if os.name==“nt”:
#windows related commands
print(“You are working with Windows machine”)

# to clear the screen – cls
os.system(‘cls’)
#create directory
os.mkdir(“Test1”)
elif os.name==“posix”:
print(“You are working on UNIX/Linux/Mac machine”)
# those related commands you use
os.system(‘clear’)
else:
print(“Some other platform. Please check”)

############
”’
Modes of file:
r : read
w : write
a : append (writing additional content to existing file)
r+ : read and write mode: file should be existing
w+ : write and read mode: file neednt be there
a+: append and read
”’

poem=”’Twinkle Twinkle little star
How I wonder what you are
Up above the world so high
like a diamond in the sky”’
print(poem)

# write – all the content
# write line – adding line by line
# write lines – multiple lines – content has to be in []
# step 1: create a file pointer – needs where and how
file_ptr = open(“Test1/abc.txt”,“a”)
file_ptr.write(poem)
file_ptr.close()

file_ptr = open(“Test1/abc.txt”,“r”)
read_content = file_ptr.read()
file_ptr.close()
print(“Content read from the file:\n,read_content)
# reading external files – .txt file
# read and write to a file

filptr = open(“Test1\\abc.txt”,“r”)
# read
content = filptr.read()
print(“1 Content of the file is: \n,content)
filptr.seek(10)
content = filptr.read(30)
print(“2 Content of the file is: \n,content)
content = filptr.read(30)
print(“3 Content of the file is: \n,content)
filptr.seek(0)

# readline
content = filptr.readline()
print(“1. Line: Content of the file is: \n,content)
content = filptr.readline(50000)
print(“2. Line: Content of the file is: \n,content)
filptr.seek(0)

# readlines
content = filptr.readlines()
print(“1. Lines: Content of the file is: \n,content)

# closing the file
filptr.close()

filptr = open(“Test1\\abc.txt”,“w”) #operations same as append
# write
content=“””Ba Ba Black sheep
Do you have any wool
Yes sir yes sir
three bags full”””

filptr.write(content)
## writelines
content=[\nHello there\n,‘How are you doing?\n,
‘I am fine\n,‘Doing awesome\n]
filptr.writelines(content)
#close
filptr.close()

”’
Mini Project:
Write a .txt file based storage program to store data about Indian batsman
for the world cup 2023. And pick up the highest score of each player through
the python code.

example: store player and their highest score
Rohit 43
Gill 55
Rahul 21
Iyer 5
Virat 86
”’

# reading external files – .csv file
import csv
header = [‘SNO’,‘NAME’,‘COUNTRY’,‘HIGHEST’]
info = [[‘1’,‘Rohit’,‘India’,43],[‘2’,‘Gill’,‘India’,67],
[‘3’,‘Iyer’,‘India’,4],[‘4’,‘Virat’,‘India’,93]]
fileptr = open(“Test1\\abc.csv”,mode=“w”,newline=)
fileptr_csv = csv.writer(fileptr, delimiter=“,”)
fileptr_csv.writerow(header)

for i in info:
fileptr_csv.writerow(i)

fileptr.close()

fileptr = open(“Test1\\abc.csv”,mode=“r”,newline=)
fileptr_csv = csv.reader(fileptr, delimiter=“,”)
for i in fileptr_csv:
print(i[1],” – “,i[3])
fileptr.close()

”’
Modify the above program to print the name and the score of the highest player only
”’
# Working files – json file
# dump, dumps, load, loads
players_data = {
“Players”:[
{
“Name”: “Rohit”,
“Type”:“Batsman”,
“City”:“Mumbai”
},
{
“Name”: “Siraj”,
“Type”:“Bowler”,
“City”:“Hyderabad”
},
{
“Name”: “Virat”,
“Type”:“Batsman”,
“City”:“Delhi”
},
{
“Name”: “Ashwin”,
“Type”:“Bowler”,
“City”:“Chennai”
}
]

}
import json

# write to a file
fileptr = open(“Test1\\data1.json”,“w”)
json.dump(players_data,fileptr, indent=4)

print(json.dumps(players_data,indent=4, sort_keys=True))
fileptr.close()

# reading a json file
fileptr = open(“Test1\\data1.json”,“r”)
json_content = json.load(fileptr)
print(json_content)
jason_txt = ”'{“Players”: [{“Name”: “Rohit”, “Type”: “Batsman”, “City”: “Mumbai”},
{“Name”: “Siraj”, “Type”: “Bowler”, “City”: “Hyderabad”}]}”’
#loads actually reads data from the screen – data will be in a string format not
# as json format
json_content2 = json.loads(jason_txt)
print(json_content2)
print(json_content2[‘Players’])
print(json_content2[‘Players’][1][‘Name’])
for i in json_content2[‘Players’]:
print(i[‘Name’])
fileptr.close()
”’
Errors:
1. Syntax error: rule not correctly written
2. Logical error: made error in writing the logic
3. Exceptions: runtime errors
”’
num1 = 5
num2 = 2
print(f”Sum of {num1} and {num2} is {num1-num2})

num1 = int(input(“Enter a number: “))
print(“Value entered is “,num1)
# ValueError:
# handling runtime errors are called Exception handling

try:
num1 = int(input(“Enter a number: “))
except ValueError:
print(“Since you have entered an invalid number, we are stopping here!”)
else:
print(“This is a valid number”)
finally:
print(“Thank you for using my application”)

”’
WAP to divide 2 numbers
”’
num1,num2 = 0,0
while True:
try:
num1 = int(input(“Enter first number: “))
except ValueError:
print(“Invalid number! Try again…”)
else:
break

while True:
try:
num2 = int(input(“Enter second number: “))
except ValueError:
print(“Invalid number! Try again…”)
else:
try:
div = num1 / num2
except ZeroDivisionError:
print(“Denominator cant be Zero! Try again…”)
else:
break

div = num1 / num2
print(“Division value is:”,div)
print(“Thank you for using my application”)
def myfun1(a,b):
print(a+b)
return a+b

def myfun2():
print(“I do nothing”)

class Sample:
def __init__(self):
print(“Object created”)
def printinfo(self):
print(“Some output here”)

if __name__ ==“__main__”:
myfun1(99,87)
myfun1(99,7)
s1 = Sample()


=====================


#import p11 as RocketScience
from p11 import myfun1

#RocketScience.myfun2()
print(myfun1(5,10))

import random
random.random()

########### FILE HANDLING
#mode of file handling: r (read), w (write-delete old content and write new), a (Append)
## r+ (read & write), w+ (write & read), a+ (append and read)

filename = “abc.txt”

fileobj = open(filename,“r+”)
content = ”’This is a sample content
story about a king
and a queen
who lived in a jungle
so I am talking about
Lion the kind of jungle”’

fileobj.write(content)
content2 = [‘THis is sample line 1\n,‘line 2 content \n,‘line 3 content \n]
fileobj.writelines(content2)
fileobj.seek(20)
output = fileobj.read()
print(“Content from the file:\n,output)
fileobj.seek(10)
output = fileobj.read()
fileobj.seek(10)
content3 = fileobj.read(15)
content4 = fileobj.readline()
print(“Content from the file:\n,output)

fileobj.seek(0)
content5 = fileobj.readlines()
print(“Content from the file:\n,content5)

fileobj.close()

## Exception handling
#SyntaxError : print(“Hello)
#logical error: you make error in the logic – very difficult to find
#runtime errors (exceptions):

num1 = int(input(“Enter a number: “))
# ValueError exception

# Exceptions
a=“k”
b=10
c=-1
try:
c = b/d

except ZeroDivisionError:
print(“Denominator is zero hence stopping the program from executing”)
except TypeError:
print(“Invalid numbers, hence exiting…”)
except NameError:
print(“One of the values has not been defined. Try again”)
except Exception:
print(“Not sure but some error has occurred, we need to stop”)
else:
print(“Answer is”,c)
finally:
print(“We have completed division process”)
#
class InvalidLength(Exception):
def __init__(self,value=0):
self.value = value

length, breadth = –1,-1
while True:
try:
length = int(input(“Enter length: “))
except ValueError:
print(“Invalid number, try again…”)
else:
#assert length > 0, “Rectangle with this diamension is not possible”
if length <=0:
try:
raise InvalidLength
except InvalidLength:
print(“Invalid value for Length hence resetting the value to 1”)
length=1
break
while True:
try:
breadth = int(input(“Enter breadth: “))
except ValueError:
print(“Invalid number, try again…”)
else:
assert breadth>0,“Rectangle with this diamension is not possible”
break

area = length * breadth
print(“Area of the rectangle is”,area)
## ### ##
# datetime, date, time

from datetime import datetime, timedelta
import time
from pytz import timezone

curr_time = datetime.now()
print(“Current time is”,curr_time)
print(“Current time is”,curr_time.strftime(“%d / %m /%Y”))
print(curr_time.year, curr_time.day, curr_time.date())
for i in range(5):
time.sleep(1) # sleep for 2 seconds
print(“Time left:”,5-i,“seconds”)
print(“Good Morning”)
print(“Current time is”,datetime.now())
print(“Date 2 days back was”,(curr_time-timedelta(days=2)).strftime(“%d/%m/%Y”))
print(“UTC Time is”,datetime.now(timezone(‘UTC’)))
print(“UTC Time is”,datetime.now(timezone(‘US/Eastern’)))
print(“UTC Time is”,datetime.now(timezone(‘Asia/Kolkata’)))

Download MYSQL database link:

 

https://dev.mysql.com/downloads/installer/

Create table employees.Employees( EMPID INT Primary Key auto_increment, FNAME VARCHAR(55) NOT NULL, LNAME VARCHAR(55), DOB DATE, EMAIL VARCHAR(35) unique, PHONE VARCHAR(11), DOJ DATE Default(‘2021-07-20’), — YYYY-MM-DD SALARY FLOAT(2), DEPTID INT, Foreign Key (DEPTID) References Departments(DID), Constraint U_UC_LN_DOB Unique(LNAME,DOB), CHECK(Salary 0.0) ) — Constraints: Primary Key, Foreign Key, Not Null, Unique, Check, Default

— Modifying a table – Table is already created and in use –  ALTER TABLE

— ADD or DELETE (DROP) or MODIFY or RENAME a Column

— DDL command to delete is DROP

— DDL command to modify is ALTER

use employees;

 

ALTER table employees ADD BONUS Float(3);

 

ALTER table employees ADD dummy Float(3);

 

ALTER TABLE Employees DROP COLUMN dummy;

 

ALTER TABLE EMPLOYEES MODIFY COLUMN BONUS float(4)

 

ALTER TABLE EMPLOYEES RENAME COLUMN BONUS to BONUS_PCT;

use employees;

— create table, drop table, alter table

— to read data we use Select

Select * from departments;

 

insert into departments (DID, HOD, DNAME, DCODE) values (100,’Sachin’,’CSE’,’AA00′);

insert into departments  values (101,’MEC’,’Virat’,’AZ00′);

 

insert into departments  values (103,’ECE’,’Rohit’,’KZ00′);

insert into departments  values (105,’CIV’,’Dhoni’,’CZ00′);

insert into departments  values (106,’TCE’,’Sunil’,’BZ00′);

 

Select * from employees;

 

Select @@GLOBAL.secure_file_priv;

 

— Import data into CSV

LOAD DATA INFILE ‘C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/EmployeesData.csv’

INTO TABLE EMPLOYEES

FIELDS TERMINATED BY ‘,’

ENCLOSED BY ‘”‘

LINES TERMINATED BY ‘\n’

IGNORE 1 ROWS;

 

— DELETE to remove a row

DELETE FROM EMPLOYEES WHERE EMPID=120;

 

— Update to modify the existing values in a row

Update Employees

Set Bonus_PCT = 0.45, Salary = 160000

where empid=122;

 

Select * from employees where empid=122

 

—  SELECT

Select FNAME, EMAIL, PHONE, SALARY From Employees;

 

Select FNAME, EMAIL, PHONE, SALARY From Employees where salary >=100000;

 

Select FNAME, EMAIL, PHONE, SALARY From Employees where EMAIL =’ramakrishnavendra@wnbco.co’;

 

— Create Index to make query faster

Create Index idx_salary

on Employees (Salary)

 

—  Relational operators in MYSQL:  =   >   <   >=   <=   <>

Select FNAME, EMAIL, PHONE, SALARY From Employees where EMAIL <>’ramakrishnavendra@wnbco.co’;

 

— BETWEEN  LIKE  IN

use employees;

 

select * from employees;

 

select FNAME, LNAME, DOB, Salary from employees where salary > 75000;

— BETWEEN is used for numbers

select FNAME, LNAME, DOB, Salary from employees where salary BETWEEN 75000 AND  95000;

— below statement is same as above (BETWEEN)

select FNAME, LNAME, DOB, Salary from employees where salary >= 75000 AND  salary <= 95000;

 

select FNAME, LNAME, DOB, Salary from employees where salary >= 75000 AND  Bonus_pct <= 0.4; — 0 to 9 rows

 

select FNAME, LNAME, DOB, Salary from employees where salary >= 75000 OR  Bonus_pct <= 0.4; — 25 to 34 rows

 

select FNAME, LNAME, DOB, Salary from employees where salary >= 75000   — 25 rows

 

select FNAME, LNAME, DOB, Salary from employees where  Bonus_pct <= 0.4;   — 9 rows

 

— LIKE – for text data comparison

Select * from employees where FNAME like ‘A%’

 

— IN – checking for given set of values

Select * from employees where FNAME IN (‘O%’, ‘A%’,’B%’);

 

Select * from employees where FNAME IN (‘Anikeet Dey’,’Amol Jain’,’Suresh Narayan Singh Yadav’ );

 

Select * from employees where SALARY IN (36107,  110266, 107799, 198890);

—  Logical operator – AND OR NOT

select * from employees where not( deptid = 100 and salary >100000)

 

—  ORDER BY

select * from employees order by salary  DESC;

 

select * from employees order by salary  ASC;  — Ascending order is by default

 

select * from employees order by DEPTID  ASC, Salary DESC;

use employees;

 

select * from employees where bonus_pct is not null;

 

select * from employees order by FNAME;

select * from employees order by FNAME DESC;

 

UPDATE employees SET BONUS_PCT = NULL WHERE BONUS_PCT > 0.88;

 

select * from employees where bonus_pct is not null limit 3;

 

select * from employees where bonus_pct is not null order by salary DESC limit 5;

 

select avg(salary) as Max_Salary, deptid from employees group by deptid;

 

select avg(salary), count(salary), sum(salary) from employees;

 

select * from employees where salary > (select avg(salary) from employees);

 

— Find highest salary earner for each department

select deptid, MAX(salary) as max_salary FROM employees group by deptid;

 

select deptid, fname, salary from employees where (Deptid, salary)  in 

(select deptid, MAX(salary) as max_salary FROM employees group by deptid);

use employees;

 

select deptid, avg(salary) as “Average Salary” from employees group by deptid;

— COUNT, AVG, SUM, MIN, MAX

 

— Like:  %a, a%,  %a% ,  %b__% (exactly 1 value for _),   ‘a%z’

select fname, email, salary from employees;

 

select fname, email, salary from employees where fname like ‘%ol%’;

 

— J followed by exactly three characters

select fname, email, salary from employees where fname like ‘%J___’;

 

— J followed by atleast four characters

select fname, email, salary from employees where fname like ‘%J____%’;

 

select * from employees where fname in (‘Pankaj Prajapati’,’Manju Mishra’ ,’Arijeet Dasgupta’);

 

select * from employees where fname in (select fname from employees where fname like ‘%J____%’);

select * from employees where (fname,salary) in (select fname,salary from employees where fname like ‘%J____%’);

 

select fname, email, salary from employees where fname like ‘%dey’;

— combining data from 2 tables

use employees;

select * from employees;

select employees.FNAME, DID, departments.DNAME from employees, departments

 

 

select employees.FNAME, DID, departments.DNAME from employees, departments where employees.deptid = departments.did;

 

insert into departments values (107, ‘BIOT’,’Kapil’,’BB09′)

 

insert into employees (EMPID, FNAME, LNAME, DOB, EMAIL,PHONE, DOJ,SALARY) 

values (133,’Sachin Tendulkar’,’T’,’1990-05-04′,’sachin@wnbco.co’,9999000009,’2023-05-15′,231456);

 

select * from departments;

 

select employees.FNAME, DID, departments.DNAME from employees, departments where employees.deptid = departments.did;

 

 

— JOINS

 

use employees;

 

select * from employees;

 

select * from departments;

 

select fname, hod from employees, departments where departments.did = employees.DEPTID;  — Inner

 

select fname, hod from employees, departments where employees.DEPTID = departments.did order by empid;

 

select fname, hod from employees INNER JOIN departments ON employees.DEPTID = departments.did order by empid;

 

SELECT T1.COL1, T2.COL2, T3.COL3

FROM (((T1 INNER JOIN T2 ON T1.K1 = T2.K1) INNER JOIN T3 ON T2.K2 = T3.K2) INNER JOIN T4 ON T3.K3 = T4.K3);

 

 

select fname, hod from employees LEFT JOIN departments ON employees.DEPTID = departments.did order by empid;

use employees;

 

select fname, hod from employees left join departments on employees.DEPTID = departments.did

UNION

select fname, hod from departments left join employees  on employees.DEPTID = departments.did;

 

 

select fname, hod from departments inner join employees  on employees.DEPTID = departments.did;

 

select * from employees;

 

select sum(EMPID) from employees;

 

select count(EMPID) from employees;

 

select count(EMPID) from employees where salary > 50000;

 

select count(EMPID) , DEPTID from employees

group by deptid

having count(EMPID) > 5;

 

select count(fname), hod from employees left join departments on employees.DEPTID = departments.did

group by deptid

having count(fname) > 3;

 

 

select * from employees;

— Salary_Grade:  <50K: E 50-100K: D    100-150K – C  150-200-B  >200K: A

 

select fname, salary, bonus_pct, 

case when bonus_pct is null then salary

else salary+salary*bonus_pct 

end as total_salary from employees;

 

select fname, salary, 

CASE

when salary < 50000 Then ‘Grade: E’

    when salary > 200000 Then ‘Grade: A’

    when salary > 150000 Then ‘Grade: B’

    when salary > 100000 Then ‘Grade: C’

    Else ‘Grade: D’

End As Salary_Grade

from employees;

use employees;

 

select * from employees;

 

select FNAME, DOB, DOJ, datediff(doj,dob) from employees;

 

 

select fname, NOW() from employees;

 

select now(); — current date and time (system)

 

select date(now());

select curdate();

— default date format in MYSQL is  YYYY-MM-DD

— DD-MM-YYYY

select date_format(curdate(), ‘%d-%m-%Y’) as Date;

 

select curdate() as todays_date, date_add(curdate(),interval 1 Day) as Tomorrow,

date_add(curdate(),interval 1 Week) as next_week,

date_add(curdate(),interval 1 Month) as Next_month,

date_add(curdate(),interval 1 Year) as Next_year;

 

— Add date: Interval, day, week or month or year;

select curdate() as todays_date, date_sub(curdate(),interval 1 Day) as Yesterday,

date_sub(curdate(),interval 1 Week) as last_week,

date_sub(curdate(),interval 1 Month) as last_month,

date_sub(curdate(),interval 1 Year) as last_year;

 

 

select day(curdate()) as day, month(curdate()) as Month, quarter(curdate()) as Quarter, Year(curdate()) as Year,

Weekday(curdate()) as Weekday, week(curdate()) as week, weekofyear(curdate()) as WeekofYear;

 

 

select * from employees where salary > (select salary from employees where empid = 104);

 

—  create a stored procedure

DELIMITER $$

 

Create Procedure GetEmpInfo()

Begin

select * from employees where salary > (select salary from employees where empid = 104);

End $$

DELIMITER ;

 

call GetEmpInfo();

 

drop Procedure GetEmpInfo;

 

— Features: exception handling, conditions and loops (While, Repeat), 

 

— Creating a View

create view EmpData

as

Select EMPID, FNAME, LNAME, DOB, EMAIL, PHONE, DOJ, DEPTID from Employees;

 

select * from empdata;

 

 

—  String functions:  LTRIM, RTRIM, Replace, substring, COncat

select concat(fname,’.’,lname,’@wnbco.co’) from employees;

 

select fname, substring(fname, 3), substring(fname, -3) from employees;

import pymysql
con_str = pymysql.connect(host=“localhost”, user=“root”, password=“learnSQL”,database=“employees”)
cursor = con_str.cursor()

q1 = ”’Create Table PY_EMP (
EMPID INT PRIMARY KEY,
NAME VARCHAR(30),
CITY VARCHAR(15))
”’
#cursor.execute(q1)

q2 = ”’INSERT INTO PY_EMP VALUES(1,’Laxman’,’Hyderabad’)”’
cursor.execute(q2)
q2 = ”’INSERT INTO PY_EMP VALUES(2,’Rahul’,’Bangalore’)”’
cursor.execute(q2)


q3 = ”’Select * from Py_Emp”’
cursor.execute(q3)
results = cursor.fetchall()
for row in results:
print(row)

con_str.commit()

con_str.close()

PYTHON TRAINING SESSIONS SEP 2023
print( “sdfsadgdsgds” )
# inbuilt functions, followed with ()
# are for comments – you are asking Python to ignore the content after #
print(“5+3”)
print(5+3)
#print(sdfsadgdsgds)
print(“5+3=”,5+3,‘and’,“6*4=”,6*4)
# parameters are the values that we give to a function
# what’s your name?
print(“what’s your name?”)
# He asked, “how are you?”
print(‘He asked, “how are you?”‘)
#He asked, “what’s your name?”
print(”’He asked, “what’s your name?””’) # ”’ or “””

# escape character \
# \\ \t \new \’ …

print(“He asked, \”what’s your name?\”)

value1 = 5
print(value1)

value1 = 10
print(value1)
sdfsadgdsgds = 100
print(sdfsadgdsgds)
# variables
a= 5
asdfsaddsg = 10
cost = 25
product1, product2 = 100,120
total_cost = product1 + product2
# variable names should begin with alphabet, and it can contain numbers & _
# invalid names: 1var, num.one
a=35

”’
Multi-line comment
Basic data types:
1. integer (int) – non-decimal numbers both -ve and +ve: -99, -66,0,33,999
2. float (float) – decimal numbers both -ve and +ve: -45.8 , -66.0 , 0.0, 5.78984744
3. complex (complex) – square root of -ve numbers: square root of -1 is i (maths) j in Python
4. Boolean (bool) – True [1] or False [0]
5. String (str) – text content
”’
# type() – type of the data
number_ppl = 55
print(“Data type of number_ppl:”, type(number_ppl))
cost_price = 66.50
print(“type(cost_price): “, type(cost_price))
value1 = 9j
print(“type(value1): “,type(value1))
print(“Square of value1 = “, value1 * value1)

bool_val1 = True # False
print(“type(bool_val1): “,type(bool_val1))

text_val1 = “HELLO”
print(“Type of text_val1 = “,type(text_val1))

# Formatting the output
quantity = 31
cost = 19
total_cost = quantity * cost
# total cost of 51 pens which cost 19 will be total_cost rupees
print(“total cost of”,quantity,“pens which cost”,cost,“will be”,total_cost,“rupees”)
# f-string – formatting the string
print(f”total cost of {quantity} pens which cost {cost} will be {total_cost} rupees”)

quantity = 31; total_cost = 900
cost = total_cost / quantity
print(f”total cost of {quantity} pens which cost {cost:.2f} will be {total_cost} rupees”)

player,country,position = “Virat”,“India”,“Opener”;
print(f”Player {player:<15} plays for {country:^10} at {position:>15} position”);
player,country,position = “Mbwangagya”,“Zimbabwe”,“Wicket-keeper”;
print(f”Player {player:<15} plays for {country:^10} at {position:>15} position”);

Read and Practice the flowchart and Algorithm:

# Operators in Python
# Arithematic Operators: + – * / // (int division), ** (power) % (modulus)
a = 13
b = 10
print(a+b)
print(a-b)
print(a*b)
print(a / b) # 1.3 – output is always float
print(a // b) # only integer – 1
print(a ** b) # 13 to the power of 10
# % – mod – gives you the remainder
print(a % b)

# relational /conditional operators: < > <= >= == !=
# asking question: is it ??? – output is always a bool value (T/F)
print(“Relational: “)
a,b,c = 10,10,13
print(a < b) # is a less than b ? F
print(b > c) # F
print(a<=b) # T
print(b >= c) # F
print(a==b) # is equal to ? – T
print(a!=b) # F

# Logical operators: and or not
# input and output – both are bool
”’
Prediction 1: Rohit and Ishan will open the batting – F
Prediction 2: Rohit or Ishan will open the batting – T
Actual: Rohit and Gill opened the batting

When using AND – Everything condition has to be true to be True
When using OR – Even one condition is true, result will be True
Not taken only 1 input: Not True = False, Not F=T
”’
a,b,c = 10,10,13
print(a < b and b > c or a<=b and b >= c or a==b and a!=b)
# F

## Input() – is used to read values from the user
# int() float() str() complex() bool()
num1 = input(“Enter a number to add: “)
num1 = int(num1) #explicit conversion
print(num1, “and the data type is”,type(num1))
num2 = int(input(“Enter second number: “))
s = num1 + num2 #implicit conversion
print(“Sum of two numbers: “,s)

## A program to convert Celcius to Fahrenheit
c = float(input(“Enter the degree celcius value: “))
f = (9*c/5) + 32
print(“Equivalent temperature in Fahrenheit = “,f)

## Program to find area and perimeter of a rectangle
l = int(input(“Enter length of the rectangle: “))
b = int(input(“Enter breadth of the rectangle: “))
a = l * b
p = 2*(l+b)
print(f”Rectangle with length {l} and breadth {b} will have area as {a} and perimeter as {p})
”’
ASSIGNMENT:
1. WAP to convert cm to inches
2. WAP to calculate area and circumference of a circle
3. WAP to calculate average of three given values
4. Input the maximum and minimum temperature recorded in a day and calculate
the average temperature for the day.
5. Convert the distance entered in metres to kilometres.
6. Write a program in C to interchange two integer numbers a and b.
7. WAP to input a,b,c and calculate z as a*b-c
8. WAP to input a two digit number and then interchange the values at 1s and 10s place:
e.g. 65 -> 56 , 83 -> 38

”’
# Conditions
# passed or failed based on the avg marks
print(“Pass”)
print(“Fail”)
avg = 55
if avg >=50: # if executes only if the condition is True
print(“Pass”)
print(“Congratulations”)
else: # above condition is False then else
print(“Fail”)
print(“Please try again next time”)
print(“Second option”)
”’
avg >= 80: Grade A: IF
avg>=70: Grade B ELIF
avg >=60: Grade C ELIF
avg >=50: Grade D ELIF
avg <50 (else): Grade E : ELSE

avg > 90: print(“Awesome”)
”’
avg =95
if avg>=80:
print(“Grade A”)
print(“Chocolate”)
if avg >= 90:
print(“Awesome”)
elif avg>=70:
print(“Grade B”)
print(“Chocolate”)
elif avg >=60:
print(“Grade C”)
elif avg >=50:
print(“Grade D”)
else:
print(“Grade E”)

avg = 85
# if 70 <= avg
if avg>=70:
if 70 < avg:
print(“Grade A”)
if avg >=90:
print(“Awesome”)
else:
print(“Grade B”)
print(“Chocolate”)
elif avg>=60:
print(“Grade C”)
elif avg >=50:
print(“Grade D”)
else:
print(“Grade E”)

a,b,c = 40,40,40
if a>=b and a>=c:
print(“A is greatest”)
if b>=c and b>=a:
print(“B is greatest”)
if c>=a and c>=b:
print(“C is greatest”)
# WAP to input a number and check if its Odd or even

num = int(input(“Enter a number: “))

if num<0:
print(“Its negative”)
elif num>0:
print(“Its positive”)
else:
print(“Its neither positive nor negative”)

if num %2==0:
print(“Its even number”)
else:
print(“Its odd”)
print(” ============= “)
if num<0:
print(“Its negative”)
else:
if num==0:
print(“Its neither positive nor negative”)
print(“Its even number”)
else:
print(“Its positive”)
if num%2==0:
print(“Its even number”)
else:
print(“Its odd”)

”’
Loops: Repeating a certain block of code multiple times
1. You know how many times to repeat : FOR loop
2. When you have a condition to repeat: WHILE loop

range(=start, <stop, =step)
range(5,17,4): 5, 9, 13
range(=start, <stop) – default step is 1
range(4,9) : 4, 5,6,7,8
range(<stop) : start = 0, step = 1
range(5): 0, 1,2, 3, 4
”’
for i in range(5,17,4):
print(“HELLO”)
print(“Value of i:”,i)

for i in range(4,9):
print(“HELLO AGAIN!”)
print(“Value of i:”,i)

for i in range(5):
print(“How are you?”)
print(“Value of i:”,i)


## While: should have condition and loop repeats only when the condition is true
cont = “yes”
i=0
while cont==“yes”:
print(“HELLO : “,i)
i+=5 # i = i + 5
if i>50:
cont = “No”

for i in range(1,11):
print(i,end=” : “)
print()
a,b,c,d,e = 50,60,50,60,70
total = a + b+c+d+e
total2 =0
for i in range(5):
val = int(input(“Enter the number: “))
total2 +=val # total2 = total2 + val

print(“Total sum of all the values from the loop = “,total2)

# * * * * *
for i in range(5):
print(“*”,end=” “)
print()
”’
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
”’
for j in range(5):
for i in range(5):
print(“*”, end=” “)
print()
”’
*
* *
* * *
* * * *
* * * * *
”’
for j in range(5):
for i in range(j+1):
print(“*”, end=” “)
print()
## For loop
”’
*
* *
* * *
* * * *
* * * * *
”’
for i in range(5):
for j in range(i+1):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for i in range(5):
for j in range(5-i):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for i in range(5):
for j in range(i):
print(” “,end=“”)
for j in range(5-i):
print(“*”,end=” “)
print()

”’
Assignment:
*
* *
* * *
* * * *
* * * * *
”’
”’
Option 1: implementing While using a condition
”’
cont =“Y”
while cont==“Y”:
num1 = int(input(“Enter a number: “))
num2 = int(input(“Enter a number: “))
print(“Choose your option:”)
print(“1. Addition”)
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“99. Exit”)
ch=input(“Enter your choice of operation:”)
if ch==“1”:
print(“Addition = “,num1 + num2)
elif ch==“2”:
print(“Subtraction = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “, num1 / num2)
elif ch==“99”:
#pass
cont=“N”
else:
print(“Invalid Input”)
print(“option1: going for one more iteration…”)

”’
Option 2: implementing While using True
”’
print(“Running option 2:”)
while True:
num1 = int(input(“Enter a number: “))
num2 = int(input(“Enter a number: “))
print(“Choose your option:”)
print(“1. Addition”)
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“99. Exit”)
ch=input(“Enter your choice of operation:”)
if ch==“1”:
print(“Addition = “,num1 + num2)
elif ch==“2”:
print(“Subtraction = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “, num1 / num2)
elif ch==“99”:
break # it breaks the loop
else:
print(“Invalid Input”)
print(“option2: going for one more iteration…”)

”’
Assignments:
1. Generate Prime numbers between given values
2. WAP to calculate average and grade for the given number of students
3. Calculate area for given choice of shape till user wants
4. Generate fibonacci series numbers till user says so
”’

Below programs to be done later

num= 5
if num<0:
print(“Positive”)
elif num>0:
pass
else:
pass

import random
number = random.randint(1,100)
count = 0
while True:
guess=int(input(“Guess the number (1-100):”))
count+=1
if guess <1 or guess>100:
print(“Invalid guess, try again”)
continue
elif number==guess:
print(f”You have correctly guessed the number in {count} attempts”)
break
elif number < guess:
print(“You have guessed a higher number. Try again…”)
else:
print(“You have guessed a lower number. Try again…”)

print(“Hello”)
print(“How are you?”)
print(“Hello”, end=“. “)
print(“How are you?”)

###############
# String
var1 = “HELLO”
var2 = ‘how are you there?’
# triple quotes can have multiline of text
var3 = ”’I am fine”’
var4 = “””I am here”””
print(var1,var2,var3,var4)
print(type(var1),type(var2),type(var3),type(var4))
var3 = ”’I am fine
I am doing well
I am alright”’
var4 = “””I am here
I am there
I am everywhere”””
print(var3)
print(var4)

# len() – gives you number of characters in a string
print(len(“hello”))
v1 = “hello”
print(len(v1))
#string
var1 = “HELLO”
var2 = “THERE”
# operations
print(var1 + var2)
print((var1+” “) * 5)

for i in var1:
print(i)

print(” indexing – [] , position starts from 0″)
var1 = “HELLO”
print(var1[0])
print(var1[4])
#backward index – starts from minus 1
print(var1[-1], var1[-5])

print(var1[len(var1)-1], var1[-1])

# strings are immutable – you cant edit/ you can replace
var1 = “hELLO there”
print(var1)
#var1[0] = ‘H’ TypeError: ‘str’ object does not support item assignment

# : to read continuous set of values
# need 2 to 4 element
print(var1[1:4])
print(“First 3 elements: “, var1[0:3],var1[:3], var1[-5:-2], var1[-len(var1):-len(var1)+3])
print(“Last 3 elements: “,var1[-3:])
print(“Content: “,var1, var1[:])

str1 =“Hello”
print(str1.isalpha())
print(str1.isdigit())
print(str1.isalnum())
print(str1.istitle())

val1 = input(“Enter a number: “)
if val1.isdigit():
val1 = int(val1)
else:
print(“You have not entered a valid number”)

#
val1 = “heLLo”
print(“lowe:”,val1.lower())
print(“upper: “,val1.upper())

# Indian citizen and over 18 yrs of age
check =0
age = int(input(“Enter your age: “))
citizen = input(“Enter your country of citizenship:”)
citizen = citizen.upper()
if age>=18 and citizen == “INDIA”:
print(“You are eligible to vote in India”)
else:
print(“You are not eligible to vote in India”)

#
str1 = “How are you?”
print(str1.isalpha())
print(“Count: “,str1.count(“o”))

if str1.count(“o”,3,10)>0:
print(“Index: “,str1.index(“o”,3,10))
else:
print(“Value o not in the given range”)

print(“FIND: “,str1.find(“o”,3,10))
print(“FIND: “,str1.find(“o”,3,7)) # -1 means value not found

str2 = ” h e l l o “
print(“Len 1: “,len(str2))
str2 = str2.strip()
print(str2)
print(“Len 2: “,len(str2))

str3 = “I am fine how are you are you fine or more than fine what?”
print(str3.split()) #output of split is a list
print(str3.split(“a”))
val2 = str3.split()
print(“Data type of val2:”,type(val2))
print(“Join = “,“…”.join(val2))

val4 = str3.replace(“fine”,“FINE”,2)
print(“Replace:”,val4)


# LIST – mutable linear ordered collection
l1 = []
print(type(l1))
l1 = [21,“Hello”, True,5.4]
print(l1[-1])
print(l1[1:3])
print(“Size of the list = “,len(l1))
l1[3] = [5.4, 6.9,8.0]
print(l1[:])
print(“Iterating over a list:”)
for i in l1:
print(i)
print(“Iterating over a list: done”)
a= l1[-1]
print(a[1])
print(“Type of 1st element: “, type(l1[0]))

## properties related to lists
l2 = [4,5,6]
l3=[“a”,“b”,“c”]
l4 = l2 + l3
print(l4)
l5 = l4 *3
print(l5)

#list methods
l2 = [4,5,6,5,6,6]
print(“Count of 5: “,l2.count(5))
search_val = 6
if l2.count(search_val) >0:
print(“Index: “,l2.index(search_val,3,5))
else:
print(“Something is wrong”)

# add elements to existing list
l2 = [4,5,6]
# append() : to add at the end of the list, and
# insert() : to add at any given position
l2.append(10)
l2.append(20)
print(l2)
l2.insert(1,8)
l2.insert(1,3)
print(l2)

”’
WAP to input marks of 5 students in 5 subjects and save them as list of marks
e.g. [[. . . . .],[. . . . .],[. . . . .],[. . . . .],[. . . . .]]
”’
# to delete elements from list, we use pop(index) and remove(value)
l2.remove(20)
print(“remove 20 from the list: “,l2)
l2.pop(3) #4th element in the list is deleted
print(“removing 4th element from the list: “,l2)

#reverse()
l2.reverse()
print(“L2 after reverse: “,l2)
#sort()
l2.sort() #default is increasing order
print(“L2 after sort: “,l2)

l2.sort(reverse=True) #sorting in reverse order- decreasing order
print(“List after reverse sorting:”,l2)

# extend() : l2 = l2 + l3
l3 = [10,30,20]
l2.extend(l3)
print(“L2 after extend: “,l2)

#copy() and =
l5 = l2 # they are pointing to each other
l6 = l2.copy() # creates a duplicate copy
print(“1. L2 = “,l2)
print(“1. L5 = “,l5)
print(“1. L6 = “,l6)

l2.append(77)
l5.append(88)
l6.append(99)

print(“2. L2 = “,l2)
print(“2. L5 = “,l5)
print(“2. L6 = “,l6)
#clear to remove all the value
l2.clear()
print(“L2 afer clear: “,l2)
del l2
#print(“After delete: “,l2) # NameError: name ‘l2’ is not defined.
# read marks of 3 students for 3 subjects
# [[],[],[]]
num_students = 3
num_subjects = 3
subjects = [‘Maths’,‘Physics’,‘Chemistry’]
students = [‘Rohit’,‘Surya’,‘Virat’]
master = []
for j in range(num_students):
print(“Accepting marks for the student “,students[j])
marks = []
for i in range(num_subjects):
m = int(input(\tEnter the marks in “+subjects[i]+“: “))
marks.append(m)
master.append(marks)
print(“Marks = “,master)

print(“Marks for each student:”)
for i in range(num_students):
print(master[i])

”’
Extend the above program to find overall topper and each subject topper with marks
[[45, 56, 89], [98, 56, 45], [90, 87, 65]]
”’

# Tuple: linear ordered immutable collection
l1 = [45,55,65]
l1[0] = 35
print(“L1: “,l1)
t1 = (45,55,65)
print(“1. type of t1:”,type(t1))
#t1[0] = 35 #’tuple’ object does not support item assignment
t1 = list(t1) # you convert tuple data to list
print(“2. type of t1:”,type(t1))
t1[0] = 35
t1 = tuple(t1)
print(“3. type of t1:”,type(t1))
print(“T1 = “,t1)
print(t1.count(35))
print(t1.index(35))
t2 = ()
print(“A. type = “,type(t2))
t2 = (2,)
print(“B. type = “,type(t2))
t2 = (2,6)
print(“B. type = “,type(t2))
t1 = (45,55,65) #packing
print(“Len = “,len(t1))
a,b,c = t1 #unpacking
print(a,b,c)
#compare the values
t3 = (4,5)
t4 = (4,1,99)
# dzat ear
print(“Last element: “,t4[-1])

for i in t4:
print(i)

print(” Dictionary – unordered mutable collection – with key:value pair”)
d1 = {}
print(“D1 type = “,type(d1))
d1 = {5:“Hello”, “Hello”:“Sachin Tendulkar”,“Runs”:25000, 5:“Mumbai”}
print(d1[5])
print(d1[“Hello”])
#keys have to be unique
# keys, values, items
print(“Keys: “,d1.keys())
print(“Values: “,d1.values())
print(“Items: “,d1.items())

for k,v in d1.items():
print(“K:V =”,k,“:”,v)

for k in d1.keys():
print(“K =”,k)
for v in d1.values():
print(“V =”,v)

d2 = {3:“Three”,6:“Six”,9:“Nine”}
d3 = {1:“Three”,2:“Six”,4:“Nine”}
d2.update(d3) # merge two dictionaries
print(d2)
d2.update(d2)
print(d2)
# Dictionary
dict1 = {}
temp_dict ={1:“One”,2:“Two”}
dict1.update(temp_dict) #dict1 = dict1 + temp
print(dict1)
”’
Assignment:
Read marks of 5 subjects for 5 students and save the data along with student’s names
”’
print(dict1[1])
dict1[1]=“Eleven”
print(dict1)

dict2 = dict1
dict3 = dict1.copy()
print(“1. Dict 1 = “,dict1)
print(“2. Dict 2 = “,dict2)
print(“3. Dict 3 = “,dict3)
dict1.update({3:“Three”})
dict3.popitem() # removes the value
print(“1. Dict 1 = “,dict1)
print(“2. Dict 2 = “,dict2)
print(“3. Dict 3 = “,dict3)
dict2.pop(2)
print(“1. Dict 1 = “,dict1)

dict2.clear()
print(dict1)

### SETS
set1 = {}
print(“Type of set1 = “,type(set1))
set1 = {5}
print(“Type of set1 = “,type(set1))
set2 = {2,4,6}
print(“Number of even numbers = “,len(set2))
print(set2)
# unordered linear mutable collection
# convert: set to list, list to set, set to tuple, tuple to set, list to tuple
list1 = [10,10,10,20,20,30]
list1 = list(set(list1))
print(list1)
set2.update({1,4,16})
print(“After update: “,set2)
set2.add(50)
print(“After add: “,set2)
# = and copy() works exactly like list and tuple
# union, intersection, minus, subset, subset

a = {1,3,5,7,8}
b = {2,4,6,8,7}
print(a.isdisjoint(b))
print(“# union”)
print(a.union(b))
print(a|b)
print(“# intersection”)
print(a.intersection(b))
print(a & b)
print(“#difference”)
print(a.difference(b))
print(a-b)
print(b.difference(a))
print(b-a)
print(“#symmetric difference”)
print(a ^b)
print(a.symmetric_difference(b))

# each of the above methods returns a new set
print(a)
print(b)
# if you dont want to create a new and update the first set instead:
# first_set…..update()
# first_set = first_set + ()
a.difference_update(b)
print(a)
# Functions
”’
Inbuilt functions: print() type() input()…
User defined functions: we are going to define
oneline function: lambda

function definition
function calling

”’
# defining the function – giving three statements a name
def sometext():
print(“WHo are you?”)
print(“What are you doing?”)
print(“Where are you going?”)


def sometext1():
print(“WHo are you?”)
print(“What are you doing?”)
print(“Where are you going?”)
return 100

def sometext2(name):
print(“WHo are you , Mr”,name)
print(“What are you doing, Mr”,name)
print(“Where are you going, Mr”,name)

def addition(a,b):
c = a + b
return c

sometext()
print(“doing something else”)
sometext()
print(“doing something else”)
sometext()
print(“Printing sometext(): “,sometext())
print(“Printing sometext1(): “,sometext1())
list1 = []
print(list1.append(5))

sometext2(“Sachin”)
print(“Addition of 5 and 10 is:”,addition(5,10))

Computational Thinking Content
# LOGIC

# Logic 1: a > b , b>c , which is greater between a and c ?
# BOOLEAN Logic – will result in boolean values – 0 (False) or 1 (True)
# AND (multiplication) OR (addition) NOT (opposite)

# prediction: Rohit and Ishaan will open the batting for India today
# actual: Rohit and Gill opened the batting
”’
# AND
True and True: True
True and False: False
False and True: False
False and False: False
”’
# prediction: Rohit or Ishaan will open the batting for India today
# actual: Rohit and Gill opened the batting
”’
# OR
True or True: True
True or False: True
False or True: True
False or False: False
”’
# Logical operator is applied between two conditions

## Conditions – checking a value
# > < <= >= == !=

# Assignment
a = 5 # take a variable a and assign a value 5 to it
print(a)

#Condition
a == 5 #Is the value of a equal to 5 ?? answer will be a bool value: 0 (F) or 1 (T)

a=“hello” #assignment

a,b,c=10,20,10

print(a >b or b>c or b<=c and c>a or a<=b and a>=c or b==c and a==c and a!=c or a<b)
# F or T or F and False or True and True or false and True and False or True
# T

# variables can hold: numeric (integer, float, complex),
# string (text – character & string ), bool (True or False)

# Data types

1/0.00000000000000000000000000000000000000000000000000000 = -infinity
Explain what above flowchart does?
Explain what above flowchart does?

Write detailed steps for making  tea and draw flowchart.

Answer the questions from the below pdf - found on page numbers: 5, 7 and 14

Psuedocode to calculate Netpay with hourly rate and deduction. When gross pay is more
than 100 only then you deduct otherwise deduction is zero


READ name, hourlyRate, hoursWorked
grossPay = hourlyRate * hoursWorked
IF grossPay >= 100
READ deductionRate
deduction = grossPay * deductionRate
ELSE
deduction = 0
ENDIF
netPay = grossPay – deduction
WRITE name, grossPay, deduction, netPay


Calculate NetPay when:

1. If hourly rate is 10 and the person worked for 6 hrs, what is his net pay?
2. If hourly rate is 5 and the person worked for 16 hrs, what is his net pay?
3. If hourly rate is 10 and the person worked for 16 hrs, what is his net pay?
Given that deduction rate is 5%

Next problem: Ask the user their choice of game number and then print, “you have bought game name”
Replace name with below game number:
1. Solitaire
2. Doom
3. Monopoly

Check the highest value among the 3 numbers

Assignment: Read marks in 5 subject, calculate average and assign grades based on average.
Avg > 80: Grade A

Avg > 70: Grade B
Avg > 60: Grade C
Avg > 40: Grade D
Avg < 40: Grade E

Scientists Who Made Contribution to Machine Learning

There are numerous scientists who have made significant contributions to the field of machine learning. Some of the most notable figures include:

  1. Arthur Samuel: Samuel coined the term “machine learning” and is known for his work on computer checkers, which pioneered the use of self-learning algorithms.
  2. Marvin Minsky: Minsky co-founded the Massachusetts Institute of Technology’s (MIT) Artificial Intelligence Laboratory and made various contributions to machine learning, including the Perceptron, a machine learning algorithm that simulates neural networks.
  3. Geoffrey Hinton: Hinton is often referred to as the “godfather of deep learning” and has made profound contributions to artificial neural networks, specifically in the field of deep learning. He is famous for his work on backpropagation, Boltzmann machines, and convolutional neural networks.
  4. Yoshua Bengio: Bengio is a leading figure in deep learning and neural networks. He contributed to the development of recurrent neural networks and is known for his work on unsupervised learning and generative models.
  5. Andrew Ng: Ng co-founded Google Brain and contributed to the development of deep learning algorithms. He is well-known for his work on convolutional neural networks and his founding role in Coursera, an online learning platform.
  6. Yann LeCun: LeCun is renowned for his work on convolutional neural networks and is one of the pioneers of deep learning. He is currently the Director of AI Research at Facebook.
  7. Vladimir Vapnik: Vapnik co-invented the support vector machine (SVM), a powerful supervised learning algorithm. His work has had a significant impact on machine learning, particularly in the field of pattern recognition.
  8. Judea Pearl: Pearl is known for his work on causal inference, specifically Bayesian networks and graphical models. His contributions have greatly influenced the field of probabilistic reasoning in machine learning.
  9. Fei-Fei Li: Li is a leading researcher in computer vision and has contributed to the development of large-scale visual recognition models. She co-created ImageNet, a widely used database that has sparked advancements in image classification.
  10. Demis Hassabis: Hassabis is a co-founder of DeepMind, an AI research company that has made significant breakthroughs in machine learning. His work has focused on reinforcement learning and developing algorithms capable of mastering complex games, including Go and chess.

These are just a few examples of the numerous scientists who have made notable contributions to the field of machine learning. Many others have also played crucial roles in advancing the field and continue to do so.