Posts

Showing posts from February, 2022

Drink water notification

Image
Drink notification import time from plyer import notification if __name__ == "__main__": notification.notify( title = "Please Drink Water", message = "Drinking water is good for your health", app_icon = "D:\Python chapter\Projects\Drink Notification\water_icon.ico", timeout = 10 ) Drink notification output

Python Exercises 1-11

Practice 1 # sum of two number '''a = 30 b = 6 print ("sum of two number:", a+b) # Remainder after divided a = 35 b = 6 print ("Remainder of when a is divided by b", a%b) ''' # Average of two number ''' a = input ("enter first number: ") b = input ("enter second number: ") a = int(a) b = int (b) avg = (a+b)/2 print ("the average of two number is " , avg) ''' # square of any number a = input ("enter your number") a = int (a) sqr = a*a print ("square of your number" , sqr) Practice 2 # problem 1 # name = input ("enter your name\n") # print ("good afternoon, " + name) # Problem 2 letter = '''Dear , Greeting from Abc coding house. I am happy to tell you about your selection you are selected ! Have a great day ahead! Thanks and regards onam date: ''' name = input ("enter your name\n") date = input (...

chapter 12

Advance python # try ''' while(True): print("press q to quit") a = input("Enter a number:") if a == 'q': break try: print("trying...") a = int(a) if a>6 : print("you enter a number greater than 6") except Exception as e: print(f"your input required in an error : {e}") print("thanks") ''' # handling different exception ''' try: a = int(input("enter a number: ")) c = 1/a print(c) except ValueError as e: print("Exception1 occured") print(e) except ZeroDivisionError as e: print("Exception2 occured") print(e) print("Thanks") ''' # raising exception #try with else ''' try: i = int(input('enter nuber')) c = 1/i except Exception as e: print(e) else: print("successful") ''...

chapter 11

Inheritence # single inheritance # base class or parent class ''' class Employee: Company = "Google" def showDetails(self): print("This is an employee") # Drive class or child class class Programmer(Employee): language = "Python" # Company = "YouTube" def showDetails(self): print("This is an programmer") def getlanguage(self): print(f"The language is {self.language}") e = Employee() p = Programmer() e.showDetails() p.showDetails() p.getlanguage() print(p.Company) ''' # multi inheritance ''' class Freelanacer: company = "Fiver" level = 0 def upgradeLevel(self): self.level = self.level + 1 class Employee: company = "Visa" eCode = 120 class Programmer(Freelanacer,Employee): name = "Rohit" p = Programmer() p.upgradeLevel() print(p.level) print(p.company) ''' # ...

chapter 10

Object oriented # Oops # class oriented ''' class RailwayForm: formType = "RailwayForm" def printData(self): print(f"name is {self.name}") print(f"train is {self.train}") application = RailwayForm() application.name = "Onam" application.train = "Rajdhani Express" application.printData() ''' # Class attibution ''' class Employee: company = "Google" harry = Employee() rajani = Employee() harry.salary = 300 rajani.salary = 400 print(harry.company) print(rajani.company) Employee.company = "youtube" print(harry.company) print(rajani.company) print(harry.salary) print(rajani.salary) ''' ''' # instant attribution class Employee: company = "Google" salary = 100 harry = Employee() rajani = Employee() # Creating instant attribute salary for both the objects # harry.salary = 300 # rajani.salary = 400 h...

chapter 9

Type casting # use a open function to read the content of a file! ''' f = open('sample.txt', 'r') # by default the mode is read(r) we can read any text file without using r # data = f.read() data = f.read(5) # only read first 5 characters from the file print(data) f.close() ''' f = open('sample.txt') # read first line data = f.readline() print(data) # read second line data = f.readline() print(data) # read third line data = f.readline() print(data) # read fourth line... and so on! data = f.readline() print(data) f.close() Type casting # # w means write, a means appending f = open ('another.txt', 'a') f.write("I am appending") f.close() # Another medhod to the same with open("another.txt", 'a') as f: f.write("I am appending 2")

chapter 8

Functions # def greet(name): # print("Good morning, " + name) # def mySum(num1, num2): # return num1 + num2 # greet("Onam") # s = mySum(76, 4) # print(s) def greet(name ="Stranger"): print("Good morning, " + name) greet() ''' def percent (marks): return (sum(marks)/400)*100 marks1 = [10, 10, 10, 10] percentage1 = percent(marks1) marks2 = [75, 98, 88, 78] percentage2 = percent(marks2) print(percentage1, percentage2) ''' Recursion # n! = 1*2*3*4*5*...(n-1)*n # # n = 0 # product = 1 # for i in range(n): # product = product * (i+1) # print(product) ''' def factorial_iter(n): product = 1 for i in range(n): product = product * (i+1) return product print(factorial_iter(4)) ''' def factorial_recursive(n): if n ==1 or n ==0: return 1 return n * factorial_recursive (n-1) f = factorial_recursive(4) print(f)

chapter 7

while loop # while loop ''' i = 0 while i # print fruits list #fruits = ['banana', 'watermelon', 'grapes', 'mangoes'] #i = 0 #while i for loop # for loop ''' fruits = ['banana', 'watermelon', 'grapes', 'mangoes'] for item in fruits: print(item) ''' # for loop with range ''' for i in range (1, 8, 2): print(i) ''' # for else loop ''' for i in range (10): print(i) else: print("this is inside else of for") ''' ''' for i in range (10): print(i) if i == 5: break ''' ''' for i in range (10): if i == 5: continue print(i) ''' # pass mean null statement ''' i = 4 if i>0: pass while i>6: pass print("harry") '''

