Keyword

Result: 213 questions

Compare Java & Python

Answer:

Criteria Java Python
Ease of use Good Very Good
Speed of coding Average Excellent
Data types Static typed Dynamically typed
Data Science & machine learning applications Average Very Good
View

How are the functions help() and dir() different?

Answer:

These are the two functions that are accessible from the Python Interpreter. These two functions are used for viewing a consolidated dump of built-in functions.

help() - it will display the documentation string. It is used to see the help related to modules, keywords, attributes, etc.
To view the help related to string datatype, just execute a statement help(str) – it will display the documentation for 'str, module.
◦ Eg: >>>help(str) or
>>>help() - it will open the prompt for help as help>
 to view the help for a module, help> module module name
Inorder to view the documentation of 'str' at the help>, type help>modules str
to view the help for a keyword, topics, you just need to type, help> “keywords python-
keyword” and “topics list”
 dir() - will display the defined symbols.
 Eg: >>>dir(str) – will only display the defined symbols.
View

Which command do you use to exit help window or help command prompt?

Answer:

quit
When you type quit at the help’s command prompt, python shell prompt will appear by closing the help window automatically.

View

Explain how Python does Compile-time and Run-time code checking?

Answer:

Python performs some amount of compile-time checking, but most of the checks such as type, name, etc are postponed until code execution. Consequently, if the Python code references a user -defined function that does not exist, the code will compile successfully. In fact, the code will fail with an exception only when the code execution path references the function which does not exists.

View

Whenever Python exists Why does all the memory is not de-allocated / freed when Python exits?

Answer:

Whenever Python exits, especially those python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de – allocated/freed/uncollectable.
It is impossible to deallocate those portions of memory that are reserved by the C library.
On exit, because of having its own efficient clean up mechanism, Python would try to deallocate/
destroy every object.

View

Explain Python’s zip() function.?

Answer:

zip() function- it will take multiple lists say list1, list2, etc and transform them into a single list of tuples by taking the corresponding elements of the lists that are passed as parameters. Eg:

list1 = ['A',
'B','C'] and list2 = [10,20,30].
zip(list1, list2) # results in a list of tuples say [('A',10),('B',20),('C',30)]

whenever the given lists are of different lengths, zip stops generating tuples when the first list ends.

View

Explain Python’s pass by references Vs pass by value . (or) Explain about Python’s parameter passing mechanism?

Answer:

In Python, by default, all the parameters (arguments) are passed “by reference” to the functions. Thus, if you change the value of the parameter within a function, the change is reflected in the calling function.We can even observe the pass “by value” kind of a behaviour whenever we pass the arguments to functions that are of type say numbers, strings, tuples. This is because of the immutable nature of them.

View

As Everything in Python is an Object, Explain the characteristics of Python’s Objects.

Answer:

• As Python’s Objects are instances of classes, they are created at the time of instantiation. Eg: object-name = class-name(arguments)
• one or more variables can reference the same object in Python
• Every object holds unique id and it can be obtained by using id() method. Eg: id(obj-name) will return unique id of the given object.
every object can be either mutable or immutable based on the type of data they hold.
• Whenever an object is not being used in the code, it gets destroyed automatically garbage collected or destroyed
• contents of objects can be converted into string representation using a method

View

Explain how to overload constructors or methods in Python.

Answer:

Python’s constructor – _init__ () is a first method of a class. Whenever we try to instantiate a object __init__() is automatically invoked by python to initialize members of an object.

View

Which statement of Python is used whenever a statement is required syntactically but the program needs no action?

Answer:

Pass – is no-operation / action statement in Python
If we want to load a module or open a file, and even if the requested module/file does not exist, we want to continue with other tasks. In such a scenario, use try-except block with pass statement in the except block.
Eg:

 try:import mymodulemyfile = open(“C:\myfile.csv”)except:pass
View

What is Web Scraping? How do you achieve it in Python?

Answer:

