Sunday 18 October 2020

Some useful differences that most of the people ask

 

What is difference between "inplace = True" and "inplace = False?


Both inplace= true and inplace = False are used to do some operation on the data but:

When inplace = True is used, it performs operation on data and nothing is returned.

df.some_operation(inplace=True)

When inplace=False is used, it performs operation on data and returns a new copy of data.

df = df.an_operation(inplace=False)                                                  


What is the difference between list and tuple?


Lists are mutable(values can be changed) whereas tuples are immutable(values cannot be changed).

You can consider the Lists as Arrays in C, but in List you can store elements of different types, but in Array all the elements should of the same type whereas a Tuple is a sequence of immutable Python objects. Tuples are sequences, just like Lists. The differences between tuples and lists are:
1) Tuples cannot be changed unlike lists
2) Tuples use parentheses, whereas lists use square brackets. Consider the example below:

Countries = ('India', 'US', 'UK')

Now you must be thinking why Tuples when we have Lists?
So the simple answer would be, Tuples are faster than Lists. If you’re defining a constant set of values which you just want to iterate, then use Tuple instead of a List. 
All Tuple operations are similar to Lists, but you cannot update, delete or add an element to a Tuple.


What is the difference between Python and IPython?


 The true flavor of Python is maintained even in the Ipython.

The best part of the IPython is the IPython notebook. You can put all your work into a notebook like script, image files, etc. But with base Python, you can only make the script in a file and execute it.

At start, you need to understand that the IPython is developed with the intention of supporting rich media and Python script in a single integrated container.


what is the difference between append and insert in python lists?


There is a simple difference between append and insert in python list,
append method can be use for adding new element in the list only but by using insert we can add as well as can modify already occupied position.append method takes one argument (which you have to insert in the list) while insert method takes two elements (first will be the position of element and second will the element itself), Below are examples for both methods use:


Use of Append:

list = [1,2,3,4,5]

list.append(6)

print(list) # [1,2,3,4,5,6]

Use of Insert:

list = [1,2,3,4,5]

list.insert(5, 10) # [1,2,3,4,5,10]

list.insert(1, 10) # [1,10,3,4,5]
 

You can insert element at any position, but till will be store just after the last element position. Reason behind this logic is list is store data in ordered format. 


list.insert(100, 50) # [1,2,3,4,5,10]


What is the difference between print and return in python?


return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

On the other hand, print() is an INBUILT function in Python, originally written in C. So what that means is, it runs the program and outputs whatever is required it to do - in this case literally output what the user wants it to.

In other words, an if-else or a loop must return some value. in case nothing is mentioned, Python is smart enough to return None.


What is the difference between python lists and arrays?


LISTARRAY
Can consist of elements belonging to different data typesOnly consists of elements belonging to the same data type
No need to explicitly import a module for declarationNeed to explicitly import a module for declaration
Cannot directly handle arithmetic operationsCan directly handle arithmetic operations
Can be nested to contain different type of elementsMust contain either all nested elements of same size
Preferred for shorter sequence of data itemsPreferred for longer sequence of data items
Greater flexibility allows easy modification (addition, deletion) of dataLess flexibility since addition, deletion has to be done element wise
The entire list can be printed without any explicit loopingA loop has to be formed to print or access the components of array
Consume larger memory for easy addition of elementsComparatively more compact in memory size

No comments:

Post a Comment