Frequently Asked Interview Questions And Answers For Python Programming Freshers

50,000+ Students Trained, 19+ Our Offices, 18+ Courses Offered,

Job Guarantee + Job Satisfaction, 2000+ Company Tie Ups For Placements

Python Interview Question

Most Important Question:-

 
  1. 1. What is Python?
Ans. Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.  
  1. 2. Name some of the features of Python.
Ans.
  • • It supports functional and structured programming methods as well as OOP.
  • • It can be used as a scripting language or can be compiled to byte-code for building large applications.
  • • It provides very high-level dynamic data types and supports dynamic type checking.
  • • It supports automatic garbage collection.
  • • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
 
  1. 3. What is the purpose of PYTHONPATH environment variable?
Ans.  PYTHONPATH – It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.  
  1. 4. What is the purpose of PYTHONSTARTUP environment variable?
Ans. PYTHONSTARTUP – It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.  
  1. 5. What is the purpose of PYTHONCASEOK environment variable?
Ans. PYTHONCASEOK − It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.  
  1. 6. What is the purpose of PYTHONHOME environment variable?
Ans.  PYTHONHOME − It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.  
  1. 7. Is python a case sensitive language?
Ans.  Yes! Python is a case sensitive programming language.  
  1. 8. What are the supported data types in Python?
Ans. Python has five standard data types −  
  • • Numbers
  • • String
  • • List
  • • Tuple
  • • Dictionary
 
  1. 9. What are Python’s dictionaries?
Ans.  Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.  
  1. 10. How will you get all the keys from the dictionary?
Ans.  Using dictionary.keys() function, we can get all the keys from the dictionary object.  
  1. 11. How will you get all the values from the dictionary?
Ans.  Using dictionary.values() function, we can get all the values from the dictionary object.  
  1. 12. Explain Inheritance in Python with an example.
Ans.  Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class. They are different types of inheritance supported by Python:
  • 1. Single Inheritance – where a derived class acquires the members of a single super class.
  • 2. Multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from base2.
  • 3. Hierarchical inheritance – from one base class you can inherit any number of child classes
  • 4. Multiple inheritance – a derived class is inherited from more than one base class.
 
  1. 13. Explain split(), sub(), subn() methods of “re” module in Python.
Ans.  To modify the strings, Python’s “re” module is providing 3 methods. They are:
  • • split() – uses a regex pattern to “split” a given string into a list.
  • • sub() – finds all substrings where the regex pattern matches and then replace them with a different string
  • • subn() – it is similar to sub() and also returns the new string along with the no. of replacements.
 
  1. 14. What is pickling and unpickling?
Ans.  Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.  
  1. 15. What is map function in Python?
Ans.  map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions  
  1. 16. What is TkInter?
Ans.  TkInter is Python library. It is a toolkit for GUI development. It provides support for various GUI tools or widgets (such as buttons, labels, text boxes, radio buttons, etc) that are used in GUI applications. The common attributes of them include Dimensions, Colors, Fonts, Cursors, etc.  
  1. 17. Is Python object oriented? what is object oriented programming?
Ans.  Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:   Encapsulation, Data Abstraction, Inheritance, Polymorphism.  
  1. 18. What is multithreading? Give an example.
Ans.   It means running several different programs at the same time concurrently by invoking multiple threads. Multiple threads within a process refer the data space with main thread and they can communicate with each other to share information more easily.Threads are light-weight processes and have less memory overhead. Threads can be used just for quick task like calculating results and also running other processes in the background while the main program is running.  
  1. 19. Does Python supports interfaces like in Java? Discuss.
Ans.  Python does not provide interfaces like in Java. Abstract Base Class (ABC) and its feature are provided by the Python’s “abc” module. Abstract Base Class is a mechanism for specifying what methods must be implemented by its implementation subclasses. The use of ABC’c provides a sort of “understanding” about methods and their expected behaviour. This module was made available from Python 2.7 version onwards.  
  1. 20. Differentiate between append() and extend() methods.?
Ans.  Both append() and extend() methods are the methods of list. These methods a re used to add the elements at the end of the list.  
  • append(element) – adds the given element at the end of the list which has called this method.
  • extend(another-list) – adds the elements of another-list at the end of the list which is called the extend method.
 
  1. 21. Name few methods that are used to implement Functionally Oriented Programming in Python?
