Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

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

Friday, December 14, 2018

Python Dictionary

Dictionary in Python


It is an ordered collection of items. If you look at the other data structures of Python(i.e. list,tuple) they consist only value items, but Dictionary consists of Key,Value pair. As Python dynamically typed language there are no need to define variable type. We can directly define variable name.

We can create dictionary by below commands.

#Empty dictionary 
my_collections = {}


#Dictionary with same type of key
my_collections  = {1:'Rahul',2:'Shayam'}

#Dictionary with different type of keys
my_collections  = {'name':'Rahul',2:[2,3,4]}

We can access the dictionary by below commands.

my_collections1  = {1:'Rahul',2:'Shayam'}
my_collections2  = {'name':'Rahul',2:[2,3,4]}

my_collections1 [1]
my_collections2  ['name']

or

my_collections2 .get('name')

We can add/update the dictionary by below commands.

#update
my_collections2  ['name'] = 'John'

#add
my_collections2 ['age']= 23

We can delete/remove the dictionary by below commands.

cubes= {1:1, 2:8 3:27, 4:64, 5:125}

#Deleting particular item
cubes.pop(4)

#Deleting arbitrary item
cubes.popitem()

#Clearing Dictionary 
cubes.clear()



Saturday, December 8, 2018

PySpark Interview Questions

PySpark Interview Questions

Big Data is evolving day by day. Now big organizations are using Python on Spark in order to derive Analytics based solutions. I would like to share some interview questions.

1. What is Spark.
2. Tell me some use case where we prefer Python over Scala in Spark framework.
3. Difference between Python and Scala.
4. How will you execute Python script in Spark framework?
5. What is Pandas in Python?
6. What is Cython?
7. Explain OOPS Feature in Python?
8. What is dictionary in Python?
9. Difference between Tuple and List in Python?
10. Tell some package which you have used in your program.
11. What is anonymous function in Python and explain the use case.
12. What is module and how will you add one module into another?
13. How to handle python I/O operation.
14. How to handle exception in Python?
15. How will you execute Hive queries in Python?

Wednesday, December 5, 2018

Python Interview Questions

Python Interview Questions 

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). Here some interview those are frequently asked in any interview.
1. What is Python?
2. How will you define function in Python?
3. What kind of typed of language Python is? Explain why?
4. How will you define variable in Python?
5. What are the different loops used in Python?
6. What kind of various String declaration are possible in Python?
7. Tell some built in function which is massively used for Scripting?
8. How to compile and run Python script?
9. Difference between Python and Shell.
10. What is Pandas in Python?
11. What is Cython?
12. Explain OOPS Feature in Python?
13. What is dictionary in Python?
14. Difference between Tuple and List in Python?
15. Tell some package which you have used in your program.
16. What is anonymous function in Python and explain the use case.


To check the answers click here.