Python Interview Questions
Learn Python in Less than 8 Hrs Sitting @ Home /Desk with HandsOn
Q41. What is the dictionary in Python?
Ans: Dictionary is a key value pair and will be used inside this {}. For instance, see below name as key and age as value.
{“Amit”:32, “Rakesh”:37, ”Jayesh”:42}
You cannot have duplicate keys in dictionary. Also, key should be immutable.
Q42. What is global variable?
Ans: A variable, which can be defined outside the function and can be accessed from any function is known as global variables.
Q43. Which module, you can use to debug Python code?
Ans : There is a module called pdb, which you can use for debugging.
Q44. Which tool you can use, which can help you to find the bugs in your Python source code?
Ans: You can use PyChecker tool, this will help you find the issue with your source code and also help you to do static analysis for Python.
Q45. Is there any tool or library which I can use to convert my Python code as windows executables or windows exe?
Ans: You can use py2exe tool, it convert the Python scripts into windows executables or exe and to run it you don’t have to explicitly install the python on your windows machine.
Q46. What is UnboundLocalError and why you get it?
Ans: UnboundLocalError is a error when , you try to assign a local variable which is not initialized.
Let’s check following steps
name='Amit'
def fullname():
print name
fullname() # call the function, it will work fine.
Now define the function as below
name='Amit'
def fullname():
print name
name= name+'Khanna'
fullname() # call the function, it will give error ‘UnboundLocalError’
Reason: This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in fullname assigns a new value to name, the compiler recognizes it as a local variable. Consequently when the earlier print name attempts to print the uninitialized local variable and an error results.
Q47. How can be avoided ‘UnboundLocalError’ with the global variable?
Ans: You have to mark the variable global explicitly
def fullname():
global name
print name
name= name+'Khanna'
print name
fullname()
Q48. I am still confused with the local and global variables of Python?
Ans : If you have defined variable outside the function and only referencing them inside the function is considered global variable. But if you try to assign them with same or new value then it will be considered a local variable and local variable must be initialized first to use it else you will get UnboundLocalError’
Q49. You are having below code with the Lambda
doubled= []
for v1 in range(5) :
doubled.append(lambda:2*v1)
print(doubled[0](), doubled[1](), doubled[2](), doubled[3]() , doubled[4]())
Why all the elements in doubled list are 10
Ans: Because v1 is not a local variable and it is defined in the outer scope and will be accessed in lambda function. Hence, when the loop ends, value of the v1 variable will be 4. Hence, all the values in the list will become 8.
To avoid this, you have to define a new variable that is local to the lambda as below
doubled= []
for v1 in range(5) :
doubled.append(lambda x=v1:2*x)
print(doubled[0](), doubled[1](), doubled[2](), doubled[3]() , doubled[4]())
Now, here x is local variable to lambda, which holds the current value and then doubled and then appended to list.
Q50. How to make a variable shared across all the modules in your application and it should be global?
Ans: If you want to make a variable global across all the modules in your application than you have to define a new module usually config.py and then define that global variable in that module.
Import the config.py module in each of your application module. And as you know, only one instance of a particular module will be created. Hence, any change you make to global variable in any module will be reflected in each module. See below example with three different module files
config.py
price=1000 # Default price for the course will be 1000INR
hadoop.py
import config
config.price=2000 #Increase the price to 2000INR and same will be reflected in each module
course_detail.py
import config
import hadoop
print(config.price)
Learn Python in Less than 8 Hrs Sitting @ Home /Desk with HandsOn
Premium Training : Spark Full Length Training : with Hands On Lab
- Spark Certification & Training Material
- Python Interview Questions-1
- Python Interview Questions-2
- Python Interview Questions-3
- Python Interview Questions-4
- Python Interview Questions-5
- Python Interview Questions-6
- Python Interview Questions-7
- Python Interview Questions-8
- Python Interview Questions-9
- Python Interview Questions-10
- Cloudera (Hadoop/Data Engineer) Certification
- Hortonworks (BigData) Certifications
Home Spark Hadoop NiFi Java
Disclaimer :
1. Hortonworks® is a registered trademark of Hortonworks.
2. Cloudera® is a registered trademark of Cloudera Inc
3. Azure® is aregistered trademark of Microsoft Inc.
4. Oracle®, Java® are registered trademark of Oracle Inc
5. SAS® is a registered trademark of SAS Inc
6. IBM® is a registered trademark of IBM Inc
7. DataStax ® is a registered trademark of DataStax
8. MapR® is a registered trademark of MapR Inc.
2014-2017 © HadoopExam.com | Dont Copy , it's bad Karma |