Python Interview Question Set 2

Categories: Python

 What is zip() function in Python?

Python zip() function returns a zip object, which maps a similar index of multiple containers. It takes an iterable, convert into iterator and aggregates the elements based on iterables passed. It returns an iterator of tuples.

Signature

zip(iterator1, iterator2, iterator3 ...)    

Parameters

iterator1, iterator2, iterator3: These are iterator objects that are joined together.

Return

It returns an iterator from two or more iterators.


What is Python's parameter passing mechanism?

There are two parameters passing mechanism in Python:

Pass by references

Pass by value

By default, all the parameters (arguments) are passed "by reference" to the functions. Thus, if you change the value of the parameter within a function, the change is reflected in the calling function as well. It indicates the original variable. For example, if a variable is declared as a = 10, and passed to a function where it's value is modified to a = 20. Both the variables denote to the same value.


How to overload constructors or methods in Python?

Python's constructor: _init__ () is the first method of a class. Whenever we try to instantiate an object __init__() is automatically invoked by python to initialize members of an object. We can't overload constructors or methods in Python. It shows an error if we try to overload.


What is the difference between remove() function and del statement?

The user can use the remove() function to delete a specific object in the list.

Example:

list_1 = [ 3, 5, 7, 3, 9, 3 ]   

print(list_1)  

list_1.remove(3)   

print("After removal: ", list_1)  

Output:

[3, 5, 7, 3, 9, 3]

After removal: [5, 7, 3, 9, 3]


What is swapcase() function in the Python?

It is a string's function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters. See an example below.

Example:

string = "IT IS IN LOWERCASE."  

print(string.swapcase())  

  

string = "it is in uppercase."  

print(string.swapcase())  

Output:

it is in lowercase. 

IT IS IN UPPERCASE. 


How to remove whitespaces from a string in Python?

To remove the whitespaces and trailing spaces from the string, Python providies strip([str]) built-in function. This function returns a copy of the string after removing whitespaces if present. Otherwise returns original string.


How to remove leading whitespaces from a string in the Python?

To remove leading characters from a string, we can use lstrip() function. It is Python string function which takes an optional char type parameter. If a parameter is provided, it removes the character. Otherwise, it removes all the leading spaces from the string.

Example:

string = "  javatpoint "   

string2 = "    javatpoint        "  

print(string)  

print(string2)  

print("After stripping all leading whitespaces:")  

print(string.lstrip())  

print(string2.lstrip())  

Output:

javatpoint 

    javatpoint        

After stripping all leading whitespaces:

javatpoint 

javatpoint

Top Blogs
Interview Question and Answer of Python Published at:- Basic Concepts of Python Programming Published at:- Learn Python Programming Language Published at:- Let’s know about Python basics Published at:- Let’s know about the Python Shell and idle Published at:- How to running Python Locally Published at:- What is Lists, tuples and strings in Python? Published at:- Some Modules In Python how to it works Published at:- Program control: Looping, conditionals and functions Published at:- Program control: Looping, conditionals and functions ( Part 2) Published at:- Top iOS interview questions for Beginners Published at:- Python Interview Question Set 1 Published at:- Python Interview Question Set 2 Published at:- Python Interview Question Set 3 Published at:- Python Interview Question Set 6 Published at:- Python Interview Question Set 7 Published at:- Python Interview Question Set 8 Published at:- How Could I at any point Manage Python Programming Language Published at:- Top Python Advancement Patterns to Be Aware In 2022 Published at:- Advantages of Learning Python Published at:- What Is Python Utilized For Published at:- What is new in Python Programming Published at:- Top Python Libraries for Legitimate Information Science Experts Published at:- Why Django is the most popular Python Framework Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.