(Feb-09-2020, 03:01 AM)sbabu Wrote: I appreciate if some one can advise me on how to train the brain to think certain way. Are there any brain exercises to improve my thinking?
One advice is keep practicing (mandatory for learning any language, whether spoken or programming). Second is use built in help (even if you understand very little of it at the beginning).
One possible scenario: you have string of numbers and need to manipulate it certain way - make all strings same length and padd with zeros if needed to achieve required length. '123' -> '000123', '123456' -> '123456'.
In interactive interpreter:
>>> s = '123'What can I do with it? Lets have a look at built in methods available to s (which happens to be a str):
>>> s. # 2 x TAB key s.capitalize( s.find( s.isdecimal( s.istitle( s.partition( s.rstrip( s.translate( s.casefold( s.format( s.isdigit( s.isupper( s.replace( s.split( s.upper( s.center( s.format_map( s.isidentifier( s.join( s.rfind( s.splitlines( s.zfill( s.count( s.index( s.islower( s.ljust( s.rindex( s.startswith( s.encode( s.isalnum( s.isnumeric( s.lower( s.rjust( s.strip( s.endswith( s.isalpha( s.isprintable( s.lstrip( s.rpartition( s.swapcase( s.expandtabs( s.isascii( s.isspace( s.maketrans( s.rsplit( s.title(I can scan all the names and pick most promising to find whether they are suitable for my purpose. Here I see zfill which may or not may mean 'zero fill'. How to find out?
>>> help(s.zfill) # or str.zfill Help on built-in function zfill: zfill(width, /) method of builtins.str instance Pad a numeric string with zeros on the left, to fill a field of the given width. The string is never truncated. # press Q to close help and return to interactive interpreterSeems something just about right. Lets try:
>>> s.zfill(6) '000123' >>> '123456'.zfill(6) '123456' >>> '1'.zfill(6) '000001' >>> ''.zfill(6) '000000'We have achieved our objective without leaving Python. You can check other string methods to find out what they do, same with other datatypes (lists, tuples, dictionaries etc). This way you don't need to reinvent wheel and can take advantage features which are already built in into language.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.