##### Quest Learning DS December 2022

Day 1:

a=5
#data type is integer
print(a)
print(type(a))
a='''hello
how are you
I am fine'''
#type is string
print(a)
print(type(a))
a="""hello"""
#type is string
print(a)
print(type(a))

a=5.0
print(a)
print(type(a))

a = True #False
print(a)
print(type(a))

a=5j
print(a);print(type(a));print(a*a)

print("How are you?",end=" -> ")
print("I am doing good.")

quant = 40
price = 10
total_cost = quant * price
print("Product quantity is",quant,"and bought at",price,"will cost total of Rs",total_cost)
print(f"Product quantity is {quant} and bought at {price} will cost total of Rs {total_cost}")

length = 50
area = length * breadth #calc
#output: A rectangle with length 50 and breadth 20 will have area of area_val and perimter of perimeter_vl

#Operators:
##Arithematic operators + - * / ** (power) // (integer division) % (reminder)
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** b)
print(a // b)
print(a % b)

##Comparison operator: == != > >= < <=
#input as numbers and output will be boolean value
a=10
b=3
print(a==b) #F
print(a!=b) #T
print(a > b)
print(a>=b)
print(a<=b)
print(a<b)

## Logical: and or not
a = 10
b = 3
print(a >b and b !=a) # T and T = T
print( True and True)
print( False and True)
print( False and False)
print( True and False)
print(not a!=b)
print( True or True)
print( False or True)
print( False or False)
print( True or False)

#bitwise: >> << & | ~
a=23
print(bin(a)) #bin - converts to binary( 0b) oct - octal 0c hex - hexadecimal 0x
print(hex(a))

print("23 >> 1: ",23 >> 1) #bitwise: right shift
print("23 >> 2: ",23 >> 2) #bitwise: right shift
print(23 << 2) #bitwise: left shift
print(int(0b1011))
# 10111. 1011

print(" & : ",23 & 12)
# 1 0 1 1 1
# 0 1 1 0 0
#&
#--------------
# 0 0 1 0 0
# 1 1 1 1 1

print(" | : ",23| 12)

a=-5

if a < 0:
print()
print()
print()
b = 6+4
print(b)
print("Thank you 1")

a = -5
if a<0:
print("This is a negative number")
else:
print("This is not a negative number")

a=0
if a<0:
print("Negative number")
elif a>0:
print("Positive number")
else:
print("Zero value")

number = 6

if number<0:
print("Its negative")
elif number>0:
print("its positive")
if number%2==0:
print("Even")
if number%3==0:
print("Divisible by 3 and 2 both")
else:
print("Its divisible by 2 only")
else:
print("Odd")
if number%3==0:
print("Divisible by 3 only")
else:
print("Its not divisible by either 2 or 3")
else:
print("Its zero")

########## LOOP
# FOR Loop
#range(a,b,c): a = starting value (inclusive), b=ending value(exclusive), c=increment
#range(2,8,2): 2,4,6
#range(a,b): c is default 1
#range(3,7): 3,4,5,6
#range(3): a=0, c=1 => 0,1,2
for i in range(3):
print(i)

# While Loop
ch="n"
while ch=='y':
print("I am in While")

for j in range(5):
for i in range(5):
print("*",end=" ")
print()

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

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

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

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

print("\n\n")
for j in range(5):
for k in range(4-j):
print(" ",end="")
for i in range(j+1):
print("*",end=" ")
print()
choice = "y"
while choice=='y' or choice=='Y':
print("Hello")
choice = input("Enter Y to continue: ")

while True:
print("Hello")
choice = input("Enter Y to continue: ")
print("Hello 2")
if choice == 'B' or choice == 'b':
continue #Take you to the beginning of loop
print("Hello 3")
if choice!='y' and choice!='Y':
break #break which will throw you out of current loop
print("Hello 4")

print("Hello 5")
print(val1)
print(type(val1))

marks1 = input("Enter your marks in subject 1: ")
marks1 = int(marks1)
marks2 = int(input("Enter your marks in subject 2: "))
marks3 = int(input("Enter your marks in subject 3: "))
sum = marks1 +marks2+marks3
print("Total marks obtained is ",sum)
avg = sum/3
print(f"{val1} has scored a total of {sum} marks with an average of {avg:.2f}")

#<class 'str'> str()
#<class 'int'> int()
#<class 'float'> float()
#<class 'complex'> complex()
#<class 'bool'> bool()

###############
choice = input("Do you want milk (Y/N): ")
if choice =='Y' or choice =='y':
print("Give milk")
print("So you want milk tea")

print("Done")
val1 = "Sachin Tendulkar"
marks1 = input("Enter your marks in subject 1: ")
marks1 = int(marks1)
marks2 = int(input("Enter your marks in subject 2: "))
marks3 = int(input("Enter your marks in subject 3: "))
sum = marks1 +marks2+marks3
print("Total marks obtained is ",sum)
avg = sum/3
print(f"{val1} has scored a total of {sum} marks with an average of {avg:.2f}")

if avg >=90:
print("Congratulations, you won President Medal")

#if avg >=40, Pass and its not then say Fail
if avg >=40:
print("Result: PASS")
else: #default condition , executed only when if is false
print("Result: FAIL")

'''
80 to 100: Grade A - IF
70 to 80: Grade B - ELIF
60 to 70: Grade C - ELIF
50 to 60: Grade D - ELIF
40 to 50: Grade E - ELIF
'''
#avg = 90
if avg>=80:
elif avg>=70:
elif avg>=60:
elif avg>=50:
elif avg>=40:
else:

number = 11
if number %2==0:
print("Its an even number")
else:
print("Its an odd number")
number = int(input("Enter a number: "))
if number <0:
print("Its a negative number")
elif number >0:
print("Its a positive number")
if number %2==0:
print("Its an even number")
if number %3 ==0:
print("Its divisible by both 2 and 3")
else:
print("Its an odd number")
else:
print("Its Zero")

number = 5
if number %5==0 and number %3==0:
print("Number is divisible by both 5 and 3")
else:
print("Its neither divisible 5 nor 3")

if number %5==0:
if number%3 ==0:
print("Divisible by both 5 and 3")
else:
print("Divisible only by 5")
else:
if number%3 ==0:
print("Divisible by only 3")
else:
print("Its neither divisible 5 nor 3")

if number ==0:
print("Zero")
else:
print("Its either positive or negative")

# Loops : FOR - you know how many times (boil water for 2 min)
#range(a,b,c): a is the starting value, b is the ending value minus 1 (UPTO), c increment
#range(2,8,2): 2,4,6
#range(2,5): 2 values these are a and b, c is default =1 || 2,3,4
#range(3) : 1 value indicate b, default a=0,c=1 || 0,1,2

#WAP to generate first 10 natural numbers
for i in range(10):
if i==9:
print(i)
else:
print(i, end=', ')

#Loops: WHILE - you know until when (boil water till you see bubble)

counter = -11
while counter <=10:
print(counter)
counter+= 1 # a = a X 5 => a X= 5

# Sum of first 10 natural numbers
sum=0
for i in range(1,11):
sum+=i # sum = sum + i
print("Sum from For loop: ",sum)

# Sum of first 10 natural numbers
sum=0
counter = 1
while counter <=10:
sum+=counter
counter+=1
print("Sum from While loop: ",sum)

str1 = 'hello'
str2 = "hi"
str3 = '''Hello there'''
str4 = """Good evening"""
print(str4[-1])
print(str4[:4])
print(str4[1:4])
print(str4[-3:])
print(str1.count('l'))
print(str4.upper())
print(str1.upper().isupper())
num1 = input("Enter a number: ")

print(num1)
#List
list1 = [2,4,6,8.9,"Hello",True,[2,3,4]]
print(type(list1))
print(list1)
print(type(list1[-1]))
var = list1[-1]
print(list1[-1][0])
print(list1[-3:])
print(len(list1[-1]))

for i in list1:
print(i, end=" , ")
print()
l1 = [2,4,6,8]
l2 = [1,3,5,7]
print((l1+l2)*3)

l1.append(19)
print(l1)
l1.insert(2,"Hello")

l1.pop(0) #index / positive to remove
l1.remove(19) #value to remove
print(l1)
l1[1] = 18
print(l1)

l11 = l1
l21 = l1.copy()
print("1")
print("L1: ",l1)
print("L11: ",l11)
print("L21: ",l21)
l11.append(66)
l1.append(55)
l21.append(66)
print("2")
print("L1: ",l1)
print("L11: ",l11)
print("L21: ",l21)

l1.extend(l11) # l1 = l1+l2
print(l1)
#l1.sort()
l1.reverse()
print(l1)
print(l1.count(66))
print(l1.index(8))
t1 = tuple(l1)
t1 = list(t1)
t1 = (2,4,5)
n1,n2,n3 = t1

t1 = (3,)
print(type(t1))
t1 = (3)
print(type(t1))

dict1 = {55:"Sachin", "Name": "Cricket"}
word = "hello"
guess = "ll"
ind = 0
word1 = word.replace("l","L",1)
print(word1)
for i in range(word.count(guess)):
ind = word.find(guess,ind)
print(ind)
ind=ind+1

word3 = "How are you doing"
l1 = word3.split("o")
print(l1)
word4 = "o".join(l1)
print(word4)

#Strings
word = "hEllo".lower()
print(word)
display_text = "* "*len(word)
print(display_text)
while True:
guess = input("Guess the character: ")
guess = guess[0].lower()

if guess.isalpha():
if guess in word:
ind = 0
for i in range(word.count(guess)):
ind = word.find(guess, ind)
#now time to reveal
#0 - 0, 1-2, 2-4
display_text = display_text[:ind*2] + guess+display_text[ind*2+1:]
ind = ind + 1
print(display_text)

if "*" not in display_text:
print("Congratulations!")
break
else:
print("Given character is not in the original word")
else:
print("Invalid character")

#List
l1 = [2,4,6.5,"Hello",True,[2,4,6]]
l1.append(11)
l1.insert(1,"Good evening")
l1.pop(0) #removes element from the given position
l1.remove(6.5) #value

l2 = l1
l3 = l1.copy
print("Set 1: ")
print("l1 : ",l1)
print("l2 : ",l2)
print("l3 : ",l3)

print("Set 2: ")
print("l1 : ",l1)
print("l2 : ",l2)
print("l3 : ",l3)
print("######")
'''
22
12
2022

22nd December 2022
'''
month_txt = ["January","February","March","April","May","June","July","August",
"September","October","November","December"]
dt_ending = ["st","nd","rd"]+["th"]*17 +["st","nd","rd"]+["th"]*7 +["st"]
date_user = int(input("Enter Date: "))
month_user = int(input("Enter month:"))
year_user = input("Enter the year: ")
display_txt = str(date_user) +dt_ending[date_user-1]+" " + month_txt[month_user-1]+" " +year_user

print(display_txt)

l1 = [5,10,15,20,25,30]
print(len(l1))

sample = [[1,2,3,4,5],
[2,4,6,8,10],
[3,6,9,12,15]]
#dictionary: unordered collection mutable
main_dict = {}
d1 = {"name":"sachin"}
d2 = {"city":"mumbai"}
main_dict.update(d1)
main_dict.update(d2)
key="sports"
val="cricket"
temp={key:val}
main_dict.update(temp)

main_dict2 = main_dict
main_dict3 = main_dict.copy()
print("Set 1")
print("Dict 1: ",main_dict)
print("Dict 2: ",main_dict2)
print("Dict 3: ",main_dict3)
key="marks"
val=[55,44,66,77,88]
temp={key:val}
main_dict.update(temp)
print(main_dict)

print("Set 1")
print("Dict 1: ",main_dict)
print("Dict 2: ",main_dict2)
print("Dict 3: ",main_dict3)

print("Set Mem Loc")
print("Dict 1: ",id(main_dict))
print("Dict 2: ",id(main_dict2))
print("Dict 3: ",id(main_dict3))

main_dict.pop('city')
print("Dict 1: ",main_dict)
main_dict.popitem()
print("Dict 1: ",main_dict)
#keys
for i in main_dict.keys():
print(i)
#values
for i in main_dict.values():
print(i)
#items
for i,j in main_dict.items():
print(i," : ",j)
# List functions

### MAP
list1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
#find cube of all these values

result = list(map(lambda x:x**3,list1))
print("Result = ",result)

### FILTER
result = filter(lambda x:x>=15,list1)
print("Filtered values: ",list(result))

### REDUCE
from functools import reduce
result = reduce(lambda x,y:x+y,list1)
print("Sum is ",result)

result = reduce(lambda x,y:x+y,[1,2,3,4,5,6,7])
print("Sum is ",result)

# 1,2,3,4,5,6,7 =
## 1. x=1, y=2 , x+y = 3
## 2. x=3, y=3, x+y=6
## 3. x=6, y=4, x+y = 10
## 4. x=10, y=5 = 15
## 5. x=15, y=6 = 21
## 6. x=21,y=7 = 28

#how to connect to the database from Python
#SQLITE3 - installed on local machine, nobody can connect from outside
import sqlite3

con_str = sqlite3.connect("classnotes.db")
cursor = con_str.cursor()
q1 = '''Create table Notes(
ID int primary key,
description text,
subject varchar(30))'''
#cursor.execute(q1)

q2 = '''Insert into Notes(ID, Description, Subject)
values(2,"This is a sample Maths notes to perform sone action",'MATHS')'''
#cursor.execute(q2)

q4 = '''UPDATE Notes set subject='Science' where ID=2 '''
cursor.execute(q4)
q4 = '''DELETE From Notes where ID=1 '''
cursor.execute(q4)

con_str.commit()
q3 = '''Select * from Notes'''
recordset = cursor.execute(q3)
#print(list(recordset))
for i in recordset:
for j in i:
print(j,end=" ")
print()

con_str.close()

##########
a=10
b=10
c =a/b
print("A/B = ",c) #ZeroDivisionError: division by zero
num1 = 0
num2 = 0
try:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))

except ValueError:
#print("We cant proceed further because input is not valid")
print("Invalid input, setting both the numbers to zero")

finally:
sum = num1 + num2
print("Sum is ", sum)
print("Thank you for using this program. See you soon")

#ValueError: invalid literal for int() with base 10: '8t'