Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
591 views
in Python Programming by Goeduhub's Expert (3.1k points)
Top asked python interview questions and detailed explained answers

2 Answers

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

Question 25: What is  difference between help() and dir() ?

Answer: 

help () : As we know Python has a lot of modules, classes , functions and methods . It  is not possible to remember these.

So if you are in middle of code and got stuck due to you don't remember the module , method and keyword. You just have to pass the keyword (module, function and any keyword) in  help() function and you will get the documentation of the related keyword (passed keyword).

Note that if you don't pass any keyword, then during program execution help() will ask you to pass one . And if keyword is not related to python, then it will return 'no documentation for the keyword'.

Let's see with an example.

#help() with keyword
l=[1,2,3,4]
help(1)

Output:

help()

Note: It's not possible to show whole output here , it's just a small snapshot.

dir(): dir() also return the information of passed keyword/object but unlike help() (help return documentation)  it just return the valid attributes and methods of the passed keyword.

With no argument passed in dir() , it return the attributes and methods in current scope.

#dir() with keyword
l=[1,2,3,4]
dir(1)

Output:

dir()

Question 26: What is decorators in python ? 

Answer:  Decorators: In simple terms decorators in python add or enhance the functionality of other  function (on which decorators applied ) without changing the structure of function itself. 

Decorator itself is a function used on another function to enhance the function functionality without changing its structure.

To understand decorators you must know functions and function's properties very well

Let's understand the concept of decorators with an example-

For example we have a simple question as mentioned below 

#defining a simple function 

def any_function():

  print("Execute anything")

any_function()

Output: 

Execute anything

Now we will defined a new function which takes function as an argument. 

#defining decorators in pyhton 

def dec_fun(fun_agr):

  def dec_fun1():

    print("executing dec_fun1 function ")

    fun_agr()

    print("dec_fun1 excuted")

  return dec_fun1

Note: As you can see we defined a function (dec_fun) which takes function as an argument and a nested function (dec_fun1) in which first we passed a print statement after that we called the argument function (passed in dec_fun) and then again a print statement. 

Now we want to use the  functionality of dec_fun() function by any_function(). Or we can say that we want to use any_function() in dec_fun(). 

#enhance the functionality of dec_fun 

obj = dec_fun(any_function)

obj()

Output:

decorators

Note: You can see that we just passed the any_function to dec_fun as an argument and the functionality of dec_fun is enhanced by any_function Or we can  say that we used an existing function and functionality by another function.  This process is actually known as decorator where we can use a function and its functionality by other function.

We can do the same concept in another way, How ?

# @ in decorators 

@dec_fun 

def any_function():

  print("Execute anything")

any_function()

Output:

decorators

Note: It is simple if you want to enhance a function's functionality you just have to write @ function_name_you_want _to _use. 

meaning @dec_function = dec_fun(any_funciton). 


Question 27: What is PEP 8 in python ? 

Answer: PEP in python stand for - Python Enhancement Proposal.  PEP is actually a designed official python documentation with set of rules to defined the format of python code writing to the python community and also new features and process for python.

pep-8 is specially important as it written by Guido van Rossum and its major task is to improve the readability and consistency of Python code.

We have pep 0 ,pep 1,.......indexes you can visit official website here -click here 


Question 28 : Named some commonly used python modules ?  (OS and SYS module )

Answer: As we know python modules are collection python files (codes) of function and objects. We can use modules directly by importing them. 

Here are some in built python modules - date time , JSON , os , sys , random, math etc...

OS  module in python 

The os (operating system) module in python used to create , remove and change directories. 

Let's see the methods/ functions in os module.

#methods and function of os 

print(dir(os))

Output: 

os

Note: It's not full output but just a snapshot , Try it yourself . Now from the output you can see that os module has functions/methods such as mkdir (to make a new directory , folder), open (to open a file in current directory), rename (to change a file name in current directory) and many more. 

Note that if you change a directory to another, that means the changed directory now is your current working directory.  And you can only modify or update the current working directory. 

Let's display current working directory. 

#os module in pyhton 

import os 

print(os.getcwd())

Output : 

C:\Users\apex

Note: The output shows the current working in which I am doing work, when you will run this program in your computer , it will display the current working in which you are working. You can change current working directory to other by using chdir() function. 

