What is the usage of enumerate() function in Python?
Python | enumerate() Function The enumerate() function is used to iterate through the sequence and retrieve the index position and its corresponding value at the same time. lst = ["A","B","C"] print (list(enumerate(lst))) #[(0, 'A'), (1, 'B'), …
What is monkey patching in Python?
Python | Monkey Patching In Python, the term monkey patch only refers to dynamic modifications of a class or module at run-time. # m.py class A: def func(self): print("Hi") import m def monkey(self): print …
What are lists and tuples? What is the key difference between the two?
Python | lists, and tuples Lists and Tuples both are sequence data types that can store a collection of objects in Python. They are both heterogeneous data types means that you can store any kind of …
Python check the memory usage of an object
Python | Check the memory usage of an object >>> import sys >>> x = 100 >>> print(sys.getsizeof(x)) 28
What is pickling and unpickling?
Python | Pickling, and Unpickling The pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using the dump function, this process is called pickling. …
Write a code to add the values of same keys in two different dictionaries and return a new dictionary.
We can use the Counter method from the collections module from collections import Counter dict1 = {'a': 5, 'b': 3, 'c': 2} dict2 = {'a': 2, 'b': 4, 'c': 3} new_dict = Counter(dict1) + Counter(dict2) …
Difference between new_style and old_style class?
Python | new_style and old_style Old-style: With old-style classes, class and type are not quite the same things. If obj is an instance of an old-style class, obj __class__ designates the class, but the type(obj) is always …
How to remove whitespaces from a string in Python?
Python | strip() Function | Remove whitespaces from a string To remove the whitespaces and trailing spaces from the string, Python provides a strip([str]) built-in function. This function returns a copy of the string after removing whitespaces …
What is docstring in Python?
Python | Docstring A documentation string or docstring is a multiline string used to document a specific code segment. The docstring should describe what the function or method does.
What is a generator in Python?
Python | Generator Functions that return an iterable set of items are called generators. It is a normal function except that it yields expression in the function. It does not implements __itr__ and next() method and …
Explain split() and join() method in Python?
Python | split() and join() You can use split() method to split a string based on a delimiter to a list of strings. and join() method is used to join a list of strings based on …
Explain Django's code reusability.
Compared to other frameworks, Django offers more code reusability. As Django is a combination of apps, copying those apps from one directory to another with some tweaks to the settings.py file won't need much time …
What is the MRO in Python?
MRO stands for Method Resolution Order. Talking of multiple inheritances, whenever we search for an attribute in a class, Python first searches in the current class. If found, its search is satiated. If not, it …
How do you debug code in python?
Using PDB we debug code in python. Some pdb commands include- <b> — Add breakpoint <c> — Resume execution <s> — Debug step by step <n> — Move to next line <l> — List source code …
What is the difference between .py and .pyc files?
The .py files are the python source code of a program. While the .pyc files contain the bytecode of the python files. We get bytecode after compilation of .py file (source code). The .pyc files are not …