PricewaterhouseCoopers Most Frequently Asked Python Latest Interview Questions Answers
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.
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 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.
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.
PricewaterhouseCoopers Most Frequently Asked Python Latest Interview Questions Answers |
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.
What is the output of print str if str = ‘Hello World!’?
It will print complete string. Output would be Hello World!.
What is the output of print str[0] if str = ‘Hello World!’?
It will print first character of the string. Output would be H.
What is the output of print str[2:5] if str = ‘Hello World!’?
It will print characters starting from 3rd to 5th. Output would be llo.
What is the output of print str[2:] if str = ‘Hello World!’?
It will print characters starting from 3rd character. Output would be llo World!.
What is the output of print str * 2 if str = ‘Hello World!’?
It will print string two times. Output would be Hello World!Hello World!.
What is the output of print str + “TEST” if str = ‘Hello World!’?
It will print concatenated string. Output would be Hello World!TEST.
What is the output of print tuple[1:3] if tuple = ( ‘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 tuple[2:] if tuple = ( ‘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 tinytuple * 2 if tinytuple = (123, ‘john’)?
It will print tuple two times. Output would be (123, ‘john’, 123, ‘john’).
What is the output of print tuple + tinytuple if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2) and tinytuple = (123, ‘john’)?
It will print concatenated tuples. Output would be (‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’).
It will print complete string. Output would be Hello World!.
What is the output of print str[0] if str = ‘Hello World!’?
It will print first character of the string. Output would be H.
What is the output of print str[2:5] if str = ‘Hello World!’?
It will print characters starting from 3rd to 5th. Output would be llo.
What is the output of print str[2:] if str = ‘Hello World!’?
It will print characters starting from 3rd character. Output would be llo World!.
What is the output of print str * 2 if str = ‘Hello World!’?
It will print string two times. Output would be Hello World!Hello World!.
What is the output of print str + “TEST” if str = ‘Hello World!’?
It will print concatenated string. Output would be Hello World!TEST.
What is the output of print tuple[1:3] if tuple = ( ‘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 tuple[2:] if tuple = ( ‘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 tinytuple * 2 if tinytuple = (123, ‘john’)?
It will print tuple two times. Output would be (123, ‘john’, 123, ‘john’).
What is the output of print tuple + tinytuple if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2) and tinytuple = (123, ‘john’)?
It will print concatenated tuples. Output would be (‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’).
Post a Comment