Posts

Showing posts from June, 2020

Coroutines

import time def searcher (): book = "Hello,I Am Ashish Dinesh Patil And Welcome To Python Programming" print ( "Reading Book" ) time.sleep( 4 ) while True : text = ( yield ) if text in book: print ( "Your Text is in the Book" ) else : print ( "Your Text is not in the Book" ) search = searcher() next (search) search.send( "Ashish" ) input ( "Press Any Key" ) search.send( "Ashish Dinesh Patil" )

Abstract Method

# There are Two ways to use abstractmethod # # First Way # from abc import ABCMeta,abstractmethod class Dog ( metaclass =ABCMeta) : # Super class # @abstractmethod # Decorator # def barking ( self ) : return 0 class BullDog (Dog) : # BullDog class is Inherited by Dog class # def barking ( self ): return "Quite/Mild Barking" class Beagle (Dog) : def barking ( self ): return "Loud/Violent Barking" bulldog = BullDog() beagle = Beagle() print (bulldog.barking()) print (beagle.barking()) # Second Way # from abc import ABC,abstractmethod from abc import ABCMeta,abstractmethod class Dog (ABC) : # Super class # @abstractmethod # Decorator # def barking ( self ) : return 0 class BullDog (Dog) : # BullDog class is Inherited by Dog class # def barking ( self ): return "Quite/Mild Barking" class Beagle (Dog) : def barking ( self ): return "Loud/Violent Barking...

Property And Setter Method

class Person : def __init__ ( self ,fname,lname): self .fname = fname self .lname = lname self .email = f" { fname } . { lname } @gmail.com" def printname ( self ): return f"The Person Name Is { self .fname } { self .lname } " harrypotter = Person( "Harry" , "Potter" ) print (harrypotter.fname) print (harrypotter.lname) print (harrypotter.email) harrypotter.fname = "A" # If you tried to change the fname it will not change its email # print (harrypotter.email) # Email will be same # ''' If you want to change email with the change in its fname and lname Then you have to define a Method ''' # Let's See # class Person : def __init__ ( self ,fname,lname): self .fname = fname self .lname = lname def printname ( self ): return f"The Person Name Is { self .fname } { self .lname } " @property # To call the function with ...

Class Method As Alternative Constructer

# Class Method As Alternative Constructer # # Class Method - Which takes class as argument # class Employee : no_of_leaves = 8 # class variable # Constructer = Use to initialize objects def __init__ ( self ,name,salary,role,workinghrs): self .name = name self .salary = salary self .role = role self .workinghrs = workinghrs # Method def printdetails ( self ): return f"The Name is { self .name } ,Salary is { self .salary } ,Role is { self .role } and Working Hours are { self .workinghrs } " @classmethod # Decorator # def change_leaves ( cls ,newleaves): cls .no_of_leaves = newleaves @classmethod def from_slash ( cls ,str): # Class Method As Alternative Constructer # emp3_list = str.split( "/" ) # split function will split the string from the slash and it will create a list # print (emp3_list) return cls (emp3_list[ 0 ],emp3_list[ 1 ],emp3_list[ ...

Class Method In OOP

# Class Method - Which takes class as argument # class Employee : no_of_leaves = 8 # class variable # Constructer = Use to initialize objects def __init__ ( self ,name,salary,role,workinghrs): self .name = name self .salary = salary self .role = role self .workinghrs = workinghrs # Method def printdetails ( self ): return f"The Name is { self .name } ,Salary is { self .salary } ,Role is { self .role } and Working Hours are { self .workinghrs } " @classmethod # Decorator # def change_leaves ( cls ,newleaves): cls .no_of_leaves = newleaves emp1 = Employee( "EMP1" , 99999 , "Senior Manager" , 11 ) # First Object emp2 = Employee( "EMP2" , 88888 , "Manager" , 12 ) # Second Object emp1.change_leaves( 19 ) # Class variable is changed by the object with the help of Class Method # Employee.change_leaves( 29 ) # Changed by the class # emp2.change_leaves( 9 ) # Aga...

