Blog

Python Interview Questions and Answers for 2021

Python Interview Questions and Answers for 2021

With the growing popularity, python certification has become one of the most sought-after skill that adds weightage to your resume. In this blog we are providing you with the frequently asked python interview questions. Let’s take a look.

Q1. What are Python’s key features?

Answer:

  • Python is an interpreted language. That means that Python does not need to be compiled before it is run.
  • Python is dynamically typed.
  • Python is very well suited to object orientated programming.
  • In Python, functions and classes are first-class objects.
  • Python is used in web applications, automation, big data applications and many more.

Q2. What is Python?

Answer:

Python is a high-level, interpreted, object-oriented scripting language. Python is designed to be highly readable. Python emphasizes on readability with easy to learn syntax. It uses English keywords frequently.

Q3.  What type of language is python? Programming or scripting?

Answer:

Python is considered as a general-purpose programming language. It is capable of scripting too.

Q4. How is Python an interpreted language?

Answer:

In an interpreted language, code is not in machine level before runtime. Hence, Python is an interpreted language.

Q5. What is namespace?

Answer:

A namespace is a naming system for making names unique to evade naming conflicts.

Q6. What is PYTHONPATH?

Answer:

It has a role similar to PATH. The PYTHONPATH tells the interpreter where to locate the module files imported into a program. In most cases these are not required as Python knows the location of its standard library.

Q7. What are python modules? Name some built-in modules in Python?

Answer:

Python modules are used to make code easier. These are files containing Python code and can be functions, classes or variables.

Some of the built-in python modules are math, random JSON.

Q8.What are local and global variables in Python?

Answer:

Global Variables are the variables declared in global space or outside a function while local variables are declared inside a function.

Python Interview Questions and Answers for 2021

When you try to access the local variable outside the function add(), it will show you an error.

Q9.  Is python case sensitive?

Answer:

Yes, Python is a case sensitive language. To learn more about Python go to https://www.ethans.co.in/course/python-training-in-pune/ python training in Pune.

Q10.  What are functions in Python?

Answer:

A function can be defined as a set of code, that executes and produces output. Functions are a way to make code more readable. Functions can be reused to save some time.

Q11. What is PYTHONSTARTUP environment variable?

Answer:

PYTHONSTARTUP contains the location of the path to a python file. In other words, it initializes the file containing Python source code. This python script is executed every time you start the interpreter.

Q12. Why was PYTHONCASEOK environment variable introduced?

Answer:

The PYTHONCASEOK is used to enable finding module files which are case- insensitive or behave in a case-insensitive manner.

It supports code written for case-insensitive filesystems.

Q13. Name the supported data types in Python?

Answer:

Python has five standard data types – Numbers, String, List, Dictionary and Tuple.

Q14. What will be the output of print str if str = ‘Ethans Tech!’?

Answer:

It will print the complete string. Output would be Ethans Tech!.

Q15. What is the output of print str[0] if str = ‘Ethans Tech!’?

Answer:

It will print only the first character of the string. Output would be E.

Q16. What is the output of print str[2:5] if str = ‘Ethans Tech!’?

Answer:

It will print characters starting from 3rd to 5th and hence the output would be han.

Q17. What is the output of print str + “PYTHON” if str = ‘Ethans Tech!’?

Answer:

Output would be a concatenated string Ethans Tech!PYTHON.

Q18. Define Python decorators?

Answer:

Decorators are very useful tool in Python. Decorators allow programmers to modify the behavior of function or class. Decorators allow programmers to wrap another function in python syntax to easily extend the behavior of wrapped function, without permanently modifying it.

Q19. What do you mean by tuples in Python?

Answer:

A tuple is a sequence data type similar to the list, consisting of values separated by commas and enclosed within parentheses. Tuple is immutable unlike lists. For example:

Fruit Tuple:

(‘banana’, ‘cherry’, ‘apple’)

Q20. State the difference between tuples and lists?

Answer:

Lists are enclosed in square brackets ( [ ] ). The elements and size can be change in lists. On the other hand, Tuples are enclosed in parentheses ( ( ) ) and is immutable i.e. cannot be changed. For more information check EthansTech python course in Pune.

Q21. Define lambda function with an example?

Answer:

An anonymous function with any number of parameters but just one statement is called lambda function. Example of the lambda function :

Python Interview Questions and Answers for 2021

Output: 6

Q22. What is self in Python?

Answer:

After creating a class in Python, we need to access its attributes and methods. In Python, the word self is explicitly included as a first parameter of methods.

Q23. What do you mean by dictionaries in python?

Answer:

A dictionary is a collection of items that are changeable, unordered, and indexed. Dictionaries are written with curly brackets, and they contain keys and values A pair of braces without any item creates an empty dictionary: {}

Q24. What is the process to get all the keys from the dictionary?

Answer:

Using dictionary.keys() function allows programmers to get all the keys from the dictionary object.

Python Interview Questions and Answers for 2021

Q25. What would you use to convert a string to a frozen set in python?

Answer:

frozenset(s) – It is used to convert a string to a frozen set.

If you want to learn Python and gain expertise, check out our interactive, live-online Python classes in Pune. The training comes with 24*7 support to help you guide throughout your learning period. We at EthansTech wish you good luck for your future endeavor.

Inquire Now