October 19, 2018

Srikaanth

McAfee Most Frequently Asked Python Latest Interview Questions Answers

What Are The Optional Statements That Can Be Used Inside A <Try-Except> Block In Python?

There are two optional clauses you can use in the <try-except> block.

The <else> clause
It is useful if you want to run a piece of code when the try block doesn’t create any exception.
The <finally> clause
It is useful when you want to execute some steps which run, irrespective of whether there occurs an exception or not.

How Does The Ternary Operator Work In Python?

The ternary operator is an alternative for the conditional statements. It combines of the true or false values with a statement that you need to test. The syntax would look like the one given below.

[onTrue] if [Condition] else [onFalse]

x, y = 35, 75
smaller = x if x < y else y
print(smaller)

What Does The <Self> Keyword Do?

The <self> keyword is a variable that holds the instance of an object. In almost, all the object-oriented languages, it is passed to the methods as hidden parameter.
McAfee Most Frequently Asked Python Latest Interview Questions Answers
McAfee Most Frequently Asked Python Latest Interview Questions Answers

 What Are Different Methods To Copy An Object In Python?

There are two ways to copy objects in Python.

copy.copy() function
It makes a copy of the file from source to destination.
It’ll return a shallow copy of the parameter.
copy.deepcopy() function
It also produces the copy of an object from the source to destination.
It’ll return a deep copy of the parameter that you can pass to the function.

What Is The Purpose Of Doc Strings In Python?

In Python, documentation string is popularly known as doc strings. It sets a process of recording Python functions, modules, and classes.

Which Python Function Will You Use To Convert A Number To A String?

For converting a number into a string, you can use the built-in function <str()>.  If you want an octal or hexadecimal representation, use the inbuilt function <oct()> or <hex()>.

 How Do You Debug A Program In Python? Is It Possible To Step Through Python Code?

Yes, we can use the Python debugger (<pdb>) to debug any Python program. And if we start a program using <pdb>, then it let us even step through the code.

 List Down Some Of The PDB Commands For Debugging Python Programs?

Here are a few PDB commands to start debugging Python code.

Add breakpoint – <b>
Resume execution – <c>
Step by step debugging – <s>
Move to next line – <n>
List source code – <l>
Print an expression – <p>

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.

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 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.

Subscribe to get more Posts :