Friday, January 18, 2019

Python Interview Questions & Answers

Python Interview Questions and Answers

Python is a massively growing  programming language in current times. There are some basic questions which is asked while anybody is going for an interview. I would like to share some of the questions which are widely asked for an interview.

1. What is Python?
Ans: Python is an object oriented , interpreted , high level programming language created by Guido van Rossum and first released in 1991. It is enriched with high level built in data structures and combined with dynamic typing and dynamic binding. More importantly it has very easy to learn syntax and therefore it enhances the readability and cost of maintenance. Python supports modularity and code re-use in a very efficient manner and therefore it is extensible and flexible as well.

2. How will you define function in Python?
Ans: A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

 a. Function block begins with def keyword.
 b. In function declaration tag there will be colon at the end. (i.e def calculate(): )
 c. There won't be any parenthesis in the start/end of the function.
 d. Return type is not required while declaring the function.

3. What kind of typed of language Python is? Explain why?
Ans. Python is dynamically typed language. It means you need not to declare the variable type while developing the code. In time of execution it will be decided depending on the value you have assigned to your variable.

Example:
a=7
b="Python"

In time of execution variable a will be considered as Integer and b as String

4. How will you define variable in Python?
Ans: We need to type the variable name and assign the value into it. As python is dynamically typed language, it will interpret the variable type at run time.

5. What are the different loops used in Python?
Ans: Like other programming languages Python also has certain conditional blocks. Please find below the loops which is used in Python.
if,else, elif, for , while etc.
Example:

if a=1:
 println("Python")
elif a=2:
 println("Java")
else:
 println("C++")


6. What kind of various String formatting are possible in Python?
 Ans: String formatting is required to generate the output with more accuracy.
%s - String (or any object with a string representation, like numbers)

%d - Integers

%f - Floating point numbers

%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.

%x/%X - Integers in hex representation (lowercase/uppercase)


7. Tell some built in function which is massively used for Scripting?
Ans: There are lot of functions which is used widely. Here are few of them.
Python abs() returns absolute value of a number
Python dict() Creates a Dictionary
Python dir() Tries to Return Attributes of Object
Python divmod() Returns a Tuple of Quotient and Remainder
Python enumerate() Returns an Enumerate Object
Python eval() Runs Python Code Within Program
Python exec() Executes Dynamically Created Program
Python filter() constructs iterator from elements which are true
Python float() returns floating point number from number, string
Python format() returns formatted representation of a value

8. How to compile and run Python script?
Ans: Please find the below steps.
Windows User:
1.First you have to install python
2. Then set path variable
(My Computer > Properties > Advanced System Settings > Environment Variables >
Create a new variable PYTHONPATH and add values C:\Python\Lib;C:\Python\DLLs;C:\Python\Lib\lib-tk)
3.After that write your python program and save
4.Create a sample python program that name "hello.py"
5.Open cmd.exe
6.Then goto the path that you saved your "hello.py" file,
and then type python hello.py and press enter key.


9. Difference between Python and Shell.
Ans: Python and Shell both are scripting languages. But Python has some additional features which makes it useful for building diversified application. Here are some differences between this two:

i. There are external libraries in Python , by importing those we can easily make any functionality with lesser code and complexity than Shell.
ii. In Python there are concept of list,dictionary,Tuple so building a data structure is very easy in Python but in case of Shell there are no such concept is there which makes it less useful.
iii. Python can be useful for web development by using it's stock of libraries but in case of Shell it is not possible.

10. What is Pandas in Python?
Ans: Pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis / manipulation tool available in any language.

11. What is Cython?
Ans: Cython is a optimistic static compiler for both Python and extended Cython programming language. The Cython is superset of Pyton language and additionally support C language and declaring C types on variable and class attributes.This allows compiler to generate very efficient C code from Cython language.
For details please go to the following link:
http://docs.cython.org/en/latest/

12. Explain OOPS Feature in Python?
Ans: Python is an object-oriented programming language besides it's scripting nature. It allows us to develop applications using an Object Oriented approach. In Python, we can easily create and use classes and objects.

Major principles of object-oriented programming system are given below.

Object
Class
Method
Inheritance
Polymorphism
Data Abstraction
Encapsulation

13. What is dictionary in Python?
Ans: Dictionary is special data type in Python. Those who are familiar with Map/HashMap in Java it could be easier to understand for them. Dictionary is actually combination of key-value pair. It is an un-ordered collection of data. So, insertion order can not be maintained. But accessing/updating the data operation is too fast.

14. Difference between Tuple and List in Python?
Ans: Tuple and List both are two important and frequently use data type in Python. But there are few basic differences between these two.
1. Tuple is immutable but List is mutable.
2. In Tuple insertion order can not be maintained but in case List insertion order is maintained properly.
3. Tuple is heterogeneous data structure but List is a homogeneous data structure.

15. Tell some package which you have used in your program.

scapy,matplotlib,kivy,nltk,keras,SQLAlchemy etc.

16. What is anonymous function in Python and explain the use case.
Ans: In Python, anonymous function is a function that is defined without a name.
While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword.
Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

double = lambda x: x * 2

# Output: 10
print(double(5))

No comments:

Post a Comment