Python Interview Questions for Experienced

Python Interview Questions for Experienced




If any body searching for python interview questions for experienced then this is the page we can provides all type selective questions. Top 30 questions here cited with real time examples. So just review before going for python face to face interview.

What is the difference between Java and Python?

Python.

  1. It is faster than java in terms of performance.
  2. Dynamically typed
  3. Not compatible to many platform.
  4. Database access is weaker than java
  5. Indentation does by only new line.

Java.

  1. It is bit slower than Python in performance wise.
  2. Static typed
  3. Compatible to all Platform.
  4. Database access is Good in Java.
  5. Indentation does by braces.

What is the difference between list and tuples in Python?

List

  1. Lists are mutable
  2. List can be edited
  3. Syntax of list is shown by [] bracket.
  4. Syntax: list_1 = [5, ‘Kumar’, 20]

tuples

  1. Tuples are immutable
  2. Tuples are list but then can’t be edited
  3. Syntax of tuples is shown by parenthesis.
  4. Syntax: tup_1 = (5, ‘Kumar’ , 20)

Write Python logic to count the number of capital letters in a file.

>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> with open('myfilename.txt') as myfile:
count=0
for i in myfile.read():
if i.isupper():
count+=1
print(count)

How to make Indentation in Python

Unlike language like C, C++, Java It has some different Indentation mechanism.
Other language follow the indentation like braces or semicolon, where are Python followed by new line. In order to code readable Programmer must have to make Indentation.
Syntax for example of Indentations

if salary >= 5000:
print('your are getting salary of more than 5k')

What is the difference between .py and .pyc files?

Python compiles the .py and saves files as .pyc
In Simple we can say .py files are Python source files.  where as .pyc files are the compiled bytecode.
Once the *.pyc file is generated, there is no need of *.py file, unless until you edit the .py  file. So it will save your time, No need to compile again an again.

What is the difference between *var and  ** var in Python?

Both  *var and ** var  allow you a pass a variable to number of argument to functions.

Here *var or **var you can change like anything. eg- *kar, **kar (take for your convenience)

*var is used to send a non-keyworded variable length argument list to the function.

# Python program to illustrate
# *var
def singleStar(arg1, *var):
print "first argument :", arg1
for arg in var:
print "Next argument through *var :", arg

singleStar('Hello', 'Welcome', 'to', 'interview-questions-answers')

**var allows you to pass keyworded variable length of arguments to a function.

def doubleStar(**var):
if var is not None:
for key, value in var.iteritems():
print "%s == %s" %(key, value)
doubleStar(name="interview-questions-answers")

What are the Python Web Frameworks for developing web applications?

There are some web frameworks provided by Python. They are follows below
TurboGears –  It permits you to quickly develop protractile data-driven Web applications.
Bottle – It is a micro-framework for developing web application.
web2py – it is the simplest of all the web frameworks used for developing web applications.
cherryPy – it is a Python-based Object  based Web framework.
Flask – it is  a micro-framework for developing and designing web applications.
Sanic– It is a Python web framework built on uvloop (event loop) which is made for fast HTTP responses.

Explain Some in-built functions to manipulate list in Python!

Compares elements of both lists – cmp(list1, list2)

Get the length of the list – len(list)

Get the max value from a list – max(list)

Get the min value from a list –min(list)

Get the lowest index in the list that object appears – list.index(obj)

Inserts object obj into list at offset index –list.insert(index, obj)

Removes and returns last object or obj from list – list.pop(obj=list[-1])

Removes object obj from list- list.remove(obj)

Reverses objects of list in place- list.reverse()

Sorts objects of list, use compare func if given – list.sort([func])

What is the usage of help() and dir() function in Python?

help() is a useful  default function in Python program that can be used to get the documentation of a particular object, method, attributes, etc.

some_list = []
help(some_list.append)

In Python to get all the attributes list we you have to use dir() function, where you need to pass the argument as a object. If you wont pass any argument then it will returns list of current local namespace.

your_list = []
print(dir(your_list))

OutPut : You will get all default local names space like [‘__add__’, ‘__class__’,…]

What are uses of  “/” and “//” operator in python?

Division(“/”): Divides left hand operand by right hand operand.

Here This work like general mathematics way.  suppose

7/2 = 3.5

In general way:
x/y= float(x/y)

Floor Division(“//”): The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored , i.e., rounded away from zero (towards negative infinity).

5//2=2
-11//3 = -4

x // y == math.floor(x/y)

What is dictionary in Python?

In Python Dictionary defines one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys like JSON data or associative array.Lets have a simple example for sake of understanding.

dict={'Name':'Michael','Age':'30','Phone':'99xxxxxx09'}

print dict[Name]

Output: Michael

print dict[Age]

Output: 30

print dict[Phone]

Output: 99xxxxxx09

Differentiate between append() and extend() methods in Python ?

append() and extend() methods are the methods of list in Python. These methods are used to a dd the elements at the end of the list.
append(element) – adds the given element at the end of the list.
extend(another-list) – adds the elements of another-list at the end of the list.

What is a monkey patch?

In Simple you can say, monkey patching is changes can done to a module or a  class while the program is running.

import datetime
datetime.datetime.now = lambda: datetime.datetime(2012, 12, 12)

What is lambda in python and how it used?

In Python functions are defined by using def keyword before function name, If the function having no name means anonymous function then python used lambda before it.

add = lambda a, b: a + b
print(add(40,50))
Output:90

What is docstring in Python?

docstrings provides a way of  associating documentation with Python functions, classes, methods and modules.
It Unlike commenting in Source Code other language C, C++, JAVA, PHP etc. The doc string line should begin with a capital letter and end with a period.

class MyClass(object):
"""The class's docstring"""

def my_method(self):
"""The method's docstring"""

def my_function():
"""The function's docstring"""

What is TkInter?

TkInter is a toolkit for GUI development. It provides support for various  controls for GUI tools  such as text boxes, radio buttons, labels, buttons etc. The common attributes of them include  Colors, Fonts, Cursors, Dimensions etc.

What are list of database access by python?

Python Database API supports Wide range of database server

  • mSQL
  • MySQL
  • PostgreSQL
  • Microsoft SQL Server 2000
  • Informix
  • Interbase
  • Oracle
  • Sybase
  • GadFly

How Could  you send a Email Using Python program?

Generally  SMTP is  controls sending e-mail and routing e-mail between mail servers in other language.But here Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Example-

#!/usr/bin/python

import smtplib

sender = 'example@from.com'
receivers = ['example@to.com']
message = """From: From Person <example@from.com>
To: To Person <example@to.com>
Subject: test email

This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Email has sent successfully"
except SMTPException:
print "Error:Send email Failed"

What is Pickling in Python?

Pickling is alternatively called as serialization. It serialize Python object to  byte stream stored in a file format. Again byte stream can be converted to object by unpickling.

How many types of sequences are supported by Python?

Python supports 7 sequence types. They are like unicode, xrange, str, list, tuple, bytearray and buffer. where xrange is deprecated in python 3.5.X.