Python Interview Questions for freshers
If anybody searching for python interview questions for freshers then this is the page we can provide all type selective questions. Top 30 questions here cited with real-time examples. So just review before going for python face to face interview.
Table of Contents
- What is the difference between Java and Python?
- What is the difference between list and tuples in Python?
- Write Python logic to count the number of capital letters in a file.
- How to make Indentation in Python
- What is the difference between .py and .pyc files?
- What is the difference between *var and ** var in Python?
- What are the Python Web Frameworks for developing web applications?
- Explain Some in-built functions to manipulate list in Python!
- What is the usage of help() and dir() function in Python?
- What are uses of “/” and “//” operator in python?
- What is a dictionary in Python?
- Differentiate between append() and extend() methods in Python ?
- What is a monkey patch?
- What is lambda in python and how it used?
- What is docstring in Python?
- What is TkInter?
- What are list of database access by python?
- How Could you send an Email Using Python program?
- What is Pickling in Python?
- How many types of sequences are supported by Python?
What is the difference between Java and Python?
Python.
- It is faster than java in terms of performance.
- Dynamically typed
- Not compatible with any platform.
- Database access is weaker than java
- Indentation does by only new line.
Java.
- It is a bit slower than Python in performance-wise.
- Static typed
- Compatible to all Platform.
- Database access is Good in Java.
- Indentation does by braces.
What is the difference between list and tuples in Python?
List
- Lists are mutable
- List can be edited
- Syntax of the list is shown by [] bracket.
- Syntax: list_1 = [5, ‘Kumar’, 20]
tuples
- Tuples are immutable
- Tuples are a list but then can’t be edited
- Syntax of tuples is shown by parenthesis.
- Syntax: tup_1 = (5, ‘Kumar’ , 20)
Write Python logic to count the number of capital letters in a file.
1 2 3 4 5 6 7 8 |
>>> import os >>> os.chdir('C:\\Users\\lifei\\Desktop') >>> with open('myfilename.txt') as myfile: count=0 for i in myfile.read(): if i.isupper(): count+=1 print(count) |
How to make Indentation in Python
Unlike language like C, C++, Java It has some different Indentation mechanism.
Other languages follow the indentation like braces or semicolon, where are Python followed by the new line. In order to code readable Programmer must have to make Indentation.
The syntax for example of Indentations
1 2 |
if salary >= 5000: print('your are getting salary of more than 5k') |
What is the difference between .py and .pyc files?
Python compiles the .py and saves files as .pyc
In Simple we can say .py files are Python source files. where as .pyc files are the compiled bytecode.
Once the *.pyc file is generated, there is no need of *.py file, unless until you edit the .py file. So it will save your time, No need to compile again an again.
What is the difference between *var and ** var in Python?
Both *var and ** var allow you a pass a variable to the number of argument to functions.
Here *var or **var you can change like anything. eg- *kar, **kar (take for your convenience)
*var is used to send a non-keyworded variable-length argument list to the function.
1 2 3 4 5 6 7 8 |
# Python program to illustrate # *var def singleStar(arg1, *var): print "first argument :", arg1 for arg in var: print "Next argument through *var :", arg singleStar('Hello', 'Welcome', 'to', 'interview-questions-answers') |
**var allows you to pass a keyworded variable length of arguments to a function.
1 2 3 4 5 |
def doubleStar(**var): if var is not None: for key, value in var.iteritems(): print "%s == %s" %(key, value) doubleStar(name="interview-questions-answers") |
What are the Python Web Frameworks for developing web applications?
There are some web frameworks provided by Python. They follow below
TurboGears – It permits you to quickly develop protractile data-driven Web applications.
Bottle – It is a micro-framework for developing the web application.
web2py – it is the simplest of all the web frameworks used for developing web applications.
cherryPy – it is a Python-based Object based Web framework.
Flask – it is a micro-framework for developing and designing web applications.
Sanic– It is a Python web framework built on uvloop (event loop) which is made for fast HTTP responses.
Explain Some in-built functions to manipulate list in Python!
Compares elements of both lists – cmp(list1, list2)
Get the length of the list – len(list)
Get the max value from a list – max(list)
Get the min value from a list –min(list)
Get the lowest index in the list that object appears – list.index(obj)
Inserts object obj into list at offset index –list.insert(index, obj)
Removes and returns last object or obj from list – list.pop(obj=list[-1])
Removes object obj from list- list.remove(obj)
Reverses objects of list in place- list.reverse()
Sorts objects of list, use compare func if given – list.sort([func])
What is the usage of help() and dir() function in Python?
help() is a useful default function in Python program that can be used to get the documentation of a particular object, method, attributes, etc.
1 2 |
some_list = [] help(some_list.append) |
In Python to get all the attributes list we you have to use dir() function, where you need to pass the argument as an object. If you won’t passes any argument then it will return list of a current local namespace.
1 2 |
your_list = [] print(dir(your_list)) |
OutPut : You will get all default local names space like [‘__add__’, ‘__class__’,…]
What are uses of “/” and “//” operator in python?
Division(“/”): Divides left hand operand by right hand operand.
Here This work like general mathematics way. suppose
1 2 3 4 |
<span class="lit">7</span><span class="pun">/</span><span class="lit">2</span> <span class="pun">=</span> <span class="lit">3.5</span> <span class="typ">In</span><span class="pln"> general way</span><span class="pun">:</span><span class="pln"> x</span><span class="pun">/</span><span class="pln">y</span><span class="pun">=</span> <span class="kwd">float</span><span class="pun">(</span><span class="pln">x</span><span class="pun">/</span><span class="pln">y</span><span class="pun">)</span> |
Floor Division(“//”): The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity).
1 2 3 4 |
5//2=2 -11//3 = -4 x // y == math.floor(x/y) |
What is a dictionary in Python?
In Python Dictionary defines one-to-one relationship between keys and values. Dictionaries contain a pair of keys and their corresponding values. Dictionaries are indexed by keys like JSON data or associative array.Let’s have a simple example for the sake of understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
dict={'Name':'Michael','Age':'30','Phone':'99xxxxxx09'} print dict[Name] Output: Michael print dict[Age] Output: 30 print dict[Phone] Output: 99xxxxxx09 |
Differentiate between append() and extend() methods in Python ?
append() and extend() methods are the methods of list in Python. These methods are used to a dd the elements at the end of the list.
append(element) – adds the given element at the end of the list.
extend(another-list) – adds the elements of another-list at the end of the list.
What is a monkey patch?
In Simple you can say, monkey patching is changes can done to a module or a class while the program is running.
1 2 |
import datetime datetime.datetime.now = lambda: datetime.datetime(2012, 12, 12) |
What is lambda in python and how it used?
In Python functions are defined by using def keyword before function name, If the function having no name means anonymous function then python used lambda before it.
1 2 3 |
add = lambda a, b: a + b print(add(40,50)) Output:90 |
What is docstring in Python?
docstrings provide a way of associating documentation with Python functions, classes, methods and modules.
It Unlike commenting in Source Code other languages C, C++, JAVA, PHP etc. The docstring line should begin with a capital letter and end with a period.
1 2 3 4 5 6 7 8 |
class MyClass(object): """The class's docstring""" def my_method(self): """The method's docstring""" def my_function(): """The function's docstring""" |
What is TkInter?
TkInter is a toolkit for GUI development. It provides support for various controls for GUI tools such as text boxes, radio buttons, labels, buttons etc. The common attributes of them include Colors, Fonts, Cursors, Dimensions etc.
What are list of database access by python?
Python Database API supports Wide range of database server
- mSQL
- MySQL
- PostgreSQL
- Microsoft SQL Server 2000
- Informix
- Interbase
- Oracle
- Sybase
- GadFly
How Could you send an Email Using Python program?
Generally, SMTP is controlled by sending e-mail and routing e-mail between mail servers in other languages.But here Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
Example-
#!/usr/bin/python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import smtplib sender = 'example@from.com' receivers = ['example@to.com'] message = """From: From Person <example@from.com> To: To Person <example@to.com> Subject: test email This is a test e-mail message. """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Email has sent successfully" except SMTPException: print "Error:Send email Failed" |
What is Pickling in Python?
Pickling is alternatively called as serialization. It serializes Python object to byte stream stored in a file format. Again byte stream can be converted to object by unpickling.
How many types of sequences are supported by Python?
Python supports 7 sequence types. They are like unicode, xrange, str, list, tuple, byte array and buffer. where xrange is deprecated in python 3.5.X.