Ans.  Python supports methods (called iterators in Python3), such as filter(), map(), and reduce(), that are very useful when you need to iterate over the items in a list, create a dictionary, or extract a subset of a list.  
  • filter() – enables you to extract a subset of values based on conditional logic.
  • map() – it is a built-in function that applies the function to each item in an iterable.
  • reduce() – repeatedly performs a pair-wise reduction on a sequence until a single value is computed.
 
  1. 22. What is the output of the following?
Ans.  
  • x = [‘ab’, ‘cd’]
  • print(len(map(list, x)))
  • A TypeError occurs as map has no len().
 
  1. 23. What is the output of the following?
Ans.  
  • x = [‘ab’, ‘cd’]
  • print(len(list(map(list, x))))
  • Explanation: The length of each string is 2.
 
  1. 24. Which of the following is not the correct syntax for creating a set?
Ans.  
    a) set([[1,2],[3,4]])
    b) set([1,2,2,3,4])
    c) set((1,2,3,4))
    d) {1,2,3,4}
Answer : a Explanation : The argument given for the set must be an iterable.  
  1. 25. How to retrieve data from a table in MySQL database through Python code?
Ans.  
  • • import MySQLdb module as : import MySQLdb
  • • establish a connection to the database.
  • • db = MySQLdb.connect(“host”=”local host”, “database-user”=”user-name”, “password”=”password”, “database-name”=”database”)
  • • initialize the cursor variable upon the established connection: c1 = db.cursor()
  • • retrieve the information by defining a required query string. s = “Select * from dept”
  • • fetch the data using fetch() methods and print it. data = c1.fetch(s)
  • • close the database connection. db.close().
 
  1. 26. What are the built-in type does python provides?
Ans.  There are mutable and Immutable types of Pythons built in types Mutable built-in types
  • • List
  • • Sets
  • • Dictionaries
Immutable built-in types
  • • Strings
  • • Tuples
  • • Numbers
 
  1. 27. What is lambda in Python?
Ans.  It is a single expression anonymous function often used as inline function.  
  1. 28. In Python what are iterators?
Ans. In Python, iterators are used to iterate a group of elements, containers like list.  
  1. 29. In Python what is slicing?
Ans.  A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.  
  1. 30. What are generators in Python?
Ans.  The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.  
  1. 31. How can you copy an object in Python?
Ans.  To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.  
  1. 32. Mention what are the rules for local and global variables in Python?
Ans.  
  • Local variables:If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local.
  • Global variables:Those variables that are only referenced inside a function are implicitly global.
 
  1. 33. Explain how can you generate random numbers in Python?
Ans.  To generate random numbers in Python, you need to import command as
  • import random
  • random.random()
  • This returns a random floating point number in the range [0,1)

Tips & Tricks

Latest Blog

Which country is better for Indian Students for Study Abroad?17th November, 2018

Canada is among the top destinations for students who choose to study abroad. Students prefer Canada

How to migrate to Canada?17th November, 2018

If you have considered moving to Canada, either you already are on the path to apply for a PR throug

Study Abroad with Minimum finance17th November, 2018

For many Indian students aspiring for high-quality education abroad, scholarships become a great h

How to Become a Good Designer?8th October, 2018

Graphic, Web, Animation, VFX, UI, UX There are plethora of options for a designer to make an amaz

Is Digital Marketing a Right Career For You?2nd October, 2018

Digital Marketing is an industry in itself that is growing rapidly since its inception. And since th

Our Events

Gyanmanjari Institute Of Technology Industrial Visits on 30th June 2018

Gyanmanjari Institute Of Technology Industrial Visits on 30th June at TOPS Technologies TOPS Techno

Android, Laravel, Python, Java and iOS Workshop at TOPS Technology

Android, Laravel, Python, Java and iOS Workshop at TOPS Technology We are happy to share that once

Project Documentation Workshop at TOPS Technologies

Project Documentation Workshop TOPS Technologies have organized “Project documentation” worksho

Final Year Project Documentation Workshop

Final Year Project Documentation Workshop Project Documentation, Project Definition, project Submis

Industrial Visit in the month of March and April at TOPS Technologies

Industrial Visits in the month of March and April at TOPS Technologies TOPS Technologies always bel