Friday, April 16, 2021

Python Interview Questions

 



Python Programming Language Interview Questions

1) What is Python?

Python was created by Guido van Rossum, and released in 1991.

It is a general-purpose computer programming language. It is a high-level, object-oriented language which can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh. It is widely used in data science, machine learning and artificial intelligence domain.

It is easy to learn and require less code to develop the applications.

It is widely used for:

  • Web development (server-side).
  • Software development.
  • Mathematics.
  • System scripting.

Why Python?

  • Python is compatible with different platforms like Windows, Mac, Linux, Raspberry Pi, etc.
  • Python has a simple syntax as compared to other languages.
  • Python allows a developer to write programs with fewer lines than some other programming languages.
  • Python runs on an interpreter system, means that the code can be executed as soon as it is written. It helps to provide a prototype very quickly.
  • Python can be described as a procedural way, an object-orientated way or a functional way.

2. What are the applications of Python?

Python is used in various software domains some application areas are given below.

  • Web and Internet Development
  • Games
  • Scientific and computational applications
  • Language development
  • Image processing and graphic design applications
  • Enterprise and business applications development
  • Operating systems
  • GUI based desktop applications
Python Interview Questions

Python provides various web frameworks to develop web applications. The popular python web frameworks are Django, Pyramid, Flask.

Python's standard library supports for E-mail processing, FTP, IMAP, and other Internet protocols.

Python's SciPy and NumPy helps in scientific and computational application development.

Python's Tkinter library supports to create a desktop based GUI applications.


3. What are the advantages of Python?

  • Interpreted
  • Free and open source
  • Extensible
  • Object-oriented
  • Built-in data structure
  • Readability
  • High-Level Language
  • Cross-platform
    Interpreted: Python is an interpreted language. It does not require prior compilation of code and executes instructions directly.
  • Free and open source: It is an open source project which is publicly available to reuse. It can be downloaded free of cost.
  • Portable: Python programs can run on cross platforms without affecting its performance.
  • Extensible: It is very flexible and extensible with any module.
  • Object-oriented: Python allows to implement the Object Oriented concepts to build application solution.
  • Built-in data structure: Tuple, List, and Dictionary are useful integrated data structures provided by the language.
Python Interview Questions

4. What is PEP 8?

PEP 8 is defined as a document that helps us to provide the guidelines on how to write the Python code. It was written by Guido van Rossum, Barry Warsaw and Nick Coghlan in 2001.

It stands for Python Enhancement Proposal, and its major task is to improve the readability and consistency of Python code.


5. What do you mean by Python literals?

Literals can be defined as a data which is given in a variable or constant. Python supports the following literals:

String Literals

String literals are formed by enclosing text in the single or double quotes. For example, string literals are string values.

E.g.:

"Aman", '12345'.

Numeric Literals

Python supports three types of numeric literals integer, float and complex. See the examples.

  1. # Integer literal  
  2. a = 10  
  3. #Float Literal  
  4. b = 12.3   
  5. #Complex Literal   
  6. x = 3.14j  

Boolean Literals

Boolean literals are used to denote boolean values. It contains either True or False.

  1. # Boolean literal  
  2. isboolean = True  

6. Explain Python Functions?

A function is a section of the program or a block of code that is written once and can be executed whenever required in the program. A function is a block of self-contained statements which has a valid name, parameters list, and body. Functions make programming more functional and modular to perform modular tasks. Python provides several built-in functions to complete tasks and also allows a user to create new functions as well.

There are two types of functions:

  • Built-In Functions: copy(), len(), count() are the some built-in functions.
  • User-defined Functions: Functions which are defined by a user known as user-defined functions.

Example: A general syntax of user defined function is given below.

  1. def function_name(parameters list):  
  2.     #--- statements---  
  3.     return a_value  

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

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

Note: If the given lists are of different lengths, zip stops generating tuples when the first list ends. It means two lists are having 3, and 5 lengths will create a 3-tuple.


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

Python Interview Questions

The pass by value is that whenever we pass the arguments to the function only values pass to the function, no reference passes to the function. It makes it immutable that means not changeable. Both variables hold the different values, and original value persists even after modifying in the function.

Python Interview Questions

Python has a default argument concept which helps to call a method using an arbitrary number of arguments.


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

  1. class student:  
  2.     def __init__(self,name):  
  3.         self.name = name  
  4.     def __init__(self, name, email):  
  5.         self.name = name  
  6.         self.email = email  
  7.        
  8. # This line will generate an error  
  9. #st = student("rahul")  
  10.   
  11. # This line will call the second constructor  
  12. st = student("rahul""rahul@gmail.com")  
  13. print(st.name)  
  14. Output:  
  15. rahul  

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

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

If you want to delete an object at a specific location (index) in the list, you can either use del or pop.

Note: You don't need to import any extra module to use these functions for removing an element from the list.

We cannot use these methods with a tuple because the tuple is different from the list.


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

  1. string = "IT IS IN LOWERCASE."  
  2. print(string.swapcase())  
  3.   
  4. string = "it is in uppercase."  
  5. print(string.swapcase())  
it is in lowercase. 
IT IS IN UPPERCASE. 

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

  1. string = "  abc "  
  2. string2 = "    abc        "  
  3. string3 = "       abc"  
  4. print(string)  
  5. print(string2)  
  6. print(string3)  
  7. print("After stripping all have placed in a sequence:")  
  8. print(string.strip())  
  9. print(string2.strip())  
  10. print(string3.strip())  
abc
    abc       
       abc

After stripping all have placed in a sequence:

abc

abc

abc


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

  1. string = "  javatpoint "   
  2. string2 = "    javatpoint        "  
  3. print(string)  
  4. print(string2)  
  5. print("After stripping all leading whitespaces:")  
  6. print(string.lstrip())  
  7. print(string2.lstrip())  
javatpoint 
    javatpoint        
After stripping all leading whitespaces:
javatpoint 
javatpoint
Python Interview Questions

After stripping, all the whitespaces are removed, and now the string looks like the below:

Python Interview Questions

14. Why do we use join() function in Python?

The join() is defined as a string method which returns a string value. It is concatenated with the elements of an iterable. It provides a flexible way to concatenate the strings. See an example below.

  1. str = "Rohan"  
  2. str2 = "ab"  
  3. # Calling function    
  4. str2 = str.join(str2)    
  5. # Displaying result    
  6. print(str2)  

Output:

aRohanb

15) Give an example of shuffle() method?

This method shuffles the given string or an array. It randomizes the items in the array. This method is present in the random module. So, we need to import it and then we can call the function. It shuffles elements each time when the function calls and produces different output.

  1. import random  
  2.   
  3. list = [12,25,15,65,58,14,5,];  
  4. print(list)  
  5. random.shuffle(list)  
  6. print ("Reshuffled list : \n",  list)  
[12, 25, 15, 65, 58, 14, 5] 
Reshuffled list :
 [58, 15, 5, 65, 12, 14, 25] 


0 Comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget