Posts

Python Comprehensions

#List Comprehensions list = [i for i in range( 100 ) if i % 4 == 0 ] print(list) # Dictionary Comprehensions dict1 = {i: f"Item { i } " for i in range( 10 ) if i % 2 == 0 } dict2 = {value:key for key,value in dict1.items()} print(dict1) print(dict2) #Set Comprehensions dresses = {dress for dress in [ "dress1" , "dress2" , "dress1" , "dress2" , "dress1" , "dress2" ]} print(dresses) #Generator Comprehensions gen = (i for i in range( 100 ) if i % 5 == 0 ) print(type(gen)) print(gen) print(gen.__next__()) print(gen.__next__()) print(gen.__next__()) print(gen.__next__()) # # for i in gen: # print(i)

Binary Number

class Binary : def __init__ ( self , bin): self .bin = bin def checkBin ( self ): self .bin = str ( self .bin) for i in range ( 0 , len ( self .bin)): if int ( self .bin[i]) != 0 and int ( self .bin[i]) != 1 : return "Invalid Binary Format,Not A Binary Number." else : return f"Your Binary Number Is - { self .bin } " def onesComplement ( self ): binary_number = bin1.checkBin() checkBinResult = f"Your Binary Number Is - { self .bin } " if binary_number == checkBinResult: self .bin = str ( self .bin) self .bin = list ( self .bin) for i in range ( 0 , len ( self .bin)): if self .bin[i] == '0' : self .bin[i] = '1' elif self .bin[i] == '1' : self .bin[i] = '0' self .bin = "" .join( self .bin) ...

Armstrong Numbers

# n = No. Of Digits In The Given Number # # Armstrong Number - The Number That Is Equal To The Sum Of nth Power Of Its Digits # userinput = int ( input ( "Enter The Number Till You Want To Print Armstrong Numbers \n " )) print ( "Armstrong Numbers Are :" ) for num in range ( 0 ,userinput): originalnum = num numstr = str (num) length = len (numstr) result = 0 while num > 0 : digit = num % 10 result = result + digit**length num = num // 10 if originalnum == result: print (originalnum)

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