Python

Python - Object Oriented Scripting Language

1. What is python?

Ans: Python is an open source language that is getting a lot of attention from the market.

1) Created by: Guido van Rossum nearly 11 years ago

2) Python is an interpreted, high-level programming language, pure object-oriented and powerful server-side scripting language for the Web.

3) Python is a good prototype language. In just a few minutes, you can develop prototypes that would take you several hours in other languages.

2. Is python the right choice for Web based Programming?

Ans: Python is another open source programming that has become popular for creating web-related applications and large programs. Scripts written in Python are often very clear to read; the language is also known for its flexibility. Whether you are looking for database tools, image manipulation scripts, or something else entirely, if it is written in Python, you will find it here.

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

3. What are uses of lambda?

Ans: It used to create small anonymous functions at run time. Like e.g.

def fun1(x):

return x**2

print fun1(2)

it gives you answer 4

the same thing can be done using

sq=lambda x: x**2

print sq(2)

it gives the answer 4

4. When you need ordered container of things, which will be manipulated, use lists.

Ans: Dictionary is key, value pair container and hence is not ordered. Use it when you need

fast access to elements, not in ordered fashion. Lists are indexed and index of the list cannot be “string” e.g. list [‘myelement’] is not a valid statement in python.

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

5. When do you use list vs. tuple vs. dictionary vs. set?

Ans: List and Tuple are both ordered containers. If you want an ordered container of

constant elements use tuple as tuples are immutable objects.

6. Do they know a tuple/list/dict when they see it?

Ans: Dictionaries are consisting of pair of keys and values. like {’key’:’value’}.

book={’cprog’:’1024’,’c++’:’4512’}

Keys are unique but values can be same. The main difference between list and tuple is you can change the list but you cannot change the tuple. Tuple can be used as keys in mapping where list is not.

7. What is used to represent Strings in Python? Is double quotes used for String representation or single quotes used for String representation in Python?

Ans: Using Single Quotes (‘)

You can specify strings using single quotes such as ‘Quote me on this’ . All white space i.e. spaces and tabs are preserved as-is.

Using Double Quotes (“”)

Strings in double quotes work exactly the same way as strings in single quotes. An example is “What’s your name?”

Using Triple Quotes (‘’’ or “””)

You can specify multi-line strings using triple quotes. You can use single quotes and double quotes freely within the triple quotes. An example is

‘’’This is a multi-line string. This is the first line.

This is the second line.

“What’s your name?,” I asked.

He said “Bond, James Bond.”

8. Who created the Python programming language?

Ans: Python programming language was created by Guido van Rossum.

9. Why cannot lambda forms in Python contain statements?

Ans: A lambda statement is used to create new function objects and then return them at runtime that is why lambda forms in Python did not contain statement.

10. How is memory managed in Python?

Ans: Memory is managed through private heaps. Private heap is managed by python memory manager.

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

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

Ans: C++ is the appropriate language that Python resemble in its class syntax.

12. Why was the language called as Python?

Ans: At the same time he began implementing Python, Guido van Rossum was also reading the published scripts from "Monty Python's Flying Circus" (a BBC comedy series from the seventies, in the unlikely case you didn't know). It occurred to him that he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.

13. Does Python support strongly for regular expressions? What are the other languages that support strongly for regular expressions?

Ans: Yes, python strongly support regular expression. Other languages supporting regular expressions are: Delphi, Java, Java script, .NET, Perl, Php, Posix, python, Ruby, Tcl, Visual Basic, XML schema, VB script, Visual Basic 6.

14. Why is not all memory freed when Python exits?

Ans: Objects referenced from the global namespaces of Python modules are not always de-allocated when Python exits. This may happen if there are circular references. There are also certain bits of memory that are allocated by the C library that are impossible to free (e.g. a tool like the one Purify will complain about these). Python is, however, aggressive about cleaning up memory on exit and does try to destroy every single object.

If you want to force Python to delete certain things on de-allocation, you can use the at exit module to register one or more exit functions to handle those deletions.

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

15. What are the disadvantages of the Python programming language?

Ans: One of the disadvantages of the Python programming language is it is not suited for fast and memory intensive tasks.

16. What is the language from which Python has got its features or derived its features?

Ans: Most of the object oriented programming languages to name a few are C++, CLISP and Java is the language from which Python has got its features or derived its features.

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

17. What is the Java implementation of Python popularly known as?

Ans: Jython

18. What is the method does join() in python belong?

Ans: String method

19. Does python support switch or case statement in Python? If not what is the reason for the same?

Ans: No. You can use multiple if-else, as there is no need for this.

20. How is the Implementation of Pythons dictionaries done?

Ans: Using curly brackets -> {}

E.g.: {'a':'123', 'b':'456'}

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

21. What is the statement that can be used in Python if a statement is required syntactically but the program requires no action?

Ans: Pass is a no-operation/action statement in python

If we want to load a module and if it does not exist, let us not bother, let us try to do other task. The following example demonstrates that.

Try:

Import module1

Except:

Pass

22. What is all the operating system that Python can run on?

Ans: Python can run of every operating system like UNIX/LINUX, Mac, Windows, and others.

23. What are the uses of List Comprehensions feature of Python?

Ans: List comprehensions help to create and manage lists in a simpler and clearer way than using map(), filter() and lambda. Each list comprehension consists of an expression followed by a clause, then zero or more for or if clauses.

24. What is used to create Unicode string in Python?

Ans: Add u before the string

>>> u 'test'

25. What is the optional statement used in a try except statement in Python?

Ans: There are two optional clauses used in try except statements:

1. Else clause: It is useful for code that must be executed when the try block does not create any exception

2. Finally clause: It is useful for code that must be executed irrespective of whether an exception is generated or not.

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.

26. explain copy of dictionary data in both ways?

Ans:

1) pass by reference

dict_temp = {"q":1,"w":2,"e":3}

dict_new = dict_temp

print "dict_temp\t",dict_temp

print "dict_new\t",dict_new

dict_new["a"]=5

print "dict_temp\t",dict_temp

print "dict_new\t",dict_new

2) Pass by value

dict_temp = {"q":1,"w":2,"e":3}

dict_new = dict_temp.copy()

#or

#dict_new = dict(dict_temp)

print "dict_temp\t",dict_temp

print "dict_new\t",dict_new

dict_new["a"]=5

print "dict_temp\t",dict_temp

print "dict_new\t",dict_new

27. Parse the directory given recursively and provide the number of files present?

Ans.

import os

rootDir = "D:\ " # set the directory you want to start from

countMe=0

for dirName,subdirList,fileList in os.walk( rootDir ) :

#print "Found directory:" , dirName

for fname in fileList :

countMe+=1

print str(countMe)+") -> "+dirName+"\\"+fname

print "Total number of files %(countMe)s"%vars()

If you have some tips/suggestion please mail me @ Anshul24mehta@gmail.com.