You can also check list of all files and directories in the specified directory.  You can use listdir() function for this purpose. See the example given below.

#display files and directories 

print(os.listdir('C:\\Users\\apex'))

Output: 

os

Sys Module in Python 

Sys module in python provide functions and variables used to manipulate python runtime environment. And access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.

Sys module basically used for system specific parameters and functions. 

We will see some examples to understand sys module.

#exit() in sys 

import sys 

print("Goeduhub tehcnologies")

sys.exit()

print("After exiting system ")

Output :

sys

#sys in python 

import sys

print(sys.version)

Output:

sys

Note: The second output display the current version of python interpreter. And the first output terminate the program where we want it to stop. 

Official Documentation of Sys module 

0 like 0 dislike
by Goeduhub's Expert (3.1k points)

Question 29: How memory managed in python ? 

Answer:  Memory management is a process of allocating and deallocating the objects of a python program and also cleaning of the memory when a object is no longer being accessed. 

In python memory management work is done by python memory manager. You need not to worry, as memory management in python is fully automatic. 

How memory management work take place ?

Memory management in python works on the concept of reference counting.  This means that memory manager keep track the number of references to each object in the program. If a object's reference count is zero means the object is no longer being used and Garbage collector automatically remove the object from the memory .

Garbage Collector: As we already seen garbage collector is basically the hero of memory management in python. In a case where no reference is pointing to an object in memory i.e. it is not in use so, the garbage collector automatically deletes that object from the memory. 

#understanding python memory management 
x=11
y=10
z=y
if id(y)=id(z):
  print("y and z refer to the same object")

Output :

y and z refer to the same object

memory management 

Note: x= 11 means, object  11 is allocated in memory by interpreter and its reference is assigned to variable x.  11 has one reference count. 

In the second where y and z refer same object 10. As you can see,  even though  both variable refer the same object 10 python only create one object in memory. This is how python memory manager works.

memory manager

Now if we change the value of z=4 and y=5. because now the reference count of object 10 is zero, means it will be deleted by garbage collector.to clean the memory.

Type of Memory in Python 

1. Stake Memory : A stake memory stores the temporary variables created by function. It is temporary and faster then heap memory. It is temporary because after computing task completed the variable will be automatically removed.

2. Private Heap Memory: Python objects and data structures stored  in  private heap memory. And allocated by python interpreter. The programmer has no access to private heap memory. 

#memory management 

def fun(x):

  a=11

  b=x*2

  return b

x=2

z=fun(x)

Memory Management for the above program memory

Memory management for the above program after fun() function calculated.

memory

Note : As you can see in the stake memory after computation done, the fun() function and variables erased from the memory (temporary storage). On the other hand 11 is erased from the heap memory because of no reference .

To understand the concept of memory management remember what happened when you refreshed a kernel in jupyter notebook. You must remember that if our program is dependent on the starting of the program, then we have to run the program again from the beginning. Because the memories is freed when kernel is refreshed. 


Question 30: Difference between xrange and range ?

Answer: In functionality both are similar both generate the sequence of integer, then what how they are different with each other. 

range () xrange()
range() function used in python 3.   xrange () is not supported in python3 . Only works in python 2.
return python list  return xrange objects 
It take more memory because of static list takes less memory as generate one object at a time 

Note: Note that xrange() does not supported in python 3 , so we used range () instead of xrange () which work exactly the same as what xrange() used to do in python 2. 

range () : syntax: range(start , end, step ) .

start- default 0 , included. , end- excluded , step - default 1.

#range in python 

x=range(1,6,2)

for i in x:

  print(i)

Output:

1

3

5


Question 31: What is Mapping in python ? (map function in python)

Answer : The map function in python takes two arguments, one is a function other is iterable and apply the function on the elements of iterable , and return map object. You can convert map objects in list, tuple etc...

For example :

#map funciton in python 

def len_of_objects(x):

  return len(x)

x = map(len_of_objects, ('goeduhub','ptyhon', 'machine learning'))

print(x)

#convert map object in to list

print(list(x))

Output:  

<map object at 0x7f0ebafd0b70> [8, 6, 16]

Note: Note that function len_of_objects is applied to each elements of iterable. And map function return the map object (print(x)) and then we converted the map object into a list. 

Top asked python interview questions and detailed explained answers. (Part4)

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...