1) What is Python? What are the advantages of utilizing Python?
Python is an interpreted, high-level, general-purpose programming language. Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python is often described as a “batteries included” language due to its comprehensive standard library.
2) What is PEP 8?
PEP 8 has emerged as the style guide that most projects adhere to it promotes a very readable and eye-pleasing coding style.
3) What is pickling and unpickling?
Pickling – is the process whereby a Python object hierarchy is converted into a string.
Unpickling – is the inverse operation, whereby a string is converted back into an object hierarchy.
4) How Python is interpreted?
The Interpreted or compiled is not a property of the language but a property of the implementation. Python program runs directly from the source code. so, Python will fall under byte code interpreted. The .py source code is first compiled to byte code as .pyc. This byte code can be interpreted. Python source code (.py) can be compiled to different byte code also like IronPython (.Net) or Jython (JVM). There are multiple implementations of Python language. The official one is a byte code interpreted one. There are byte code JIT-compiled implementations too.
As concluding remarks, Python is neither a true compiled time nor purely interpreted language but it is called interpreted language.
5) How memory is managed in Python?
Python memory 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 and interpreter takes care of this Python private heap.
The allocation of Python heap space for Python objects is done by the Python memory manager. The core API gives access to some tools for the programmer to code.
Python also has an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.
6) What are the tools that help to find bugs or perform the static analysis?
PyChecker is a static analysis tool that finds bugs in Python source code and warns about code complexity and style. You can get PyChecker from this link http://pychecker.sf.net.
Pylint is another tool that verifies whether the module meets the coding standard.
7) What are Python decorators?
A Python decorator is a specific change that we make in Python syntax to alter functions easily.
Example: def dec1(func1): def insidedec(): print("1st Value") func1() print("2nd Value") return insidedec @dec1 def callinsidedec(): print("Middle Value") # outside = dec1(callinsidedec) callinsidedec()
8) What is the difference between list and tuple?
The difference between list and tuple is that list is mutable while tuple is not.
List Can be changed Example: We assigning in the variable list item
a = ["apples", "bananas", "oranges"] a = "berries" print(a) Output: ['berries', 'bananas', 'oranges']
The tuple is immutable:
a = ("apples", "bananas", "oranges") a = "berries" Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment
9) How are arguments passed by value or by reference?
Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result, you cannot change the value of the references. However, you can change the objects if it is mutable.
10) What is Dict and List comprehensions are?
They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.
11) What is the built-in type does python provides?
There are mutable and Immutable types of Pythons built-in types Mutable built-in types
Immutable built-in types
13) What is lambda in Python?
It is a single expression anonymous function often used as an inline function.
r = lambda x, y: x * y print(r(5, 5)) Expected Output: 25
14) What is namespace in Python?
A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.
15) What are the functions in Python?
A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used.
def Myfun(): print("Hi, Welcome to Real Programmer") Myfun();
16) Is indentation required in python?
Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block. It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.
17) What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.
18) 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.
class Student: def __init__(self, name, age, fees): self.name = name self.age = age self.fees = fees S1 = Student("Test", 20, 20000) print(S1.name) print(S1.age) print(S1.fees)
19) How can you randomize the items of a list in place in Python?
Consider the example shown below:
from random import shuffle x = ['Siddharth', 'Shyam', 'Shivendra', 'Ram', 'Deepak', 'Suman'] shuffle(x) print(x)
20) 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.
""" Using docstring as a comment. This code divides 2 numbers """ x=8 y=4 z=x/y print(z) Output: 2.0
21) Python Collections
There are four collection data types in the Python programming language:
List: is a collection which is ordered and changeable. Allows duplicate members.
Tuple: is a collection which is ordered and unchangeable. Allows duplicate members.
Set: is a collection which is unordered and unindexed. No duplicate members.
Dictionary: is a collection which is unordered, changeable and indexed. No duplicate members.
22) What is split used for?
The split() method is used to separate a given string in Python.
a="Real Programmer" print(a.split()) Output: ['Real', 'Programmer']
23) How to import modules in python?
Modules can be imported using the import keyword. You can import modules in three ways-
import array #importing using the original module name import array as arr # importing using an alias name from array import * #imports everything present in the array module
24) Does python support multiple inheritances?
Multiple inheritances mean that a class can be derived from more than one parent classes. Python does support multiple inheritances, unlike Java.
25) Define encapsulation in Python?
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition.
class SeeMee: def youcanseeme(self): return 'You can call from outside' def __youcannotsee(self): return 'You cannot call from outside' #Outside class Check = SeeMee() print(Check.youcanseeme()) print(Check._SeeMee__youcannotsee()) #Changing the name causes it to access the function
26) Generators vs Iterators?
Table of difference between Iterator vs Generators
|Class is used to implement an iterator||Function is used to implement a generator.|
|Local Variables aren’t used here.||All the local variables before the yield function are stored.|
|Iterators are used mostly to iterate or convert other objects to an iterator using iter() function.||Generators are mostly used in loops to generate an iterator by returning all the values in the loop without affecting the iteration of the loop|
|Iterator uses iter() and next() functions||Generator uses yield keyword|
|Every iterator is not a generator||Every generator is an iterator|
Using an iterator-
- iter() keyword is used to create an iterator containing an iterable object.
- next() keyword is used to call the next element in the iterable object.
- After the iterable object is completed, to use them again reassign them to the same object.
iter_list = iter(['Geeks', 'For', 'Geeks']) print(next(iter_list)) print(next(iter_list)) print(next(iter_list))
def sq_numbers(n): for i in range(1, n+1): yield i*i a = sq_numbers(3) print("The square of numbers 1,2,3 are : ") print(next(a)) print(next(a)) print(next(a))
27) Write a program reverse a string ?
def reverse(s): str = "" for i in s: str = i+str return str s = "Siddharth" print(reverse(s))