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 humanreadable, while repr() generates representations which can be read by the interpreter. repr() returns a machinereadable representation of values, suitable for an exec command. Following code sniipets shows working of repr() & str() :
print” y :”,y
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.
SERVER = smtplib.SMTP(‘smtp.server.domain’)
FROM = firstname.lastname@example.org
TO = [“email@example.com”] # must be a list
SUBJECT = “Hello!”
TEXT = “This message was sent with Python’s smtplib.”
Main message message = “””
From: Lincoln < firstname.lastname@example.org >
To: CarreerRide email@example.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
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.
[227, 122, 223]
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 :
>>>fori inrange (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 clientside 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 ‘contenttype’ line of the HTTP request, returned as a dictionary.
form.disposition: The field ‘contentdisposition’; None if unspecified.
form.disposition_options: The options for ‘contentdisposition’.
form.headers: All of the HTTP headers returned as a dictionary.
A code snippet of form handling in python:
ifnot(form.has_key(“name”)andform.has_key(“age”)): print”<H1>Name & Age not Entered</H1>” print”Fill the Name & Age accurately.”
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.