Python Interview Questions and Answers Set 7

61. Explain the role of repr function.

Python can convert any value to a string by making use of two functions repr() or str(). The str() function returns representations of values which are human­readable, while repr() generates representations which can be read by the interpreter. repr() returns a machine­readable representation of values, suitable for an exec command. Following code sniipets shows working of repr() & str() :

deffun():

y=2333.3

x=str(y)

z=repr(y)

print” y :”,y

print”str(y) :”,x

print”repr(y):”,z

fun()

————-

Output:

2333.3 str(y):2333.3 repr(y):2333.3000000000002

62. What is LIST comprehensions features of Python used for?

LIST comprehensions features were introduced in Python version 2.0, it creates a new list based on existing list. It maps a list into another list by applying a function to each of the elements of the existing list. List comprehensions creates lists without using map() , filter() or lambda form..

63. Explain how to copy an object in Python.?

There are two ways in which objects can be copied in python. Shallow copy & Deep copy. Shallow copies duplicate as minute as possible whereas Deep copies duplicate everything. If a is object to be copied then …

­copy.copy(a) returns a shallow copy of a.

copy.deepcopy(a) returns a deep copy of a.

64. Describe how to send mail from a Python script?

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine.

A sample email is demonstrated below.

import smtplib

SERVER = smtplib.SMTP(‘smtp.server.domain’)

FROM = sender@mail.com

TO = [“user@mail.com”] # must be a list

SUBJECT = “Hello!”

TEXT = “This message was sent with Python’s smtplib.”

Main message message = “””

From: Lincoln < sender@mail.com >

To: CarreerRide user@mail.com

Subject: SMTP email msg

This is a test email. Acknowledge the email by responding.

“”” % (FROM, “, “.join(TO), SUBJECT, TEXT) server = smtplib.SMTP(SERVER) server.sendmail(FROM, TO, message) server.quit()

65. Which of the languages does Python resemble in its class syntax

C++.

66. Python ­ How to create a multidimensional list?

There are two ways in which Multidimensional list can be created:

By direct initializing the list as shown below to create myList below.

>>>myList =[[227,122,223],[222,321,192],[21,122,444]]

>>>printmyList[0]

>>>printmyList[1][2]

___________________

Output

[227, 122, 223]

192


67. The second approach is to create a list of the desired length first and then fill in each element with a newly created lists demonstrated below :

>>>list=[0]*3

>>>fori inrange(3):

>>>list[i]=[0]*2

>>>fori inrange (3):

>>>forj inrange(2):

>>>list[i][j]=i+j

>>>printlist

__________________________

Output

[[0,1],[1,2],[2,3]]


68. Explain the disadvantages of python

Disadvantages of Python are: Python isn’t the best for memory intensive tasks.

Python is interpreted language & is slow compared to C/C++ or Java.

69. Explain how to make Forms in python.

As python is scripting language forms processing is done by Python. We need to import cgi module to access form fields using FieldStorage class.

Every instance of class FieldStorage (for ‘form’) has the following attributes: form.name: The name of the field, if specified

form.filename: If an FTP transaction, the client­side filename.

form.value: The value of the field as a string.

form.file: file object from which data can be read.

form.type: The content type, if applicable.

form.type_options: The options of the ‘content­type’ line of the HTTP request, returned as a dictionary.

form.disposition: The field ‘content­disposition’; None if unspecified.

form.disposition_options: The options for ‘content­disposition’.

form.headers: All of the HTTP headers returned as a dictionary.

A code snippet of form handling in python:

Import cgi

form =cgi.FieldStorage()

ifnot(form.has_key(“name”)andform.has_key(“age”)): print”<H1>Name & Age not         Entered</H1>” print”Fill the Name & Age accurately.”

return

print”<p>name:”,form[“name”].value print”<p>Age:”,form[“age”].value

70. Explain how python is interpreted.

• Python program runs directly from the source code. Each type Python programs are executed code is required. Python converts source code written by the programmer into intermediate language which is again translated it into the native language machine language that is executed. So Python is an Interpreted language.