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:
- 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()
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]
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()
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
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
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())
.
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 | str | byte | |
Python 2 | unicode characters | raw 8 bit values | n/a |
Python 3 | n/a | unicode characters | raw 8 bit values |
*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.