Difference between list and tuples in Python and more such comparisons

differences in python

In this article we will see key differences between commonly used terms in python. For example difference between tuple and list, difference between range and xrange and so on.

List and Tuples:

– are mutable i.e. we can add, extend or update a list.
– generally are homogeneous data structures.

– are immutable i.e. we can not update a tuple.
– generally are heterogeneous data structure.
– is a sequence where position have semantic value.
– are more like records, collection of fixed number of fields.


range() and xrange():

– In python2 range() function returns a list of integers.

– In python3 range() do whatever xrange use to do in python2 i.e. returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called “lazy evaluation”.

– There is no xrange() function in python3. We already know what xrange () use to do in python2.

– Since we are moving towards python3, we will not have any further discussion about it.

If you are really interested visit this link.

raw_input() and input():

– In python2, input() tries to run the user input as a valid python expression.

– In python3, return the user input as string.

– In python2, return the user input as string.
– Doesn’t exists in python3. To simulate the raw_input() in python3, use eval(input()) .

shallow and deep copy:

Before comparing shallow and deep copy, we must know that normal assignment is just pointing new variable to existing object.

comparison of shallow copy and deep copy is relevant for compound objects i.e. where objects contains another objects.

shallow copy:
Shallow copy creates a new object and then use the reference to refer the inner objects.

Deep copy:
deep copy create a new object and recursively copy the inner objects too.

id() function return the identity of the location of the object in memory.

Unicode and str:

Here is a nicely written article explaining the difference between str, unicode and byte.

unicode str byte
Python 2 unicode characters raw 8 bit values n/a
Python 3 n/a unicode characters raw 8 bit values
*args and **kwargs:

– is used to pass the variable length list of non-keyworded arguments to function.

– is used to pass the variable length list of keyworded arguments to a function.

Always pass arguments in this order : formal arguments then non-keyworded argument list and then keyworded arguments list.



(Visited 367 times, 1 visits today)

You must read this :

1 thought on “Difference between list and tuples in Python and more such comparisons”

Leave a Reply

Your email address will not be published. Required fields are marked *