WNS Global Python Latest Interview Questions

WNS Global Most Frequently Asked Python Latest Interview Questions Answers

How Do I Share Global Variables Across Modules?

The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere.

For example:

config.py:

x = 0 # Default value of the 'x' configuration setting
mod.py:
import config
config.x = 1
main.py:
import config
import mod
print config.x

How Do I Copy An Object In Python?

In general, try copy.copy() or copy.deepcopy() for the general case. Not all objects can be copied, but most can.
Some objects can be copied more easily. Dictionaries have a copy() method:
newdict = olddict.copy()
Sequences can be copied by slicing:
new_l = l[:]
WNS Global Most Frequently Asked Python Latest Interview Questions Answers
WNS Global Most Frequently Asked Python Latest Interview Questions Answers

What Are The Built-In Types Available In Python?

Here is the list of most commonly used built-in types that Python supports:

Immutable built-in types of Python

Numbers
Strings
Tuples

Mutable built-in types of Python

List
Dictionaries
Sets

How To Find Bugs Or Perform Static Analysis In A Python Application?

You can use PyChecker, which is a static analyzer. It identifies the bugs in Python project and also reveals the style and complexity related bugs.
Another tool is Pylint, which checks whether the Python module satisfies the coding standard.

When Is The Python Decorator Used?

Python decorator is a relative change that you do in Python syntax to adjust the functions quickly.

 What Is The Key Difference Between A List And The Tuple?

List Vs Tuple.
The major difference between a list and the tuple is that the list is mutable while tuple is not. A tuple is allowed to be hashed, for example, using it as a key for dictionaries.

 How Does Python Handle The Memory Management?

Python uses private heaps to maintain its memory. So the heap holds all the Python objects and the data structures. This area is only accessible to the Python interpreter; programmers can’t use it.
And it’s the Python memory manager that handles the Private heap. It does the required allocation of the heap for Python objects.
Python employs a built-in garbage collector, which salvages all the unused memory and offloads it to the heap space.

What Are The Principal Differences Between The Lambda And Def?

Lambda Vs Def.
def can hold multiple expressions while lambda is a uni-expression function.
def generates a function and designates a name so as to call it later. lambda forms a function and returns the function itself.
def can have a return statement. lambda can’t have return statements
lambda supports to get used inside a list and dictionary.

Write A Reg Expression That Confirms An Email Id Using The Python Reg Expression Module <Re>?

Python has a regular expression module <re>.

Check out the <re> expression that can check the email id for .com and .co.in subdomain.

import re
print(re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","micheal.pages@mp.com"))


What Do You Think Is The Output Of The Following Code Fragment? Is There Any Error In The Code?

list = ['a', 'b', 'c', 'd', 'e']
print (list[10:])
The result of the above lines of code is []. There won’t be any error like an IndexError.

You should know that trying to fetch a member from the list using an index that exceeds the member count (for example, attempting to access list[10] as given in the question) would yield an IndexError. By the way, retrieving only a slice at an opening index that surpasses the no. of items in the list won’t result in an IndexError. It will just return an empty list.

Explain the dictionary in Python ?

Python's built-in data type is dictionary, which defines one-to-one relationships between keys and values.

Dictionaries consist of pairs of keys and their corresponding values.

Dictionaries are indexed by keys.

Dictionary is similar to associative array or hash table of other languages.

As following example explains further- India, Angel & Cartoon are keys & their corresponding values are Bharat, Teresa & Mickey respectively.

>>> dict = {'India': 'Bharat', 'Angel': ‘Teresa’, 'Cartoon': 'Mickey'}
>>>print dict[India]
Bharat
>>>print dict[Angel]
Teresa.

What is the output of print list if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]?

It will print concatenated lists. Output would be [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ].

What is the output of print list[0] if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]?

It will print first element of the list. Output would be abcd.

What is the output of print list[1:3] if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]?

It will print elements starting from 2nd till 3rd. Output would be [786, 2.23].

What is the output of print list[2:] if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]?

It will print elements starting from 3rd element. Output would be [2.23, ‘john’, 70.200000000000003].

What is the output of print tinylist * 2 if tinylist = [123, ‘john’]?

It will print list two times. Output would be [123, ‘john’, 123, ‘john’].

What is the output of print list + tinylist * 2 if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ] and tinylist = [123, ‘john’]?

It will print concatenated lists. Output would be [‘abcd’, 786, 2.23, ‘john’, 70.2, 123, ‘john’, 123, ‘john’].

What is tuples in Python?

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

Post a Comment

Previous Post Next Post