Object Oriented Programming

# Class - Template , Blueprints # Objects - Instances of the class # DRY - Do Not Repeat Yourself class Employee : no_of_leaves = 8 # class variable # Constructer = Use to initialize objects def __init__ ( self ,name,salary,role,workinghrs): self .name = name self .salary = salary self .role = role self .workinghrs = workinghrs # Method def printdetails ( self ): return f"The Name is { self .name } ,Salary is { self .salary } ,Role is { self .role } and Working Hours are { self .workinghrs } " emp1 = Employee( "EMP1" , 99999 , "Senior Manager" , 11 ) # First Object emp2 = Employee( "EMP2" , 88888 , "Manager" , 12 ) # Second Object ''' Both the objects have different memory location ''' print (emp1.printdetails()) print (emp2.printdetails()) ''' The below method is used when we don't use Construter ''' # # Instance Variables #...

Python In One Blog

# import numpy # Module # # import math # Built In Module # # This is a comment. """This is Multiline comment""" # print("Hello World") # print(math.gcd(2,4)) # Using Math Module # print("5 + 8") # This is a String. # print(5 + 8) # age = 15 # if (age<18): # print("Hello") # Variables # a = 44 # int Variable b = "Apple" # String Variable c = 45.82 # Float Variable # Arithmetic Calculations # # print(a + c) # print(a - c) # print(a * c) # print(a / c) # print(a % c) # Modulus # print(a // c) # Floor Division # print(a ** c) # Exponent # Wrong Syntax To Create Variable # Ex. HCL Project = 45 """ Rules For Creating Variable 1)Variable should start with a letter or underscore 2)Variable should not start with a number 3)It should not contain space between two letters 4)It should contain only AlphaNumeric characters 5)Variables names are case sensitive Ex. A and a are two different variables ...

JSON Module

import json data = '{"First":"Audi","Second":"BMW","Third":"Maruti Suzuki","Fourth":"Ferrari"}' print (data) parsed = json.loads(data) print (parsed[ 'First' ]) print ( type (parsed)) # g = open("CARS","r") # var1 = g.read() # print(var1) # g.close() data2 = { "channel" : "star plus" , "cars" :[ "bmw" , "ferrari" , "ford" , "maruti suzuki" ], "numbers" : 123456789 , "isgood" : True } jscomp = json.dumps(data2) print (jscomp)

Regular Expressions

#Importing Regular Expressions Module import re # **Meta Characters** # [] A set of characters # \ Signals a special sequence (can also be used to escape special characters) # . Any character (except newline character) # ^ Starts with # $ Ends with # * Zero or more occurrences # + One or more occurrences # {} Exactly the specified number of occurrences # | Either or # () Capture and group # **Special Sequences** # \A Returns a match if the specified characters are at the beginning of the string # \b Returns a match where the specified characters are at the beginning or at the end of a word r"ain\b" # \B Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word # \d Returns a match where the string contains digits (numbers from 0-9) # \D Returns a match where the string DOES NOT contain digits # \s Returns a match where the string contains a white space character # \S Returns a match where the string DOES NOT contain a white...

Guess The Number

# Guess The Number """This Is The Game In Which You Have To Guess The Number Which Is Hidden In The Code, If You Guess It Correctly You Will Win The Game.""" print ( " * Let's Play The Game Of Guessing The Number * \n " ) print ( "No. Of Guesses Is Limited Only To 9 \n " ) #Initializing No. Of Guesses numberofguesses = 9 #Conditions while numberofguesses > 0 : userinput = int ( input ( "Guess The Number \n " )) if userinput < 10 : print ( "No,The Number Is Greater Than Your Input! Please Guess Again." ) numberofguesses = numberofguesses - 1 print ( "Number Of Guesses Remaining =" ,numberofguesses, " \n " ) if 10 < userinput < 18 : print ( "No,The Number Is Little Greater Than Your Input! Please Guess Again." ) numberofguesses = numberofguesses - 1 print ( "Number Of Guesses Remainin...