What is the difference between list and tuples in Python?
LIST:
-
Lists are mutable i.e they can be edited.
-
Lists are slower than tuples.
-
Syntax: list_1 = [10, ‘Chelsea’, 20]
TUPLES:
-
Tuples are immutable (tuples are lists which can’t be edited).
-
Tuples are faster than list.
-
Syntax: tup_1 = (10, ‘Chelsea’ , 20)
Differentiate between deep and shallow copy.
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)
A shallow copy, however, 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)
What is the difference between .py and .pyc files?
.py files are Python source files. .pyc files are the compiled bvtecode files that are generated by the Python compiler.
Explain the ternary operator in Python.
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
2
>>> print("Hi") if a<b else print("Bye")
Hi
How is memory managed in Python?
-
Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.
-
The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code.
-
Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.
What is namespace in Python?
A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.
How is multithreading achieved in Python?
A thread is a lightweight process, and multithreading allows us to execute multiple threads at once. As you know, Python is a multithreaded language. It has a multi-threading package.
The GIL (Global Interpreter Lock) ensures that a single thread executes at a time. A thread holds the GIL and does a little work before passing it on to the next thread. This makes for an illusion of parallel execution. But in reality, it is just threaded taking turns at the CPU. Of course, all the passing around adds overhead to the execution.
What is PYTHONPATH?
It is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
Explain inheritance in Python.
When one class inherits from another, it is said to be the child/derived/sub class inheriting from the parent/base/super class. It inherits/gains all members (attributes and methods).
Python Interview Questions – inheritance in Python.
Inheritance lets us reuse our code, and also makes it easier to create and maintain applications. Python supports the following kinds of inheritance:
-
Single Inheritance- A class inherits from a single base class.
-
Multiple Inheritance- A class inherits from multiple base classes.
-
Multilevel Inheritance- A class inherits from a base class, which, in turn, inherits from another base class.
-
Hierarchical Inheritance- Multiple classes inherit from a single base class.
-
Hybrid Inheritance- Hybrid inheritance is a combination of two or more types of inheritance.
How are arguments passed by value or by reference?
Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result, you cannot change the value of the references. However, you can change the objects if it is mutable.
.png)