Basic Python Interview Questions
Q1. What is the difference between list and tuples?
LIST | TUPLES |
Lists are mutable i.e they can be edited. | Tuples are immutable (tuples are lists which can’t be edited). |
Lists are slower than tuples. | Tuples are faster than list. |
Syntax: list_1 = [10, ‘Chelsea’, 20] | Syntax: tup_1 = (10, ‘Chelsea’ , 20) |
Q2. What are the key features of Python?
- Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
- Python is dynamically typed,
this means that you don’t need to state the types of variables when you
declare them or anything like that. You can do things like
x=111
and thenx="I'm a string"
without error - Python is well suited to object orientated programming
in that it allows the definition of classes along with composition and
inheritance. Python does not have access specifiers (like C++’s
public
,private
), the justification for this point is given as “we are all adults here” - In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects
- Writing Python code is quick
but running it is often slower than compiled languages.
Fortunately,Python allows the inclusion of C based extensions so
bottlenecks can be optimized away and often are. The
numpy
package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python - Python finds use in many spheres – web applications, automation, scientific modelling, big data applications and many more. It’s also often used as “glue” code to get other languages and components to play nice.
Q3. What is the difference between deep and shallow copy?
Ans: 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.
Q4. How is Multithreading achieved in Python?
Ans:- 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?
Ans: 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.
Syntax:
The Ternary operator will be given as:[on_true] if [expression] else [on_false]x, y = 25, 50big = x if x < y else y
Example:
The
expression gets evaluated like if x<y else y, in this case if x<y
is true then the value is returned as big=x and if it is incorrect then
big=y will be sent as a result.
No comments:
Post a Comment