Python Interview Question and Answer
Categories: Interview questions and answers Experienced Freshers Python
Question: - What is Python?
Answer: - Python was created by Guido van Rossum, and released in 1991.
It is a general-purpose computer programming language. It is a high-level, object-oriented language which can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh. Its high-level built-in data structures, combined with dynamic typing and dynamic binding. It is widely used in data science, machine learning and artificial intelligence domain.
It is easy to learn and require less code to develop the applications.
It is widely used for:
- Web development (server-side).
- Software development.
- Mathematics.
- System scripting.
Question: - What type of language is python? Programming or scripting?
Answer: - Python is capable of scripting, but in general sense, it is considered as a general-purpose programming language.
Python an interpreted language. Explain.
An interpreted language is any programming language which is not in machine-level code before runtime. Therefore, Python is an interpreted language.
Question: - What is pep 8?
Answer: - PEP stands for Python Enhancement Proposal. It is a set of rules that specify how to format Python code for maximum readability.
Question: - What are Python namespaces?
Answer: - A namespace in python refers to the name which is assigned to each object in python. The objects are variables and functions. As each object is created, its name along with space(the address of the outer function in which the object is), gets created. The namespaces are maintained in python like a dictionary where the key is the namespace and value is the address of the object. There 4 types of namespace in python-
- Built-in namespace– These namespaces contain all the built in objects in python and are available whenever python is running.
- Global namespace– These are namespaces for all the objects created at the level of the main program.
- Enclosing namespaces– These namespaces are at the higher level or outer function.
- Local namespaces– These namespaces are at the local or inner function.
Question: - What are decorators in Python?
Answer: - Decorators are used to add some design patterns to a function without changing its structure. Decorators generally are defined before the function they are enhancing. To apply a decorator we first define the decorator function. Then we write the function it is applied to and simply add the decorator function above the function it has to be applied to. For this, we use the @ symbol before the decorator.
Question: -What are the common built-in data types in Python?
Answer: - The common built in data types in python are-
- Numbers– They include integers, floating point numbers, and complex numbers. eg. 1, 7.9,3+4i
- List– An ordered sequence of items is called a list. The elements of a list may belong to different data types. Eg. [5,’market’,2.4]
- Tuple– It is also an ordered sequence of elements. Unlike lists , tuples are immutable, which means they can’t be changed. Eg. (3,’tool’,1)
- String– A sequence of characters is called a string. They are declared within single or double quotes. Eg. “Sana”, ‘She is going to the market’, etc.
- Set– Sets are a collection of unique items that are not in order. Eg. {7,6,8}
- Dictionary– A dictionary stores values in key and value pairs where each value can be accessed through its key. The order of items is not important. Eg. {1:’apple’,2:’mango}
- Boolean– There are 2 boolean values- True and False.
Question: - What is the difference between .py and .pyc files?
Answer: - The .py files are the python source code files. While the .pyc files contain the bytecode of the python files. .pyc files are created when the code is imported from some other source. The interpreter converts the source .py files to .pyc files which helps by saving time.
Question: - What is slicing in Python?
Answer: - Slicing is used to access parts of sequences like lists, tuples, and strings. The syntax of slicing is-[start:end:step]. The step can be omitted as well. When we write [start:end] this returns all the elements of the sequence from the start (inclusive) till the end-1 element. If the start or end element is negative i, it means the ith element from the end. The step indicates the jump or how many elements have to be skipped. Eg. if there is a list- [1,2,3,4,5,6,7,8]. Then [-1:2:2] will return elements starting from the last element till the third element by printing every second element.i.e. [8,6,4].
Question: - What are Keywords in Python?
Answer: - Keywords in python are reserved words that have special meaning.They are generally used to define type of variables. Keywords cannot be used for variable or function names. There are following 33 keywords in python-
- And
- Or
- Not
- If
- Elif
- Else
- For
- While
- Break
- As
- Def
- Lambda
- Pass