In the dynamic realm of programming languages, Python has emerged as a versatile and powerful force. Whether you’re a seasoned developer or just stepping into the world of coding, understanding Python is akin to acquiring a master key for the digital age. This article will help you to prepare for your interview questions
Byte Code represents the fixed set of instruction created by Python developers representing all type of operations like arithmetic operations, comparison operation, memory related operation etc. The size of each byte code instruction is 1 byte or 8 bits. We can find byte code instruction in the .pyc file.
A Python Compiler converts the program source code into byte code and Cpython used when we download from official site.
How Python Work?
- Write Source Code
- Compile the Program using Python Compiler
- Compiler Converts the Python Program into byte Code
- Computer/Machine Can not understand Byte Code so we convert it into Machine Code using PVM
- PVM uses an interpreter which understands the byte code and convert it into machine code
- Machine Code instructions are then executed by the processor and results are displayed
Python Virtual Machine
Python Virtual Machine (PVM) is a program which provides programming environment. The role of PVM is to convert the byte code instructions into machine code so the computer can execute those machine code instructions and display the output.
An identifier is a name having a few letters, numbers and special characters (underscore). It should always start with a non-numeric character. It is used to identify a variable, function, symbolic constant, class etc.
In Python, a variable is considered as tag that is tied to some value. Python considers value as objects.
Datatype represents the type of data stored into a variable or memory.
Type of Data type :
Built-in Data type
1. None Type
2. Numeric Types
User Defined Data type
Types of Operators
An operator is a symbol that performs an operation.
Arithmetic Operators arc used to perform basic arithmetic operations like addition, subtraction, division etc.
Relational Operators / Comparison Operators
Relational operators are used to compare the value of operands (expressions) to produce a logical value. A logical value is either True or False.
Logical operators arc used to connect more relational operations to form a complex expression called logical expression. A value obtained by evaluating a logical expression is always logical, i.e. either True or False.
Assignment operators are used to perform arithmetic operations while assigning a value to a variable.
Bitwise operators arc used to perform operations at binary digit level. These operators are not commonly used and are used only in special applications where optimized use of storage is required.
The membership operators are useful to test for membership in a sequenc esuch as string, lists, tuples and dictionaries.
There are two type of Membership operator:-
- not in
The identity operators compare the memory locations of two objects. Hence, it |is possible to know whether two objects are same or not.
There are two types of Identity operator
- is not
Operator Precedence and Associativity
The computer scans an expression which contains the operators from left to right and performs only one operation at a time. The expression will be scanned many times to produce the result. The order in which various operations are performed is known as hierarchy of operations or operator. Some of the operators of the same level of precedence are evaluated from left to right or right to left. This is referred to associativity.
Converting one data type into another data type is called Type Conversion.
Type of Type Conversion:-
- Implicit Type Conversion
In the Implicit type conversion, python automatically converts one data type into another data type.
2. Explicit Type Conversion
In the Cast/Explicit Type Conversion, Programmer converts one data type into another data type.
It is used to execute an instruction or block of instructions only if a condition is fulfilled.
Pass Statement is used to do nothing. It can be used inside a loop or if statement to represent no operation. Pass is useful when we need statement syntactically correct but we do not want to do any operation.
Break statement is used to jump out of loop to process the next statement in the program.
Continue statement is used in a loop to go back to the beginning of the loop.
In Python, Array is an object that provide a mechanism for storing several data items with only one identifier, thereby simplifying the task of data management. Array is beneficial if you need to store group of elements of same datatype.
pip is the package manager for Python. Using pip we can install python packages.
Mutable and Immutable Object
Mutable objects are those object whose value or content can be changed as and when required.
Ex:- List, Set, Dictionaries
2. Immutable Object
Immutable objects are those object whose value or content can not be changed.
Ex:- Numbers, String, Tuple
Anonymous Function or Lambdas
A function without a name is called as Anonymous Function. It is also known as Lambda Function. Anonymous Function are not defined using def keyword rather they are defined using lambda keyword.
The variable which are declared inside a function called as Local Variable. Local variable scope is limited only to that function where it is created. It means local variable value is available only in that function not outside of that function.
When a variable is declared above a function, it becomes global variable. These variables are available to all the function which are written after it.
A function calling itself again and again to compute a value is referred to Recursive Function or Recursion.
A Decorator function is a function that accepts a function as parameter and returns a function. A decorator takes the result of a function, modifies the result and returns it. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. We use @function_name to specify a decorator to be applied on another function
1. A list represents a group of elements.
2. Lists are very similar to array but there is major difference, an array can store only one type of elements whereas a list can store different type of elements.
3. Lists are mutable so we can modify it’s element.
4. A list can store different types of elements which can be modified.
5. Lists are dynamic which means size is not fixed.
Higher Order Function
filter () Function — The filter function is used to filter out the elements of an iterable (sequence) depending on a function that tests each clement in the sequence to be true or not.
map () Function — This function executes a specified function on each element of the iterable (sequence) and perhaps changes the elements.
reduce () Function — This function is used to reduce a sequence of elements to a single value by processing the elements according to a function supplied. It returns a single value.
What are Python generators?
Generators are functions that allow you to iterate over a potentially large set of data using lazy evaluation.
Object Oriented Programming
Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.
Concepts of OOPS
Encapsulation in object-oriented programming involves bundling data and methods within a class, hiding the internal state and controlling access to the data through methods. It promotes data hiding and implementation hiding for better modularization and code organization.
Abstraction is the concept in object-oriented programming that focuses on simplifying complex systems by modeling classes based on relevant characteristics and ignoring irrelevant details. It involves creating abstract classes with a set of common attributes and behaviors, allowing for generalized representations and efficient problem-solving.
Inheritance is a fundamental concept in object-oriented programming where a class (subclass or derived class) inherits attributes and behaviors from another class (superclass or base class). It promotes code reusability and establishes an “is-a” relationship between classes, allowing the subclass to reuse and extend the functionality of the superclass.
Polymorphism is a concept in object-oriented programming that allows objects of different types to be treated as objects of a common type. It enables a single interface to represent multiple underlying data types, providing a way to use a single operation or method in different contexts. Polymorphism is often expressed through method overloading and method overriding, allowing objects to respond to the same message in different ways based on their specific types or classes.
What is Class and objects?
A Python class is a group of attributes and methods and Object is class type variable or class instance. To use a class, we should create an object to the class.
Python supports a special type of method called constructor for initializing the instance variable of a class.
A class constructor, if defined is called whenever a program creates an object of that class. A constructor is called only once at the time of creating an instance. If two instances are created for a class, the constructor will be called once for each instance.
In Python, Namespace represents a memory block where names are mapped to objects.
Class Namespace — A class maintains it’s own namespace known as class namespace. In the class namespace, the names are mapped to class variables.
Instance Namespace — Every instance have it’s own namespace known as instance namespace. In the instance namespace, the names are mapped to instance variables.
Instance methods are the methods which act upon the instance variables of the class. Instance method need to know the memory address of the instance which is provided through self variable by default as first parameter for the instance method.
When more than one method with the same name is defined in the same class, it is known as method overloading.
If any operator performs additional actions other than what it is meant for, it is called operator overloading.
A module is a file containing Python definitions and statements. .
A module is a file containing group of variables, methods, function and classes ete.
They are executed only the first time the module name is encountered in an import
The file name is the module name with the suffix py appended.
Packages arc a way of structuring Python’s module namespace by using “dotted module names”. A package can have one or more modules which means, a package is collection of modules and packages. A package can contain packages. Package is nothing but a Directory/Folder |
The with statement can be used while opening a file. When we open a file using with statement there is no need to close the file explicitly.
Pickling and Unpickling in Python
Pickling is a process of converting a class object into a byte stream so that it can be stored into a file. This is also called as object serialization. We use pickle module to perform pickling and unpickling.
Unpickling is a process whereby byte stream is converted back into a class object. It is inverse operation of pickling. This is also called as de-serialization. Pickling and unpickling should be done using binary files since they support
List comprehension is a concise way to create a new list from an existing list.
In Python, zip is a built-in function that is used to combine two or more iterables (such as lists, tuples, or strings) element-wise. The resulting iterator stops when the shortest input iterable is exhausted.
Enumerate is a built-in function that allows you to iterate over a sequence (such as a list, tuple, or string) while keeping track of the index of the current item.
What is Data abstraction?
Data abstraction in Python refers to the concept of hiding the implementation details of data objects and exposing only the essential features through the use of classes and objects.