Python (General) Flashcards
Based off https://www.edureka.co/blog/interview-questions/python-interview-questions/
What are the key features of Python (name 5)?
Interpreted language: Unlike languages like C and its variants, Python does not need to be compiled before it is run.
Dynamically typed: Don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x=”I’m a string” without error.
Suited to OOP: Allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s public, private).
Writing is quick but running is often slower than compiled languages: However, Python allows the inclusion of C-based extensions eg. NumPy’s number-crunching isn’t done in Python so it is fast
Functions and classes are first-class objects. This means that they can be assigned to variables, returned from other functions/classes and passed into functions/classes
Python is 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.
What is PEP 8?
PEP stands for Python Enhancement Proposal. It is a set of rules that specify how to format Python code for maximum readability.
How is memory managed in Python (3 parts)?
- Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The Python interpreter takes care of this instead.
- The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code.
- Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.
What is namespace in Python?
A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.
What is PYTHONPATH?
It is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
What are Python modules? Name some commonly used built-in modules in Python.
Python modules are files containing Python code. This code can either be functions, classes or variables. A Python module is a .py file containing executable code.
Some of the commonly used built-in modules are:
os, sys, math, random, datetime, JSON
What is __init__?
__init__ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the __init__ method.
What is a lambda function?
An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.
What is self in Python?
Self is an instance or an object of a class. In Python, this is explicitly included as the first parameter. However, this is not the case in Java where it’s optional. It helps to differentiate between the methods and attributes of a class with local variables.
How does break, continue and pass work?
Break: Allows loop termination when some condition is met and the control is transferred to the next statement
Continue: Allows skipping some part of a loop when some specific condition is met and the control is transferred to the beginning of the loop
Pass: Used when you need some block of code syntactically, but you want to skip its execution. This is basically a null operation. Nothing happens when this is executed.
How do you reverse the order of an array or sequence?
[::-1] or reverse()
What is the difference between range & xrange?
For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and x range returns an xrange object. If you have a really gigantic range you’d like to generate a list for, say one billion, xrange is the function to use.
What is a generator in python? How do you create one?
A generator is a function used to create iterator objects. it returns an object that can only be used with the for-in loop or next() methods. We can use the yield keyword to create a generator.
What is pickling and unpickling?
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.
What are docstrings in Python?
Docstrings are not actually comments, but, they are documentation strings. These docstrings are within triple quotes. They are not assigned to any variable and therefore, at times, serve the purpose of comments as well.