/    /  Python – Interview Questions Part 7

1. What Is A String In Python?

Answer: A string in Python is a sequence of alpha-numeric characters. They are immutable objects. It means that they don’t allow modification once they get assigned a value. It provides several methods, such as join(), replace(), or split() to alter strings. But none of these change the original object.


2. What Is Slicing In Python?

Answer: Slicing is a string operation for extracting a part of the string, or some part of a list. A string (say text) in Python begins at index 0, and the nth character stores at position text[n-1]. It can also perform reverse indexing, i.e., in the backward direction, with the help of negative numbers. In this, the slice() is also a constructor function which generates a slice object. The result is a set of indices mentioned by range(start, stop, step).

The slice() method allows three parameters.

start – starting number for the slicing to begin.

stop – the number which indicates the end of slicing.

step – the value to increment after each index (default = 1).


3. What Is The Index In Python?

Answer: An index is an integer data type which denotes a position within an ordered list or a string.

In Python, strings are also lists of characters. We can access them using the index which begins from zero and goes to the length minus one.

For example, in the string “Program,” the indexing happens like this:

Program 0 1 2 3 4 5


4. How Do We Write A Function In Python?

Answer: We can write a Python function in the following manner.

Step: 1 start writing with the keyword def and then mention the function name.

Step: 2: We can now pass the arguments and enclose them using the parentheses. A colon, in the end, represents the end function header.

Step: 3 after pressing an enter; we can add the desired Python statements for execution.


5. What Is A Function Call Or A Callable Object In Python?

Answer: A function in Python gets treated as a callable object. It can allow some arguments and also return a value or multiple values in the form of a tuple. Apart from this, Python has other constructs, such as classes or the class instances which fits in the same category.


6. What Is “Call By Value” In Python?

Answer: In call-by-value, the argument whether an expression or a value gets bound to the respective variable in the function. It will treat that variable as local level function in python. Any changes made to that variable will remain local and will not reflect outside the function.


7. What Is “Call By Reference” In Python?

Answer: We use both “call-by-reference” and “pass-by-reference” interchangeably. When we pass an argument by reference, then it is available as an implicit reference to the function, rather than a simple copy. Hence, any modification to the argument will also be visible to the caller.

This scheme also has the advantage of bringing more time and space efficiency because it leaves the need for creating local copies.

On the contrary, the disadvantage could be that a variable can get changed accidentally during a function call. Hence, the programmers need to handle in the code to avoid such uncertainty.


8. What Does The *Args Do In Python?

Answer: We use *args as a parameter in the function header. It gives us the ability to pass N (variable) number of arguments.

Please note that this type of argument syntax doesn’t allow passing a named argument to the function.

Example of using the *args:

# Python code to demonstrate # *args for dynamic arguments def fn(*argList):  for argx in argList:  print (argx)     fn(‘I’, ‘am’, ‘a’, ‘Data scientist’)


The output:

IamaData scientist


9. What Does The **Kwargs Do In Python?

Answer: We can also use the **kwargs syntax in a Python function declaration. It will let us pass N (variable) number of arguments which can be named or key worded.

Example of using the **kwargs:

# Python code to demonstrate # **kwargs for dynamic + named arguments def fn(**kwargs):  for emp, age in kwargs.items(): print (“%s’s age is %s.” %(emp, age))     fn(Tom=26, Sally=23, Dim=30)


The output:

Tom’s age is 26.Sally’s age is 23.Dim’s age is 30.


10. What Does The __ Name __ Do In Python?

Answer: The __name__ is a unique variable. Since Python doesn’t expose the main() function, so when its interpreter gets to run the script, it first executes the code which is at level 0 indentation.

To see whether the main() gets called, we can use the __name__ variable in an if clause compares with the value “__main__.”