Python Interview Question Set 7
Categories: Python
What is pickling and unpickling in Python?
The Python pickle is defined as a module which accepts any Python object and converts it into a string representation. It dumps the Python object into a file using the dump function; this process is called Pickling.
The process of retrieving the original Python objects from the stored string representation is called as Unpickling.
Which programming language is a good choice between Java and Python?
Java and Python both are object-oriented programming languages. Let's compare both on some criteria given below:
CriteriaJavaPython
Ease of useGoodVery Good
Coding SpeedAverageExcellent
Data typesStatic typeDynamic type
Data Science and Machine learning applicationAverageVery Good
What is the usage of help() and dir() function in Python?
Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.
Help() function: The help() function is used to display the documentation string and also facilitates us to see the help related to modules, keywords, and attributes.
Dir() function: The dir() function is used to display the defined symbols.
What are the differences between Python 2.x and Python 3.x?
Python 2.x is an older version of Python. Python 3.x is newer and latest version. Python 2.x is legacy now. Python 3.x is the present and future of this language.
The most visible difference between Python2 and Python3 is in print statement (function). In Python 2, it looks like print "Hello", and in Python 3, it is print ("Hello").
String in Python2 is ASCII implicitly, and in Python3 it is Unicode. The xrange() method has removed from Python 3 version. A new keyword as is introduced in Error handling.
How Python does Compile-time and Run-time code checking?
In Python, some amount of coding is done at compile time, but most of the checking such as type, name, etc. are postponed until code execution. Consequently, if the Python code references a user-defined function that does not exist, the code will compile successfully. The Python code will fail only with an exception when the code execution path does not exist.
What is the shortest method to open a text file and display its content?
The shortest way to open a text file is by using "with" command in the following manner:
Example:
with open("FILE NAME", "r") as fp:
fileData = fp.read()
# To print the contents of the file
print(fileData)
Output:
"The data of the file will be printed."
Give the output of this example: A[3] if A=[1,4,6,7,9,66,4,94].
Since indexing starts from zero, an element present at 3rd index is 7. So, the output is 7.
What is type conversion in Python?
Type conversion refers to the conversion of one data type iinto another.
int() - converts any data type into integer type
float() - converts any data type into float type
ord() - converts characters into integer
hex() - converts integers to hexadecimal
oct() - converts integer to octal
tuple() - This function is used to convert to a tuple.
set() - This function returns the type after converting to set.
How to send an email in Python Language?
To send an email, Python provides smtplib and email modules. Import these modules into the created mail script and send mail by authenticating a user. It has a method SMTP(smtp-server, port). It requires two parameters to establish SMTP connection.
What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.
Example:
import array as arr
User_Array = arr.array('i', [1,2,3,4])
User_list = [1, 'abc', 1.20]
print (User_Array)
print (User_list)
Output:
array('i', [1, 2, 3, 4])
[1, 'abc', 1.2]