Python Interview Questions
In this Python Interview Questions blog, I will introduce you to the most frequently asked questions in Python interviews. This blog is the perfect guide for you to learn all the concepts required to clear a Python interview. Our Python Interview Questions is the one-stop resource from where you can boost your interview preparation.
List is mutable and tuples is immutable. The main difference between mutable and immutable is memory usage when you are trying to append an item. When you create a variable, some fixed memory is assigned to the variable. However, if you know that you are not frequently add new elements, then you should use tuples
Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.
|Lists are mutable i.e they can be edited.||Tuples are immutable (tuples are lists which can’t be edited).|
|Lists are homogeneous sequences||Tuples are heterogeneous data structures i.e their entries have different meanings|
|lists have order.||Tuples have structure.|
|Lists are slower than tuples.||Tuples are faster than list.|
list_ex = ["Monday", "Tuesday"]
tuple_ex = ("Monday", "Tuesday")
Q2. What are the key features of Python?
- Python is an interpreter-based language, which allows execution of one instruction at a time.
- Extensive basic data types are supported e.g. numbers (floating point, complex, and unlimited-length long integers), strings (both ASCII and Unicode), lists, and dictionaries.
- Variables can be strongly typed as well as dynamic typed.
- Supports object-oriented programming concepts such as class, inheritance, objects, module, namespace etc.
- Cleaner exception handling support.
- Supports automatic memory management.
- GUI Programming, Dynamically Typed, Embeddable and Portable.
Q3. What is the difference between deep and shallow copy?
Shallow copy is used when a new instance type gets created and it keeps the values that are copied in the new instance. Shallow copy is used to copy the reference pointers just like it copies the values. These references point to the original objects and the changes made in any member of the class will also affect the original copy of it. Shallow copy allows faster execution of the program and it depends on the size of the data that is used.
Deep copy is used to store the values that are already copied. Deep copy doesn’t copy the reference pointers to the objects. It makes the reference to an object and the new object that is pointed by some other object gets stored. The changes made in the original copy won’t affect any other copy that uses the object. Deep copy makes execution of the program slower due to making certain copies for each object that is been called.
Let's take an example where we create a list named old_list and pass an object reference to new_list using = operator.
Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ID of Old List: 52620464 New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ID of New List: 52620464
Adding new nested object using Shallow copy
Old list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]] New list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]]
Adding a new nested object in the list using Deep copy
Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]] New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Q4. How is Multithreading achieved in Python?
- Python has a multi-threading package but if you want to multi-thread to speed your code up, then it’s usually not a good idea to use it.
- Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your ‘threads’ can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread.
- This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core.
- All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn’t a good idea.
Q5. How can the ternary operators be used in python?
The Ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it.
Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It simply allows to test a condition in a single line replacing the multiline if-else making the code compact.
10 10 10 10 b is greater than a b is greater than a b is greater than a