Basic Python Interview Questions Flashcards
What is Python?
- Python is a high-level, interpreted, interactive, and object-oriented scripting language. It uses English keywords frequently. Whereas other languages use punctuation, Python has fewer syntactic constructions.
- Python is designed to be highly readable and compatible with different platforms such as Mac, Windows, Linux, Raspberry Pi, etc.
Python is an interpreted language. Explain.
An interpreted language is any programming language where the source code is run through an interpreter which then reads statements line by line, “interpreting” the code into something the machine can understand on the fly.
This is opposed to compiled languages which must have their code parsed through a compiler that converts the source code into machine code before it can actually be run on a machine.
Programs written in Python run directly from the source code, with no intermediary compilation step.
What is the difference between lists and tuples?
Lists
- Lists are mutable, i.e., they can be edited
- Lists are usually slower than tuples
- Lists consume a lot of memory
- Lists are less reliable in terms of errors as unexpected changes are more likely to occur
- Lists consist of many built-in functions.
- Syntax:
list_1 = [10, ‘Intellipaat’, 20]
Tuples
- Tuples are immutable (they are lists that cannot be edited)
- Tuples are faster than lists
- Tuples consume less memory when compared to lists
- Tuples are more reliable as it is hard for any unexpected change to occur
- Tuples do not consist of any built-in functions.
- Syntax:
tup_1 = (10, ‘Intellipaat’ , 20)
What is pep 8?
PEP in Python stands for Python Enhancement Proposal. It is a set of rules that specify how to write and design Python code for maximum readability.
What are the Key features of Python?
- Python is an interpreted language, so it doesn’t need to be compiled before execution, unlike languages such as C.
- Python is dynamically typed, so there is no need to declare a variable with the data type. Python Interpreter will identify the data type on the basis of the value of the variable.
For example, in Python, the following code line will run without any error:
a = 100 a = “Intellipaat”
- Python follows an object-oriented programming paradigm with the exception of having access specifiers. Other than access specifiers (public and private keywords), Python has classes, inheritance, and all other usual OOPs concepts.
- Python is a cross-platform language, i.e., a Python program written on a Windows system will also run on a Linux system with little or no modifications at all.
- Python is literally a general-purpose language, i.e., Python finds its way in various domains such as web application development, automation, Data Science, Machine Learning, and more.
How is Memory managed in Python?
- Memory in Python is managed by Python private heap space. All Python objects and data structures are located in a private heap. This private heap is taken care of by Python Interpreter itself, and a programmer doesn’t have access to this private heap.
- Python memory manager takes care of the allocation of Python private heap space.
- Memory for Python private heap space is made available by Python’s in-built garbage collector, which recycles and frees up all the unused memory.
What is PYTHONPATH?
PYTHONPATH has a role similar to PATH. This variable tells Python Interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by Python Installer.
What are Python Modules?
Files containing Python codes are referred to as Python Modules. This code can either be classes, functions, or variables and saves the programmer time by providing the predefined functionalities when needed. It is a file with “.py” extension containing an executable code.
Commonly used built modules are listed below:
- os
- sys
- data time
- math
- random
- JSON
What are python namespaces?
A Python namespace ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as dictionaries with ‘name as key’ mapped to its respective ‘object as value’.
Let’s explore some examples of namespaces:
- Local Namespace consists of local names inside a function. It is temporarily created for a function call and gets cleared once the function returns.
- Enclosed Namespace: When a function is defined inside a function, it creates an enclosed namespace. Its lifecycle is the same as the local namespace.
- Global Namespace: Belongs to the python script or the current module. The global namespace for a module is created when the module definition is read. Generally, module namespaces also last until the interpreter quits.
- Built-in Namespace consists of built-in functions of core Python and dedicated built-in names for various types of exceptions. It is created when the Python interpreter starts up and is never deleted.
Explain Inheritance in Python with an example?
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Inheritance provides the code reusability feature.
Parent class is the class being inherited from, also called base class or super class.
Child class is the class that inherits from another class, also called derived class.
The following types of inheritance are supported in Python:
- Single inheritance: When a class inherits only one superclass
- Multiple inheritance: When a class inherits multiple superclasses
- Multilevel inheritance: When a class inherits a superclass, and then another class inherits this derived class forming a ‘parent, child, and grandchild’ class structure
- Hierarchical inheritance: When one superclass is inherited by multiple derived classes
What is scope resolution?
The scope of an object name is the region of a program in which that name has meaning.
Scope resolution is required when a variable is used to determine where should its value come from. The interpreter determines this at runtime based on where the name definition occurs and where in the code the name is referenced. Scope resolution in Python follows the LEGB rule.
L, Local — Names assigned in any way within a function (or lambda), and not declared global in that function.
E, Enclosing-function locals — Name in the local scope of any and all statically enclosing functions(or lambdas), from inner to outer.
G, Global (module) — Names assigned at the top-level of a module file, or by executing a global statement in a def within the file.
B, Built-in (Python) — Names preassigned in the built-in names module : open, range,SyntaxError, etc.
What is a dictionary in Python?
Python dictionary is one of the supported data types in Python. It is an unordered collection of elements. The elements in dictionaries are stored as key-value pairs. Dictionaries are indexed by keys.
For example, below we have a dictionary named ‘dict’. It contains two keys, Country and Capital, along with their corresponding values, India and New Delhi.
Syntax:
dict={‘Country’:’India’,’Capital’:’New Delhi’, }
Output: Country: India, Capital: New Delhi
What are functions in Python?
A function is a block of code which is executed only when a call is made to the function.
The def keyword is used to define a particular function as shown below:
def function():
print(“Hi, Welcome to Intellipaat”)
function(); # call to the function
Output:
Hi, Welcome to Intellipaat
What is __init__ in Python?
Equivalent to constructors in OOP terminology, __init__ is a reserved method in Python classes. The __init__ method is called automatically whenever a new object is initiated. This method allocates memory to the new object as soon as it is created. This method can also be used to initialize variables.
Syntax
(for defining the __init__ method):
class Human:
init method or constructor
def __init__(self, age):
self.age = age
Sample Method
def say(self):
print(‘Hello, my age is’, self.age)
h= Human(22)
h.say()
Output:
Hello, my age is 22
What are the common built-in data types in Python?
Python supports the below-mentioned built-in data types:
Immutable data types:
- Number
- String
- Tuple
Mutable data types:
- List
- Dictionary
- Set