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.
What are operators? What is the purpose of is, not and in operators?
Operators are special functions. They take one or more values and produce a corresponding result.
is: returns true when 2 operands are true (Example: “a” is ‘a’)
not: returns the inverse of the boolean value
in: checks if some element is present in some sequence
Whenever Python exits, why isn’t all the memory de-allocated (3 reasons)?
- Whenever Python exits, especially those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.
- It is impossible to de-allocate those portions of memory that are reserved by the C library.
- On exit, because of having its own efficient clean up mechanism, Python would try to de-allocate/destroy every other object.
What is a dictionary in Python?
A dictionary defines a one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.
How can the ternary operators be used in Python? What is the syntax?
The Ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it.
Syntax: [on_true] if [expression] else [on_false]
What does this mean: *args, **kwargs? And why would we use it?
We use *args when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargs is used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.
What are Python packages?
Python packages are namespaces containing multiple modules. They are also modules too; they just contain other modules with an __init__.py file.
Explain split(), sub(), subn() methods of “re” module in Python.
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.
What are the built-in types of python?
The principal built-in types are: Text: str Boolean: bool Numerics: int, float, complex Sequences: list, tuple, range Mappings: dict Set: set, frozenset Binary: bytes, bytearray, memoryview