Posts

RECURSION (RECURSIVE FUNCTION)

Image
  TASK:     To display the factorial of a number entered by the user. CODE: def   factorial ( n ):      if   n == 1 :          return   1      else :          return   n * factorial ( n -1 ) num = int ( input ()) print ( factorial ( num )) LOGIC:

DICTIONARY (INPUT FROM USER), EOF ERROR(TRY AND EXCEPT)

Image
  inp = int ( input ()) phonebook = {} for   i   in   range ( inp ):      user = input ()      temp = user . split ()      phonebook [ temp [ 0 ]] = temp [ 1 ] keys = [] try :      while   True :          key = input ()          keys . append ( key ) except   EOFError :      pass l = len ( keys ) for   j   in   range ( l ):      if   keys [ j ]   not   in   phonebook :          print ( "Not found" )      else :          print ( keys [ j ], end = "" )          print ( "=" , end = "" )          print ( phonebook [ keys [ j ]])    ...

LIST INPUT FROM USER

Image
  if  __ name__  ==  '__main__' :      n  =  int ( input ())      l = list ( map ( int , input () . strip () . split ()))      l . reverse ()      for   i   in   range ( n ):          print ( l [ i ], end = " " )

LOOP, STRING, LIST, CONDITION

Image
  l=[] T= int ( input ()) while ( 1 <=T<= 10 ):      for  i  in   range (T):         str1= input ()         l.append(str1)      break l_len= len (l) for  s  in   range (l_len):     even= ""     odd= ""     word=l[s]     w_len= len (word)      for  m  in   range (w_len):          if  m% 2 == 0 :             even=even+word[m]          else :             odd=odd+word[m]      print (even,odd)

CLASS AND OBJECT

Image
  age = 0 class   Person :      def  __ init__ ( self , initialAge ):          if   ( initialAge < 0 ):              self . age = 0              print ( "Age is not valid, setting age to 0." )          else :              self . age = initialAge      def   amIOld ( self ):          if   self . age < 13 :              print ( "You are young." )          elif   self . age >= 13   and   self . age < 18 :          ...