Convergys Python Interview Questions Answers

What Is The Command To Debug A Python Program?

The following command helps run a Python program in debug mode.

$ python -m pdb python-script.py

Why And When Do You Use Generators In Python?

A generator in Python is a function which returns an iterable object. We can iterate on the generator object using the <yield> keyword. But we can only do that once because their values don’t persist in memory, they get the values on the fly.

Generators give us the ability to hold the execution of a function or a step as long as we want to keep it. However, here are a few examples where it is beneficial to use generators.

We can replace loops with generators for efficiently calculating results involving large data sets.
Generators are useful when we don’t want all the results and wish to hold back for some time.
Instead of using a callback function, we can replace it with a generator. We can write a loop inside the function doing the same thing as the callback and turns it into a generator.
Convergys Most Frequently Asked Python Latest Interview Questions Answers
Convergys Most Frequently Asked Python Latest Interview Questions Answers

What Does The <Yield> Keyword Do In Python?

The <yield> keyword can turn any function into a generator. It works like a standard return keyword. But it’ll always return a generator object. Also, a function can have multiple calls to the <yield> keyword.

See the example below.

def testgen(index):
  weekdays = ['sun','mon','tue','wed','thu','fri','sat']
  yield weekdays[index]
  yield weekdays[index+1]

day = testgen(0)
print next(day), next(day)

output: sun mon

How To Convert A List Into Other Data Types?

Sometimes, we don’t use lists as is. Instead, we have to convert them to other types.

Turn A List Into A String.

We can use the <”.join()> method which combines all elements into one and returns as a string.

weekdays = ['sun','mon','tue','wed','thu','fri','sat']
listAsString = ' '.join(weekdays)
print(listAsString)

output: sun mon tue wed thu fri sat

Turn A List Into A Tuple.

Call Python’s <tuple()> function for converting a list into a tuple. This function takes the list as its argument. But remember, we can’t change the list after turning it into a tuple because it becomes immutable.

weekdays = ['sun','mon','tue','wed','thu','fri','sat']
listAsTuple = tuple(weekdays)
print(listAsTuple)

output: ('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
Turn A List Into A Set.
Converting a list to a set poses two side-effects.

Set doesn’t allow duplicate entries, so the conversion will remove any such item if found.
A set is an ordered collection, so the order of list items would also change.
However, we can use the <set()> function to convert a list to a set.

weekdays = ['sun','mon','tue','wed','thu','fri','sat','sun','tue']
listAsSet = set(weekdays)
print(listAsSet)

output: set(['wed', 'sun', 'thu', 'tue', 'mon', 'fri', 'sat'])

Turn A List Into A Dictionary.

In a dictionary, each item represents a key-value pair. So converting a list isn’t as straight forward as it were for other data types.

However, we can achieve the conversion by breaking the list into a set of pairs and then call the <zip()> function to return them as tuples.

Passing the tuples into the <dict()> function would finally turn them into a dictionary.

weekdays = ['sun','mon','tue','wed','thu','fri']
listAsDict = dict(zip(weekdays[0::2], weekdays[1::2]))
print(listAsDict)

output: {'sun': 'mon', 'thu': 'fri', 'tue': 'wed'}

How Do You Count The Occurrences Of Each Item Present In The List Without Explicitly Mentioning Them?

Unlike sets, lists can have items with same values. In Python, the list has a <count()> function which returns the occurrences of a particular item.

Count The Occurrences Of An Individual Item.
weekdays = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print(weekdays.count('mon'))

output: 3

What Is NumPy And How Is It Better Than A List In Python?

NumPy is a Python package for scientific computing which can deal with large data sizes. It includes a powerful N-dimensional array object and a set of advanced functions.

Also, the NumPy arrays are superior to the built-in lists. There are a no. of reasons for this.

NumPy arrays are more compact than lists.
Reading and writing items is faster with NumPy.
Using NumPy is more convenient than to the standard list.
NumPy arrays are more efficient as they augment the functionality of lists in Python.

How will you get all the keys from the dictionary?

Using dictionary.keys() function, we can get all the keys from the dictionary object.
print dict.keys()   # Prints all the keys

How will you get all the values from the dictionary?

Using dictionary.values() function, we can get all the values from the dictionary object.
print dict.values()   # Prints all the values

How will you convert a string to an int in python?

int(x [,base]) – Converts x to an integer. base specifies the base if x is a string.

How will you convert a string to a long in python?

long(x [,base] ) – Converts x to a long integer. base specifies the base if x is a string.

How will you convert a string to a float in python?

float(x) − Converts x to a floating-point number.

How will you convert a object to a string in python?

str(x) − Converts object x to a string representation.

How will you convert a object to a regular expression in python?

repr(x) − Converts object x to an expression string.

How will you convert a String to an object in python?

eval(str) − Evaluates a string and returns an object.

How will you convert a string to a tuple in python?

tuple(s) − Converts s to a tuple.

How will you convert a string to a list in python?

list(s) − Converts s to a list.

How will you convert a string to a set in python?

set(s) − Converts s to a set.

Post a Comment

Previous Post Next Post