Need help in debugging?
The following are some useful python functions that can help you whenever you are stuck with a code.
print(): This function allows us to give out output to the console, and this can give out messages for the user or print out the variable's value, etc.
print("Welcome to Coders Gambit")
Output:
Welcome to Coders Gambit
help(): This function can fetch the documentation of any python object, its function, variables and how it can be used. Some documents also have examples of how to use the object which is really helpful when you suddenly forget the syntax of that code.
print(help(str))
Output:
Help on class str in module builtins:
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
-- More --
dir(): This function can fetch all the methods associated with a particular object, this is really helpful when you just want to know what all methods you can use with an object, or if you just forget the name of any method that you want to use you can just look up in the list of methods it provides.
print(dir(str))
Output:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
type(): This function tells you what is the data type of the object. This can be useful when you are trying to debug your code as errors usually occur due to the wrong usage of a datatype.
x = 15.5
print(type(x))
<class 'float'>
x = ['Place', 'for', 'all', 'the', 'code' , 'geeks']
y = x
print('address of x:',id(x) )
print('address of y:',id(y) )
y.append('!') # appending a value in list y
print(x) # changes the value in list x too!!
Output:
address of x: 2587994205120
address of y: 2587994205120
['Place', 'for', 'all', 'the', 'code', 'geeks', '!']
0 Comments