Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us

Python Interview Questions
11. What does len() do?
A: It is used to determine the length of a string, a list, an array, etc.
12. Differentiate between deep and shallow copy.
A: A shallow copy, copies one object’s reference to another. So, if we make a change in the copy, it will affect the original object. For this, we have the function copy(). We use it like:
>>> b=copy.copy(a)

A deep copy copies an object into another. This means that if you make a change to a copy of an object, it won’t affect the original object. In Python, we use the function deepcopy() for this, and we import the module copy. We use it like:

>>> import copy
>>> b=copy.deepcopy(a)
13. Explain the ternary operator in Python.
A: Unlike C++, we don’t have ?: in Python, but we have this:

[on true] if [expression] else [on false]

If the expression is True, the statement under [on true] is executed. Else, that under [on false] is executed.

Below is how you would use it:

>>> a,b=2,3
>>> min=a if a<b else b
>>> min
14. Write Python code to count the number of capital letters in a file.
>>> with open('file.txt') as f:
    for i in
        if i.isupper():
15. In a List and in a Dictionary, What Are the Typical Characteristics of Elements?
A: Elements in lists maintain their ordering unless they are explicitly commanded to be re-ordered. They can be of any data type, they can all be the same, or they can be mixed. Elements in lists are always accessed through numeric, zero-based indices.

In a dictionary, each entry will have a key and a value, but the order will not be guaranteed. Elements in the dictionary can be accessed by using their key.

Lists can be used whenever you have a collection of items in an order. A dictionary can be used whenever you have a set of unique keys that map to values.

16. Is There a Way to Get a List of All the Keys in a Dictionary? If So, How Would You Do It?
A: To obtain a list of all the keys in a dictionary, we have to use function keys():



dict_keys([‘a’, ‘b’, ‘c’, ‘e’])

17. When Would You Use a List vs. a Tuple vs. a Set in Python?
A: A list is a common data type that is highly flexible. It can store a sequence of objects that are mutable, so it’s ideal for projects that demand the storage of objects that can be changed later.

A tuple is similar to a list in Python, but the key difference between them is that tuples are immutable. They also use less space than lists and can only be used as a key in a dictionary. Tuples are a perfect choice when you want a list of constants.

Sets are a collection of unique elements that are used in Python. Sets are a good option when you want to avoid duplicate elements in your list. This means that whenever you have two lists with common elements between them, you can leverage sets to eliminate them.

18. In Python, How is Memory Managed?
A: In Python, memory is managed in a private heap space. This means that all the objects and data structures will be located in a private heap. However, the programmer won’t be allowed to access this heap. Instead, the Python interpreter will handle it. At the same time, the core API will enable access to some Python tools for the programmer to start coding. The memory manager will allocate the heap space for the Python objects while the inbuilt garbage collector will recycle all the memory that’s not being used to boost available heap space.
19. What are membership operators?
A: With the operators 'in' and 'not in', we can confirm if a value is a member/part of another.

>>>"python" in "python is good"
>>>"bad" not in "python is good"

20. Explain identity operators in Python.
A: The operators ‘is’ and ‘is not’ tell us if two values have the same identity.
>>> 10 is '10'
>>> True
>>> True is not False
>>> True

DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap