Top Python Interview Questions and Answers
Top Python Interview Questions and Answers
1. List.
2. Number.
3. String.
4. Dictionary.
5. Tuples.
Tuples Lists
Example of Tuple Code is, tup = (1, "a", "string", 1+2) Example of Lists Code is, L = [1, "a" ,
"string" , 1+2]
def reverse(s):
str = ""
for i in s:
str = i + str
return str
Question: Why do we need a break in Python?
Answer: Break helps in controlling the Python loop by breaking the current loop from
execution and transfer the control to the next block.
Question: How many ways can be applied for applying reverse string?
Answer: There are five ways in which the reverse string can be applied which include
the following.
1. Loop
2. Recursion
3. Stack
4. Extended Slice Syntax
5. Reversed
Question: What are the different stages of the Life Cycle of a Thread?
Answer: The different stages of the Life Cycle of a Thread can be stated as follows.
Stage 1: Creating a class where we can override the run method of the Thread
class.
Stage 2: We make a call to start() on the new thread. The thread is taken
forward for scheduling purposes.
Stage 3: Execution takes place wherein the thread starts execution, and it
reaches the running state.
Stage 4: Thread wait until the calls to methods including join() and sleep() takes
place.
Stage 5: After the waiting or execution of the thread, the waiting thread is sent
for scheduling.
Stage 6: Running thread is done by executing the terminates and reaches the
dead state.
Question: Explain how is it possible to Get the Google cache age of any URL
or webpage using Python.
Answer: In order to Get the Google cache age of any URL or webpage using Python,
the following URL format is used:
http://webcache.googleusercontent.com/search?q=cache:URLGOESHERE
Simply replace URLGOESHERE with the web address of the website or webpage
whose cache you need to retrieve and see in Python.
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.sqlite3',
}
}
If you need to use a database server other than the SQLite, such as MS SQL, MySQL,
and PostgreSQL, then you need to use the database’s administration tools to create a
brand new database for your Django project.
You have to modify the following keys in the DATABASE ‘default’ item to make the new
database work with the Django project:
NOTE: - Settings like Host, Password, and User needs to be added when not choosing
SQLite as the database.
Check out the advantages and disadvantages of Django.
A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A4 = [i for i in A1 if i in A3]
A5 =
print(A0,A1,A2,A3,A4,A5,A6)
dict={‘Website’:‘hackr.io’,‘Language’:‘Python’:‘Offering’:‘Tutorials’}
Question: Python supports negative indexes. What are they and why are they
used?
Answer: The sequences in Python are indexed. It consists of positive and negative
numbers. Positive numbers use 0 as the first index, 1 as the second index, and so on.
Hence, any index for a positive number n is n-1.
Unlike positive numbers, index numbering for the negative numbers start from -1 and it
represents the last index in the sequence. Likewise, -2 represents the penultimate
index. These are known as negative indexes. Negative indexes are used for:
Removing any new-line spaces from the string, thus allowing the string to except
the last character, represented as S[:-1]
Showing the index to representing the string in the correct order
Question: Suppose you need to collect and print data from IMDb top 250
Movies page. Write a program in Python for doing so. (NOTE: - You can limit
the displayed information for 3 fields; namely movie name, release year, and
rating.)
Answer:
import requests
import sys
url = 'http://www.imdb.com/chart/top'
response = requests.get(url)
soup = BeautifulSoup(response.text)
tr = soup.findChildren("tr")
tr = iter(tr)
next(tr)
row = title + ' - ' + year + ' ' + ' ' + rating
print(row)
try: if '1' != 1:
raise "someError"
else: print("someError has not occured")
except "someError": pr
int ("someError has occured")
# m.py
class MyClass:
def f(self):
print "f()"
We can monkey-patch the program something like this:
import m
def monkey_f(self):
print "monkey_f()"
m.MyClass.f = monkey_f
obj = m.MyClass()
obj.f()
Question: What is Flask and what are the benefits of using it?
Answer: Flask is a web microframework for Python with Jinja2 and Werkzeug as its
dependencies. As such, it has some notable advantages:
Question: Whenever Python exits, all the memory isn’t deallocated. Why is it
so?
Answer: Upon exiting, Python’s built-in effective cleanup mechanism comes into play
and try to deallocate or destroy every other object.
However, Python modules that are having circular references to other objects or the
objects that are referenced from the global namespaces aren’t always deallocated or
destroyed.
This is because it is not possible to deallocate those portions of the memory that are
reserved by the C library.
import numpy as np
print(arr.argsort()[-3:][::-1])
Output:
[4 3 1]
shuffle(x)
print(x)
Output:
Lists
Sets
Dictionaries
Strings
Tuples
Numbers
os.remove(filename)
os.unlink(filename)
os
sys
math
random
data time
JSON
1. Import OS module
2. Use os.remove() function
import array
import array as arr
from array import *