-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpython31 (range vs xrange).py
37 lines (30 loc) · 1.29 KB
/
python31 (range vs xrange).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# range() vs xrange()
# in python2, there are 2 functions range and x range()
# though they both are used when we are working with a range of values.
# they have some difference.
# range() returns a list while xrange() returns a generator.
# the type of the range object is <class "list"> where the type of a xrange object is <class "xrange">
# range() take much more space in the memory compared to xrange()
# range() is little faster than xrange() but that it tottaly neglectable
# we can do slicing with range() as it is a list, but we can do it with xrange()
# we have to iterate over xrange() object to use it.
# in python3, there is no xrange() function.
# there is only range() function which works as the xrange() function
# and all the disadvantages of xrange() function has solved.
# 1. so range() function in python3 returns a generator and the range() object is <class "range">
# 2. it takes little space in the memory and it is fast too.
# 3. we can slice the range() object and the slice will also be a range object.
# 4. we have to iterate over range() object to use it.
# actually range() use lazy evolution technic to do its operation.
# 1
print(range(1,6))
print(type(range(1,6)))
# 2
import sys
print(sys.getsizeof(range(100)))
# 3
a=range(1,6)[2:5]
print(a)
# 4
for i in range(1,6):
print(i)