Swapnil Saurav

JULY Weekend Data Science
print(); print(3+5*25+ 100); print(‘3+5*2-5+100 ‘)
print(‘3+5*2-5+100=’, 3 + 5*25+100) #parameters = 2
print(‘3+5*2-5+100=’,3+5*25+100, “Hello”,5*2)
# Comments – this for humans not for computer
# indentation is mandatory in Python

#Process finished with exit code 0 = Exit code 0 means no error as expected

#variable
value1 = 50 #defining a varaible called value1
# lets tale value1 and assume its value is 50
print(value1)
print(“Value1 =”,value1)
value1 = 100
print(“Value1 =”,value1)

value1 = “HELLO”
print(“Value1 =”,value1)

#Basic types of data in Python
# type()
value1 = 50
print(type(value1)) # <class ‘int’>

# int -> integer: -9999999, -87866,0,1,1000,4534563463
#
value1 = 50.0
print(type(value1)) #<class ‘float’>
# e.g. of float: 0.0, -9,0, 89.8777777777777777
print(5+3)
print(5+3.0)
## indentation, ; variables, int, float, type()

# 3. complex
# square root of -1 called complex numbers, example:
num1 = 5j
print(type(num1) ) # <class ‘complex’>
print(num1)
print(num1 * num1)
print(“===========”)
#4. str – string (text)
num1 = “hell” \
“o”
print(num1)
print(type(num1) ) #<class ‘str’>

num1 = ‘5.6’
print(type(num1) )
num1 = “””hello”””
print(type(num1) )
num1 = ”’How
are
you?”’
print(type(num1) )
print(num1)

# 5. bool (boolean): True or False
val1 = True
print(type(val1))

# # # #
# variable names in Python – should always begin with alphabet
# it accepts, alphabets, digits and _
a=5
b=10
c=a+b
print(c)

awetrwetwetwe=5
bveryaDAGDBBB=10
cYTIUYOYUOJJDHJGN= awetrwetwetwe + bveryaDAGDBBB
print(cYTIUYOYUOJJDHJGN)

num1 = 10
num2 = 5
total = num1 + num2
print(total)

# # # # #
cost_of_each_pen = 59
total_pens = 16
total_cost = cost_of_each_pen * total_pens
print(“Total cost = “,total_cost)
print(“Cost of each pen is”,cost_of_each_pen,“so the cost of”,total_pens,“pens will be”,total_cost)
#above output using format string (f-string)
print(f”Cost of each pen is {cost_of_each_pen} so the cost of {total_pens} pens will be {total_cost})
print(“Cost of each pen is {cost_of_each_pen} so the cost of {total_pens} pens will be {total_cost}”)

total_pens = 3
total_cost = 100
cost_of_each_pen = total_cost / total_pens
print(“Total cost = “,total_cost)
print(“Cost of each pen is”,cost_of_each_pen,“so the cost of”,total_pens,“pens will be”,total_cost)
#above output using format string (f-string)
print(f”Cost of each pen is {cost_of_each_pen:.2f} so the cost of {total_pens} pens will be {total_cost})
print(“Cost of each pen is {cost_of_each_pen} so the cost of {total_pens} pens will be {total_cost}”)

name,country,position = “Virat”,“India”,“Opener”
print(f”Player {name:>15}, plays for the country {country:^20} at {position:<20})
name,country,position = “Manbagwama”,“New Zealand”,“Wicket-Keeper”
print(f”Player {name:>15}, plays for the country {country:^20} at {position:<20})

# operators: Arithematic operations
## + – * / // (integer division) ** (power) %(modulus – remainder)
num1 = 56
num2 = 3
print(num1 + num2)
print(num1 – num2)
print(num1 * num2)
print(num1 / num2)

print(num1 // num2)
print(num1 % num2)
print(num1 ** num2)

print(100 ** (1/2))

# Relational, Logical, Membership

## Assignments ##
# WAP to calculate area and perimeter of a rectangular field when length and breadth are given
# WAP to calculate area and circumference of a circular field when radius is given

DAY 2 : Recording Basic Python


# Comparison / Relational Operators
# check relationship: > < >= <= == !=
# output is bool (True / False)
var1, var2, var3 = 10,20,10 #lets assign value 10 to var1
print(var1 > var2) # False
print(var1 > var3) # False
print(var1 < var2) # True
print(var1 < var3) #False
print(var1 >= var2) #False
print(var1 >= var3) #True
print(“===============”)
print(var1 <= var2) # True
print(var1 <= var3) # True
print(var1 == var2) #False – is var1 equal to var2 ?
print(var1 == var3) #True
print(var1 != var2) # True
print(var1 != var3) # False

# Logical operator: and or not
# input and output are bool
# prediction: Rohit and Surya will open the batting – False
# actual: Ishan and Surya opened the batting
# in AND, even if one is False entire thing becomes False
# prediction: Rohit or Surya will open the batting – True
# actual: Ishan and Surya opened the batting
# in OR, even if one is True entire thing becomes True
print(True and True) #True
print(False and False) #False
print(False and True) #False
print(True and False) #False

print(True or True) #True
print(False or False) #False
print(False or True) #True
print(True or False) #True

##
print(“check”)
var1, var2, var3 = 10,20,10
print(var1 > var2 and var1 > var3 or var1 < var2 and var1 == var3 or var1 != var2 and var1 <= var3)
#(T or T)
print(not True)
print(not False)

# Membership operator: in , not in
print(“i” in “india”)

# looping – when you run a block of code multiple times
## keep looping till yot get the stopping condition – WHILE loop
## exatly how many times to run a code: FOR loop

# if is to check the conditoon
num=8
# condition – usng if
if num ==8:
print(“Value is “,num)
print(“if is executed now”)

# you can have only IF
# or when you have something to talk about when IF is false – ELSE
if num ==18:
print(“Value is “,num)
print(“if is executed now”)
else:
print(“I am in ELSE”)


#WAP to check if a number is positive or not
num = 5
if num >0:
print(“Number is positive”)
else:
print(“Number is not positive”)

# WAP to check if a number is odd or even
num = 11
if num%2!=0:
print(“Its odd number”)
else:
print(“Its even number”)

## input() – take input from the user
val1 = input(“Enter your marks: “)
print(“1. Data type of val1 is: “,type(val1))
val1 = int(val1) # int() float() bool() str() complex() – explicit conversion
print(“2. Data type of val1 is: “,type(val1))
print(“Value you entered is “,val1)



## WAP a program to input value from the user and check if it has 0 at the end
## Value = 57 – it doesnt have zero at the end
## value = 60 – it should say it has zero at the end.

value = int(input(“Enter the number:”))
if value %10==0:
print(“This has zero at the end”)
else:
print(“It doesnt have zero at the end”)

# if a number is positive or negative or zero
num = int(input(“Enter a number: “))
if num == 0:
print(“Number is zero”)
elif num < 0:
print(“Number is negative”)
else:
print(“Number is Positive”)

sum = 360
avg = sum/5

if avg >= 75:
print(“Grade is A”)
elif avg >=60:
print(“Grade is B”)
elif avg>=50:
print(“Grade is C”)
elif avg >=35:
print(“Grade is D”)
else:
print(“Grade is E”)



# if the score is more than 90, invite them for tea with director
avg = 98 #98: 2 88: 2 68: 3 & #98: 2 88: 1 68: 3

if avg >=80:
if avg >= 90:
print(“You are invited for tea @5pm auditorium”)
if avg >=95:
print(“You win President Award!”)
print(“A+ Grade”)
print(“Excellent result”)
print(“Great going!”)
elif avg >=60:
print(“A grade”)
elif avg >=40:
print(“B Grade”)
elif avg >=30:
print(“D Grade”)
else:
print(“Sorry you have failed”)
print(“Try again next time”)


# find the highest number of the given 3 numbers
Num1 = 99
Num2 = 78
Num3 = 123
if Num1 > Num2:
if Num1 > Num3:
print(f”For the assignment given by Swapnil Num1={Num1} value is highest”)
if Num2 > Num1:
if Num2 > Num3:
print(f”For the assignment given by Swapnil Num2={Num2} value is highest”)
if Num3 > Num1:
if Num3 > Num3:
print(f”For the assignment given by SwapnilNum3={Num3} value is highest”)
## ##
num1 = 198
num2 = 198
num3 = 168

if num1 > num2:
if num1 > num3:
print(“{num1} is greater”)
else:
print(“{num3} is greater”)
else:
if num2 >num3:
print(“{num2} is greater”)
else:
print(“{num3} is greater”)

###
num1 = 98
num2 = 128
num3 = 168
high = num1
if high < num2:
high = num2
if high <num3:
high = num3
print(f”{high} is the highest”)


# WAP to input 3 sides of a triangle and find if its a equilateral, isoceles or scalene
num1 = 128
num2 = 128
num3 = 128
s1=num1; s2=num2; s3=num3
if s1==s2:
if s2==s3:
print(“As all 3 sides of triangle is equal hence it is an equilateral triangle”)
else:
print(“As 2 sides of an triangle are equal hence it is an isosceles triangle”)
else:
if s2==s3 or s3==s1:
print(“As 2 sides of an triangle are equal hence it is an isosceles triangle”)
else:
print(“As no sides are equal it is an scalene triangle”)


num1 = 128
num2 = 108
num3 = 128

if num1 == num2:
if num1 == num3:
print (“equilateral triangle”)
else:
print(“isoscales triangle”)
else:
if num2 == num3 or num1==num3:
print (“isoscales triangle”)
else:
print(“scalene triangle”)

## below: bad example of the code
num1 = 128
num2 = 108
num3 = 128

if num1 == num2 and num1==num3:
print (“equilateral triangle”)
if (num1 == num2 and num2!=num3) or (num1==num3 and num3!=num2) or (num2==num3 and num3!=num1):
print (“isoscales triangle”)
if (num1 !=num3 and num1!=num2) or (num2 !=num1 and num2!=num3) or (num3 !=num1 and num2!=num3):
print(“scalene triangle”)

# for loop
# range(a,b,c): a=start, b< end (upto, not equal to), c = increment
#range(2,12,3)# generates range of values- 2,5,8
# range(a,b) default c value is ONE
#range(5) #default start = 0, increment =1: 0,1,2,3,4

for counter in range(10):
print(counter)

# while: loops until the given condition
z=0
while z<=10:
print(z)
z=z+ 1
# divisible by 100 & 400 – leap year, divisible by 100 but not 400 – not a leap year
# divisible by 100 but divisible by 4 – leapyear

year = 400
if year %100==0:
if year %400==0:
print(“Leap Year”)
else:
print(“Not a leap year”)
else:
if year %4==0:
print(“Leap Year”)
else:
print(“Not a leap Year”)

####
a,b,c,d = 40,10,20,5
small = a
if b <small:
small=b
if c <small:
small=c
if d <small:
small=d

num=str(1234567)
print(“Number of characters = “,len(num))
num = 158
length = 1
a = num//10
if a!=0:
length+=1 # length = length + 1
num = num//10

a = num//10
if a!=0:
length+=1 # length = length + 1
num = num//10

a,b,c=20,10,30

if a < b:
a,b=b,a
if b < c:
if a>c:
print(c, “is second highest”)
else:
print(a,“is second highest”)
else:
print(b,“is second highest”)

eng = 85
maths,science,ss = 90,90,80
if eng >=80:
if ss>=80:
if maths>=80 and science >= 80:
print(“Science Strean”)
else:
print(“Humanities Stream”)
elif maths >= 50 and science >= 50:
print(“Commerce”)

else:
print(“Admission not possible”)
# loops – repeat multiple times
# for: used when you know how many times to repeat
# while: in which conditions to repeat

#range(): which generates range of values
# range(=start, <end, increment)
# range(3,11,2): 3,5,7,9
# range(=start, <end) : increment is default 1
# range(5,9): 5,6,7,8
# range(<end): default start = 0 & increment is 1
# range(4): 0,1,2,3

for counter in range(3,11,2):
print(“Counter = “,counter)
print(“Hello”)

#print even numbers from 6 to 20
for i in range(6,21,2):
print(i,end=“, “)
print()

# find sum of numbers between 5 and 9 both including
sum=0
for i in range(5,10):
sum+=i
print(“Sum = “,sum)

# generate first 4 multiples of 5: 5 10 15 20
num = 15
for j in range(4):
print(f”{num} * {j+1} = {num*(j+1)})

print(“WHILE : condition based repeation”)
count = 1
while count <=8:
count += 2
print(count)

# print hello till user says yes

while True:
print(“HELLO”)
ch = input(“Enter y to print again or anyother key stop: “)
if ch!=‘y’:
break

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

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

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

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

Assignment 2:
*
* *
* * *
* * * *
* * * * *

Assignment 3: Multiplication Table:

1 * 1 = 1 2 * 1 = 2 … 10 * 1 = 10
1 * 2 = 2 2 * 2 = 4 10 * 2 = 20

1 * 10 = 10 2 * 10 = 20 10 * 10 = 100
”’

## Check if Prime or not
num = 53
isPrime = True
for i in range(2, num // 2 + 1):
if num % i == 0:
isPrime = False
break

if isPrime:
print(f”{num} is Prime”)
else:
print(f”{num} is not Prime”)

import time
curr = time.time()
line = 0
## Generate Prime numbers between 20000 and 30000
for num in range(20000,30001):
isPrime = True
for i in range(2,num//2+1):
if num % i==0:
isPrime = False
break

if isPrime:
if line >= 20:
print()
line = 0
print(num, end=“,”)
line+=1
end = time.time()
print(\n\nTotal time taken to generate prime numbers is “,end-curr,“seconds”)
print(“Time is: “,time.time())
”’
1*1 = 1 2 * 1 = 2 …
1 * 2..

1 * 10

”’
s=0
for i in range(1,11,1):
s += 1 # s=s+1
for j in range(1,11):
print(f”{j:<2}*{i:>2}={j*i: <3}, end=” “)

print()

print(“S = “, s)
## WHILE LOOP
num = 1
ch = “y”
while ch==“y”:
num+=1
ch=input(“Enter y to continue or anyother key to stop: “)

print(“num = “,num)
##
# Menu option
while True:
print(“Now, pick up the option:”)
print(“1. Addition”)
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“5. Quit”)
choice = int(input(“Enter your option from the above menu:”))

if choice>=1 and choice <=4:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))

if choice==1:
print(“Addition of given numbers: “,num1+num2)
elif choice==2:
print(“Subtraction of given numbers: “,num1-num2)
elif choice==3:
print(“Multiplication of given numbers: “,num1*num2)
else:
print(“Division of given numbers: “,num1/num2)
elif choice==5:
break
else:
print(“Invalid option, try again!”)

##
”’
Guess the number game!
Computer v Human

Module: has collection of related functions
randint(1,100) – will return random integer number between 1 and 100 (both including)
”’
import random

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

attempt += 1
if num == guess:
print(f”Congratulations! You guessed it right in {attempt} attempts.”)
break
elif num < guess:
print(f”Your {guess} is higher.. try with a lower guess!”)
else:
print(f”Your {guess} is lower.. try with a higher guess!”)
”’
Guess the number game!
Computer v Computer

Module: has collection of related functions
randint(1,100) – will return random integer number between 1 and 100 (both including)
”’
import random
import time

start = time.time()
num = random.randint(1,100)
low,high =1,100
attempt = 0
while True:
guess = random.randint(low,high)
attempt+=1
if num==guess:
end = time.time()
print(f”Congratulations! You guessed it right in {attempt} attempts and in {end-start} seconds.”)
break
elif num < guess:
print(f”Your {guess} is higher.. try with a lower guess!”)
high = guess-1
else:
print(f”Your {guess} is lower.. try with a higher guess!”)
low = guess + 1

## ### ### ##
”’
WAP a Program to generate prime numbers from 1 till user continue to ask
2
you want more? y
3
you want more? y
5
you want more? y
7
you want more? y
11
you want more? n
Thank you

