Python Irerview Question and Answers
Python Irerview Question and Answers
- Python follows a depth-first lookup order and hence ends up calling the
method from class A. By following the method resolution order, the lookup
order as follows. Python follows depth-first order to resolve the methods
and attributes.
ans: shallow copy:will take a copy of the original object and create a
new compound object but if the object we are copying is a compound
object the inner
objects will be the same as the ones found in the original object.
deep copy: A deep copy will take a copy of the original object and
will then recursively take copy of the inner objects which are found (if
any)
- What is 'init' keyword in python?
This method called when an object is created from the class and it
allow the class to initialize the attributes of a class
class it does not have to be named self you can call whatever you
like but it has to be the first parameter of any function in class
-what is polymorphism in python?
ans: A child class inherits all the methods from the parent class.
However, in some situations
the method inherited from the parent class doesn’t quite fit into the
child class. In such cases, you will
ans: Two methods cannot have the same name in Python; hence
method overloading is a feature that allows the same operator to have
different meanings
ans: the default arguments is nothing but when you use parameter
inside the function but you didn't call parameter
ans: If you have some functions with many parameters and you want to
specify only some of them, then you can give values
Suppose you want to terminate a loop and skip to the next code
after the loop; break will help you do that
ans:list and tuple are heterogeneous ordered data type means we can
any type of values like int,string,flot,complex
but lists are mutable datatype and tuple are immutable datatype we
cannot update once we assigne the value
- What is dictnorys in python?
print(even_nums)
values=[1,2,3,4,5,6,7,8,9,10]
The iterator simply implements the Python's iterator protocol. The iterator
protocol is a Python class which comes with two special methods,
namely __iter__() and __next__() . With these two methods, the iterator is
able to compute the next value in the iteration.
Generators:
A package must hold the file __init__.py. This does not apply to
modules.
To import everything from a module, we use the wildcard *. But this
does not work with packages.
ans: the diffference resides in memory when using range() the entire
list will be allocated in memory where as xrange() returns a
genarator(actually it returns an xrange object
-What is Polymorphism?
- What are inbuild data types available in Python -OR Explain Mutable
and Immutable data Types
ans: numirical(int,flot,complex)
sequence(list,tuple dictinories)
boolen(true,false)
sets
dictionary
ans: global varibles are declared outside of the function and local are
declared inside the function while global varibles cannot be directly
chainged in the function
you can use global keyword to creat a function that will chaing the
value of global varible
class it does not have to be named self you can call whatever you
like but it has to be the first parameter of any function in class
ans:
Ans: In Python, you can access a file by using the open() method. However,
using the open() method directly requires you to use the close() method to
close the file explicitly. Instead, you can create a context using the with Open
statement in python. It returns a file object, which has methods and attributes
for getting information about and manipulating the opened file
1. Ans: Read Only (‘r’) : Open text file for reading. The handle is positioned at
the beginning of the file. If the file does not exists, raises the I/O error. This is
also the default mode in which a file is opened.
2. Read and Write (‘r+’): Open the file for reading and writing. The handle is
positioned at the beginning of the file. Raises I/O error if the file does not
exist.
3. Write Only (‘w’) : Open the file for writing. For the existing files, the data is
truncated and over-written. The handle is positioned at the beginning of the
file. Creates the file if the file does not exist.
4. Write and Read (‘w+’) : Open the file for reading and writing. For an existing
file, data is truncated and over-written. The handle is positioned at the
beginning of the file.
5. Append Only (‘a’): Open the file for writing. The file is created if it does not
exist. The handle is positioned at the end of the file. The data being written
will be inserted at the end, after the existing data.
6. Append and Read (‘a+’) : Open the file for reading and writing. The file is
created if it does not exist. The handle is positioned at the end of the file.
The data being written will be inserted at the end, after the existing data.
- What is Pythonpath?
- How exception handled in Python. [Try, Except, Else And Finally Block]
Ans: The methods dict. keys() and dict. values() return lists of
the keys or values explicitly. There's also an items() which
returns a list of (key, value) tuples, which is the most efficient
way to examine all the key value data in the dictionary
- Bonus Tip