Posts

Showing posts from July, 2020

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)