Python is an interpreted language that executes code line by line without compilation. It uses namespaces to uniquely identify variables and avoid naming conflicts. Objects in Python are passed by reference. Common data structures include lists and tuples, with lists being mutable and tuples immutable. Python has various scopes including local, global, and module level for variables. Modules and packages help organize code and namespaces. The language includes many built-in functions for tasks like documentation lookup and object introspection. Memory management is automatic via a garbage collector.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
56 views
Python Interview Questions
Python is an interpreted language that executes code line by line without compilation. It uses namespaces to uniquely identify variables and avoid naming conflicts. Objects in Python are passed by reference. Common data structures include lists and tuples, with lists being mutable and tuples immutable. Python has various scopes including local, global, and module level for variables. Modules and packages help organize code and namespaces. The language includes many built-in functions for tasks like documentation lookup and object introspection. Memory management is automatic via a garbage collector.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Python Interview Questions
Why Python is called Interpreted Language ?
a. An Interpreted language executes its statement line by line. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step. Python Enhancement Proposal (PEP) a. It is official design document providing information to python community about new features of Python and its processes. Scope in Python a. Every object in python functions within a scope. A scope is a block of code where an object in python remains relevant. b. Examples: Local Scope, global scope, module level scope List is mutable whereas Tuple is immutable. Modules a. Modules are simply python files with .py extension and can have a set of functions, classes or variables. They can be imported and initialized by Using import statement Packages a. Package is collection of modules. b. How to import package and its contents. Packagename.modulename c. As module help avoid clashes between global variable names, in a similar manner, packages help avoid clashes between module names. What is global, protected and private attributes in Python ? a. Global: Global variables are public variables that are defined in the global scope. To use the variable in global scope inside a function, we use the global keyword. b. Protected: Protected attributes are attributes defined with an underscore prefixed to their identifier e.g. _variablename. They should not be accessed and modified from outside of the class but if one want, he can. c. Private: Private attributes are attributes with double underscore prefixed to their identifier. Eg: __variablename. They cannot be accessed or modified from the outside directly Unit tests in Python a. It is unit testing framework of Python Difference between Python arrays and List a. Arrays can contain elemenst of same data type. List in Python can contain elemenst of different data types. Python Namespaces a. A namespace in Python ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as the dictionaries with ‘name as key’ mapped to a corresponding ‘object as value’. This allows for multiple namespcaes to use the same name and map it to a separate object. Few examples of namespace: b. (i) Local Namespace: includes local names inside a function. The namespace is temporarily created for a function call and gets cleared when the function returns. c. (ii) Global namespace: includes names from various imported packages/modules that are being used in the current project. This namespace is created when the package is imported in the script and lasts until the execution of the script. Python Namespace from another site a. A Namespace in python refers to the name which is assigned to each object in Python. The objects are variable and functions. b. As each object is created, its name alongwith space (the address of the outer function in which the object is), gets created. c. The namespaces are maintained like a dictionary where the key is the name space and value is the address of the object. d. There are 4 types of namespace in python: e. (i) Built in Namespace (ii) Global Namespace (iii) Enclosing Namespaces (iv) Local Namespaces Scope Resolution in Python a. Sometimes object within the same scope have the same name but function differently. In such cases scope resolution comes into play in python automatically. Examples: b. Python modules namely math and cmath have a lot of functions that are common to both of them – log10(), acos(), exp() etc. to resolve this ambiguity, it is necessary to prefix them with their respective module, like math.exp(), cmath.exp(). How do you copy an object in Python ? a. To create copies of an object in python, use the copy module . b. There are two ways of creating copies for the given object using the copy module: c. Shallow Copy: Shallow copy is a bit-wise copy of an object. The copies object created has an exact copy of the values in the original object. If either of the values is a reference to other objects, just the reference addresses for the same are copied. d. Deep Copy: Copies all values from source to target object, i.e. it even duplicates the objects referenced by the source object. e. from copy import copy, deepcopy f. list_1 = [1, 2, [3, 5], 4] g. ## shallow copy h. list_2 = copy(list_1) i. list_3 = deepcopy(list_1) Pickling and Unpickling a. Serialization of an object refers to transforming it into a formant that can be stored, so as to be able to deserialize it, later on, to obtain the original object. (Pickle module is used) . b. Pickling is the name of serialization process in Python. Any object in Python can be serialized into a byte stream and dumped as file in the memory. Function used is: pickle.dump c. Unpickling: Unpickling is the complete inverse of pickling. It deserializes the bytestream to recreate the objects stored in the file and loads the object to memory. Function used is: pickle.load() Generators a. Generators are function that return an iterbale collection of items, one at a time, in a set manner. b. Generators employ the use of yield keyword rather than return to return a generator object. c. def fib(n): d. p, q = 0, 1 e. while(p < n): f. yield p Use of help() and dir() functions a. Help() function in python is used to display the documentation of the modules, classes, functions, keywords etc. If no parameter is passed to the help() function, then an interactive help utility is launched on the console. b. Dir() function tries to return a valid list of attributes and methods of the object it is called upon. Difference between .py and .pyc files ? a. .py files contain the source code of the program whereas .pyc file contain the bytecode of the program. We get bytecode after compilation of .py file (source code). .pyc files are not created for all the files you run. It is only created for the files that you import. b. Before executing a python program, python interpreter checks for the compiled files. If the file is present, the virtual machine executes it. If not found, it checks for .py file. If found, compiles it to .pyc file and then python virtual machine (PVM) executes it. c. Having .pyc files saves you the compilation time. How Python is Interpreted a. Python as a language is not interpreted or compiled. Interpreted or compiled is the property of implementation. b. Python is a bytecode(set of interpreter readable instructions) interpreted generally. c. Source code is a file with .py extension. d. Python Compiles the source code to a set of instructions for a virtual machine. Python Interpreter is an implementation of that virtual machine. This intermediate format is called as ‘bytecode’. e. .py source code is first compiled to give .pyc which is bytecode. This bytecode can be then interpreted by the official CPython or JIT (Just in Time Compiler) compiled by PyPy. How are arguments passed in Python ? Passed by value ? or Passed by Reference ? a. In Python, arguments are passed by reference. b. Pass by Value: Copy of the actual object is passed. Changing the value of the copy of the object will not change the value of original object. c. Pass by Reference: Reference to the actual object is passed. Changing the value of new object will change the value of Original Object. d. Ex: e. def appendNumber(arr): f. arr.append(4) g. arr = [1, 2, 3] h. print(arr) #Output: => [1, 2, 3] i. appendNumber(arr) j. print(arr) #Output: => [1, 2, 3, 4] what is iterators ? a. An iterator is an object. It remembers its state i.e. where it is during iteration. b. __iter__ () method initializes an iterator. How to delete a file a. Import os. Os.remove(filename) Split() and join() functions a. Split() function: to split a string based on a delimiter to a list of strings. b. Join() function: to join a list of strings based on a delimiter to give a single string. *args and **kwargs a. *args is a special syntax used in the function definition to pass variable length arguments. b. **kwargs is a special syntax in the function definition to pass variable length keyworded arguments. How is memory managed in Python ? a. Memory management in python is done 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. Python interpreter takes care of this instead. b. The allocation of heap space for Python Objects is done by Python’s memory manager. c. Python has also an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space. What is PYTHONPATH a. It is an environment variable which is used when a mdule 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. Global and Local Variables a. Global Variables: Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program b. Local Variables: Any variable declared inside a function is known as global variable. This variable is present in local space and not in global space. Functions a. A function is a block of code which is executed only when it is called. Shuffle(x) is used to randomize the items of list x. Capitalize() method capitalizes (in upper case) the 1 st letter of the string. Some libraries of python: Numpy, Pandas, Matplotlib, Scikit-learn