”’
num = 1
while True:
num+=1
isPrime = True
for i in range(2,num//2+1):
if num%i==0:
isPrime=False
break
if not isPrime:
continue
print(num)
ch=input(“you want more (y for yes)?”)
if ch!=“y”:
break
## ##
## STRINGS
str1 = ‘Hello’ # one line text
str2 = “Hi there” # one line text
str3 = ”’How are you?
Where are you?
How you doing?”’ # multiline text
str4 = “””I am fine
I am here
i am great””” # multiline text
print(type(str1),type(str2),type(str3),type(str4))
print(str1,\n,str2,\n,str3,\n,str4)

# \n is used for newline
# \ – escape character { this will add power / remove power if you have power}
# \\ – will make one \
print(\\n is used for newline \\t is used for Tab spaces \\c has not meaning”)

# \\n \\t \\c
print(\\\\n \\\\t \\\\c”)
## STRING
str1 = “hello 5”
str2 = “how are you”
# he asked,”how are you?”
print(‘he asked,”how are you?”‘)
print(“he asked,\”how are you?\”)
# he asked,”what’s your name?”
print(‘he asked,”what\’s your name?”‘)
print(str1 + ” “+ str2)
print((str1+” “)*5)

for i in str1:
print(i)

# len() – will tell you the length of the variable
print(f”Number of elements in {str1} is”,len(str1))

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

# indexing: accessing the member from a string /data collection
str1 = “helloshnrtmd”
print(str1[0])
last_idx = len(str1) – 1
print(str1[-1])
# str1[0] = “H” – error

# string is immutable – you cant edit
a=5
a=6
str1 = “hello”
str1 = “Hello”
print(“ell”, str1[1:4], str1[-4:-1])
print(“Hell”,str1[0:4],str1[:4], str1[:-1])
print(“ello”,str1[1:5],str1[1:], str1[-4:])

# Functions (global) and Methods (specific to the class)
str1 = “HelLo how are you”
str2 = “hello”
anything =
print(“Lower: “,str1.lower())
print(“Uppercase: “,str1.upper())
print(“STR1 = “,str1)
print(“Title Case: “, str1.title())
print(str2.islower())
print(str2.upper().isupper())
print(str2.isalnum())
print(str2.isdigit())

num1 = input(“Enter length of the rectangle: “)
if num1.isdigit():
num1 = int(num1)
else:
print(“Invalid number”)

# modify below program to accept space also
name = input(“Enter your name: “)
if name.isalpha():
print(“Name accepted”)
else:
print(“Invalid name”)
# modify below program to accept space also
name = “Sachin Tendulkar”
if name.isalpha():
print(“Name accepted”)
else:
print(“Invalid name”)

output = name.split() #split by default will split on ‘ ‘
output_str = “”.join(output)
print(output_str)
if output_str.isalpha():
print(name,“Name accepted”)
else:
print(“Invalid name”)

print(“Strip —“)
txt = ” 2 4 6 8 “
print(txt.strip(), “length is”,len(txt.strip()))

# replace find count
print(“blank spaces are:”,txt.count(” “))
txt2 = “Twinkle Twinkle Twinkly Star”
print(“Count Twin: “,txt2.count(“Twin”))
print(“Find Twin: “,txt2.find(“Twin”))

# Print the position of all the occurrences
st=0
for i in range(txt2.count(“Twin”)):
cnt = txt2.find(“Twin”,st)
print(“Find Twin: “, cnt)
st = cnt+1

txt2 = “Twinkle Twinkle Twinkly Star”
txt3 = txt2.replace(“Twin”,“Quin”)
print(txt3)
txt3 = txt2.replace(“Twin”,“Quin”,2)
print(txt3)
print(“Check if txt2 starts with t: “,txt2.startswith(“t”))
print(“Check if txt2 starts with t: “,txt2.startswith(“T”))
print(“Check if txt2 starts with t: “,txt2.endswith(“r”))
print(“Check if txt2 starts with t: “,txt2.endswith(“R”))


# LIST: linear mutable ordered collection
l1 = []
print(type(l1))
l1 = [3,4,6,8,9]
print(“Getting last 3 values:”)
print(l1[2:])
l1[0]=0
print(l1)

l2 = [“Hello”, 45, 66.6, 5j , True, [5,10,15,20]]
print(l2)
print(type(l2))
print(type(l2[3]))

print([2,4,6]+[4,8,12])
print([2,4,6] * 5)

###
for i in l2:
print(i)

fare=[]
fare.append(50) # append is used to add members to a list at the end
fare.append(60)
fare.append(45)
fare.insert(1,77)
fare.insert(1,44)
print(“Fare = “,fare)

fare.pop(2)
print(“Fare = “,fare)
fare.remove(60)
print(“Fare = “,fare)
fare.clear()
print(“Fare = “,fare)

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

print(“Marks = “,marks)
print(“Total marks is”,sum(marks))
print(“Total = “,sum(marks)/len(marks))

#LIST

”’
## Assignment: Implement Stack and Queue operations using List
Stack is called as – Last in First out structure (LIFO)

Queue: First In First Out (FIFO)

”’
l1 = [2,4,8,14,16]
num= 140
if l1.count(num) >0:
print(“Index: “,l1.index(num))
else:
print(f”{num} is not in the list”)

print(l1.index(14,2,6))
print(“0. L1 = “,l1)
l2 = l1.copy() # shallow copy – creates another copy
l3 = l1 # deep copy – another name for same data
print(“1. L1 = “,l1)
print(“1. L2 = “,l2)
print(“1. L3 = “,l3)
l1.append([18,20])
l1.append(20)
print(“2. L1 = “,l1)
print(“2. L2 = “,l2)
print(“2. L3 = “,l3)

l1.extend(l3) # l1 = l1 + l3
print(l1)

l4 = [5,9,1,4,2,3,6]
l4.reverse()
print(l4)
l4.sort()
print(l4)
l4.sort(reverse=True)
print(l4)

# [[4,6,8],[9,5,2],[5,9,1]] – find highest value at each position.
l1 = [[5, 6, 8], [5, 0, 0], [5, 9, 1]]
print(“the largest element is”, max(l1))

data = []
for i in range(3):
temp = []
for j in range(3):
val = int(input(“Enter value: “))
temp.append(val)
data.append(temp)

print(“Data = “,data)
# data = [[5, 4, 3], [6, 7, 5], [9, 8, 7]]
max_list = [-999,-999,-999]
for i in range(3):
for j in range(3):
if max_list[i] < data[j][i]:
max_list[i] = data[j][i]

print(max_list)

# Assignment 2: Find the highest element for each of the sub-list

# TUPLE: Linear immutable Ordered collection
t1 = (1,2,3)
print(type(t1))
print(t1.count(3))
print(t1.index(3))
# indexing is exactly same as Str/List
for i in t1:
print(i)
a,b,c = t1
print(a,b,c)
d = [(1,2,3,4,5,6),(1,2,3,4,5,6),(1,2,3,4,5,6),(1,2,3,4,5,6)]
t1 = list(t1)
t1 = tuple(t1)

## ##
# Dictionary: Linear mutable unordered collection
# dictionary is your key:value (key value pairs)
d1 = {“Name”:“Sachin”,“Runs”:21000,3:“Mumbai”, True:“Mumbai Indians”}
print(d1)
print(d1[“Name”])
d1[“Name”] = “Sachin Tendulkar”
print(d1)

d2 = d1 #deep copy
d3 = d1.copy() #shallow copy
print(“1. D1 = “,d1)
print(“1. D2 = “,d2)
print(“1. D3 = “,d3)
t = {“Country”:“India”}
d1.update(t)
print(“2. D1 = “,d1)
print(“2. D2 = “,d2)
print(“2. D3 = “,d3)

print(d1.keys())
print(d1.values())
print(d1.items())

for i in d1.keys():
print(i)
print(“================”)
for i in d1.values():
print(i)
print(“================”)
for i in d1.items():
print(i)
print(“================”)

for i,j in d1.items():
print(i,“:”,j)
print(“================”)

d1.pop(“Name”)
print(d1)
d1.popitem()
print(d1)
## 7th Oct 2023
list1=[“Maths”,“Physics”,“Chemistry”]
Students_marks = {}
for i in range(3):
key = input(“Enter the Roll No. of the Student:”)
marks_list = []
for j in range(3):
marks = int(input(f”Enter the marks in {list1[j]}: “))
marks_list.append(marks)
t = {key:marks_list}
Students_marks.update(t)

print(“Details of marks are:\n,Students_marks)
# {‘101’: [67, 78, 89], ‘105’: [69, 71, 90], ‘110’: [90, 45, 76]}
”’
Assignment: Extend the above program to find:
1. Total of each student
2. Highest marks obtained among the given students
3. Give roll no of topper of each subject
”’
# Dictionary : unordered mutable collection
d1 = {10:“ONE”, 20:“TWO”,30:“THREE”}
d1.pop(20)
print(d1)

d2 = d1
d3 = d1.copy()
print(“1. D1 = “,d1)
print(“1. D2 = “,d2)
print(“1. D3 = “,d3)
d1.update({40:“Four”})
d1.update({50:“Five”})
print(“2. D1 = “,d1)
print(“2. D2 = “,d2)
print(“2. D3 = “,d3)

# SET : unordered collection
set1 = {5,8, 8, 5, 5,12}
print(type(set1))
print(set1)
set1.update({1,2,3})
print(set1)
set1.pop()
print(“After pop: “,set1)

l1 = [5,10,10,15,15,15,20,20,20,20]
l1 = list(set(l1))
print(“List l1 = “,l1)

# SET OPERATIONS
set1 = {1,3,5,7,6}
set2 = {2,4,6,8,7}
print(set1)
print(set2)
print(“UNION”)
print(set1.union(set2))
print(set1 | set2)
print(“INTERSECTION”)
print(set1.intersection(set2))
print(set1 & set2)
print(“SET1 – SET2”)
print(set1.difference(set2))
print(set1 – set2)
print(“SET2 – SET1”)
print(set2.difference(set1))
print(set2 – set1)

print(“SYMM SET1 – SET2”)
print(set1.symmetric_difference(set2))
print(set1 ^ set2)
print(“SYMM SET2 – SET1”)
print(set2.symmetric_difference(set1))
print(set2 ^ set1)

print(set1.isdisjoint(set2))
set4 = {10,20,30}
print(set1.isdisjoint(set4))

d1 = {“Good”:{“Excellent”,“Positive”,“Nice”}}
print(d1)

# one line loops
words = ‘abcdefghijklmnopqrstuvwxyz’
# go through words list and save to another list
words2 =[]
for w in words:
words2.append(w)

print(“Words 2: “,words2)

words3 = [w*2 for w in words]
print(“Words 3: “,words3)

words = ‘abcdefghijklmnopqrstuvwxyz’
words3 = [w*2 for w in words]
print(“Words 3: “,words3)

# one line conditions
num1 = 50
#check if its odd or even
result = “even” if num1%2==0 else “odd”
print(f”{num1} is {result})

# Functions
”’
Inbuilt functions: print(), type(), int(),…
User defined functions: we create our own functions
One line function:
”’



#this is how you define a function by name three_sentences()
def three_sentences():
print(“Who are you?”)
print(“How are you?”)
print(“Where are you?”)

def three_sentencesv1(var_name):
print(f”Who are you, {var_name}?”)
print(f”How are you, {var_name}?”)
print(f”Where are you, {var_name}?”)

def addition(num1, num2):
add = num1 + num2
#print(“Addition is”,add)
return add

#Calling the functions
result = addition(10,20)
three_sentences()
three_sentencesv1(“Mr Sachin”)
three_sentencesv1(“Ms Sindhu”)
print(“Output is”,result)
if result >= 0:
print(f”I got {result} sweets”)


# Functions
# arguments:
# required – you have to provide the values for the arguments
# positional – the order of the arguments should match
# default (like non-required values)
# keyword (like non-positional)

country = “INDIA” #global variable
#function definition
def func1(num1, num2):
print(“A block of code”)
print(“Num1 =”,num1)
print(“Num2 = “,num2)
total = num1 + num2
return total
def func2(num1, num2=1): #default value for num2
print(“A block of code”)
print(“Num1 =”,num1)
print(“Num2 = “,num2)
total = num1 * num2
return total

def func3(num1=10, num2=5): #default value for num2
print(“A block of code”)
print(“Num1 =”,num1)
print(“Num2 = “,num2)
total = num1 * num2
return total

def func4(x,y,num1=10, num2=5,num3=25,num4=85): #default value for num2
print(“A block of code”)
print(“Num1 =”,num1)
print(“Num2 = “,num2)
total = num1 * num2
return total

def test():
global country #local – local will preceed global
print(“Country is”,country)
country=“india” #local – local will preceed global
print(“Country is”, country)

# variable length arguments: using * and **
# * read as a tuple
# ** read as a dictionary
def var_len_demo(num, *names, **favorite):
print(“Variable length arguments functon”)
print(“Num is”,num)
if len(names)>0:
print(“Names = “,names)
if len(favorite) > 0:
print(“Favorites = “,favorite)

result = func1(5,10)
print(“func1 Output is “,result)
result = func2(5)
print(“func2 Output is “,result)
result = func3(5,7)
print(“func2 Output is “,result)
result = func3()
print(“func2 result that we have is “,result)

result = func3(num2=15,num1=99)
print(“func2 Output is “,result)
result = func3(num2=15)
result *=10
print(“func2 Output is “,result)
print(“func4 Output is”,func4(5,6))

# wap to check if a number is prime or not
def checkprime(num):
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
else:
isPrime=True
return isPrime

for val1 in range(15,18):
result = checkprime(val1)
if result:
print(f”{val1} is a prime number”)
else:
print(f”{val1} is not a prime number”)

print(‘generate series of prime numbers between 50000 and 51000:’)
for i in range(50000,51001):
result = checkprime(i)
if result:
print(i,end=“, “)
print()
”’
Assignment:
All the programs that you have done so far – convert them
into functions – all your core logic should be in the function
and only the values to the arguments should be passed to the
function and the output displayed in the main function.
”’
def printpattern(value,num):
for i in range(num):
for j in range(num):
print(value,end=” “)
print()

printpattern(“#”,7)
test()

# calling variable length
var_len_demo(100)
var_len_demo(11, “Sachin”,“Virat”,“Rohit”,“MS”)
var_len_demo(11, “Sachin”,“Virat”,“Rohit”,“MS”,color=“blue”,game=“badminton”,drink=“water”)
var_len_demo(11, color=“blue”,game=“badminton”,drink=“water”)
# Class and Objects
## —————–
class Book:
total_books = 0 # class member/variable

def input_data(myname):
myname.title = “” #object variable
myname.author = “” #object variable
type = “book”
myname.mytype = type

def set_data(myname):
myname.title = “Python Programming” #object variable
myname.author = “Saurav” #object variable

b1= ‘hello’
print(“Type of b1 = “,type(b1))
print(b1.upper())
book1 = Book()
print(“Type of book1 = “,type(book1))

book1.total_books += 10
print(book1.total_books)
book1.input_data() #object function
print(“Author: “,book1.author)
book1.set_data()
print(“Author: “,book1.author)


# Class and Objects
# Program 2
## —————–
class Book:
total_books = 0 # class member/variable

def input_data(self):
self.title = “” #object variable
self.author = “” #object variable
Book.total_books +=1

def set_data(self, title,author=“Saurav”):
self.title = title #object variable
self.author = author #object variable


book2 = Book()
book2.input_data()
book2.set_data(“Python Programming”,“Sachin”)
book3 = Book()
book3.input_data()
book3.set_data(“Machine Learning”,“Rohit”)
book4 = Book()
book4.input_data()
book4.set_data(“SQL Programming”)
print(“Total books = “,book2.total_books)
print(“Total books = “,book3.total_books)
print(“Total books = “,book4.total_books)
print(“Total books = “,Book.total_books)

print(“Title = “,book2.title)
print(“Title = “,book3.title)
print(“Title = “,book4.title)
#print(“Title = “,Book.title)

# class and objects

class Books:
total_books = 0 #class variable

def getdata(self,title,author): #object method
self.title = title #object variable
self.writer = author
Books.total_books +=1
print(“Hello”)

def putdata(self):
print(“Title of the book: “,self.title)
print(“Author of the book: “, self.writer)

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

b1 = Books()
b2 = Books()
print(“Total books = “,b1.total_books)
print(“Total books = “,Books.total_books)
b1.getdata(“Python”,“Rohit”)
b2.getdata(“SQL”,“Virat”)
b2.putdata()
b1.putdata()
print(“Total books = “,Books.total_books)
print(“Total books = “,b1.total_books)
print(“Total books = “,b2.total_books)
b2.CountBooks()

### ### ####
# class and objects
class Library:
def libname(self):
print(“I am being called from Library”)
class Books(Library):
total_books = 0 #class variable

def __init__(self,title,author): #object method
self.title = title #object variable
self.writer = author
Books.total_books +=1
print(“Hello”)

def putdata(self):
print(“Title of the book: “,self.title)
print(“Author of the book: “, self.writer)

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

b1 = Books(“Python”,“Rohit”) #__init__()
b2 = Books(“SQL”,“Virat”)
print(“Total books = “,b1.total_books)
print(“Total books = “,Books.total_books)
#b1.getdata(“Python”,”Rohit”)
#b2.getdata(“SQL”,”Virat”)
b2.putdata()
b1.putdata()
print(“Total books = “,Books.total_books)
print(“Total books = “,b1.total_books)
print(“Total books = “,b2.total_books)
b2.CountBooks()

”’
Properties of class and objects:
1. Inheritance: inheriting properties from another class
2. Polymorphism: Multiple forms
3. Abstraction: information/implementation hiding
4. Encapsulation: data hiding
”’
b1.libname()

class text_display(Books):
def __init__(self, title, author):
print(“INIT in text_display”)
Books.__init__(self, title, author)

def display(self):
print(“Display in text_display”)

def putdata(self):
print(“Title of the book: “)
print(“Author of the book: “)

t1 = text_display(“Waste Management”,“Dhoni”)
t1.putdata()

# Encapsulation in Python
”’
Public members (variable or method)
Protected members (_membersname): logically these members can only
be called by the derived classes. concept is there but not enforeced
Private members (__membername): they are not accessible outside the class

”’
class Library:
def __lib_data(self):
print(“Lib data from Library”)
def get_data(self):
self.__lib_data()
class Books(Library):
def book_data(self):
print(“Book data from Books”)

def get_data(self):
Library.__lib_data(self)
print(“Get data from Books”)

class Test:
def test_data(self):
print(“Test data from Test”)

b1 = Books()
b1.get_data()
## #####


# work with external files – text files
”’
Modes of opening file:
r : read the content (default)
w : write the content by deleting previous content
a : add to the existing content (append)

r+ : read and write
w+ : write and read
a+ : append and read

Writing to a text file:
write()
writelines()

Reading the content from a text file:
read()
readline()
readlines()
”’
content = ”’Twinkle Twinkle little star
How I wonder what you are
Up above the world so high
like a diamond in the sky
”’

cont_lt = [‘Ba Ba Black Sheep\n,‘Do you have any wool?\n,
‘Yes sir yes sir\n,‘Three bags full\n]
fileptr = open(“TestNov\\abc.txt”,“a”)
if fileptr.writable():
fileptr.write(content)
fileptr.writelines(cont_lt)
else:
print(“File cant be written”)

fileptr.close()

## open the file for reading
fileptr = open(“TestNov\\abc.txt”,“r”)
if fileptr.readable():
# read the content
mycontent = fileptr.read()
else:
print(“Not readable”)

print(“===================”)
print(mycontent.replace(“Twinkle”,“TWINKLE”))
mycontent = mycontent.replace(“Twinkle”,“TWINKLE”)
print(mycontent)

print(“Second reading…………”)
# go to beginning
fileptr.seek(30) #go to the position 0
mycontent = fileptr.read()
print(mycontent)
fileptr.seek(0)
print(“============ Read line =================”)
content = fileptr.readline()
print(content)
content = fileptr.readline(200)
print(content)
content = fileptr.readline()
print(content)

content = fileptr.read(200)
print(content)

fileptr.seek(0)
content_lines = fileptr.readlines()
print(content_lines)
print(“Total number of lines = “,len(content_lines))
fileptr.seek(0)
allcontent = fileptr.read()
print(“Number of characters = “,len(allcontent))
txt1 = “welcome”
print(len(txt1))
fileptr.close()

## Working with CSV files
import csv
”’
r, r+, w, w+, a, a+ : different modes of files to read
”’
fileptr = open(“TestNov/abc.csv”,“w”,newline=“”)
csv_writer = csv.writer(fileptr)
content_list = [[“SNO”,“Name”,“City”,“Runs”],
[1,“Rohit”,“Mumbai”, 3500],
[2,“Virat”,“Delhi”,4210],
[3,“Laxman”,“Hyderabad”,1990]]

for i in content_list:
csv_writer.writerow(i)
fileptr.close()

fileptr = open(“TestNov/abc.csv”,“w”,newline=“”)
csv_writer = csv.writer(fileptr,delimiter=“,”)
content_list = [[“SNO”,“Name”,“City”,“Runs”],
[1,“Rohit”,“Mumbai”, 3500],
[2,“Virat”,“Delhi”,4210],
[4,“Laxman”,“Hyderabad”,1990]]

csv_writer.writerows(content_list)
fileptr.close()
print(“Reading the CSV file\n)
fileptr = open(“TestNov/abc.csv”) #default mode is read
csv_reader = csv.reader(fileptr, delimiter=“,”)
for i in csv_reader:
print(i)
fileptr.close()

fileptr = open(“TestNov/abc.csv”) #default mode is read
csv_reader = csv.reader(fileptr, delimiter=“,”)
count=0
for i in csv_reader:
if count==0:
count+=1
else:
print(i[1])
break
fileptr.close()

### working with json file
json1 = {
“Player1”:[
{
“Name”:“Sachin”,
“City”:“Mumabi”,
“Runs”: 13980
}
]
}

import json
”’
dump: writing to a file
dumps: writing to screen
load: reading from a file
loads: reading from a string
”’
# dump – writing to a file
fileptr = open(“TestNov/abc.json”,“w”)
json.dump(json1, fileptr,indent=4)
fileptr.close()
#load – reading json content from the file
fileptr = open(“TestNov/abc.json”,“r”)
content = json.load(fileptr)
print(“printing from the file:\n,content)
#writing in json format on the screen
print(json.dumps(content,indent=4))
fileptr.close()

json_str = “””{“Player1”: [{“Name”: “Sachin”, “City”: “Mumabi”, “Runs”: 15980}]}”””
#convert above string into json format
json_json =json.loads(json_str)
#above json_json content we will display on the screen
# and also save to the file
fileptr = open(“TestNov/abc.json”,“w”)
json.dump(json_json, fileptr,indent=4)
fileptr.close()

print(json.dumps(json_json,indent=4))

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

### Assingment ###
### Read India and South Africa today’s match data as a csv
## write using Python (csv.writer)
### then use reader to answer following question
### 1. Who is the top scorer
### 2. Who is the highest wicket taker
### 3. Who has the best bowling average
### 4. Who has the best strike rate



### Example
### working with json file
json1 = {
“Players”:[
{
“Name”:“Sachin”,
“City”:“Mumabi”,
“Runs”: 13980
},
{
“Name”:“Laxman”,
“City”:“Hyderabad”,
“Runs”: 15980
},
{
“Name”:“Rahul”,
“City”:“Bangalore”,
“Runs”: 11980
}
]
}

import json
”’
dump: writing to a file
dumps: writing to screen
load: reading from a file
loads: reading from a string
”’
# dump – writing to a file
fileptr = open(“TestNov/abc.json”,“w”)
json.dump(json1, fileptr,indent=4)
fileptr.close()
#load – reading json content from the file
fileptr = open(“TestNov/abc.json”,“r”)
content = json.load(fileptr)
print(“printing from the file:\n,content)
print(content[‘Players’][2][‘Name’])
#writing in json format on the screen
fileptr.close()
#databases – store and make data manipulation easy
# databases – Oracle, SQL Server, DB2, Postgres, SQLITE, MYSQL,
# language – SQL language -Structured Query Language- to access
# relational databases you need to learn SQL
# Roles – DBA (database administrators- manage),
# Database programmers (users)
# Database architects – they are responsible for creating the
# tables and storing them

# Operations on RDBMS – Read (reading the existing content – 80%)
# Add, Edit, Delete
# DB knowledge – Physical data v logical data
# all RDBMS systems will have same logical view
# ANSI – sets the standards for relational databases

# Rules to be followed by the database to be called itself RDBMS
# – data is stored in table and in row and column format, cell
#cell should have one information

#components: Table, Views, Triggers

#creating table to store values: list of column and their dattypes, primary key

#Relational DBMS – relationship between various column values
## 1 to 1
## 1 to Many or Many to 1
## Many to Many

# download and install MYSQL
# https://dev.mysql.com/downloads/installer/
# follow the instructions: download all the additional things:
## like Workbench, Sample database also install
## While installing save the root user password
## – username – root
## password – learnSQL
## servername – localhost

# Tutorial: https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html

# Constraints: Primary Key, Foreign Key, Unique, Not Null, Check

Create table myemployees.departments(

DID INTEGER PRIMARY KEY,

DEPT VARCHAR(10),

DEPTCODE INTEGER,

DHEAD VARCHAR(20)

);

 

INSERT INTO myemployees.departments values(1,’Sales’,109,’Sunil’);

 

INSERT INTO myemployees.employees VALUES(1,’Rohit Sharma’,’23456′,’rohit@rohit.com’,’1985-05-21′,21000.89,1);

 

INSERT INTO myemployees.employees (EID, ENAME, SALARY,DID) 

VALUES(2,’Virat Kohli’,20000.89,1);

 

select * from myemployees.employees;


”’
Database
Exception handling

”’
”’
https://www.geeksforgeeks.org/convert-python-script-to-exe-file/

”’
import csv
fname =
input(“Enter the filename: “)

fileobj =
open(fname)
csv_content = csv.reader(fileobj,
delimiter=“,”)
print(“Content on the screen: “)
for row in csv_content:
   
print(row)
fileobj.close()

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

”’
Converting Python program to exe:
https://www.datacamp.com/tutorial/two-simple-methods-to-convert-a-python-file-to-an-exe-file

”’

# from the tkinter library
from tkinter import *
# import filedialog module
from tkinter import filedialog
from tkinter import messagebox
import csv
# to read filename
filename1 = “”

def ReadCSV():

   
if filename1==“”:
        messagebox.showerror(
“File Process”,“Filename has not be selected”)
   
else:
       
print(“I am in Else!”)
        fileobj =
open(filename1)
        csv_content = csv.reader(fileobj,
delimiter=“,”)
       
print(“Content on the screen: “)
       
for row in csv_content:
           
print(row)
        fileobj.close()

# Function for opening the
# file explorer window
def browseFiles():
    filename = filedialog.askopenfilename(
initialdir=“/”,
                                         
title=“Select a File”,
                                         
filetypes=((“Text files”,
                                                     
“*.txt*”),
                                                     (
“all files”,
                                                     
“*.*”)))
   
filename1 = filename
   
# Change label contents
   
label_file_explorer.configure(text=“File Opened: ” + filename)


# Create the root window
window = Tk()

# Set window title
window.title(‘Selecting the file’)

# Set window size
window.geometry(“500×500”)

# Set window background color
window.config(background=“white”)

# Create a File Explorer label
label_file_explorer = Label(window,
                           
text=“File Explorer”,
                           
width=100, height=4,
                           
fg=“blue”)

button_explore = Button(window,
                       
text=“Browse Files”,
                       
command=browseFiles)

button_process = Button(window,
                       
text=“Analyze the file”,
                       
command=ReadCSV)

# specifying rows and columns
label_file_explorer.grid(column=1, row=1)
button_explore.grid(
column=1, row=2)
button_process.grid(
column=2, row=2)


#fname = input(“Enter the filename: “)



# Let the window wait for any events
window.mainloop()

##########  CSV File ##########
import csv
#fname = input(“Enter the filename: “)
diff_list=[]

fname1=
“D:/MyApp/B1.csv”
fname2=“D:/MyApp/B2.csv”

fileobj1 = open(fname1)
fileobj2 =
open(fname2)
csv_content1 = csv.reader(fileobj1,
delimiter=“,”)
csv_content2 = csv.reader(fileobj2,
delimiter=“,”)
print(“Content on the screen: “)

list_content1=[]
for row1 in csv_content1:
    list_content1.append(row1)

list_content2=[]
for row2 in csv_content2:
    list_content2.append(row2)


for row1 in list_content1:
   
for row2 in list_content2:
       
#print(“ROW 1 and ROW 2: “,row1,row2)
       
if row1[0]== row2[0] and row2[0]!=‘id’:
            temp_list = []
            temp_list.append(row1[
0])
           
for i in range(1,5):
               
if row1[i].isnumeric() and row2[i].isnumeric():
                    temp_list.append(
int(row1[i])-int(row2[i]))
            diff_list.append(temp_list)

print(“Final list:\n,diff_list)
fileobj1.close()
fileobj2.close()

#writing to a csv file
fileobj = open(“D:/MyApp/B3.csv”,“w”,newline=“”)
csv_writer = csv.writer(fileobj,
delimiter=‘,’)
csv_writer.writerow([
“ID”,“A”,“B”,“C”,“D”])
csv_writer.writerows(diff_list)
fileobj.close()

######

####  Exception handling  #####
## 3 types of errors:
# syntax errors
# logical errors
# runtime error = exception
#print(“Hello)
print(“Sum of 2 and 3 is “,2*3)

a=
5
b=10
if b==0:
   
print(“Cant move ahead”)
else:
   
print(“Division is “,a/b)

#
a = “6t”

b = 4
try:
    a =
int(a)
    c = a/b

except ZeroDivisionError:
   
print(“Divide by zero is not allowed”)
except ValueError:
   
print(“Numerator is not a valid number”)
except Exception:
   
print(“Some exception has occurred , not sure what”)
else:
   
print(“Value of c is”,c)

finally:
   
print(“I am in finally”)



# ZeroDivisionError: division by zero

“””
Connecting to MYSQL Database

“””
import pymysql
conn = pymysql.connect(user=“root”,host=“localhost”,password=“learnSQL”,database=“employees”)
#cursor is database object to take commands from python to DB
curobj = conn.cursor()

table1 = ”’
Create table books(
ID Integer Primary Key,
TITLE Text,
PRICE Integer
)
”’
#curobj.execute(table1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(1,”Python Programming”, 390)”’
#curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(2,”SQL Programming”, 290)”’
#curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(3,”Java Programming”, 490)”’
#curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(4,”C++ Programming”, 395)”’
#curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(5,”C# Programming”, 492)”’
#curobj.execute(insert1)

up1 = ”’Update Books set title=”Java Language Programming” where ID=3”’
curobj.execute(up1)
del1 = ”’Delete from Books where ID=4”’
curobj.execute(del1)
conn.commit()
select1 = “Select * from books;”
curobj.execute(select1)
output = curobj.fetchall()
#print(“Total rows = “,output)

for rows in output:
print(rows)


curobj.close()
## Database – type is RDBMS – relational database management system
#table is where data is stored
# server can have multiple databases:
#SQLITE3 free database, which comes with Python installation
# doesnt have username password, cant be accessed from outside

import sqlite3
conn = sqlite3.connect(“DECDATA.SQLITE”)
# SQLITE3 DB data types tutorial:
# https://www.sqlite.org/datatype3.html
table1 = ”’
Create table books(
ID Integer,
TITLE Text,
PRICE Integer
)
”’

#cursor is database object to take commands from python to DB
curobj = conn.cursor()
#curobj.execute(table1) – already run
“””
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(1,”Python Programming”, 390)”’
curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(2,”SQL Programming”, 290)”’
curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(3,”Java Programming”, 490)”’
curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(4,”C++ Programming”, 395)”’
curobj.execute(insert1)
insert1 = ”’Insert into books(ID,TITLE,PRICE) values(5,”C# Programming”, 492)”’
curobj.execute(insert1)
conn.commit()
“””
“””
up1 = ”’Update Books set title=”Java Language Programming” where ID=3”’
curobj.execute(up1)
conn.commit()
“””
“””
del1 = ”’Delete from Books where ID=4”’
curobj.execute(del1)
conn.commit()
“””

select1 = “Select * from Books”
output = curobj.execute(select1)
for rows in output:
print(rows[1],rows[2])
curobj.close()
Data Structure using Python
### MAPS – based on Dictionaries
import collections
d1 = {“name”:“Sachin”,“city”:“Mumbai”,“age”:50}
d2 = {“sports”:“Hockey”,“team”:“India”,“Goals”:322}

output = collections.ChainMap(d1,d2)
print(output.maps)

print(“Keys = “,list(output.keys()))
print(“Values = “,list(output.values()))
print(“Items = “,list(output.items()))

# in operator
print(“Is city in the map? “,“city” in output)
print(“Value at city = “,output[“city”])
#update
output[“city”] = “Hyderabad”
print(output)

# LINKED LIST: sequence of data elements
## Properties: Traverse, Insert & Delete
## Insert: Begining, Middle, End
class Node:
def __init__(self,data=None):
self.value = data
self.nextnode = None

class ListPointer:
def __init__(self):
self.head = None

#function to traverse through the Linked List
def traverse(self):
header = self.head
val=[]
while header is not None:
#print(“Value from the Linked List:”,header.value)
val.append(header.value)
header = header.nextnode
return val
# Inserting a node to the beginning of the linked List
def Insert_Beginning(self,newdata):
NewNode = Node(newdata)
NewNode.nextnode = self.head
self.head = NewNode
#Inserting at the end
def Insert_End(self,newdata):
NewNode = Node(newdata) #nextnode is null

#check if there is existing node or not
if self.head is None:
self.head = NewNode
return
last = self.head
while last.nextnode is not None:
last = last.nextnode
last.nextnode = NewNode

#Inserting Between 2 Nodes
def Insert_Between(self, betweennode, newdata):
if betweennode is None:
print(“The between Node is not available!”)
return

NewNode = Node(newdata)
NewNode.nextnode = betweennode.nextnode
betweennode.nextnode = NewNode

#Remove a node
def Remove(self,value):
# check for 3 conditions – First, Inbetween and Last

return


if __name__==“__main__”:
list = ListPointer() # created header
list.head = Node(“January”) # first node with value January and pointing to null
n2 = Node(“February”) # second node with value February and pointing to null
list.head.nextnode = n2 #first node is now linked to second node
n3 = Node(“March”) #third node with value March and pointing to null
n2.nextnode = n3 #second node is now linked to third node

# traversing through the list
print(list.traverse())

# add at the beginning
list.Insert_Beginning(“December”)
# traversing through the list
print(list.traverse())

#Add April at the end
list.Insert_End(“April”)
print(list.traverse())

list.Insert_Between(n3,“November”)
print(list.traverse())

#remove




# LINKED LIST: sequence of data elements
## Properties: Traverse, Insert & Delete
## Insert: Begining, Middle, End
class Node:
def __init__(self,data=None):
self.value = data
self.nextnode = None

class ListPointer:
def __init__(self):
self.head = None

#function to traverse through the Linked List
def traverse(self):
header = self.head
val=[]
while header is not None:
#print(“Value from the Linked List:”,header.value)
val.append(header.value)
header = header.nextnode
return val
# Inserting a node to the beginning of the linked List
def Insert_Beginning(self,newdata):
NewNode = Node(newdata)
NewNode.nextnode = self.head
self.head = NewNode
#Inserting at the end
def Insert_End(self,newdata):
NewNode = Node(newdata) #nextnode is null

#check if there is existing node or not
if self.head is None:
self.head = NewNode
return
last = self.head
while last.nextnode is not None:
last = last.nextnode
last.nextnode = NewNode

#Inserting Between 2 Nodes
def Insert_Between(self, betweennode, newdata):
if betweennode is None:
print(“The between Node is not available!”)
return

NewNode = Node(newdata)
NewNode.nextnode = betweennode.nextnode
betweennode.nextnode = NewNode

#Remove a node
def Remove(self,value):
# check for 3 conditions – First, Inbetween and Last
header = self.head
if header is None:
# Do nothing – linked list is blank
return

if header is not None:
if header.value == value: #first node in the list to be removed
self.head = header.nextnode
header = None
while header is not None:
if header.value == value:
break
previous = header #
header = header.nextnode
if header ==None: #you have reached last node and still value not matched
#simply it means no value matched
print(“Value not found in the linked list”)
return

previous.nextnode = header.nextnode
header = None

return


if __name__==“__main__”:
list = ListPointer() # created header
list.head = Node(“January”) # first node with value January and pointing to null
n2 = Node(“February”) # second node with value February and pointing to null
list.head.nextnode = n2 #first node is now linked to second node
n3 = Node(“March”) #third node with value March and pointing to null
n2.nextnode = n3 #second node is now linked to third node

# traversing through the list
print(list.traverse())

# add at the beginning
list.Insert_Beginning(“December”)
# traversing through the list
print(list.traverse())

#Add April at the end
list.Insert_End(“April”)
print(list.traverse())

list.Insert_Between(n3,“November”)
print(list.traverse())

#remove
list.Remove(‘December’) # first value
print(list.traverse())
list.Remove(“March1”)
print(list.traverse())



# Building Stack
# Push (last) and Pop (last)

# assignment: create a parent class for both Stack and Queue
## and implement display function
class Stack:
def __init__(self):
self.stack = []

def push(self, value):
#do not accept duplicate values
self.stack.append(value)
return
def pop(self):
# what is there is no element to remove – handle
self.stack.pop(-1)
return
def display(self):
#print(self.stack)
# handle empty
if len(self.stack) !=0:
return self.stack

class Queue:
def __init__(self):
self.queue = []

def add(self,value):
if value in self.queue:
print(“This value is already in Queue”)
return
self.queue.append(value)
def remove(self):
if len(self.queue)>0:
self.queue.pop(0)
else:
print(“Queue is empty!”)
def display(self):
if len(self.queue) !=0:
return self.queue


class DoubleLinkedList:
def __init__(self,data):
self.data = data
self.next = None
self.prev = None


def LRTraverse(self):
pass
def RLTravserse(self):
pass

if __name__ ==“__main__”:
import june2023 as st

mystack = st.Stack()
mystack.push(55)
print(mystack.display())
mystack.push(75)
print(mystack.display())
mystack.pop()
print(mystack.display())

myq = st.Queue()
myq.add(55)
print(myq.display())
myq.add(75)
print(myq.display())
myq.remove()
print(myq.display())

## DEQUEUE – Double ended queue
import collections

names = [‘Sachin’, ‘Virat’, ‘Dhoni’]
dq = collections.deque(names)
dq.pop()
print(dq)
dq.popleft()
print(dq)
dq.append()
#SORTING: BUBBLE, LINEAR, INSERTION, SHELL, SELECTION

list1 = [10,30,70,20,80,40,60,50]

for i in range(len(list1)-1):
for idx in range(0,len(list1)-1-i):
if list1[idx] > list1[idx+1]:
list1[idx],list1[idx+1] =list1[idx+1], list1[idx]

print(“Bubble Sorted List = “,list1)

list1 = [10,30,70,20,80,40,60,50]
for i in range(len(list1)-1):
for idx in range(i+1,len(list1)):
if list1[i] > list1[idx]:
list1[i],list1[idx] =list1[idx], list1[i]

print(“Linear Sorted List = “,list1)

list1 = [10,30,70,20,80,40,60,50]

def merge(left,right):
result = []
while len(left)!=0 and len(right)!=0:
if left[0] > right[0]:
result.append(right[0])
right.remove(right[0])
else:
result.append(left[0])
left.remove(left[0])
if len(left) ==0:
result+=right
else:
result+=left
return result
def divide(list1):
if len(list1)<2:
#print(“Merge Sort: “,list1)
return list1
else:
center = len(list1) //2
left_list = list1[:center]
right_list = list1[center:]
left_list = divide(left_list)
right_list = divide(right_list)
return list(merge(left_list,right_list))

print(“List 1 before Merge Sort: “,list1)
list1 = divide(list1)
print(“List 1 after Merge Sort: “,list1)
list1 = [80,70,60,50,40,30,20,10]

#for k in range(len(list1)-1):
for i in range(1,len(list1)):
j = i-1 #represents sorted array
next = list1[i] # unsorted array
while list1[j] > next and j>=0:
list1[j+1] = list1[j]
j=j-1

list1[j+1] = next

print(“Insertion Sort: “,list1)

#Shell sort
list1 = [80,70,60,50,40,30,20,10]
gap = len(list1) // 2

while gap >0:
for i in range(gap,len(list1)):
value = list1[i]
j=i #tracker
while j>=gap and list1[j-gap] > value:
list1[j] = list1[j-gap]
j=j-gap
list1[j] = value
gap=gap//2
print(list1)

## Shell sort: o(n square) BEST O(nlongn) worst case
# Search
l1 = [10,50,30,70,40,20]
num = 10
idx = 0
found = False
for i in range(len(l1)):
if num==l1[i]:
found=True
idx = i
break

if found:
print(f”{num} is in the list at {idx})
else:
print(f”{num} is not in the list”)

#
num = 10
idx = 0
found = False
while idx<len(l1) and found is False:
if num==l1[idx]:
found=True
else:
idx+=1
if found:
print(f”{num} is in the list at {idx})
else:
print(f”{num} is not in the list”)


# Binary Search: works on sorted list
l1 = [10,50,30,70,40,20]
l1.sort() #sort the list before use
print(l1)
num = 10
idx = 0
found = False
low,high = 0, len(l1)
while low<high and found is False:
mid = (low+high)//2
if l1[mid] == num:
idx=mid
found=True
elif l1[mid] > num:
high = mid-1
else:
low = mid+1
if found:
print(f”Binary Search: {num} is in the list at {idx})
else:
print(f”Binary Search: {num} is not in the list”)

## BINARY TREE

class Node:
def __init__(self,key):
self.value = key
self.left = None
self.right = None
#Preorder traversal
def pretraverse(self):
print(self.value, end=“, “)
if self.left:
self.left.pretraverse()
if self.right:
self.right.pretraverse()
def intraverse(self):
if self.left:
self.left.intraverse()
print(self.value, end=“, “)
if self.right:
self.right.intraverse()


root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.pretraverse()
print(\n In traverse:”)
root.intraverse()
# Quick sort: divide and conquer
## pick a pivot element:
# 1. first number is pivot
# 2. last number is pivot
# divide the list based on this pivot value:
# left list (numbers < pivot) and right list (numbers > pivot)
l1 = [90,80,70,60,50,40,30,20,10]
def QuickSortAlgo(l1, low,high):
if low < high:
pivot = l1[high]
##logic to partition the dataset into 2 parts
i=low-1
for j in range(low, high):
if l1[j] <= pivot:
i+=1
l1[i],l1[j] = l1[j],l1[i]
l1[i+1], l1[high] = l1[high], l1[i+1]
pivot = i+1
##

#Partioning the left sub-list
QuickSortAlgo(l1,low,pivot-1)

# Partioning the right sub-list
QuickSortAlgo(l1, pivot + 1, high)



size = len(l1)
QuickSortAlgo(l1, 0,size-1)
print(“Sorted list: \n,l1)

def sum_n(n):
if n==0:
return 0
else:
return n + sum_n(n-1)

ans = sum_n(10)
print(ans)

# 10 + 9 + 8 + .. 0

def fibo(n):
if n <=1:
return n
else:
return fibo(n-2) + fibo(n-1)

# fibo(4)= fibo(2) + fibo(3) = 0 + 1 + 1 + 0 + 1 = 3
for i in range(15):
print(fibo(i))

def toh_rec(n_disk, source,inter, target):
if n_disk ==1:
print(f”Move disk 1 from Tower {source} to Tower {target})
return
toh_rec(n_disk-1, source,target, inter)
print(f”Move disk {n_disk} from Tower {source} to Tower {target})
toh_rec(n_disk – 1, inter, source, target)

disks = 3
toh_rec(disks, “Source”,“Inter”,“Target”)

## Backtracking
# CIRCULAR DOUBLY LINKED LIST

class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

class CircularDLL:
def __init__(self):
self.first = None
def getnode(self,index):
current = self.first
for i in range(index):
current = current.next
if current == self.first:
return None
return current
def insertafter(self, beforenode, newnode):
newnode.prev = beforenode
newnode.next = beforenode.next
newnode.next.prev = newnode
beforenode.next = newnode

def insertbefore(self,beforenode, node):
self.insertafter(beforenode.prev, node)

def insertbegin(self,node):
self.insertend(node)
self.first = node

def insertend(self,node):
if self.first is None:
self.first = node
node.next = node
node.prev = node
else:
self.insertafter(self.first.prev)

def remove(self):
pass

def display(self):
pass

#Main Menu
print(“Type the operation you want to perform: “)
print(“insert <data> after <index>”)
print(“insert <data> before <index>”)
print(“insert <data> at beginning”)
print(“insert <data> at end”)
print(“remove <index>”)
print(“display”)
print(“quit”)
cdll = CircularDLL()
while True:
print(“The values in the Circular Doubly Linked List are: “)
cdll.display()
ch=input(“What do you want to do?”)
ch = ch.lower().strip().split()
print(“CH = “,ch)
if ch[0]==“insert”:
value = int(ch[1])
newNode = Node(value)

if ch[2]==“after”:
data = cdll.getnode(int(ch[3]))
if data is None:
print(“Given index doesnt exist”)
else:
cdll.insertafter(data, newNode)

elif ch[2]==“before”:
cdll.insertbefore(newNode)
elif ch[2]==“at”:
if ch[3]==“beginning”:
cdll.insertbegin(newNode)

elif ch[3]==“end”:
cdll.insertend(newNode)
else:
print(“Error! Try again!”)
continue
else:
print(“Error! Try again!”)
continue
elif ch[0]==“remove”:
idx = int(ch[1])
idx_node = cdll.getnode(idx)
if idx_node is None:
print(“No such index, cant perform remove option.”)
continue
# if condition is false
cdll.remove()
elif ch[0]==“display”:
print(“The values in the Circular Doubly Linked List are: “)
cdll.display()

elif ch[0]==“quit”:
break
else:
print(“Invalid option, try again!”)
# CIRCULAR DOUBLY LINKED LIST

class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

class CircularDLL:
def __init__(self):
self.first = None
def getnode(self,index):
current = self.first
for i in range(index):
current = current.next
if current == self.first:
return None
return current
def insertafter(self, beforenode, newnode):
newnode.prev = beforenode
newnode.next = beforenode.next
newnode.next.prev = newnode
beforenode.next = newnode

def insertbefore(self,beforenode, node):
self.insertafter(beforenode.prev, node)

def insertbegin(self,node):
self.insertend(node)
self.first = node

def insertend(self,node):
if self.first is None:
self.first = node
node.next = node
node.prev = node
else:
self.insertafter(self.first.prev,node)

def remove(self, node):
if self.first.next == self.first:
self.first = None
else:
node.prev.next = node.next
node.next.prev = node.prev
if self.first == node:
self.first = node.next # node.prev

def display(self):
if self.first is None:
print(“NO VALUES”)
return
current = self.first
print(\n)
while True:
print(current.data, end=‘, ‘)
current = current.next
if current == self.first:
break
print(\n)
#Main Menu
print(“Type the operation you want to perform: “)
print(“insert <data> after <index>”)
print(“insert <data> before <index>”)
print(“insert <data> at beginning”)
print(“insert <data> at end”)
print(“remove <index>”)
print(“display”)
print(“quit”)
cdll = CircularDLL()
while True:
print(“The values in the Circular Doubly Linked List are: “)
cdll.display()
ch=input(“What do you want to do?”)
ch = ch.lower().strip().split()
print(“CH = “,ch)
if ch[0]==“insert”:
value = int(ch[1])
newNode = Node(value)

if ch[2]==“after”:
data = cdll.getnode(int(ch[3]))
if data is None:
print(“Given index doesnt exist”)
else:
cdll.insertafter(data, newNode)

elif ch[2]==“before”:
ref_node = cdll.getnode(int(ch[3]))
cdll.insertbefore(ref_node,newNode)
elif ch[2]==“at”:
if ch[3]==“beginning”:
cdll.insertbegin(newNode)

elif ch[3]==“end”:
cdll.insertend(newNode)
else:
print(“Error! Try again!”)
continue
else:
print(“Error! Try again!”)
continue
elif ch[0]==“remove”:
idx = int(ch[1])
idx_node = cdll.getnode(idx)
if idx_node is None:
print(“No such index, cant perform remove option.”)
continue
# if condition is false
cdll.remove(idx_node)
elif ch[0]==“display”:
print(“The values in the Circular Doubly Linked List are: “)
cdll.display()

elif ch[0]==“quit”:
break
else:
print(“Invalid option, try again!”)

## Assignment: Modify above code to create CircularQueue

## ## PRIORITY QUEUE ## ##
#Priority Queue
from queue import PriorityQueue
apq = PriorityQueue()
apq.put(50)
apq.put(30)
apq.put(40)

print(“Printing elements of Priority Queue:\n)
while not apq.empty():
print(apq.get(), end=“, “)
print()
print(“Adding values with Priority:”)
apq = PriorityQueue()
apq.put((5, “Study”)) # combination of priority and values are added as tuple
apq.put((2, “Sleep”))
apq.put((9, “Eat”))
apq.put((4, “Play”))
print(“2. Printing elements of Priority Queue:\n)
while not apq.empty():
print(apq.get(), end=“, “)
print()

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

class BST:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def bst_insert(node,key):
if node is None:
return BST(key)
if node.value <key:
node.right = bst_insert(node.right, key)
if node.value >key:
node.left = bst_insert(node.left, key)
return node

def bst_search(node, key):
if node is None or node.value == key:
return root

if node.value < key:
return bst_search(node.right, key)
if node.value > key:
return bst_search(node.left, key)

# 50, 30, 10, 70, 90, 80, 15, 25, 35
if __name__ == “__main__”:
root = None
root = bst_insert(root,50)
bst_insert(root,30)
bst_insert(root, 10)
bst_insert(root, 70)
bst_insert(root, 90)
bst_insert(root, 80)
bst_insert(root, 15)
bst_insert(root, 25)
bst_insert(root, 35)

key = 70
isFound = bst_search(root,key)
if isFound is None:
print(“Value “,key,“not in the BST!”)
else:
print(“Value”,key,” is found in the BST!”)

key = 75
isFound = bst_search(root, key)
if isFound is None:
print(“Value”,key,” not in the BST!”)
else:
print(“Value”,key,“is found in the BST!”)

# insertion, deletion and displaying the tree
# left, node, right
Mini Python Projects

BILLING PROJECT USING CSV FILE

file = “expenses.csv”
import csv
def add_details():
while True:
code = input(“Enter Item Code:”)
desc = input(“Enter Description:”)
amount = float(input(“Enter Amount: “))
fileobj = open(file,“a”,newline=“”)
csv_writer = csv.writer(fileobj, delimiter=“,”, quotechar=‘”‘,quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow([code,desc,amount])
fileobj.close()
ch=input(“Enter y to continue adding more items:”)
if ch!=‘y’:
break
def view_details():
print(\n\n)
sno = “S.No.”
code = “Code”
desc = “Description”
amt = “Amount”
total = 0
print(“-“*55)
print(f”{sno:<6} {code: <6} {desc:<30} {amt:<6})
print(“-” * 55)
fileobj = open(file, “r”)
csv_reader = csv.reader(fileobj,delimiter=“,”)
line=0
for data in csv_reader:
line+=1
print(f”{line:<6},end=” “)
for i in data:
if data.index(i)==1:
print(f”{i:<30},end=” “)
else:
print(f”{i:<6}, end=” “)
if data.index(i)==2:
total+=float(i)
print()
print(“-” * 55)
print(” “*36,“Total: “,total)
print(“-” * 55)
fileobj.close()

print(\n)

def edit_info():
fileobj = open(file, “r”)
csv_reader1 = csv.reader(fileobj, delimiter=“,”)
#print(list(csv_reader1))
all_data = list(csv_reader1)
code = input(“Enter the Item Code to delete:”)
for row in all_data:
if code in row:
print(“Found!”)
idx = all_data.index(row)
all_data.pop(idx)
fileobj.close()
print(“Final data to push into csv: “,all_data)
fileobj = open(file, “w”, newline=“”)
csv_writer = csv.writer(fileobj, delimiter=“,”, quotechar=‘”‘, quoting=csv.QUOTE_MINIMAL)
for r in all_data:
csv_writer.writerow(r)
fileobj.close()


while True:
print(“MENU”)
print(“1. Add Details\n2. View Details\n3. Calculate Total\n4. Edit the data\n5. Exit”)
ch= input(“Choose your option:”)
if ch==“1”:
add_details()
elif ch==“2”:
view_details()
elif ch==“3”:
pass
elif ch==“4”:
edit_info()
elif ch==“5”:
break
else:
print(“Try again!”)
Mastering Data Science – June 2023

Mastering Data Science Course - JUNE 2023

# comments – ignored by the compiler/interpreter
# Python is case sensitive language
print(5 + 5) # inbuilt function – prints whatever you give to it on the screen
print(“5 + 5”) # “” – used for repeating by print
print(“5 + 5 =”,5+5)
print(‘3 + 5 =’,5+5)

# variables
x = 777
y = 75
print(x+y)
print(x,“+”,y)
print(x,“+”,y,“=”,x+y)
print(x,‘+’,y,‘=’,x+y)
# variables naming rule: it should start with an alphabet, it can have numbers and _
num = 10
num1 = 101
n1m2 = 56
first_number = 56

# write a program to add 2 numbers
num1 = 45
num2 = 66
print(“Sum of two numbers is”,num1+num2)

x = 45
y = 66
print(“Sum of two numbers is”,x+y)


sdfdsagtarebgdvdzdczvdv = 45
dfsgdfsgbsysbsdfhnjdjshff = 66
print(“Sum of two numbers is”,sdfdsagtarebgdvdzdczvdv+dfsgdfsgbsysbsdfhnjdjshff)

# write a program to calculate area and perimeter of a rectangle, when their sides are given
# input -> process -> output
# area = length * breadth
# perimeter = 2 * (length + breadth)
# input:
length = 52
breadth = 31
#process:
area = length * breadth;
perimeter = 2*(length + breadth);
#output
print(“Area of the rectangle is”,area);print(“The perimeter is”,perimeter)

########### Assignment ################
# WAP to calculate area and circunference of a circle when radius is given
# WAP to calculate perimeter of a triangle who 3 sides are given
##########################################

# Basic data types – 5 types
length = 30 # int=integer = numbers without decimal values
print(“type of data = “,type(length)) # <class ‘int’>
# type() – gives the type of the data that is in the variable

length = 29.8 # float = decimal values
print(“type of data = “,type(length)) # <class ‘float’>

number1 = 5+2j
print(“type of data = “,type(number1)) #<class ‘complex’>
# imaginary numbers are the root of -ve numbers = i
print(“Doing a calculation = “, number1 * (52j)) # (a+b)(a-b) = a square – b square
# 25 – (-4) = 29 + 0j

# bool (boolean)- True (1) & False (0)
val1 = True # False
print(“type of data = “,type(val1))

#text -str = string
name = “Sachin Tendulkar”
print(“type of data = “,type(name))

########### Assignment ################
# WAP to calculate area and circunference of a circle when radius is given
# WAP to calculate perimeter of a triangle who 3 sides are given
##########################################

# Basic data types – 5 types

cost_price = 45
selling_price = 67
quantity = 78
profit = (selling_price – cost_price) * quantity
print(“Total profit after selling”,quantity,“items bought at”,cost_price,
“and sold at”,selling_price,“is”,profit,end=\n)
print(f”Total profit after selling {quantity} items bought at {cost_price}
f”and sold at {selling_price} is {profit},end=\n) #f string
print(“Total profit after selling {} items bought at {} and sold at “
“{} is {}”.format(quantity, cost_price,selling_price,profit))
print(“Total profit after selling {0} items bought at {2} and “
“sold at {3} is {1}”.format(quantity, profit,cost_price,selling_price))

quantity = int(“14”)
# int(), str(), bool(), float(), complex()
total_cost = 500
cost_per_quantity = total_cost/quantity
# implicit and explicit conversion
print(f”Cost per items for quantity {quantity} and total cost {total_cost} is {cost_per_quantity:.2f})

player = “Kohli”
country = “India”
position =“Opener”
print(f”This is {player:<15} who plays for {country:>15} as {position:^20} in cricket.”)

player,country,position = “Mubwangwa”, “Zimbabwe”,“Wicket-Keeper”
print(f”This is {player:<15} who plays for {country:_>15} as {position:.^20} in cricket.”)

print(“I am fine \nhow are \nyou”) #escape character (works only inside quotes) \n, \t
# \n will create newline
print(\\n will create newline”)
# \\n will create newline
print(\\\\n will create newline”)

print(f”{player} \n{country} \n{position})
print(“First line content”,end=” – “)
print(“Second line content”,end=\n)

value = 50
print(“Decimal number = “,int(value))
print(“Hexa number = “,hex(value)) #0x
print(“Octal number = “,oct(value)) #0o
print(“Binary number = “,bin(value)) #0b

value = 0b110011001100
print(“Decimal number = “,int(value))
print(“Hexa number = “,hex(value)) #0x
print(“Octal number = “,oct(value)) #0o
print(“Binary number = “,bin(value)) #0b

#### Operations –
##Arithmematic operations
val1, val2 = 54, 5
print(val1 + val2) # 55
print(val1 – val2) #
print(val1 * val2) #
print(val1 / val2) #

print(val1 // val2) #integer division – will return only the integer part
print(val1 ** val2) # power (raise to)- 54 to the power of 5
print(54 ** (1/2)) #square root of 54
print(val1 % val2) # modulo – remainder

#Relational (comparison operator): Output is always a bool value
# == != < > <= >=
val1,val2 = 15, 15
print(val1 == val2) # is val1 equal to val2 ? – T
print(val1 != val2) # F
print(val1 < val2) # F
print(val1 > val2) # F
print(val1 <= val2) # T
print(val1 >= val2) # T

## Logical operations

#Arithematic: + – * / // ** %
#Comparison: > < >= <= == !=
#Logical: and or not: input and output both values are boolean
# Prediction 1: Sachin or Sehwag will open the batting
# Prediction 2: Sachin and Sourav will open the batting
# Actual: Sachin and Rahul opened the batting

print(True or False) # OR gives True even if one value is True
print(True and False) # AND gives False even if one value is False
print(5 > 6 and 8<10) # False and True
print(3 + 4 * 4)
val1,val2,val3 = 10,15,10
print(val1 > val2 and val2 ==val3 or val3==val1 and val1!=val3 and val2>=val3 or val3 !=val1)
print(3 + 5 * 2 6 * 3 + 5 3 * 4 / 3)

#BIT WISE Operators: and (&) or (|), left shift and right shift
print(50 & 30)
print(bin(50), bin(30))
print(int(0b10010))
print(50 | 30)
print(int(0b111110))

print(312 >> 3)
print(12 << 3)

########### Refer the document for assignment ########
# Conditions – if, elif else
# Pass or Fail
print(“PASS”)
print(“FAIL”)

avg = 5
if avg >=35:
print(“PASS”)
print(“Congratulations on great result”)
else:
print(“Result: FAIL”)
#if avg > 90 – A+, 80 -A, 70: B+, 60: B-, 50: C, >35: D, <35: E
avg = float(input(“Enter the average value of the student: “))
if avg>=90:
print(“Super Duper Result: A+”)
elif avg>=80:
print(“Grade: A-“)
elif avg>=70:
print(“Grade: B+”)
elif avg >=60:
print(“Grade: B-“)
elif avg >=50:
print(“Grade: C”)
elif avg >=35:
print(“Grade: D”)
else:
print(“Grade: E”)

### input()
val = int(input(“Enter a number: “)) #takes value from the user

print(val)
print(type(val))
# by default input() takes in value as string

#### Take marks of 5 subjects from the user and calculate sum and average and then assign the grade
# based on above discussion
”’
Write a program to input a value from the user and check if its positive, negative or zero.
if its positive, then check if its odd or even. For even numbers, check if they are multiple of 4
”’
#Nested if conditions
num = int(input(“Enter a number: “))
if num >0:
print(num,“is positive”,end=“”)
if num %2 ==0:
print(” and also even”)
if num%4==0:
print(num,“is divisible by 4.”)
else:
print(” and also odd”)
elif num<0:
print(num, “is negative”)
else:
print(num,“is neither positive not negative”)

#Assignment: Write a program to input 3 sides of a triangle anc check
#if they are: right angled, isoceles, scalene or equilateral

# short form of one line if-else condition
a,b = 10,5
if a>b:
print(a,” is greater”)
else:
print(b, ” is greater”)

#short
result = a if a>b else b

print(result,“is greater”)

## let’s take example of 3 values and check which number is highest
a,b,c = 500,100,500
if a>b:
if a>c:
print(a,“is highest”)
else:
print(c, “is highest”)
else: # b is greater than a
if b>c:
print(b,“is highest”)
else:
print(c, “is highest”)

result = a if a>b else b
result = result if result>c else c
print(result,“is highest – II”)

if a>=b and a>=c:
print(a,“is highest – III”)
elif b>=a and b>=c:
print(b,“is highest – III”)
else:
print(c,“is highest – III”)
# LOOPS:
#1. FOR – when you know exactly how many times to repeat
#2. WHILE – when you dont know exactly how many times but you know when to stop

# range() generates range of values from given =position to the <final position with given increment
range(1,49,6) #range(=start,<end,increment): 1,7,13,19,25,31,37,43
range(5,11) #range(start,end), increment is default = 1: 5,6,7,8,9,10
range(5) # range(end), start default = 0, increment=1: 0,1,2,3,4
# for loop using range
for variable in range(1,49,6):
print(“=================”)
print(“HELLO : “,variable )

for variable in range(5,11):
print(“xxxxxxxxxxxxxxxxx”)
print(“HELLO : “,variable )

for variable in range(5):
print(“………..”)
print(“HELLO : “,variable )

# for counter in range_of_values
# while true condition
final,start = 5,0
while start< final:
print(“I am in while now, start = “,start)
start +=1 #start = start+1
print(“Thats it for today”)
n=10
“””
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
“””
for j in range(n):
for i in range(n):
print(“*”,end=” “)
print()

”’
*
* *
* * *
* * * *
* * * * *
”’
#z=0
for j in range(n):
#z+=1 # z=z+1
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(n-j-1):
print(” “,end=“”)
for i in range(j+1):
print(“*”,end=” “)
print()

##### Assignment ##########
#Print below pattern:
“””
* * * * *
* * * *
* * *
* *
*
“””
## assignment code
”’
A pattern

”’
n=15
for j in range(n):
for k in range(n-j-1):
print(” “,end=“”)
if j==0 or j==int(round(n/2)):
for i in range(j+1):
print(“* “,end=“”)
else:
print(“*”,end=“”)
print(” “*j,end=“”)
print(“*”,end=“”)
print()

#### Assignment
# Practice patterns A to Z

for num in range(1,11):
for i in range(1,11):
print(f”{i:>2} * {num:>2} = {num*i: >2},end=” “)
print()

user_says = “y”
while user_says ==“y”:
print(“Hello”)
user_says = input(“Enter y to print again or any other key to stop:”)

check = True
while check:
print(“Hello Again”)
user_says = input(“Enter y to print again or any other key to stop:”)
if user_says!=“y”:
check = False

while True:
print(“Hello Again Again”)
user_says = input(“Enter y to print again or any other key to stop:”)
if user_says!=“y”:
break

user_says = input(“Enter y to print again or any other key to stop:”)
while user_says ==“y”:
print(“Hello”)
user_says = input(“Enter y to print again or any other key to stop:”)

st,end,co=11,100,11
for i in range(st,end,co):
if end%co==0:
if i == (end // co-1) * st:
print(i)
else:
print(i, end=“, “)
else:
if i==end//co*st:
print(i)
else:
print(i, end=“, “)

st,end,co=11,100,11
for i in range(st,end,co):
if i+co>=end:
print(i)
else:
print(i, end=“, “)

”’
Find 10 multiples of even number
If the number is negative then do not execute
if you come across multiple value as a multiple of 12 then skip it
if you come across multiple value as a multiple of 20 then stop it
”’
while True:
num = int(input(“Enter a number: “))
if num>=0:
for i in range(1,11):
if num*i%12 ==0:
continue
if num*i%20 ==0:
break
print(f”{num} * {i} = {num*i})
ch=input(“Hit any key to stop: “)
if len(ch)!=0:
break
#ATM Transaction
while True:
print(“Enter your choice from below menu:”)
print(“1. Deposit \n2. Withdraw \n3. Account Transfer \n4. Statement \n5. Quit”)
ch = input(“Enter your option here: “)
if ch==“1”:
ch=input(“Enter the amount: “)
print(“… continue witht he logic”)
elif ch==“2”:
print(“Logic is not ready”)
elif ch==“3”:
print(“Logic is not ready”)
elif ch==“4”:
print(“Logic is not ready”)
elif ch==“5”:
print(“Thank you for Banking with us, Have a Good Day!”)
break
else:
print(“Invalid Option, please try again!”)
continue


# Check for Prime
is_prime = True
for i in range(2,num//2+1):
if num %i==0:
is_prime = False
break
if is_prime:
print(f”{num} is a Prime number”)
else:
print(f”{num} is not a Prime number”)

# generate prime numbers between 5000 and 10000

for num in range(5000,10001):
is_prime = True
for i in range(2,num//2+1):
if num %i==0:
is_prime = False
break
if is_prime:
print(f”{num} ,end=“, “)



# Assignments:
## 1. WAP to generate first 5 fibonacci numbers: 0,1, 1, 2,3,5,8,13,21…
# guess the number: Computer v Human
import random

num = random.randint(1,100)
#print(“Number = “,num)
#print(“===============”)
counter = 0
while True:
guess = int(input(“Guess the number (1-100): “))
if guess <1 or guess>100:
print(“Invalid number! Try again…”)
continue
counter+=1 #counter = counter + 1
if guess == num:
print(f”Congratulations! You have successfully guessed the number is {counter} steps.”)
break
elif guess < num:
print(“Sorry! Your guess is lower. Try again…”)
else:
print(“Sorry! Your guess is higher. Try again…”)

############
# guess the number: Computer v Computer
import random

num = random.randint(1,100)
print(“Number = “,num)
print(“===============”)
counter = 0
st,en = 1,100
while True:
#guess = int(input(“Guess the number (1-100): “))
guess = random.randint(st,en)
print(“Guess number = “,guess)
if guess <1 or guess>100:
print(“Invalid number! Try again…”)
continue
counter+=1 #counter = counter + 1
if guess == num:
print(f”Congratulations! You have successfully guessed the number is {counter} steps.”)
break
elif guess < num:
print(“Sorry! Your guess is lower. Try again…”)
st=guess+1
else:
print(“Sorry! Your guess is higher. Try again…”)
en=guess-1

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

# guess the number: Computer v Human (automation)
import random

num = random.randint(1,100)
print(“Number = “,num)
print(“===============”)
counter = 0
st,en = 1,100
while True:
guess = (st+en)//2
#guess = random.randint(st,en)
print(“Guess number = “,guess)
if guess <1 or guess>100:
print(“Invalid number! Try again…”)
continue
counter+=1 #counter = counter + 1
if guess == num:
print(f”Congratulations! You have successfully guessed the number is {counter} steps.”)
break
elif guess < num:
print(“Sorry! Your guess is lower. Try again…”)
st=guess+1
else:
print(“Sorry! Your guess is higher. Try again…”)
en=guess-1

###########################
# guess the number: Computer v Human (automation)
import random
import time
TOTAL = 0
max = 100000
start_time = time.time()
for i in range(max):
num = random.randint(1,100)
#print(“Number = “,num)
#print(“===============”)
counter = 0
st,en = 1,100
while True:
guess = (st+en)//2
#guess = random.randint(st,en)
print(“Guess number = “,guess)
if guess <1 or guess>100:
#print(“Invalid number! Try again…”)
continue
counter+=1 #counter = counter + 1
if guess == num:
print(f”Congratulations! You have successfully guessed the number is {counter} steps.”)
TOTAL +=counter
break
elif guess < num:
#print(“Sorry! Your guess is lower. Try again…”)
st=guess+1
else:
#print(“Sorry! Your guess is higher. Try again…”)
en=guess-1
end_time = time.time()
print(“Total steps = “,TOTAL)
print(“Average number of steps = “,TOTAL/max,” total time taken to run”,max,“steps is”,(end_time – start_time))
# Average number of steps = 5.80429 total time taken to run 100000 steps is 36.42937159538269

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

# guess the number: Computer v Computer (measure steps)
import random
import time
TOTAL = 0
max = 100000
start_time = time.time()
for i in range(max):
num = random.randint(1,100)
#print(“Number = “,num)
#print(“===============”)
counter = 0
st,en = 1,100
while True:
#guess = (st+en)//2
guess = random.randint(st,en)
print(“Guess number = “,guess)
if guess <1 or guess>100:
#print(“Invalid number! Try again…”)
continue
counter+=1 #counter = counter + 1
if guess == num:
print(f”Congratulations! You have successfully guessed the number is {counter} steps.”)
TOTAL +=counter
break
elif guess < num:
#print(“Sorry! Your guess is lower. Try again…”)
st=guess+1
else:
#print(“Sorry! Your guess is higher. Try again…”)
en=guess-1
end_time = time.time()
print(“Total steps = “,TOTAL)
print(“Average number of steps = “,TOTAL/max,” total time taken to run”,max,“steps is”,(end_time – start_time))
# Average number of steps = 7.4783 total time taken to run 100000 steps is 28.571797370910645
# STRINGS
txt1 = “HELLO”
txt2 = ‘HI’
print(txt1, txt2)
txt3 = ”’I am fine”’
txt4 = “””I am here”””
print(txt3, txt4)

txt5 = ”’I am fine here
Hope you are
doing well
too. So how are you”’
txt6 = “””I am here
Where are you?
How are you?”””
print(txt5)
print(txt6)

# I’m fine
print(“I’m fine”)
# He asked,”How are you?”
print(‘He asked,”How are you?”‘)
# \ – escape sequence
print(“I am \nfine”)
print(‘I\’m fine’)

txt7 = “HELLO”
print(len(txt7))
print(txt7[1])
print(txt7[1],txt7[4])
print(txt7[1:4]) #ELL
print(txt7[:4]) #HELL
print(txt7[2:]) #LLO
print(txt7[:]) #HELLO

txt8 = input(“Enter a text:”)
length = len(txt8)
print(txt8[-1], txt8[length-1]) #last character
print(txt8[0], txt8[-length]) #first character
print(txt8[1:4], txt8[-4:-1]) # ELL
print(txt8[:4], txt8[-5:-1], txt8[:-1]) # HELL
print(txt8[1:], txt8[1:5], txt8[-4:]) # ELLO

a = “HELLO”
b = “THERE”
print(a +” “+ b)
c = (a+” “) * 4
print(c)
print(c[:-1])
#a[0]=”h” #’str’ object does not support item assignment – IMMUTABLE
a = “h”+a[1:]
print(a)

for i in b:
print(i)

for j in range(len(b)):
print(j, b[j])

### ### String Methods #####
a= “heLLLLLLLLLlllo”
## Refer String Methods – https://docs.python.org/3.11/library/stdtypes.html#string-methods
print(a.islower())
print(a.lower())

ch = input(“Enter Yes to continue: “)
if ch.upper()==“YES”:
print(“YES”)

num1 = input(“Enter a number: “)
if num1.isdigit():
num1 = int(num1)
else:
print(“Exiting because of invalid number”)
# String methods
str1 = “Hello How Are You?”
print(“Capitalize the content: “,str1.capitalize())
print(str1.isalpha())
print(“”.join(str1.split(” “)))
str1_1 = “”.join(str1.split(” “))
print(“is alpha for joined text: “,str1_1.isalpha())
if str1_1.isalpha():
print(“Your name has been saved into the database”)
else:
print(“Invalid name, try again!”)

str2 = ” name “
print(str2.isspace())
print(“Strip txt:”,str2.strip())
print(“Strip txt:”+str2.strip())

str3 = “Hello How Are You are you doing you are?”
print(“count of are:”,str3.lower().count(“are”, 0, 21))
print(“Finding find:”, str3.find(“find”)) #returns the position of first occurrence
print(“Finding find:”, str3.find(“you”))
print(“Finding find:”, str3.find(“you”,23,35))

## Assignment – find the position of all the occurrences and print the output in below format:
# found 2 words and they are at positions 22 and 32

print(str3.startswith(“Hello How”))
print(str3.endswith(“?”))

# Replace –
print(str3.replace(“you”,“YOUR”))
print(str3.replace(“you”,“YOUR”, 1))
print(str3[:24]+str3[24:].replace(“you”,“YOUR”, 1))
print(str3) # original text will not change becasue of any method as strings are immutable
# List: linear ordered mutable collection
list1 = [20,30.0, True, “Hello”, [2,3,5]]
print(“Datatype: “, type(list1))
print(“Number of values = “,len(list1))
print(list1[0], type(list1[0]))
print(list1[-1], type(list1[-1]))

print([1,2,3]+[10,20,30])
print([1,2,3] *3)

for i in list1:
print(i)

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

for i in list1[-1]:
print(i)

list1[0] = “Twenty”
print(list1)

#Tuple: linear ordered immutable collection
t1 = (20,30.0, True, “Hello”, [2,3,5])
print(type(t1))
print(t1[0],t1[-1])
#t1[0]= 5 # ‘tuple’ object does not support item assignment

# you can convert list to tuple and tuple to list
l1 = [3,5,9]
print(type(l1))
l1 = tuple(l1)
print(type(l1))
l1 = list(l1)
print(type(l1))

t1 = (2,100,1000,10000,100)
t2 = (2,100,1000,10000,100000)
if t1 > t2:
print(“T1”)
elif t2 > t1:
print(“T2”)
else:
print(“They are equal”)

print(“Count = “,t1.count(100))
print(“Index = “,t1.index(100))

l1 = list(t1)
print(“Count = “,l1.count(100))
print(“Index = “,l1.index(100))

l1.append(99)
l1.append(199)
l1.append(299)
print(“L1 after append= “,l1)

#insert & append – both will add members to the list
l1.insert(2,555)
print(l1)
l1.insert(200,444)
print(l1)
# FIFO – Queue
total = 0
marks=[]
for i in range(5):
m1 = int(input())
marks.append(m1)
total += m1
print(“Marks obtained are:”,marks)
print(“Total = “,total)
#Marks obtained are: [50, 60, 70, 80, 90]
# LIFO – stack
total = 0
marks=[]
for i in range(5):
m1 = int(input())
marks.insert(0,m1)
total += m1
print(“Marks obtained are:”,marks)
print(“Total = “,total)
#Marks obtained are: [90, 80, 70, 60, 50]

# using sum() to do total


marks=[]
for i in range(5):
m1 = int(input())
marks.append(m1)
total = sum(marks)
print(“Marks obtained are:”,marks)
print(“Total = “,total)

l1 = [90, 80, 70, 60, 50]
idx =10
if len(l1) >idx:
l1.pop(10) #default is -1: pop works on index
print(l1)
if 90 in l1:
l1.remove(90) #remove works on value
print(l1)

if l1.count(90) > 0:
l1.remove(90)
l2 = [90, 80, 70, 60, 50]
print(sum(l2))

l2 = [80, 90, 60, 50, 70]
l2.reverse()
print(l2)
l2.sort()
l2.sort(reverse=True)
print(l2)

# extend
l1 = [2,4,6]
l2 = [4,5,9]
l3 = l1 + l2
print(l3)
l1.extend(l2) # l1 = l1 + l2
print(l1)

l3 = [2, 4, 6, 4, 5, 9]
l4 = l3 # deep copy [renaming= duplicate name]
l5 = l3.copy() #shallow copy [taking a photocopy]
print(“l3 = “,l3)
print(“l4 = “,l4)
print(“l5 = “,l5)
l3.append(11)
l4.append(22)
l5.append(33)
print(“After append:”)
print(“l3 = “,l3)
print(“l4 = “,l4)
print(“l5 = “,l5)

”’
Assignment: Write a program using List to read the marks of
5 subjects for 5 students and find the highest marks for:
i) Each Student
ii) Each Subject

e.g. [[44,77,66,55,88],[64,54,74,84,94],[99,66,44,77,55],[83,74,65,56,67],[91,81,71,61,51]]
Highest for Student 1: 88
2: 94
3: 99
4: 83
5: 91
Highest for Subject 1: 99
2: 81
3: 74
4: 84
5: 94
”’
all_marks = []
for i in range(5):
students=[]
print(“Marks for Student “,i+1,“: “)
for j in range(5):
m = int(input(“Marks obtained in Subject “+str(j+1)+“: “))
students.append(m)
all_marks.append(students)
print(“All marks = “,all_marks)
# [[44,77,66,55,88],[64,54,74,84,94],[99,66,44,77,55],[83,74,65,56,67],[91,81,71,61,51]]

#All marks = [[44, 77, 66, 55, 88], [64, 54, 74, 84, 94],
# [99, 66, 44, 77, 55], [83, 74, 65, 56, 67], [91, 81, 71, 61, 51]]


l3 = [2, 4, 6, 4, 5, 9]
l3.clear()
print(l3)

#Tuple
t1 = ()
print(type(t1))
t1 = (100,)
print(type(t1))
t1 = (100,200)
print(type(t1))

######### DICTIONARY ##########
# dictionary -> unordered mutable collection
# dict = {key:value}
dict1 = {}
print(type(dict1))
dict1 = {1:500,“Two”: “Sachin Tendulkar”,“some index”:999,“City”:“Mumbai”, 10:1000}
#Key shouldnt be duplicated
print(dict1[1])

#DICTIONARY
dict1 = {}
print(type(dict1))

all_info= {}
# all_info= {“”:[],””:[],””:[]}
for i in range(3):
rno = input(“Enter the Roll No. “)
marks=[]
for j in range(3):
m = float(input(“Enter the marks: “))
marks.append(m)
t_dict = {rno:marks}
all_info.update(t_dict)

print(“All Info: \n,all_info)

t1 = (3,6,9)
a,b,c = t1
#a,b= t1 ValueError: too many values to unpack (expected 2)
# a,b,c,d = t1 ValueError: not enough values to unpack (expected 4, got 3)


all_info = {‘101’: [3.0, 4.0, 5.0], ‘102’: [7.0, 8.0, 9.0], ‘103’: [4.0, 5.0, 6.0],
‘104’:[2.0, 4.0, 6.0], ‘105’:[3.0,6.0, 9.0]}
chk_rno = input(“Enter the roll number for information: “)

print(all_info.keys())
print(all_info.values())
print(all_info.items())

for k in all_info.items():
if k[0]==chk_rno:
print(k[1])

found = False
for k,v in all_info.items():
if k==chk_rno:
print(v)
found = True

if not found:
print(“Sorry, roll no is not in the list”)

chk_rno = input(“Enter the roll number to delete: “)
for k in all_info.items():
if k[0]==chk_rno:
all_info.pop(chk_rno)
break
print(“After removal: \n,all_info)
all_info.popitem() #removes last updated value
print(“After popitem: \n,all_info)

all_info = {‘101’: [3.0, 4.0, 5.0], ‘102’: [7.0, 8.0, 9.0], ‘103’: [4.0, 5.0, 6.0],
‘104’:[2.0, 4.0, 6.0], ‘105’:[3.0,6.0, 9.0]}
#copy
all_info1 = all_info #Deep copy
all_info2 = all_info.copy() # Shallow copy
print(“First:”)
print(all_info)
print(all_info1)
print(all_info2)
all_info.update({9:“Orange”})
all_info1.update({10:“Apple”})
all_info2.update({11:“Mango”})
print(“Second:”)
print(all_info)
print(all_info1)
print(all_info2)

### SETS : linear unordered collection

set1 = {3,5,6,8}
print(type(set1))

list1 = [‘Mango’,‘Mango’,‘Mango’,‘Mango’,‘Mango’]
set1 = {‘Mango’,‘Mango’,‘Mango’,‘Mango’,‘Mango’}
print(len(list1))
print(len(set1)) # set will have unique values
list1 = [“A”,“B”,“C”,“D”,“E”]
list2 = [“C”,“D”,“A”,“B”,“E”]
set2 = {“C”,“D”,“A”,“B”,“E”}
print(set2) # order doesnt matter in set
# SETs are used to represent values
set2 = {“C”,“D”,“A”,“B”,“E”}
set1 = {“C”,“D”,“A”,“F”,“G”}

#frozen_set is the immutable version of set
#sets are mutable
set1.add(“Z”)
set3 = {“P”,“Q”}
set2.update(set3)
print(“Set1: “,set1)
print(“Set2: “,set2)
set2.pop()
print(“Set2: “,set2)
set2.remove(“Q”)

#Operations on sets: Union, Intersection, Difference, difference_update
print(“Set1: “,set1)
print(“Set2: “,set2)
#union – combining all the elements from the sets
print(set1.union(set2))
print(set1 | set2)

#intersection: only the common elements
print(set1.intersection(set2))
print(set1 & set2)

#difference
print(set1 – set2)
print(set2 – set1)
print(set1.difference(set2))

#symmetric difference
print(set1 ^ set2)
print(set2 ^ set1)
print(set1.symmetric_difference(set2))

set1.union(set2) #union update – set1 will have new values
set1.intersection_update(set2) # set1 will be updated with output values
set1.difference_update(set2)
set1.symmetric_difference_update(set2)
set1.issubset(set2)

# FUNCTIONS –

# inbuilt functions: print(), input(), ….
# user defined functions

#just created a function called as myquestions()
# required positional argument
def myquestions(choice,a):
print(“Choice =”,choice,“and A =”,a)
if choice==1:
print(“Whats your name?”)
print(“What are you doing here?”)
print(“When will you be done?”)
else:
print(“Whats your Work here?”)
print(“What are your interests?”)
print(“Why dont you stand out?”)
return “Job Done”

myquestions(1,5) #passing while calling
#print(“My Out = “,myout)
print(“Another time let me repeat”)
myout = myquestions(0,4)
print(“My Out = “,myout)

str1 = “HELLO”
output = str1.lower() #returns a value
print(output)

list1 = [5,1,9,6,2,7,3]
output2 = list1.reverse() #doesnt return a value
print(output2)
print(list1)

out=print(“heloo”)
print(“OUT=”,out) #doesnt return
out = input() #returns
print(“OUT=”,out)


# non-required = default argument
# non-positional = keyword argument
def myquestions(choice,a=-99): #default value
print(“Choice =”,choice,“and A =”,a)
if choice==1:
print(“Whats your name?”)
print(“What are you doing here?”)
print(“When will you be done?”)
else:
print(“Whats your Work here?”)
print(“What are your interests?”)
print(“Why dont you stand out?”)
return “Job Done”



myquestions(1,5) #passing while calling
myquestions(1)
myquestions(a=1,choice=5) #keyword argument

def myfavoritefruits(f1,*fruits, **interests): #* and ** for variable number of arguments
# * – as tuple, ** will read as dictionary
if len(fruits)==0:
print(“My favorite fruit is”,f1)
else:
#tuple(list(fruits).append(f1))
print(“My favorite fruits are:”,tuple(set(fruits)))
if len(interests)!=0:
print(“My other interests are: “)
for i,j in interests.items():
print(“Favorite”,i,“is”,j)


myfavoritefruits(“Guava”,“Orange”)
myfavoritefruits(“Orange”)
myfavoritefruits(“mango”,“mango”,“mango”,“apple”, color=“red”,place=“hyderabad”,food=“Biryani”)
# required, default, keyword, variable length arguments

def myfun1(x,y):
print(f”x={x}, y={y} and sum is {x+y})


myfun1(10,20)
a,b = 5,6
myfun1(a,b)
x,y=2,8
myfun1(x=y,y=x)
myfun1(y,x)

#Scope of a variable
PI = 3.14

def myfun1(x,y):
print(f”x={x}, y={y} and sum is {x+y})
global g
print(“Print G =”,g)
h=70
g=20
print(“Local: “,g)

g=100 #Global variable
myfun1(10,20)
#print(“H =”,h)

def my_fun2(n):
all_values = []
for i in range(n):
all_values.append(input(“Enter values: “))
return all_values

val = my_fun2(10)
print(val)
# class and objects
# functions and variables – for class and specific for objects

class Library:
total_books = 0 #class level variable
var2 = –1
name = “ABC International School Library”
def __init__(self,title, author,copies=1): # auto called when object is created
print(“Adding books to the library”)
Library.total_books+=1
self.total_copies = copies #object level variable
self.title = title
self.author = author

def display_detail(self):
print(“================================”)
print(“Title \t\t Author \t\t\t Copies”)
print(“——————————–“)
print(f”{self.title} {self.author} {self.total_copies})
print(“================================”)

@classmethod
def print_total(cls):
print(“Total book count is: “,cls.total_books)

# variables – needs to be told if its object level – by default its class level
# methods – needs to be told if its class level – by default its object level

lib1 = Library(“Python Programming”,“Sachin Tendulkar”,10) # __init__ will be called
lib2 = Library(“Machine Learning”,“Virat Kohli”) # __init__ will be called
lib3 = Library(“SQL Programming”, “Dhoni”, 12) # __init__ will be called
print(Library.total_books) #calling using class name
print(lib1.total_books) #calling using object
print(lib2.total_books)

print(Library.total_books)
print(lib1.total_books)
print(lib2.total_books)

print(lib1.total_copies)
print(lib2.total_copies)
print(lib2.author)
print(lib3.author)
lib1.display_detail()
lib1.print_total()
lib3.print_total()
Library.print_total()


class MathsOps:
def __init__(self,a:int, b=1):
self.num1 = a
self.num2 = b # Protected members are defined as _ (single underscore)
self.__mul = –1 #we are making it as PRIVATE (__)
self.div = –1
self.square = self.num1 ** 2
self._power = self.num1 ** self.num2 #protected

def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num1 – self.num2
def multiply(self):
self.__mul = self.num1 * self.num2
return self.__mul
def divide(self):
self.div = self.num1 / self.num2

m1 = MathsOps(100)
print(m1.add())
print(m1.subtract())
#print(m1.__mul)
print(“Multiply: “,m1.multiply())
division = m1.divide()
print(“Division = “,division)
print(m1._power) #protected concept is there Python but not implemented
print(m1.square)
class CountryInfo:
def __display1(self): #private
print(“Country = India”)
def _display2(self): #protected
print(“Country is India”)
class University (CountryInfo):
def __init__(self, name,year_estb):
self.uname = name
self.year_estb = year_estb
def display(self): #public
print(f”The university {self.uname} was established in the year {self.year_estb})

def generateInfo(self):
print(“Sorry this is not implemented”)

def makeitready(self):
print(“Sorry this is not implemented”)
class CityInfo:
def display(self):
print(“City = Hyderabad”)

class Professors(University, CityInfo):
def __init__(self,name,univ,salary, year_estb):
University.__init__(self,univ, year_estb)
self.name = name
self.salary = salary
def printinfo(self):
print(self.name,“: “,self.salary)
University.display(self)
CityInfo.display(self)

def generateInfo(self):
print(“generating payslip”)
class Students(CityInfo,University):
def __init__(self,name,univ,marks, year_estb):
University.__init__(self,univ,year_estb)
self.name = name
self.marks = marks

def printinfo(self):
print(self.name,“:”,self.marks)
University.display(self)
CityInfo.display(self)
def generateInfo(self):
print(“Marks are being generated”)

u1 = University(“ABC University”, 1968)
u2 = University(“XYZ Management University”, 1998)
u2.display()
p1 = Professors(“Sachin”,“ABC University”,167000,1968)
p1.printinfo()
s1 = Students(“Virat”,“XYZ Management University”, 75,1998)
s1.printinfo()

s1.display()
p1.display()
#p1.__display1() #private members are not accessible outside the class
p1._display2() #protected members are accessible by derived classes

ct1 = CityInfo()
ct1.display()

s1.generateInfo() # foreced the derived class to have implemeted the method
p1.generateInfo() # foreced the derived class to have implemeted the method

# Please do the program from:
# https://designrr.page/?id=206786&token=2174026775&type=FP&h=8743
# Page no. 154

#Android: STORY MANTRA:
# categories -> Python -> Python book
class CityInfo:
def cityinfoprint(self):
print(“City is Hyderabad”)
class University:
def __init__(self,univ):
self.univ = univ
def printInfo(self):
print(“University is “,self.univ)
def sampleM(self,num1,num2):
print(“Sample M 3”)
class Student(University):
def __init__(self,name,age,univ):
#University.__init__(self, univ)
super().__init__(univ)
self.name=name
self.age = age
def printInfo(self):
print(“Name, Age & University :”,self.name, self.age, self.univ)
super().printInfo()
CityInfo.cityinfoprint(self)
def sampleM(self):
”’

:return:
”’
print(“Sample M 1”)

s1 = Student(“Sachin”, 49,“Mumbai University”)
s1.printInfo()
s1.sampleM()

def myfunction1(a=10,b=20):
”’
This is my sample function to do nothing but to show off
:param a:int is for length
:param b:int for for taking input data as breadth
:return: nothing
”’
return a + b

num1 = 78
num2 = 88
print(num1 + num2) # operator overloading
print([2,46] + [5,9,8]) # operator overloading

print(input.__doc__)
print(print.__doc__)
print(len.__doc__)
print(myfunction1.__doc__)
from abc import ABCMeta, abstractmethod
class Shape:
__metaclass__ = ABCMeta

def __init__(self, shapeType):
self.shapeType = shapeType
@abstractmethod
def area(self):
pass

@abstractmethod
def perimeter(self):
pass

class Rectangle(Shape):
def __init__(self, length,breadth):
Shape.__init__(self,“Rectangle”)
self.length = length
self.breadth = breadth

def perimeter(self):
return 2*(self.length + self.breadth)

def area(self):
return self.length * self.breadth

class Circle(Shape):
def __init__(self, radius):
Shape.__init__(self,“Circle”)
self.rad = radius

def perimeter(self):
return 2* 3.14 * self.rad

def area(self):
return 3.14 * self.rad**2


r1 = Rectangle(10,5)
print(“Perimeter is”,r1.perimeter())
print(“Area is”,r1.area())

c1 = Circle(5)
print(“Perimeter is”,c1.perimeter())
print(“Area is”,c1.area())

class Books:
count = 0
def __init__(self,title):
self.title = title
Books.count+=1

@classmethod
def totalcount(cls):
print(“Total titles in the library = “,cls.count)

def __del__(self):
print(f”The object with title {self.title} is getting destroyed. You cant use it again!”)
b1 = Books(“Python Programming”)
b2 = Books(“SQL Programming”)
b3 = Books(“Machine Learning”)
b4 = Books(“Tableau book”)
print(b1.title)
b1.totalcount()
print(b3.title)
b3.totalcount()
b4.totalcount()
print(b4.title)

del b4
input()

b4.totalcount()

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 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;

Python and Machine Learning Course May 2023

print(‘5 + 3 =’, 5+3)

pi = 3.1 # variable
g = 9.8
radius = 15
# area of a circle = pi * r square
# commentsm jhjkhjkhkjhjghg
print(“Area of a circle =”,3.1 * radius * radius)

# Write a program to find area and perimeter of a rectangle by taking length and breadth as input
length = 21
breadth = 38
area = length * breadth
perim = 2 * (length + breadth)
print(“Area of rectangle =”,area)
print(“Perimeter of rectangle =”,perim)

#Write a program to find area and perimeter of a square
side = 35
area = side * side
perim = 4 * side
print(“Area of square =”,area)
print(“Perimeter of square =”,perim)

#Find total and average of five numbers
num1 = 78
num2 = 82
num3 = 79
num4 = 91
num5 = 59
total = num1 + num2 + num3 + num4 + num5
avg = total / 5
print(“Total = “,total,”Average is”,avg)

#Find total and average of five numbers
num1, num2, num3,num4,num5 = 78,82,79,91,59
total = num1 + num2 + num3 + num4 + num5
avg = total / 5
print(“Total = “,total,“Average is”,avg)
# find the value of 2x square -10x +30, when x = 8
x = 8
y = 2 * x * x – (10 * x) + 30 # expression
print(“Value of y =”,y)

#different types
# basic data types – they can store only one value at a time
# integer (int) – numbers without decimal values
num1 = 5
num2 = 0
num3 = –99 #<class ‘int’>
print(“1. “,type(num1)) #type(num1) – this is first to be called and then print() is called

# float (float) – decimal values
num1 = 5.0
num2 = 0.9
num3 = –99.1 #<class ‘float’>
print(“2. “,type(num1))

# string (str)
var1 = “Hello”
var2 = ‘Good Evening’
var3 = ”’Hi there”’
var4 = “””Python learning”””
print(type(var1), type(var2),type(var3),type(var4))

# boolean (bool) – True & False
var1 = True
var2 = False
print(type(var1), type(var2))
# complex (complex): complex numbers are square root of negative numbers
# square root of -25 = 5i = 5j
var1 = 5j
print(type(var1))

## Operations

#Arithematics operations (maths op)

#Relational operations

#logical operations (and/or/not)

Programming with Hari
print(‘Hello’)
print()   #comment is for human being
#sdfsdfdsfsdfdsf
print(‘hello again hhjvjbhjhlbbuihuhuhuhu’)
print(‘5+3’)
print(5+3)
print(“4+6=”,4+6)

# y = x + 5, find y when x is 3

# 5 is constant – you cant change its value
# x, y are variables – their values can change
# y dependent variable because its value depends on x
# x is independent variable because you can assign any value to it

x = 3
y = x+ 5
print(“When X is “,x,“then Y is “,y)

# write a program to calculate area of a rectangle


l = 5
b = 8
area = l * b
print(” Length is”,l,“breadth is”,b,“and area is”,area)
# f-string format string
print(f“Length is {l} and breadth is {b} so the area is {area})

#calculate area and circumference of a circle and print the output as:
# Circle with a radius will have area of area and circumference of circumference
# arithematic
val1 , val2 = 25,20
print(val1 + val2)
print(val1 – val2)
print(val1 * val2)
print(val1 / val2)
print(val1 // val2) # integer division
print(val1 % val2) # mod – remainder
print(5 ** 3) #power

#relational operators: > < >= <= == !=
#output is always – True or False
val1, val2, val3 = 25,20,25
print(val1 > val2) #T
print(val1 > val3) #F
print(val1 >= val3) #T
print(val1 < val2) #F
print(val1 < val3) #F
print(val1 <= val3) #T
print(val1 == val2) #F
print(val1 != val3) #F
print(val1 == val3) #T

# Logical operators: input &output are bool: and , or, not
print(“Logical operators”)
print(True and True) #I did both the tasks
print(False and True)
print(True and False)
print(False and False)

print(True or True) #T
print(False or True) #T
print(True or False) #T
print(False or False) #F

print(not True)
print(not False)

r= –5
if r>0:
area = 22/7*(r**2)
print(“area of the circle is”,area)
else:
print(“Invalid data”)

num=4
if num>0:
print(“Positive”)
elif num<0:
print(“Negative”)
else:
print(“Neither positive not negative”)
# wap to check if a number is divisible by 3 or not
# wap to check if a number is odd or even
# for: when you know how many times you need to repeat something
# while:
# range(=start, < end, =increment) range(3,9,2): 3,5,7
for counter in range(3,9,2):
print(“HELLO, The counter value is”,counter)
##
for i in range(1,101): #when increment is not given, default value is 1
print(i)


#Generate even numbers from 2 to 100 (both inclusive)
n=12
for i in range(n): #when start number is not given, default value is zero
print(“* “,end=“”)
print()

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

”’
*
* *
* * *
* * * *
”’
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):
if j==0 or j==n-1:
for i in range(n):
print(“* “,end=“”)
print()
else:
for k in range(n-j-1):
print(” “,end=” “)
print(“*”)


#while
cont = “y”
while cont==“y”:
print(“Hello from While”)
cont = input(“Do you want to continue: hit y for yes anyother key for no:”)
num = 10
while num >0:
print(“hello”)
num=num-1

ch=‘y’
while ch==‘y’ or ch==“Y”:
print(“I am fine”)
ch=input(“Do you want to continue (y for yes):”)

while True:
print(“How are you?”)
ch=input(“Enter n to stop:”)
if ch==“n”:
break #that will throw you out of the loop

# guessing game
num = 45

while True:
guess = int(input(“Guess the number (1-100): “))
if guess<1 or guess>100:
continue
if num==guess:
print(“Congratulations… You have guessed it correctly!”)
break
elif num<guess:
print(“Incorrect! You have guessed higher number”)
else:
print(“Incorrect! You have guessed lower number”)

## Menu Options
print(“WELCOME TO MY LIBRARY APPLICATION”)
while True:
print(“1. Issue the Book”)
print(“2. Return the Book”)
print(“3. Add new Book to the library”)
print(“4. Remove a book from the library”)
print(“5. Exit”)
choice = input(“Enter you choice: “)
if choice==“1”:
print(“Given book has been issued!”)
elif choice==“2”:
print(“Book has been returned to the library!”)
elif choice==“3”:
print(“Added the book to the library!”)
elif choice==“4”:
print(“Removed the book from the library database!”)
elif choice==“5”:
print(“Have a good day!”)
break
else:
print(“Invalid option! Try again”)
continue
####
str1 = 'HELLO'
str2 = "I am fine"
str3 = '''Where are you going?
How long will you be here?
What are you going to do?'''
str4 = """I am here
I will be here for next 7 days
I am going to just relax and chill"""
print(type(str1),type(str2),type(str3),type(str4))
print(str1)
print(str2)
print(str3)
print(str4)

# What's you name?
str5 = "What's your name?"
print(str5)
#He asked,"Where are you?"
str6 = 'He asked,"Where are you?"'
print(str6)

#He asked,"What's your name?"
#escape sequence \
print('''He asked,"What's your name?"''')
print("He asked,\"What's your name?\"")

print('nnnnn\nnn\tnn')

print("\FOlder\\newfolder")
# \n is used to print newline in python
print("\\n is used to print newline in python")

# \\n will not print newline in python
print("\\\\n will not print newline in python")

str1 = "Hello You"
str2 = "There"
print(str1 + str2)
print(str1 *5)
for i in str1:
print("Hello")

#indexing
print(str1[2])
print("last element: ",str1[4])
print("last element: ",str1[-1])
print("second element: ",str1[-8])
print("ell: ",str1[1:4])
print("ell: ",str1[-8:-5])
print("First 3: ",str1[:3])
print("First 3: ",str1[:-6])
print("Last 3: ",str1[6:])
print("Last 3: ",str1[-3:])

#Methods - exactly same as your functions - only difference is they are linked to a class
import time
str1 = "HELLO"
print(str1.replace("L","X",1))

sub_str = "LL"
str2 = "HELLO HOW WELL ARE YOU LL"
cnt = str2.find(sub_str)
print("Count = ",cnt)

if cnt<0:
print("Sorry, no matching value hence removing")
else:
print("Value found, now replacing")
for i in range(5):
print(". ",end="")
time.sleep(0.5)
print("\n")
print(str2.replace(sub_str,"OOOO"))


out_res = str2.split("LL")
print("Output Result = ",out_res)

out_str = "LL".join(out_res)
print(out_str)

print(str2.title())
print(str2.lower())
print(str2.upper())

str3 = 'hello how well are you ll'
print(str3.islower())
print(str3.isupper())

num1 = input("Enter a number: ")
if num1.isdigit():
num1 = int(num1)
else:
print("Invaid input")

ename = input("Enter your first name: ")
if ename.isalpha():
print("Your name is being saved...")
else:
print("Invaid name")

#WAP to count of vowels in a sentence
para1 = "Work, family, and endless to-do lists can make it tough to find the time to catch up. But you'll never regret taking a break to chat with your friend, Frost reminds us. Everything else will still be there later."
sum=0
for l in para1:
if l=='a' or l=='A' or l=='e' or l=='E' or l=='i' or l=='I' or l=='o' or l=='O' or l=='u' or l=='3':
sum+=1
print("Total vowesl = ",sum)
sum=0
for l in para1.lower():
if l=='a' or l=='e' or l=='i' or l=='o' or l=='u':
sum+=1
print("Total vowesl = ",sum)

sum=0
for l in para1.lower():
if l in 'aeiou':
sum+=1
print("Total vowesl = ",sum)
#Strings – indexing
str1 = “Hello”
print(“=>”, str1[0]+str1[2]+str1[4])

#methods in Python
print(str1.isdigit())

num1 = input(“Enter a number: “)
if num1.isdigit():
num1 = int(num1)
print(num1)
else:
print(“Sorry you have entered a invalid number”)

username = “abcd3456”
if username.isalnum():
print(“Valid username”)
else:
print(“Username is not valid, enter again!”)
username=“abcd”
print(“alpha: “,username.isalpha())
print(“isalnum: “,username.isalnum())
print(“isdigit: “, username.isdigit())
txt1 = “Hello how are you are you are doing”
print(“lower: “,txt1.islower()) #isupper(), istitle()
print(“title: “,txt1.title())
print(“title: “,txt1.istitle())
# islower() -> lower(), isupper() -> upper(), istitle() -> title()
print(“=> “,txt1.split())
print(“=> “,txt1.split(“o”))
txt_list1 = [‘Hello’, ‘how’, ‘are’, ‘you’, ‘doing’]
txt_list2 = [‘Hell’, ‘ h’, ‘w are y’, ‘u d’, ‘ing’]
print(“Join = “, ” “.join(txt_list1))
print(“Join = “, “o”.join(txt_list2))
print(“”,txt1.count(“are”,10,25))
print(“”,txt1.count(“hello”))
print(” “,txt1.replace(“are”,“is”))

COMPLETE THE PYTHON COURSE FROM HERE:

https://www.netacad.com/

Direct course link: https://www.netacad.com/courses/programming/pcap-programming-essentials-python

# List: linear ordered mutable collection
list1 = [5,6.1,“Hello”,True,[2,4,9]]

# mutable means you can edit the list
list1[1] = 6.9
print(“List 1 = “,list1)

# non mutables (immutable) objects cant be edited eg. String
str1 = “Hello”
#str1[1]= “Z” #TypeError: ‘str’ object does not support item assignment
print(“You can iterate through the list:”)
for i in list1:
print(i)

list2 = [3,6,9,12]
list3 = [4,8,12,16]
print(“list2 + list3: “,list2 + list3)

print(“last member = “,list1[-1])
print(“First 4 values = “,list1[:4])
print(“Number of elements in List 1 = “,len(list1))

### Methods in List ###
list2 = [3,6,9,12]
list2.append(18)
print(“List 2 after append: “,list2)
list2.append(15)
print(“List 2 after append 2: “,list2)

# add the element at a specific position
list2.insert(1,24)
print(“List 2 after insert: “,list2)

## removing
list2.pop(2) #pop takes the index value
print(“List 2 after pop: “,list2)
list2.remove(18) #remove takes the value of the element to remove
print(“List 2 after remove: “,list2)

list4 = list2
list5 = list2.copy()
print(“1. List 2 = “,list2)
print(“1. List 4 = “,list4)
print(“1. List 5 = “,list5)
list2.append(30)
list2.append(40)
print(“2. List 2 = “,list2)
print(“2. List 4 = “,list4)
print(“2. List 5 = “,list5)

print(“10 in the list = “,list2.count(10))
list2.reverse()
print(“List after reverse = “,list2)
list2.sort() #sort in the ascending order
print(“Sorted list = “,list2)

list2.sort(reverse=True) #sort in the descending order
print(“Sorted list = “,list2)

list2.clear()
print(“List after clear = “,list2)
# Tuples – linear ordered immutable collection

t1 = (4,6,7,8)
print(type(t1))
for i in t1:
print(i)
t1 = list(t1)
t1.append(10)
t1 = tuple(t1)
# Dictionary : key:value pair

dict1 = {}
print(type(dict1))
dict1 = {3:“Sachin”,“Second”:2,True:False,“Flag”:True}
# 3 ways – keys, values, items
for k in dict1.keys(): #keys
print(“Key = “,k,” and value is”,dict1[k])
for values in dict1.values():
print(“Value is”,values)

# iterate through items:
for item in dict1.items():
print(item,“: key is”,item[0],“and value is”,item[1])

# only a dictionary can be added to a dictionary
temp = {“City”:“Mumbai”}
dict1.update(temp)
print(“Dictionary 1: “,dict1)

#dictionary – mutable [list is mutable, tuple and string – immutable]
dict1[3]=“Tendulkar”
print(“Dictionary 1: “,dict1)

#copy
dict2 = dict1
dict3 = dict1.copy()
print(“1. Dict1 = “,dict1)
print(“1. Dict2 = “,dict2)
print(“1. Dict3 = “,dict3)
dict1.update({“Team”:“India”})
dict3.update({“Runs”: 12908})
dict2.update({“Wickets”: 432})

print(“2. Dict1 = “,dict1)
print(“2. Dict2 = “,dict2)
print(“2. Dict3 = “,dict3)

dict1.clear()
print(dict1)
Django Learning with Jeswanth

All videos are available here:    https://www.youtube.com/playlist?list=PLF9fJE4vTtTLnXYzr_19r1NVRWuNV892t

Access all the videos here

BASIC INFORMATION

For Basic information like installation, software to use, history, refer this link:

#Server DB Server(Modal – SQL) Controller (Python) View (HTML, Javascript)

print(‘Hello There’);print(“How are you?”)

print(5+3);
print ( “5+3=”,5+3,“and 6*2 =”, 6*2 )
# y = x+5, find the value of y when x is 4
# 5 is constant – value will remain 5
# x,y – their value can change – variables
# x independent variable, we can put any value
# y is dependent variable (on x), it can take multiple values but that depends on x

#calculate area and perimeter of a rectangle
l=50
b=85
#input values
# input – process (formula) – output (display the result)
area = l * b
perimeter = 2*(l+b)
print(“Rectangle with length”,l,“and breadth”,b,“has an area of”,area,“and perimeter of”,perimeter)

# f-string – formatting the string
print(f”Rectangle with length {l} and breadth {b} has an area of {area} and perimeter of {perimeter})

#calculate area and circumference of a circle
#independent variable here is radius
r = 13
area = 3.14 * r * r #pi is constant value 3.14
c = 2*3.14*r
#area and c are dependent variables
print(“Circle with radius”,r,“will have area of”,area,“and circumference of”,c)
print(f”Circle with radius {r} will have area of {area:.1f} and circumference of {c:.4f})

name,team,pos=“Virat”,“India”,“Captain”
print(f”Player’s name is {name:<10} from the team {team:^10} and represents as {pos:>15} at the event.”)
name,team,pos=“Manbgwabe”,“Zimbabwe”,“Wicket-Keeper”
print(f”Player’s name is {name:<10} from the team {team:^10} and represents as {pos:>15} at the event.”)

#numeric data types – int, float n complex
var1 = 50 #int – integer, numbers without decimal point
print(type(var1)) #type() – will give the type of the data that is stored
var1 = 50.0 #float
print(type(var1))
var1 = 5j # complex
# square root of -1 : i (in python we say j)
print(type(var1))
print(5j * 5j)
#text type – string (str)
”’ This is a
multi line
comment in python”’
var1 =‘5’
print(type(var1)) #<class ‘str’>
var1 = True #bool – True or False
print(type(var1))
# 5 types: int, float, bool, str,complex
#arithematic
val1, val2 = 20,3
print(val1 + val2) #adding
print(val1 – val2) #subtracting
print(val1 * val2) #multiplying
print(val1 / val2) #dividing – gives a float value
print(val1 // val2) # integer division – gives integer value
print(val1 ** val2) #power
print(val1 % val2) #mod – remainder

# 20/3 = 6 2/3
#conditional / relational operators: > < >= <= == !=
# output: True or False
val1, val2,val3 = 20,3,20
print(val1 > val2) #True
print(val1 >= val3) #T
print(val1 > val3) # F
print(val1 < val3) #
print(val1 <= val3) #T
print(val1 == val3)
print(val1 != val3)

# logical operator: and or not
#input is boolean and output is also boolean
print(False and False)
print(False and True)
print(True and False)
print(True and True)
print(“===============”)

print(False or False)
print(False or True)
print(True or False)
print(True or True)
print(not True)
print(not False)
print(“========”)
val1, val2,val3 = 20,3,20
print(val1 >= val3 and val1 > val3 or val1 < val3 and val1 <= val3 or val1 == val3)
# T
num= 0
if num>0:
print(“Positive”)
elif num<0:
print(“Negative”)
else:
pass

print(“Thank you”)

####
avg =75
#avg>=85: Grade A, 70-85: Grade B, 60 to 70: Grade C, 50-60: D, <50: E
# if avg>=50: You have passed!
if avg>=50:
print(“You’ve passed!”)
if avg >= 70:
print(“Grade A”)
if avg>=90:
print(“You win special award”)
elif avg >= 85:
print(“Grade B”)
elif avg >= 60:
print(“Grade C”)
else:
print(“Grade D”)
else:
print(“You’ve not passed!”)
print(“Grade E”)
#LOOPS – repeating a block of code
## FOR Loop: used when you know how many times to execute a code
## WHILE Loop: beforehand you are not sure how many times but repeat based on some condition
# for: when you know how many times you need to repeat something
# while:
# range(=start, < end, =increment) range(3,9,2): 3,5,7
for counter in range(3,9,2):
print(“HELLO, The counter value is”,counter)
##
for i in range(1,101): #when increment is not given, default value is 1
print(i)


#Generate even numbers from 2 to 100 (both inclusive)
n=12
for i in range(n): #when start number is not given, default value is zero
print(“* “,end=“”)
print()

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

”’
*
* *
* * *
* * * *
”’
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):
if j==0 or j==n-1:
for i in range(n):
print(“* “,end=“”)
print()
else:
for k in range(n-j-1):
print(” “,end=” “)
print(“*”)


#while
cont = “y”
while cont==“y”:
print(“Hello from While”)
cont = input(“Do you want to continue: hit y for yes anyother key for no:”)
DS-Weekend-022023
print(“Hello”,5+4,end=” “)
print(“6+3=”,6+3,end=\n)
print(“How are you?”,end=\n)

print(“This line \n for new line”) ;
# integer – int
a=5;
print(type(a))
#string – str

#boolean – bool

#float

#complex

a = 3+5j
print(a*a) # -16 +30j

#####
#1. WAP to find area and perimeter of a rectangle
# input: what you give to the computer
# process: what is the ask from the computer
# output: what you get back in return
# input: length & breadth
# process: area = length * breadth and perimeter as 2*(length + breadth)
# output: print the answers (area and perimeter) on to the screen

length = 25
breadth = 15
area = length * breadth
perimeter = 2*(length + breadth)
print(“Area = “,area)
print(“Perimeter = “,perimeter)

length , breadth,name = 25, 15,“Sachin” #implicit conversion
area = length * breadth
perimeter = 2*(length + breadth)
print(“Area = “,area,“and Perimeter = “,perimeter)

#unpack

#input() – to get input value from the user
length = input(“Enter the length value = “) #implicit into str
length = int(length) #explicit conversion to int
print(“Data type of length is “,type(length))
breadth = int(input(“Enter the breadth value = “))
print(“Data type of breadth is “,type(breadth))
area = length * breadth
perimeter = 2*(length + breadth)
# f string – format string
print(f”A rectangle with length {length} and breadth {breadth} has an area of {area} and perimeter of {perimeter})
# f-string expanded to float and str
total_cost = 100
num_units = 33
print(f”Total cost came to Rs {total_cost} for {num_units} pens so the cost of each pen is Rs {total_cost/num_units:.2f})
print(f”{3.69:.1f})

player = “Virat”
position = “captain”
country = “India”
print(f”Player {player:.<12} is {position:X^15} of {country:->12} team”)

player = “Mbanwaweba”
position = “wicket-keeper”
country = “Zimbabwe”
print(f”Player {player:<12} is {position:^15} of {country:>12} team”)
#Arithematic operations
val1 = 7
val2 = 3
print(val1 + val2) # addition
print(val1 – val2) # subtract
print(val1 * val2) # multiply
print(val1 / val2) # division
print(val1 % val2) # remainder
print(val1 // val2) # integer division
print(val1 ** val2) # power
print(int(10 ** (1/2)))

#binary value
print(bin(10)) #0b1010
print(hex(0o12)) #0xa
print(oct(10)) #0o12
print(int(0b1010))

# binary operators: << >> & |
#Shift
print(10 <<3) # 1010
print(int(0b101000))
print(10 >> 2)
#Relational or conditional: > < <= >= == !=
val1 = 10
val2 = 20
val3 = 10
print(val1 > val2) # is val1 greater than val2? – False
print(val1 >= val2) # is val1 greater than or equal to val2? – False
print(val1 >= val3) # is val1 greater than or equal to val3? – True
print(val1 > val3) # is val1 greater than val3? – False
print(val1 < val3) #False
print(val1 <= val3) #True
print(“val1 == val3: “,val1 == val3)
print(val1 != val3)
print(“A40” > “90”)

# Logical operator: and or not
#input and output both are bool values
# and – both have to True to result into True (otherwise its False)
print(“T and T: “, True and True)
print(“T and F: “, True and False)

# or (+)
print(“F or T : “, False or True)
# 2+5*3 = ?
val1 = 10
val2 = 20
val3 = 10
print(val1 <= val3 or val1 > val3 and val1 > val2) # = ?
# T

print(bin(15))
print(“15 & 10 = “, 15&10)
print(“15 | 10 = “, 15|10)

#print(f”15 | 10 = 15{|}10″, )

print(f”val1 <= val3 or val1 > val3 and val1 > val2 = {val1 <= val3 or val1 > val3 and val1 > val2})
#print(f”True {and} False”)
l = int(input(“Enter the value of length: “))
b = int(input(“Enter the value of breadth: “))

#conditional statements checks the value
if l>0 and b>0:
area = l * b
perimeter = 2*(l+b)
print(f”Rectangle with length {l} and breadth {b} has area of {area} and perimeter of {perimeter})

#another set of if condition
if l>0 and b>0:
area = l * b
perimeter = 2*(l+b)
print(f”Rectangle with length {l} and breadth {b} has area of {area} and perimeter of {perimeter})
else:
print(“Sides of rectangle doesnt look valid”)

#
# Check is a number is positive and if positive chck if divisible by 5

value = 50
if value > 0:
print(f”{value} is positive”,end=” “)
if value %5==0:
print(“and its divisible by 5”)
elif value <0:
print(f”{value} is negative”)
else:
print(f”{value} is neither positive nor negative”)

marks1, marks2,marks3,marks4,marks5 = 96, 96,96,96,95

#assign grade on the basis on avg marks:
# avg>90: A, 75-90: B, 60-75: C, 50-60:D, <50: E
avg = (marks1 + marks2+marks3+marks4+marks5)/5
print(“Average: “,avg)
if avg>=90:
print(“Grade A”)
#print(“Result: You have Passed”)
elif avg>=75:
print(“Grade B”)
#print(“Result: Passed”)
elif avg>=60:
print(“Grade C”)
elif avg>=50:
print(“Grade D”)
else:
print(“Grade E”)
print(“Result: Failed”)

#
print(“===================”)
flag = 0 #didnt win dean’s award
if avg>=50:
#print(“Result: You’ve passed!”)
if avg >= 90:
print(“Grade A”)
if avg >=95:
flag=1
elif avg >= 75:
print(“Grade B”)
elif avg >= 60:
print(“Grade C”)
else:
print(“Grade D”)
print(“Result: You’ve passed!”)
else:
print(“Result: Sorry You’ve failed!”)
print(“Grade E”)
if flag==1:
print(“You win special dean’s award”)

## checking the greater number between 2 values
val1,val2 = 40,20

if val1 > val2:
print(f”{val1} > {val2})
elif val1 < val2:
print(f”{val2} > {val1})
else:
print(f”{val2} = {val1})

#########
val1,val2 , val3 = 50, 90,20

if val1 > val2:
#print(f”{val1} >= {val2}”)
if val1 > val3:
if val3 > val2:
print(f”{val1} >= {val3} >= {val2})
else:
print(f”{val1} >= {val2} >= {val3})
else:
print(f”{val3} >= {val1} >= {val2})
else:
#print(f”{val2} >= {val1}”)
if val2>val3:
if val1 > val3:
print(f”{val2} >= {val1} >= {val3})
else:
print(f”{val2} >= {val3} >= {val1})
else:
print(f”{val3} >= {val2} >= {val1})


#LOOPS- to execute block of code multiple times

#range(a,b,c): a=starting (=), b= ending value (<), c=increment
#range(5,30,5): 5,10,15,20,25
#range(a,b) c is default = 1
#range(4,10): 4,5,6,7,8,9
#range(b), a is default = 0, c is default = 1
#range(4): 0,1,2,3
#FOR Loop – when we know how many times to run

print(“i” in “India”) #True
print(“A” in “India”) #False because A is not in India

for counter in range(5,10,3):
print(“In For Loop:”,counter)

for counter in “India”:
print(“In For Loop:”,counter)

#Run a loop 5 times:
for i in range(5):
print(“i = “,i)

#a way of printing even numbers upto 20
for i in range(0,21,2):
print(i,end=“, “)
print()

for i in range(1,101):
if i %5==0:
print(i,end=“, “)
print()

# WHILE Loop –
ch=“n”
#extry controlled loop
while ch==“y”: #while will execute if the condition is True
print(“How are you?”)
ch=input(“Enter y to continue, anyother key to stop: “)

#exit controlled
while True:
print(“I am fine”)
ch = input(“Enter n to stop, anyother key to stop: “)
if ch==“n”:
break
###
”’
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
”’

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()

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

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


#Multiplication table
for j in range(1,11):
for i in range(1,11):
print(f”{i:<2} * {j:<2} = {j*i:<2},end=” “)
print()

#match and case
ch = input(“Enter you favorite programming language: “)
match ch:
case “Python”:
print(“You are on Data Scientist track”)
case “Java”:
print(“You are on Mobile App Developer track”)
case “Javascript”:
print(“You are on Web Developer track”)

#Program to take input marks and find avg
ch=“y”
sum=0
counter=0
while ch==“y”:
marks = int(input(“Enter marks: “))
counter+=1
sum+=marks # sum=sum+marks
ch=input(“Do you have more marks to add? y for yes: “)
avg = sum/counter
print(f”Total marks= {sum} and Average marks ={avg})

#
# guessing number game – human v computer
import random
num = random.randint(1,100) #both start and end is inclusive
attempt = 0
while True:
val = int(input(“Guess the number (1-100): “))
if val<1 or val>100:
print(“Invalid number!”)
continue #take you to the beginning of the loop
attempt+=1
if val==num:
print(f”You have guessed it correctly in {attempt} attempts”)
break #throw you out of the loop
elif val <num:
print(“Incorrect! Your guess is low”)
else:
print(“Incorrect! Your guess is high”)

##
# guessing number game – computer v computer
import random
num = random.randint(1,100) #both start and end is inclusive
attempt = 0
start,end=1,100
while True:
val = random.randint(start,end) #int(input(“Guess the number (1-100): “))
if val<1 or val>100:
print(“Invalid number!”)
continue #take you to the beginning of the loop
attempt+=1
if val==num:
print(f”You have guessed it correctly in {attempt} attempts”)
break #throw you out of the loop
elif val <num:
print(“Incorrect! Your guess is low”)
start = val + 1 #guess a higher number
else:
print(“Incorrect! Your guess is high”)
end = val – 1 #guess a lower number

### Using IF Condition in one single line – one line condition
# Ternary operator: condition logic should not be more than 1 line

val1, val2 = 30,40

var1 = val1 if val1 > val2 else val2
print(“1. Higher number is “,var1)

var1 = “val1 is higher” if val1 > val2 else “val2 is higher”
print(“2. Message: “,var1)

### One line for loop
#square the values greater than 5 and cube the values for others
for i in range(10):
if i >5:
val = i**2
else:
val = i**3
print(val)

#Above code can be implemented in one line:
print(“Using one line loop and condition:”)
for i in range(10): print(i**2) if i>5 else print(i**3)

Assignment Programs

Exercise 1: Write a program in Python to display the Factorial of a number.

Exercise 2: Write a Python program to find those numbers which are divisible by 7 and multiples of 5, between 1500 and 2700 (both included).

Exercise 3: Write a Python program to reverse a number.

Exercise 4: Write a program to print n natural number in descending order using a while loop.

Exercise 5: Write a program to display the first 7 multiples of 7.

Exercise 6: Write a Python program to convert temperatures to and from Celsius and Fahrenheit.

[ Formula : c/5 = f-32/9 [ where c = temperature in celsius and f = temperature in fahrenheit ]

Expected Output :

60°C is 140 in Fahrenheit

45°F is 7 in Celsius


Exercise 7: Write a Python program that iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for multiples of five print “Buzz”. For numbers that are multiples of three and five, print “FizzBuzz”.

Sample Output :

fizzbuzz

1

2

fizz

4

buzz


Exercise 8: Write a Python program to print the alphabet pattern ‘A’.

Expected Output:


  ***                                                                   

 *   *                                                                  

 *   *                                                                  

 *****                                                                  

 *   *                                                                  

 *   *                                                                  

 *   *



Exercise 9: Write a Python program to print the alphabet pattern ‘G’. 

Expected Output:


  ***                                                                   

 *   *                                                                  

 *                                                                      

 * ***                                                                  

 *   *                                                                  

 *   *                                                                  

  *** 


#String
name = “Sachin” \
“Cricket God”
name1 = ‘Virat’
name2 = ”’Rohit
Captain of Team India”’
name4=“””Dhoni
won the world
cup for India
in multiple formats of the
game”””

print(name)
print(name2)
print(name4)

#substring- subset of data – indexing
name = “Sachin Tendulkar”
print(name[0])
print(name[1])
print(“SCI => “,name[0]+name[2]+name[4])
print(“First 3 characters: “,name[0:3], name[:3]) #nothing on left on : means start from zero
print(“chin => “,name[2:6])
print(“kar =>> “, name[13:16])
#len()
print(“Total characters in name is”,len(name))
print(name[15], name[len(name)-1], name[-1])
print(“First character using backward indexing: “,name[-16], name[-len(name)])

print(“kar using backward indexing=>> “, name[-3:]) #when left black on the right means go upto end

fname = “Sachin”
age = 49
print(“First name of the player is “+fname)
print(“Age = “+str(age))

for i in range(5):
print(“* “*(5-i))

#Data structures
# list, tuple, dictionary, sets

print(“Print all characters: “,name,name[:])

# Strings are immutable: TypeError: ‘str’ object does not support item assignment
val = “HELLO”
val = “hELLO”

txt = “abade”
for i in txt:
print(“* “*(5– txt.index(i)))

## is…() – return either True or False
txt = “highway route number. I AM driving there. since one month”
print(“txt.isupper: “,txt.isupper())
print(“txt.islower: “,txt.islower())
print(“Alphabet & Numeric”,txt.isalnum())
print(“Alphabet & Numeric”,txt.isalpha())
print(“Alphabet & Numeric”,txt.isdigit())

print(txt.upper())
print(txt.lower())
print(txt.title())
print(txt.capitalize())

# split()
print(txt.split(‘o’))
out = [‘highway’, ‘route’, ‘number.’, ‘I’, ‘AM’, ‘driving’, ‘there.’, ‘since’, ‘one’, ‘month’]
print(“====”)
out2 = ” “.join(out)
print(out2)

txt = “I am driving on highway route on number fifteen.”
print(txt.lower().count(“i”))
start_pos=0
for i in range(txt.lower().count(“i”)):
print(txt.lower().index(“i”,start_pos),end=“, “)
start_pos=txt.lower().index(“i”,start_pos) + 1
print()

print(txt.replace(“on”,“over”,1))
print(txt)
#strings are immutable

#LIST
l1 = [2,3,5.5,True,“Hello”,[4,8,12]]
print(len(l1))

print(type(l1))
print(type(l1[0]))
print(type(l1[-1]))

print(l1[-2].upper())
l2 = [False,5,115]
l3 = l1 + l2
print(l3*3)

#lists are mutable
l2[1] = “How are you?”
print(l2)

for i in l2:
print(i,end=“, “)
print(\n\n### Methods”)
### Methods
l1 = [2,4,6,8,10,2,14]
print(l1.index(2,2))
print(l1.count(2))
print(“1. Current list = “,l1)
print(l1.pop(2)) #removes index element
print(“2. Current List = “,l1)
print(l1.remove(14)) #removes value element
print(“3. Current List = “,l1)
l1.append(21) #adds at the end of the list
print(“4. Current List = “,l1)
l1.insert(3,31) #takes position and the value
print(“5. Current List = “,l1)
#below creating 2 new lists based on l1 values
l2 = l1 #deep copy
l3 = l1.copy() #shallow copy
print(“1. L1 = “,l1)
print(“1. L2 = “,l2)
print(“1. L3 = “,l3)
l1.append(42)
l2.append(52)
l3.append(62)
print(“2. L1 = “,l1)
print(“2. L2 = “,l2)
print(“2. L3 = “,l3)
l1.extend(l3)
print(l1)
l1.reverse()
print(l1)
l1.sort() #increasing order
print(l1)
l1.sort(reverse=True) #decreasing order
print(l1)

#definition: List is an ordered linear mutable collection
subject_list = [‘Maths’,‘Science’,‘English’,‘Social Science’,‘German’]
marks_list = []
sum=0
for i in range(5):
#marks = int(input(“Enter marks in Subject “+str(i+1)+”: “))
marks = int(input(“Enter marks in Subject ” + subject_list[i] + “: “))
sum+=marks
marks_list.append(marks)
print(“Marks obtained in each subject = “,marks_list,“and total marks = “,sum)

sum=0
for i in marks_list:
sum+=i
print(“Total marks = “,sum)



#Reduce, Map, Filter => later after functions
# TUPLE:
#definition: Tuple is an ordered linear immutable collection
t1 = ()
t1 = (5,)
t1 = (5,4)
t1 = (5,6,7,8.0,“9”)
print(type(t1))
#tuples are converted to list and vice-versa
t1 = list(t1)
t1.append(45)
t1 = tuple(t1)
#tuples are faster than list for reading

#packing
t1 = (3,30,“Hello”) #packing
#unpacking
a,b,c = t1
print(a,b,c)

print((2,3,99) > (3,1)) #checks one by one member untill it finds the greater value

#List – linear ordered mutable collection
# dictionary – ordered collections – key:value pair

dict1 = {}
print(type(dict1))
dict1 = {“name”:“Sachin”,“city”:“Mumbai”,“Runs”:12345,“name”:“Tendulkar”}
print(dict1)
print(dict1[‘name’])
dict2 = {(“Team IPL”,“Team Ranji”):“Mumbai Indians”}
dict1.update(dict2)
print(dict1)

for i in dict1.keys():
print(i, dict1[i])
print(“Iterating through values:”)
for i in dict1.values():
print(i)
print(“Iterating through items (key,value):”)
for i,j in dict1.items():
print(i,j)
for i in dict1.items():
print(list(i))

print(“Printing the values: “)
print(“Keys :”,dict1.keys())
print(“Values:”,dict1.values())
print(“Items:”,dict1.items())

dict1.pop(‘Runs’)
print(“After pop: “,dict1)
dict2 = dict1 #deep copy
dict3 = dict1.copy() # shallow copy
t_dict = {“Country”:“India”}
dict1.update(t_dict)
print(“Printing all 3 Dictionaries: “)
print(“Dict1: “,dict1)
print(“Dict2: “,dict2)
print(“Dict3: “,dict3)
#dict1[‘name’]=”Tendulkar”
t_dict = {“name”:“Tendulkar”}
dict1.update(t_dict)
for i in range(2):
print(“Dict1 before popitem: “,dict1)
dict1.popitem()
print(“Dict1 after popitem: “,dict1)

”’
Write a program to input Roll no. and marks of 3 students in 3 subjects
{101:[], 102:[]}

”’
dict_marks={}

for i in range(3):
t_dict = {}
roll=int(input(“Enter the Roll number:”))
t_list=[]
for j in range(3):
marks=int(input(“Enter the marks for Roll no.”+str(roll)+” in Subject “+str(j+1)+” :”))
t_list.append(marks)
t_dict = {roll:t_list}
dict_marks.update(t_dict)
print(“Final data:”,dict_marks)

”’
Assignment program:
from the below dictionary find the topper for each subject:
{100: [55, 66, 78], 102: [90, 87, 54], 105: [67, 76, 87]}

e.g. Highest in Subject 1: 102 with 90 marks
Highest in Subject 2: 102 with 87 marks
Highest in Subject 3: 105 with 87 marks
”’
#Example 2:
master_dict = {}

for k in range(2):
t_dict = {“Name”:0,‘Age’:0,‘Email’:0,“Address”:0}
for i in t_dict.keys():
j=input(“Enter the client’s “+i+” :”)
t_dict[i] = j
master_dict.update(t_dict)
print(“Master Dictionary information: \n,master_dict)

##################################################
# SETS
set1 = {}
print(type(set1))
set1 = {“Apple”,“Banana”,“Mango”,“Grapes”,“Guava”}
print(type(set1))
set2 = {“Mango”,“Grapes”,“Guava”,“Apple”,“Banana”}

set1 = {1,2,3,4,5,6}
set2 = {1,3,5,7,9}
print(“Union:”)
print(set1 | set2)
print(set1.union(set2))
print(“Intersection:”)
print(set1 & set2)
print(set1.intersection(set2))
print(“Minus – difference”)
print(set1 – set2)
print(set2.difference(set1)) #set2 – set1
print(“Symmetric Difference”)
print(set1 ^ set2)
print(set1.symmetric_difference(set2))
print(“Set1 before pop:”,set1)
set1.pop()
print(“Set1 after pop:”,set1)

list1 = [1,2,3,4,1,2,3,1,2,1]
list1 =list(set(list1))
print(type(list1))
print(list1)
#################### FUNCTIONS ##############################
def myquestions():
”’this is a sample function to demonstrate how function works
it doesnt take any parameter nor does it return anything
-written on 22nd april”’
print(“whats your name?”)
print(“how are you?”)
print(“Where are you going?”)

def mycalc1(a,b,c):
print(“MY CALC 1”)
print(f”A,B and C values are {a},{b},{c})
total = a+b+c
print(total)

def mycalc2(a,b=0,c=9):
print(“MY CALC 2”)
print(f”A,B and C values are {a},{b},{c})
total = a+b+c
print(total)

def myfunc1(a,*b,**c):
print(“A = “,a)
print(“B = “,b)
print(“C = “,c)


myquestions()
print(myquestions.__doc__)
print(“Doc for print”)
print(input.__doc__)
#doc – first line of code inside the function, must be multiline comment
print(\n\n\n)
mycalc1(10,50,90) #required positional arguments
mycalc2(3,7,19) #calling default arguments
mycalc1(c=1,a=7,b=3) #use keywords to avoid positional

print(“Calling variable length arguments:”)
myfunc1(10,1,2,3,4,5,6,7,8,9,0,4,5,66,44,333,33, name=“Sachin”,age=43,runs=19890)

def myfun1(a,b,c):
#
global x
print(“X = “,x)
x = 5
print(“X = “, x)


x=50
myfun1(5,10,15)
print(“in Main x = “,x)
###
def isPrime(n):
check= True
for i in range(2,n//2+1):
if n%i==0:
check=False
break
return check

check = isPrime(51)
if check:
print(“51 number is prime”)
else:
print(“51 number is not prime”)

#generate list of prime number between 1000 and 2000

for i in range(1000,2001):
out =isPrime(i)
if out:
print(i)
def myfunc1():
“””This is a sample function to see the working of a function”””
print(“What’s your name?”)
print(“How are you?”)
print(“Where do you live?”)

def myfunc2(a,b,c): # required positional arguments
print(f”Values of a,b and c are {a},{b} and {c} respectively”)
total = a+b+c
print(“Total is “,total)

def myfunc3(a,b=0,c=0): # a & b required positional and c is positional not required (default)
print(f”Values of a,b and c are {a},{b} and {c} respectively”)
total = a+b+c
print(“Total is “,total)



def isPrime(n):
”’isPrime is a function that takes a parameter n and
check and prints if its a prime number of not”’
prime = True
for i in range(2,n//2 +1):
if n%i==0:
prime=False
break
return prime

if __name__ ==“__main__”:
myfunc1()
print(“————-“)
print(myfunc1.__doc__)
# doc string: a multi line string and first line in the function
print(print.__doc__)
print(int.__doc__)

myfunc2(5, 10, 15) # required positional
print(“Calling My Func3 below:”)
myfunc3(10, 20)
myfunc3(10, 20, 30)

n = 11
out = isPrime(n)
if out:
print(n, “is a prime number”)
else:
print(n, “is not a prime number”)

# I want to print a list of all the values that are prime between 100 and 500
print(“Printing list of prime numbers from 100 to 500:”)
for k in range(100, 501):
if isPrime(k):
print(k)

# We are talking about non-positional (or KEYWORD arguments)
print(“Working on keyword arguments:”)

myfunc2(b=45, c=50, a=70) # for keyword – use same arguments that are already there

# Module:

P5.py file:

from p4 import myfunc2
def mytask(n):
print(“Hello : “,n)
if n==0:
return 100
mytask(n-1)

# 5! = 5 * 4!

def myfacto(n):
if n==1:
return 1
return (n * myfacto(n-1))


###### decorators

def outer():
print(“Line one of outer”)
def inner():
print(“Line 1 of inner”)
print(“Line two of outer”)
inner()
print(“Line three of outer”)



def myouter11():
print(“This is line 1 from myouter 11”)
def myouter22():
print(“This is line 1 from myouter 22”)
def myouter33():
print(“This is line 1 from myouter 33”)
def myouter2(var1):
print(“This is line 1 from myouter 2”)
var1()
print(“This is line 2 from myouter 2”)


if __name__ ==‘__main__’:
out = myfacto(50)
print(“Factorial of 4 is”, out)
outer()
myouter2(myouter33)

P6.py:

#import p5 as SuperFunctions
from p5 import mytask, myfacto, myfunc2
from MyPack1 import modul1
modul1.myfunc1()

#SuperFunctions.mytask(7)
mytask(50)
myfacto(10)
# one line for loop
print(“Option 1”)
for i in range(5):
print(“Hello”)
print(“Option 2”)
for i in range(5): print(“Hello”)
print(“Option 3”)
list1 = [2,4,6,8,10]
for i in list1: print(i)
print(“Option 4”)
prod=1
for i in range(1,10): prod*=i
print(prod)
print(“Option 5”)
mylist1 = [i for i in range(1,10)]
print(“Mylist1 = “,mylist1)

# one line if condition
print(“Condition Option 1”)
num=-5
if num>0:
print(“Positive”)
else:
print(“Not positive”)
output = “Positive” if num>0 else “Not Positive”
print(“Option 1 output = “,output)

print(“Condition Option 1 with Loops”)
### calculate cube of values between 1 and 9
print([num**3 for num in range(1,10)])
### calculate cube of values between 1 and 9 if the value is odd
print([num**3 for num in range(1,10) if num%2==1])
# one line function
print(“one line function Option 1”)
myfun1 = lambda x,y:print(“Total = “,x+y)
myfun1(10,20)
print(“one line function Option 2”)
friends=[“Rohit”,“Rahul”,“Surya”,“Kohli”]
batting = lambda team: [print(“Now batting:”,x) for x in team]
batting(friends)

#MAPS, FILTER & REDUCE
input = [2000,3000,100,200,5000,6000,3000,900,600,500,230,8000]
# all the three concepts works on list – input is a list and output depends upon the task
## 1. map – if there is a single logic (formula) that you need to apply on entire list values
#example: convert these feet into metres: divide by 3.1
some_func = lambda num:num/3.1
out = list(map(some_func,input))
print(“Output = “,out)

# Filter: if there is a single logic (condition) based on whicch you select subset
## subset of values are created when the condition returns True
out = list(filter(lambda x: (x//100)>=10,input))
print(“Filtered values are: “,out)

# Reduce: takes entire data in a list and reduces them to just 1 single value based on the given formula
from functools import reduce
print(“Sum of all the values are: “,reduce(lambda a,b:a+b, input))

PROJECT 1: Working with Dictionary

## Project 1: Create a menu option for billing
## Dictionary: {Item_code: [“Item_Description”,price]}
## create bills for each individual: {item_code: [quantity, price, total_cost]}
from datetime import date, datetime, timedelta, timezone
import time
start = time.time()
for i in range(1000000):
out = i**3+500*i**2+9
time.sleep(1)
end = time.time()

print(“Total time taken by the program to run: “,end-start)


from datetime import datetime
currenttime= datetime.now()
print(“Current time: “,currenttime)
print(“Date: “,currenttime.strftime(“%y-%m-%d”))
print(“Get: “,currenttime.year, currenttime.day, currenttime.minute)
print(“out: “,currenttime.today())
print(“Weekday: “,currenttime.weekday())

from datetime import timedelta,datetime
print(“Yesterday: “,currenttime-timedelta(days=1))
print(“Next week: “,currenttime+timedelta(days=7))
class BookMagazine:
__publisher = “Eka Publishers”
def __init__(self, title,page):
print(“Publisher is: “,BookMagazine.__publisher)
self.title=title
self.pages=page
class Books(BookMagazine):
total_books = 0 # class level variable – all objects and class will return same value
#object level variables will be inside object methods
def __init__(self, title,author,page):
BookMagazine.__init__(self,title, page)
self.author=author
Books.total_books +=1

def display_book(self):
print(“Dummy Function: Book Created”)
print(“Title = “, self.title)
if self.author==“”:
print(“There is no author name declared”)
else:
print(“Author = “, self.author)
print(“Pages = “, self.pages)

@classmethod
def display_count(cls):
print(“Total book count = “,cls.total_books)
##
b1=Books(“Python Programming”,“Swapnil Saurav”,330)
print(“B1 Display”)
b1.display_book()

b2=Books(“Data Science”,“Swapnil Saurav”,550)
print(“B2 Display”)
b2.display_book()

b3=Books(“Data Visualization”,“Swapnil Saurav”,250)
b3.display_book()
print(b1.total_books)

print(Books.total_books)

#Today’s assignment; Using Class and objects perform addition, subtraction, multiplication,
# and division. Each of these should have unique functions. init should take in 2 input values
# from the user
# Implement atleast one of these: class variable and method, object variable and method

class Magazines(BookMagazine):
def __init__(self,title,pages,genre):
BookMagazine.__init__(self,title,pages)
self.genre = genre

class Library:
def lib_fun1(self):
print(“Printing from Library class:”)
#print(“Publishers = “,BookMagazine.__publisher) #throws error as private members cant be accessed
print(“Total Books = “,Books.total_books)

#print(Books.__publisher) #throws error as private members cant be accessed
#print(b2.)
l1= Library()
l1.lib_fun1()
#Access Modifiers:
## private: only the members of the same class can access
## protected: (one _ variable name): _name, _pub => only derived class can be called
### concept of protected is there but practically its not yet updated

## public: any member can call members of any class
class Books:
total_books=0
def __init__(self,title,author):
self.title = title
self.author = author
Books.total_books +=1
def display_data(self):
print(f”Title = {self.title} and Author = {self.author})

b1 = Books(“Python Programming”,“Saurav”)
print(type(b1))
b1.display_data()
b2 = Books(“Machine Learning”,“Saurav”)
b2.display_data()
l1 = []
print(type(l1))

##############
#Errors
# Syntax errors – when you dont follow Python rules
# print 5

#logical errors – wrong logic.. a + b = 4 *2

# Exception errors – runtime errors
num1=0
try:
num1 = int(input(“Enter a number: “))
#10/0
except (ValueError, ZeroDivisionError):
print(“You have not entered a valid number, hence exiting…”)
except Exception:
print(“Some error has occured, please retry!”)
else:
print(num1) # ValueError
finally:
print(“Error or No error I will be called”)
#10/0: ZeroDivisionError
num1=0
while True:
try:
num1 = int(input(“Enter a number: “))
break
except (ValueError, ZeroDivisionError):
print(“You have not entered a valid number, hence exiting…”)
except Exception:
print(“Some error has occured, please retry!”)
else:
print(num1) # ValueError
finally:
print(“Error or No error I will be called”)

# Assertion Error
def print_data(num):
#perform this only when num >100
assert (num>100), “Value entered is too small to process”
return num**num
try:
out = print_data(100)
except AssertionError:
print(“Give larger value and run again”)
else:
print(“Output is “,out)

#########


### create my own exception
class TooSmallValue(Exception):
def __init__(self,value=0,min=100):
self.value = value
self.min = min

#driving code
value = int(input(“Enter a value >100: “))
try:
if value <=100:
raise TooSmallValue
except TooSmallValue:
print(“TooSmallValue: Give larger value and run again”)
else:
print(“Output is “,value*value)


######### WORKING WITH OPERATING SYSTEMS

import os
os_name = os.name
print(os.name)
if os_name==‘nt’:
print(“You are using a Windows machine”)
elif os_name==‘posix’:
print(“This is Mac or Linux or Unix machine”)
else:
print(“not sure which OS you are using”)

## Some OS specific command
#os.rename(“infy.py”,”infy_apr.py”)
#os.mkdir(“TEST”)
from pathlib import Path
import os

path_loc = Path(“C:/Users/HP/PycharmProjects/pythonProject/”)
for p in path_loc.iterdir():
print(p, ” : Is it a directory: “, p.is_dir())

# Text file processing
fileobj=open(“file1.txt”,“a+”) # read(r), write (w), append (a): r+ w+ a+
if fileobj.readable():
print(“read operations”)
content = fileobj.read()
print(“Entire content:”)
print(content)
fileobj.seek(5) #move to first character
print(“First 10 characters:”)
print(fileobj.read(10))
# readline will read maximum one line at a time that too current
fileobj.seek(0)
line = fileobj.readline(1000)
print(“Line: \n,line)
lines = fileobj.readlines()
print(“==========reading lines”)
print(lines)
fileobj.seek(0)
print(“==========reading lines”)
print(lines)
else:
print(“Its not readable”)

if fileobj.writable():
lines = [‘Twinkle Twinkle Little Star\n,‘How I wonder\n,
‘What you are\n,‘Up Above the World\n]
fileobj.writelines(lines)
fileobj.close()

######### CSV Files
import csv

fileobj = open(“D:/datasets/tcs_stocks.csv”) # default mode is r (read)
csv_file = csv.reader(fileobj, delimiter=“,”)
print(list(csv_file))
fileobj.seek(0)
for i in csv_file:
for j in i[:2]:
print(j, end=” “)
print()

fileobj.close()

# create a csv file
header = [‘Name’,‘Team’,‘Matches’]
row1 = [‘Sachin’,‘Mumbai’,222]
row2 = [‘Laxman’,‘Hyderabad’,212]
row3 = [‘Rahul’,‘Bangalore’,333]
import csv
fileobj = open(“sample1.csv”,‘w’,newline=)
row_writer = csv.writer(fileobj,delimiter=‘|’)
row_writer.writerow(header)
row_writer.writerow(row1)
row_writer.writerow(row2)
row_writer.writerow(row3)
fileobj.close()

###### JSON #############
#json: load, loads, dump, dumps
import json
fileoj = open(“json1.json”,“r”)
content = json.load(fileoj)
#print to check if we got the content or not – this is not how to
#display the json content
print(type(content))
#we will use json dumps to display on to the screen
print(json.dumps(content, indent=4, sort_keys=True))
fileoj.close()

fileoj = open(“json2.json”,“w”)
content1 = ‘{“Name”:”Virat”,”Game”:”Cricket”}’
print(type(content1))
content1 = json.loads(content1)
print(type(content1))
print(json.dumps(content1, indent=4, sort_keys=True))
json.dump(obj=content1,fp=fileoj, indent=4)
fileoj.close()

############ DATABASE ################

# structured v unstructured
# Name, Age, Country, Runs, Wickets

## Library Application
# Table 1: Books
## Columns: BookID (INTEGER), BookTitle (TEXT), Price (FLOAT), Copies (INTEGER)

# Table 2: Members
#Columns: MemberID (INTEGER), Name (TEXT), Email (TEXT), Phone (TEXT), Address (TEXT)

#Relationship:
# one to one:
# one to many / many to one:
# many to many:

# Table 3: BOOKSMEMBERS
# columns: TID (INTEGER), BOOKID(INTEGER) , MID(INTEGER),
# ISSUEDATE (DATE), RETURNDATE (DATE)

# OLTP – Online Transaction Processing (normal ) – Reading + (Editing done in bulk)
# OLAP – Online Analytical Processing (Analytics) – Reading

# CRUD : Create (INSERT), Read (SELECT), Update (UPDATE), Delete (DELETE)

# Roles in DBMS:
## Admin (DBA)
## Database Design (ER diagram, create tables) – SQL
## Application Developers: SQL – CRUD
#### Working with Databases #########
”’
# Constraints: Keys (Primary Key, Foreign Key), NOT NULL, UNIQUE, CHECK, DEFAULT

Table 1: Publisher
Create Table Publisher(
PUBID int Primary Key,
Address varchar(100),
Name varchar(25));

INSERT INTO Publisher Values (101, ‘Hyderabad’,’Eka Publishers);
INSERT INTO Publisher Values (102, ‘Mumbai’,’Best Publishers);

Table 2: Books:

Create Table Books(
BookID int Primary Key,
Author varchar(25) NOT NULL,
Title varchar(25) NOT NULL,
Price float(7,2),
Available bool Default 1,
PubID int,
CHECK(Price>=0.0),
Foreign Key (PubID) references Publisher(PubID)
);

INSERT INTO Books (BookID, Author, Title, PubID) Values (101,’Swapnil’,’Python Programming’,101);

INSERT INTO Books (BookID, Author, Title, PubID) Values (102,’Saurav’,’Machine Learning’,101);


Table 3: Member
Create Table Member(
MEM_ID Int Primary key,
Memb_date Date,
Memb_Type varchar(1),
Address varchar(100),
Name varchar(30),
Expiry_date date);

INSERT INTO MEMBER(MEM_ID, Name) Values(101,’%s’)
INSERT INTO MEMBER(MEM_ID, Name) Values(102,’%s’)
INSERT INTO MEMBER(MEM_ID, Name) Values(103,’%s’)

Table 4: BooksMember

Create table BooksMember(
BMID Int Primary Key,
MEMID int Foreign Key References MEMBER(MEMID),
BOOKID int Foreign Key References BOOKS(BOOKID),
BORROW_DATE Date,
RETURN_DATE Date,
);

”’
import pymysql
db_connect = pymysql.connect(host=‘localhost’,password=‘learnSQL’,db=‘library’,user=‘root’)
cursor = db_connect.cursor()
#cursor.execute(‘Drop table Publisher;’)
tab1 = ”’
Create Table Publisher(
PUBID int Primary Key,
Address varchar(100),
Name varchar(25));
”’
#cursor.execute(tab1)
tab2 = ”’
Create Table Books(
BookID int Primary Key,
Author varchar(25) NOT NULL,
Title varchar(25) NOT NULL,
Price float(7,2),
Available bool Default 1,
PubID int,
CHECK(Price>=0.0),
Foreign Key (PubID) references Publisher(PubID)
);
”’
#cursor.execute(tab2)

tab3 = ”’
Create Table Member(
MEM_ID Int Primary key,
Memb_date Date,
Memb_Type varchar(1),
Address varchar(100),
Name varchar(30),
Expiry_date date);
”’
#cursor.execute(tab3)
tab4 = ”’
Create table BooksMember(
BMID Int Primary Key,
BORROW_DATE Date,
RETURN_DATE Date,
MEM_ID int,
BOOKID int,
Foreign Key (MEM_ID) References MEMBER (MEM_ID),
Foreign Key (BOOKID) References BOOKS (BOOKID));
”’
#cursor.execute(tab4)

########### CRUD Create (Insert), Read (Select), Update, Delete ###
## Peforming Create – using Insert
list_insert = [“INSERT INTO Publisher Values (101, ‘Hyderabad’,’Eka Publishers’);”,
“INSERT INTO Publisher Values (102, ‘Mumbai’,’Best Publishers’);”,
“INSERT INTO Books (BookID, Author, Title, PubID) Values (101,’Swapnil’,’Python Programming’,101);”,
“INSERT INTO Books (BookID, Author, Title, PubID) Values (102,’Saurav’,’Machine Learning’,101);”]
list_insert = []
for statement in list_insert:
cursor.execute(statement)
db_connect.commit() # to save the changes

# Insert by dynamic query

#Remove the multi line comment to practice:
”’
name1 = input(“Enter the Member 1 name: “)
insert1 = “INSERT INTO MEMBER(MEM_ID, Name) Values(101,’%s’)”%(name1)
cursor.execute(insert1)
name2 = input(“Enter the Member 2 name: “)
insert2 = “INSERT INTO MEMBER(MEM_ID, Name) Values(102,’%s’)”%(name2)
cursor.execute(insert2)

name3 = input(“Enter the Member 3 name: “)
insert3 = “INSERT INTO MEMBER(MEM_ID, Name) Values(‘%d’,’%s’)”%(103,name3)
cursor.execute(insert3)
db_connect.commit()
”’
## Update existing value in Member table
update1 = “Update Member Set Name=’Sachin Tendulkar’ where mem_id=101”
cursor.execute(update1)
## Delete existing member from Member table
delete1 = “Delete from Member where mem_id=102”
cursor.execute(delete1)
db_connect.commit()
## Reading using Select
select1 = “Select * from Member”
cursor.execute(select1)
results = cursor.fetchall()
for r in results:
print(r)
db_connect.close()

”’
To practice SELECT Commands, please login to:
https://livesql.oracle.com/

create an account and start practicing
”’

— Reading all the rows and columns

select * from HR.Employees;


— All the rows but given columns only

Select Employee_ID, FIRST_NAME, EMAIL from HR.Employees;


— Restricted columns and restricted rows 

Select Employee_ID, FIRST_NAME, EMAIL from HR.Employees where Employee_ID =120;



select first_name||’ has a salary of $’||  salary “Salary Information” , email from hr.employees order by email;


select first_name||’ has a salary of $’||  salary “Salary Information” , email, HIRE_DATE from hr.employees order by HIRE_DATE, email desc;


select first_name, last_name, email, salary from hr.employees where salary > 10000  and salary <18000;


select first_name, last_name, email, salary from hr.employees where salary between 10000 and 18000;


select first_name, last_name, email, salary from hr.employees where salary in (17000, 11000, 13500);


select count(*) from hr.employees


select avg(salary) from hr.employees


select * from HR.Employees;

 

select * from hr.departments;

 

select first_name, last_name, email, to_char(hire_date, ‘Month DD, YYYY’), round(months_between(sysdate, hire_date)) Tenure_in_months from hr.employees;

 

select * from dual;

 

select 3+3 from HR.Employees where rownum<2;

 

select 3+3, abs(-80), to_date(‘May 14, 2023 08:01 P.M.’, ‘Month DD, YYYY HH:MI P.M.’) from dual

 

select Decode(3+5,8,’CORRECT’,’INCORRECT’)  from dual;

 

— Aggregate functions

 

select JOB_ID, count(*), round(avg(salary)), round(sum(salary)) TOTAL_SALARY from hr.employees group by JOB_ID having count(*)>=5;

 

— JOINING TABLES

 

select FIRST_NAME , LAST_NAME, t1.DEPARTMENT_ID, t2.DEPARTMENT_ID, Department_name, HIRE_DATE from HR.Employees t1, hr.departments t2 where t1.department_id = t2.department_ID;

 

— SUB QUERY

select * from HR.Employees where Employee_ID in (select employee_id from hr.employees);

 

select * from HR.Employees where Employee_ID = (select employee_id from hr.employees where rownum < 2);

 

— SET OPERATIONS

select * from HR.Employees where salary <11000

INTERSECT

select * from HR.Employees where salary >20000

import numpy as np