Differentiate between an expression and a statement?
Expressions contain only identifiers, literals, and operators. These operators consist of arithmetic and boolean operators, function call operators (), and subscription operators [], and can be reduced to some "value" that may be any Python object.
Statements are the smallest executable units of code, such as creating variables or displaying values. In any programming language, every line of code is called a statement. The interpreter or the programming language compiler can execute all of the lines in this code.
What are the positive and negative indices in Python?
In the positive indices, the search proceeds from left to right. With negative indices, the search moves from right to left. For example, in the array list of size x, the positive index, the first index is 0, then comes 1, and finally, the last index is x-1. In contrast, in the negative index, the first index is -x, followed by -(x-1) until the last index is -1.
What are the different stages of the Life Cycle of a Thread in Python?
The different stages of the life cycle of a thread are stated below:
- Overriding the run method of the thread class by creating your own class.
- As soon as a new thread is created, call start(), and the thread moves forward for scheduling purposes.
- Execution occurs when the thread starts execution, and reaches the running state.
- The thread waits until the calls to methods, including join() and sleep(), occur.
- Following the waiting or execution of the thread, the waiting thread is scheduled.
- A running thread terminates and reaches the dead state when it executes the terminates.
What is PEP 8 in Python, and why is it important?
Python Enhancement Proposal, also known as PEP, is an official design document that provides information to the Python community or proposes changes to features for Python or its processes. PEP 8 is especially significant since it documents the style guidelines for Python Code. Contributing to the Python open-source community requires you to follow these rules strictly.
How can we remove spaces from a string in Python?
With the use of strip() or replace() functions, spaces can be removed from a string in Python. Strip() function in a string can be used to remove the leading and trailing white spaces, whereas all the white spaces can be removed by using replace() function
How will you display a Text File's Contents in a Reverse Order?
The following steps can be used to display the contents of a text file in reverse order:
- Use open() function to open the file
- Store contents of the file into the list
- Now, reverse the contents of a list
- To iterate through the list, run a for loop
Is Python an Object-oriented or Functional Programming Language?
Python is a multi-paradigm language and follows an object-oriented paradigm. It enables the creation of objects and their manipulation with specific methods. It supports OOPS's features such as inheritance and polymorphism. You can use its paradigm functions as a first-class object and can also execute the Lambda function in Python.
What is a Flask in Python?
Based on "Werkzeug, Jinja 2, and good intentions", Flask is a microframework for web applications in Python. It depends on Werkzeug and Jinja.Flask is a component of the micro-framework. Thus, it will be independent of external libraries. In addition to being light, the framework is less likely to have security issues and is less dependent on updating.
What is the difference between Pickling and Unpickling?
There is a feature - serialization - built into the Python library. The action of serializing an object involves transforming it into a format that can be stored so that it can be deserialized later on to return the original object. It is here that the pickle module is useful.
Pickling:
- In Python, the serialization process is known as Pickling. Python can serialize any byte stream and dump it as a file in memory. Although Pickling is a compact process, pickle objects can be compressed more. The serialization is also portable across versions, and the pickle keeps track of objects it serializes.
- The function used for Pickling is pickle.dump().
Unpickling:
- In Unpickling, the byte stream is deserialized to recreate the objects stored in the file and then loads them into the memory.
- The function used for unpickling is pickle.load().
What is the difference between deep copy and shallow copy?
Deep Copy
Shallow Copy
You can store already copied values in deep copies.
You can use a shallow copy when a new instance type is created. The copied values are kept in the new instance.
The reference pointers to the objects are not copied.
The shallow copy also copies reference pointers.
You must know that deep copies refer to an object and store the new object pointed by another object. It will not affect any other copies that reference or store the object referenced in the original copy.
The shallow copies are based on the reference points in the original. If you change any member of a class, it will affect its original copy