Here is a compilation of basic interview questions for python programmers:
1. What is Python?
Fundamentally focused on code readability and usage of whitespace, Python can be described as an interpreted, high-level, and general-purpose programming language. The language is simple and comes with easy to learn syntax and readability, thus, making it easier to maintain in terms of costs. The language enables code reuse, program modularity, and supports modules and packages. It is an easy language for beginners level programmers, too.
2. How do lists and tuples differ in Python?
Lists are the mutable components of Python, which means that programmers can edit them. But tuples are the components that are immutable or can be considered as lists that can not be edited. While lists are slower in Python, tuples are faster.
Here is an example of List and tuples syntax:
Syntax: list_1 = [10, ‘Chelsea’, 20] Syntax: tup_1 = (10, ‘Chelsea’ , 20)
3. List down a few key features of Python
- By Python being an interpreted language, it means that Python need not be compiled before running it, unlike some other language like C, C++, etc.
- The language is dynamically typed. Hence, programmers do not need to state the types of variables upon declaration, and the code will be executed without error.
- Although Python code writing is quite a fast task, running it can take a lot of time. Hence, Python allows for the inclusion of C based extensions for easy execution of bottlenecks.
- The language is optimal for object-oriented programming and enables the definition of classes, composition, and inheritance. But Python does not come with access specifiers such as public and private, unlike C++.
- Functions are termed as the first-class objects in Python. Hence, they can be easily returned from and passed into other functions and assigned to other variables. Classes are also listed under first-class objects in Python.
4. Is Python a programming or scripting language?
Though Python is actually a scripting language, commonly, it is considered as a general-purpose language.
5. What is PYTHONPATH?
PYTHONPATH is an environmental variable. The variable is used when a module is imported. Also, upon the importing of a module, the PYTHONPATH variable is deployed for checking other imported modules in the various directories. Hence, it becomes easy for the interpreter to see and interpret the module that needs to be loaded.
6. What are Python modules and list down a few commonly used built-in modules in Python?
Files containing Python code are known as Python modules. The code can be anything from functions, classes to variables. The Python module can be seen as a .py file with executable code.
A few commonly used built-in modules in Python are as below:
- data time
7. What are core default modules and list down a few of the core default modules available in Python?
These codes are written in C and are part of the core. They are integrated with the Python interpreter. The built-in modules contain resources for system-specific functions.
- email: the module is used to parse, handle, and generate email messages.
- string: the module is used to contain functions that help to process standard Python strings.
- XML: the module is used to enable XML support.
- sqlite3: the module is used to employ the work of SQLite database.
- traceback: the module is used to enable extraction and print of stack trace details.
- logging: it is used to allow the support for log classes and methods.
8. How long can an identifier be in Python?
An identifier can be of any length according to the Python documentation. But as the readability of the code is a major factor of consideration in Python as per PEP 20 and PEP 8, too, limits the identifiers to be 79 characters per line. Though a longer code can be allowed, it will result in the violation of PEP 20 and PEP 8.
It is important to remember that Python is case sensitive, and the same goes for identifiers. The identifier can only begin with A-Z or a-z, while the rest can contain any letters or numbers. Also, keywords can not be used as identifiers.
9. What are local and global variables in Python?
In Python, the variables declared outside a function or in global space are called the global variables. You can access these variables from any function in the program.
On the other hand, variables declared inside functions are known as local variables. It is because the variable is present in local space and not in global space. If an attempt is made to access a local variable outside the function, an error will be shown.
10. What are generators in Python?
The generators in Python are a way to implement iterators. A generator function is mostly like normal functions themselves. The only difference is that the generator function contains yield expression in the function.
11. Does Python require indentation?
Indentation is compulsory in Python and is used to specify a block of code. An indented block is used to determine all code within loops, classes, functions, etc. The indentation is usually done using four space characters.
12. What are the functions in Python?
A block of code that is executed only when called for is known as a function in Python. It is defined by a def keyword.
def Newfunc(): print("Hi, hello world") Newfunc(); #calling the function
13. What is Self in Python?
Self can be defined as an instance of an object of a class in Python and is included as the first language parameter.
In the init method, a Self variable points to a newly created object, while in other methods, it is used to refer to an object whose method is called.
14. How are comments written in Python?
To write a comment in a Python program, you need to begin with a # character. Comments can also be written by enclosing the strings in triple quotes known as docstrings.
#This is an example of a comment print("This is an example of a comment")
15. What is pickling and unpickling in Python?
A pickle module in Python pickling is used to convert a Python object into a string, which can then be dumped into a file using the dump function. The process is known as pickling.
Unpickling refers to the vice versa of this process. The retrieval of an original Python object from a string is called unpickling.