chapter 6

conditions # if-elif-else ladder in python # a = 8 # if(a>3): # print("the value of a is greater than 3") # elif(a>7): # print("the value of a is greater than 7") # elif(a>13): # print("the value of a is greater than 13") # elif(a>17): # print("the value of a is greater than 17") # else: # print("the value of a is greater than 3,7,13 or 17") # 2. multiple if statements a = 15 if(a>3): print("the value of a is greater than 3") if(a>7): print("the value of a is greater than 7") if(a>13): print("the value of a is greater than 13") if(a>17): print("the value of a is greater than 17") else: print("the value of a is not greater than 17") print("true") # b = 22 # if(a>9): # print("greater") # else: # print("lesser") if-elif-else # age = int(input("enter your age")...

Chapter 5

dictionary #syntax myDict = { "fast": "in a quick manner", "harry": "A coder", "marks": [1,2,5], "anotherDict" : {'harry': 'player'} } # print (myDict["fast"]) # print(myDict["harry"]) myDict['marks'] = [45,34] print(myDict["marks"]) print(myDict['anotherDict']['harry']) dictionary method myDict = { "fast": "in a quick manner", "harry": "A coder", "marks": [1,2,5], "anotherDict" : {'harry': 'player'}, 1:2 } # dictionary meathods print(list(myDict.keys())) # print the key of the dictionary print(myDict.values()) # print the key values of the dictionary print(myDict.items()) # print the (key, values) for all content of the dictionary print(myDict) updateDict = { "lovish": "friend", 2:4, 5:6, } myDict.update...

Python chapter 4 List and Tuples

List # creat a list using [] a = [1,2,3,4,5,6] # print the List using print() function print(a) # Access using index using a[0], a[1], a[2] print(a[2]) # change the value of list using a[0] = 98 print(a) # we can create list with different type of items c = [43, "onam", 89, 89.3] print(c) # list slicing friends = ["harry", "tom", "jarry",78] print(friends[0:2]) print(friends[-2:]) List method l1 = [1,8,7,2,21,15] print(l1) # l1.sort() #short the the list # l1.reverse() # reverse the list # l1.append(54) #adds 54 at the end of list # l1.insert(3, 10) # insert 10 at index 3 # l1.pop(2) #remove element at index 2 # l1.remove(21) #remove 21 from list print(l1) Tuple t = (1,2,4,5) # print(t[0]) # cannot update the value of a tuple # t[0] = 34 # error occour # t1 = () # empty tuple # t1 = (1) #wrong way to declear a tuple with single element # t1 = (1,) # tuple with single element # print (t1) t = (1,2,4,5,1,1) print(t.co...

Python chapter 3

String Functions # b = '''onam"s and onam's''' # print (b) # concatenating two strings # greeting = "good morning, " # name = "Onam" # c = greeting + name # print(c) # sring index name = "Onam" # print (name[4]) # print (name[0:4]) # print (name[:4]) # is same as name[0:4] # print (name[-4:-2]) # is same as name[2:4] # skip string name = "Awesome" d = name[0:5:2] print (d) String function ''' story = "once upon a time there was a youtuber who upload python course with notes" # # String function # print (len(story)) # print (story.endswith("notes")) # print (story.count("a")) # print (story.capitalize()) print (story.find("who")) print (story.replace("youtuber", "onam")) ''' story = "onam is good. \nhe\t is\' go\\od" # \n= new line \t=tab \'=sigle quotes print (story)

Python chapter 2

input function a = input ("enter your name:") a = int (a) # convert a to an integer if possible print (type(a)) Operators a = 3 b = 4 # arithmetic operators print ("the value of 3+4 is ", 3+4) print ("the value of 3-4 is ", 3-4) print ("the value of 3*4 is ", 3*4) print ("the value of 3/4 is ", 3/4) # Assignment operators a = 34 a -= 2 a *= 2 a /= 2 print (a) # Comparison operators b = (14>7) b = (14 =7) b = (14==7) b = (14!=7) , #14 is not equal to 7 print (b) # Logical operators bool1 = True bool2 = False print ("the value of bool1 and bool2 is", (bool1 and bool2)) print ("the value of bool1 and bool2 is", (bool1 or bool2)) print ("the value of not bool2 is", (not bool2)) Type casting # string to int a = "345" a = int(a) print (type(a)) print (a + 5) # string to float a = "345" a = float(a) print (type(a)) print (a + 5) # int to string a = 345 a = str(a) print (t...

Python chapter 1

chapter 1 print hello world ''' Author : Onam license : onam ***************Thank for reading ********** ''' # importing the model print ("Hello world")

Adding two numbers

Require Code is below def sum(a, b): return (a + b) a = int(input('Enter 1st number: ')) b = int(input('Enter 2nd number: ')) print(f'Sum of {a} and {b} is {sum(a, b)}')

Testing

Copy below code def table (num): print ("Hello World") print ("This is testing") def sum(a, b): return (a + b) a = int(input('Enter 1st number: ')) b = int(input('Enter 2nd number: ')) print(f'Sum of {a} and {b} is {sum(a, b)}')