Web Scrapping is a way of extracting the large amounts of information which is available on the web sites and saving it onto the local machine or onto the database tables.
In order to scrap the web:load the web page which is interesting to you. To load the web page, use “requests” module.
parse HTML from the web page to find the interesting information.Python has few modules for scraping the web. They are urllib2, scrapy, pyquery, BeautifulSoap, etc.

View

What is a Python module?

Answer:

A module is a Python script that generally contains import statements, functions, classes and variable definitions, and Python runnable code and it “lives” file with a ‘.py’ extension. zip files and DLL files can also be modules.Inside the module, you can refer to the module name as a string that is stored in the global variable name .
A module can be imported by other modules in one of the two ways. They are
1. import
2. from module-name import or

View

Name the File-related modules in Python?

Answer:

Python provides libraries / modules with functions that enable you to manipulate text files and binary files on file system. Using them you can create files, update their contents, copy, and delete files. The libraries are : os, os.path, and shutil.
Here, os and os.path – modules include functions for accessing the filesystem
shutil – module enables you to copy and delete the files.

View

Explain the use “with” in python?

Answer:

In python generally “with” statement is used to open a file, process the data present in the file, and also to close the file without calling a close() method. “with” statement makes the exception handling simpler by providing cleanup activities.
General form of with:
with open(“file name”, “mode”) as file-var:
processing statements
note: no need to close the file by calling close() upon file-var.close()

View

Explain all the file processing modes supported by Python ?

Answer:

Python allows you to open files in one of the three modes. They are:
read-only mode, write-only mode, read-write mode, and append mode by specifying the flags “r”, “w”, “rw”, “a” respectively.
A text file can be opened in any one of the above said modes by specifying the option “t” along with
“r”, “w”, “rw”, and “a”, so that the preceding modes become “rt”, “wt”, “rwt”, and “at”.A binary file can be opened in any one of the above said modes by specifying the option “b” along with “r”, “w”, “rw”, and “a” so that the preceding modes become “rb”, “wb”, “rwb”, “ab”.

View

Explain how to redirect the output of a python script from standout(ie., monitor) on to a file ?

Answer:

They are two possible ways of redirecting the output from standout to a file.
1. Open an output file in “write” mode and the print the contents in to that file, using sys.stdout attribute.
import sys
filename = “outputfile” sys.stdout = open() print “testing”
2. you can create a python script say .py file with the contents, say print “testing” and then redirect it to the output file while executing it at the command prompt.
Eg: redirect_output.py has the following code:
print “Testing”
execution: python redirect_output.py > outputfile.

View

How do you create a dictionary which can preserve the order of pairs?

Answer:

We know that regular Python dictionaries iterate over <key, value> pairs in an arbitrary order, hence they do not preserve the insertion order of <key, value> pairs.
Python 2.7. introduced a new “OrderDict” class in the “collections” module and it provides the same interface like the general dictionaries but it traverse through keys and values in an ordered manner depending on when a key was first inserted.
Eg:

from collections import OrderedDict
d = OrderDict([('Company-id':1),('Company-Name':'Intellipaat')])
d.items() # displays the output as: [('Company-id':1),('Company-Name':'Intellipaat')]
View

What is the use of enumerate() in Python?

Answer:

Using enumerate() function you can iterate through the sequence and retrieve the index position and its corresponding value at the same time.
>>> for i,v in enumerate([‘Python’,’Java’,’C++’]):
print(i,v)
0 Python
1 Java
2 C++

View

How many kinds of sequences are supported by Python? What are they?

Answer:

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

View

How do you perform pattern matching in Python? Explain

Answer:

Regular Expressions/REs/ regexes enable us to specify expressions that can match specific “parts” of a given string. For instance, we can define a regular expression to match a single character or a digit, a telephone number, or an email address, etc.The Python’s “re” module provides regular expression patterns and was introduce from later versions of Python 2.5. “re” module is providing methods for search text strings, or replacing text strings along with methods for splitting text strings based on the pattern defined.

View

© 2017 QuizBucket.org