Thursday 23 July 2020

Python Interview Questions and Answers for Freshers

Python Interview Questions and Answers for Freshers

Question: What is current Stable version of Python?

Version: 3.5.1 Dated: 7 December 2015


Question: What is Filename extension of Python?
py, .pyc, .pyd, .pyo, pyw, .pyz


Question: What is offical website of Python?
www.python.org


Question: What is the difference between deep copy and shallow copy?
  1. Shallow copy is used when a new instance type gets created and it keeps the values that are copied.
    Deep copy is used to store the values that are already copied.
  2. Shallow copy is used to copy the reference pointers just like it copies the values.
  3. Shallow copy allows faster execution of the program whereas deep copy makes slow.

Question: How to use ternary operators?
[on_true] if [expression] else [on_false]
x, y = 25, 50
big = x if x < y else y



Question: What are different data-type in Python?
  1. Numbers
  2. Strings
  3. Strings
  4. List
  5. Dictionaries
  6. Sets



Question: What is module in python?
Module is set of related functionalities. Each python program file is a module, which imports other modules to use names they define using object.attribute notation.


Question: What is lambda in python?
lamda is a single expression anonymous function often used as inline function.


Question: How to validate Email Address in python?
re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","myemail@domain.com")



Question: What is pass in Python?
pass is no-operation Python statement and used to indicate nothing to be done.

Question: What is iterators?
iterators is used iterate over a group of elements, containers, like list


Question: What is slicing in Python?
Slicing is a mechanism to select a range of items from Sequence types like strings, list, tuple, etc.


Question: What is docstring in Python?
Python documentation string is a way of documenting Python modules, functions, classes. PEP 257 standardize the high-level structure of docstrings.


Question: Name few modules that are included in python by default?
  1. datetime
  2. re (regular expressions)
  3. string
  4. itertools
  5. ctypes
  6. email
  7. xml
  8. logging
  9. os
  10. subprocess


Question: What is list comprehension?
Creating a list by doing some operation over data that can be accessed using an iterator.
>>>[ord(i) for i in string.ascii_uppercase]
     [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
 >>>



Question: What is map?
MAP executes the function given as the first argument on all the elements of the iterable given as the second argument.


Question: What is the difference between a tuple and a list?
A tuple is immutable i.e. can not be changed. It can be operated on only.
List is mutable. Changes can be done internally to it.


Question: How to Remove white spaces from string?
filter(lambda x: x != ' ', s)




Question: What are metaclasses in Python?
A metaclass is the class of a class.
A class defines how an instance of the class (i.e. object) behaves while a metaclass defines how a class behaves. A class is an instance of a metaclass.



Question: How do I check whether a file exists without exceptions?
os.path.isfile("/etc/password.txt");//true




Question: How to call an external command?
import subprocess
subprocess.run(["ls", "-l"])