tips python-tips   0   257
Difference between list, set and tuples in Python and more such comparisons

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:


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


>>> [x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Tuples:
- 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.

>>> import time
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=25, tm_hour=21, tm_min=52, tm_sec=24, tm_wday=0, tm_yday=268, tm_isdst=0)
 

Set:

- is just like mathematical set.
- is unordered.
- is mutable.
- doesn't contain duplicate values.

rana@Brahma: ~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [1,2,5,2,3]
>>> a = set(mylist)
>>> a
{1, 2, 3, 5}



range() and xrange():


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

rana@Brahma: ~$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 - 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".

rana@Brahma: ~$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = xrange(10)
>>> a
xrange(10)
>>> [i for i in xrange(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

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

>>> a = xrange(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xrange' is not defined

 - 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():


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

rana@Brahma: ~$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input()
2^3
1
>>> input(2**3)
8

 - In python3, return the user input as string.


raw_input():
- 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.

rana@Brahma: ~$ python3
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2]
>>> b = a
>>> print(id(a) == id(b))
True


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.

>>> a = [[1,2],[3,4]]
>>> b = a.copy()
>>> print(id(a) == id(b))
False
>>> print(id(a[0]) == id(b[0]))
True


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

>>> from copy import deepcopy
>>> a = [[1,2],[3,4]]
>>> b = deepcopy(a)
>>> print(id(a) == id(b))
False
>>> print(id(a[0]) == id(b[0]))
False

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

>>> print(id(a))
139724773442120


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:

*args:
- is used to pass the variable length list of non-keyworded arguments to function.

*kwargs:
- 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.    



tips python-tips   0   257

Related Articles:
Displaying custom 404 error (page not found) page in Django 2.0
How to create custom 404 error page in Django. Handler404 example Django 2.0. Page not found error in Django 2.0. Custom Error handlers in Django 2.0...
How to share data between two systems using python simple HTTP server
How to share large files between two system using python simple HTTP server. share movies using python server. transfer data without pen drive using python....
Designing custom 404 and 500 error pages in Django
How to create your own awesome 404 error or 500 error page in django. Step by step guide to help you design your own 404 not found error page in Django. Design your own 500 internal server error page in Django application...
What is the meaning of different aphorism in zen of python
What is zen of python. What is the meaning of zen of python. Explanation of zen of python terms with example. Easter egg Zen of python. import this....

0 thoughts on 'Difference Between List, Set And Tuples In Python And More Such Comparisons'
Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019