Skip to content

Understanding the Python xrange() Method

Python Xrange() Method

Today in this tutorial, we are going to discuss the Python xrange() method.

The xrange() method is only available for use in Python 2.x versions and used in loops for traversing or iterating through the sequence.

Basics of the Python xrange() Method

The Python xrange() method returns a xrange type object which is an immutable sequence commonly used for looping. These objects have very little behavior and only support indexing, iteration, and the len() function.

xrange(start, stop[, step])

Here:

  • start(optional) is the starting point from which the sequence generation would start. It is included in the sequence and if not mentioned is by default set to 0,
  • stop is the number before which the sequence generation would stop(exclusive),
  • step(optional) is the step jump the function would to take while sequence generation or iteration. By default, it has a value of 1.

Using the Python xrange() Method

Now let us look at the various ways we can actually use the Python xrange() method.

1. With only stop parameter

The two parameters, step and start are optional as they have default values of 1 and respectively. Hence we can use the Python xrange() method just by specifying the stop parameter.

When only stop is mentioned, the xrange() function creates a sequence ranging from 0 to (stop-1) with step 1. Look at the example below.

# xrange() with only stop parameter

x = xrange(4)

print "Type of returned object = ", type(x)
print "Sequence generated by xrange() 1 parameter: ", list(x)

Output:

Type of returned object =  <type 'xrange'>
Sequence generated by xrange() 1 parameter:  [0, 1, 2, 3]

Here, the type of sequence generated by the method is of type xrange as mentioned earlier. Type-casting the xrange() output into a list gives us a list containing the values 0 to 3(4-1) with step 1.

2. With start and stop parameters

Similarly, we can also use the Python xrange() method with two parameters. In this case by default, the step has a value 1.

# xrange() with start & stop parameters

x = xrange(2, 9)

print "Type of returned object = ", type(x)
print "Sequence generated by xrange() 2 parameters: ", list(x)

Output:

Type of returned object =  <type 'xrange'>
Sequence generated by xrange() 2 parameters:  [2, 3, 4, 5, 6, 7, 8]

As we can see from the above output, the Python xrange object this time contains values ranging from 2(start) to 8(stop-1) with a default step 1.

3. With all start, stop, and stop values

When all the parameters are mentioned, the Python xrange() function gives us a xrange object with values ranging from start to stop-1 as it did in the previous case. But this time, the jump from one element to another is equal to the step passed.

Have a look at the example below carefully. Here we have considered step as 5, start=5 and, stop=40.

# xrange() with 3 parameters

x = xrange(5, 40, 5) #positive step
y = xrange(9, 1, -1) #negative step

print "Sequences generated for 3 parameters by xrange"
print "x : ", list(x)
print "y : ", list(y)

Output:

Sequences generated for 3 parameters by xrange
x :  [5, 10, 15, 20, 25, 30, 35]
y :  [9, 8, 7, 6, 5, 4, 3, 2]

From the output, it is clear that the sequence is generated with values in range 5 to 39(40-1). For the last element, since 40 exceeds the stop-1 mark it is not considered.

Using Python xrange() in loops

As we mentioned earlier, xrange() is widely used in for loop structures in Python 2. For example in the code below we try to print the table for the number 10.

# using xrange in loops

print "Table of 10: "

for i in xrange(10, 101, 10):
    print i

Output:

Table of 10: 
10
20
30
40
50
60
70
80
90
100

Here, we have mentioned start=10, stop=101 and step=10. Stop has been considered as 101 since we want the sequence to include the 100(100-1) element. As we can see we get all the multiples of 10 ranging from 10 to 100.

Hence, the output is justified.

Python xrange() vs range()

Normally, xrange() is only used when the user wants his/her code to be only designed for Python 2.x versions as it is not available for Python 3.x versions. For Python 3 the range() method replaces the xrange() method instead.

The major difference between the two function is that, both of them return different objects with different properties.

The xrange() method returns a xrange object which is an immutable sequence and which only supports iteration, indexing and len function as mentioned earlier.

On the other hand, the range() method returns a list which supports major functions like slicing, pop() etc.

# xrange() vs range()

x = xrange(1, 10)
y = range(1, 10)

print "Type(x) = ", type(x)
print "Type(y) = ", type(y)

Output:

Type(x) =  <type 'xrange'>
Type(y) =  <type 'list'>

Moreover, xrange objects are more useful when we need to build code with minimal space complexity as it takes fairly constant memory size independent of the range of values it stores.

If you want to write a program that can be run or executed on both Python 2 and 3, using the range() method makes more sense and hence is recommended.

Conclusion

So in this tutorial, we understood the working as well as use of the Python xrange() method.

For any further questions, feel free to ask them in the comments below.

References

  • Python range() Method,
  • What is the difference between range and xrange functions in Python 2.X? – StackOverflow Question.

民权资讯网故事新编补天读后感周易八卦的名字女装店起名时尚简约国际会议网站建设网站内部链接优化冬奥会直播足球比分yaoji1真钱姓薛女孩起名大全具体怎么做seo许国华孙思颖小说免费无弹窗阅读笔趣享诗词温字取名起名大全大全seo学习网站有哪些算命先生免费seo描述是什么福州网站建设公司哪家好陌字起名字的寓意好吗的毛的男孩子起名靖江建设网站周易免费测姓名网站建设+长沙秦起名男童知名建筑设计网站深圳网站制作开发费用济南餐饮设计公司寒风吹起细雨淋漓是什么歌以煜起名字吗徐姓起名女孩取名如何设计自己的签名宝宝起名十画的字有哪些字少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

民权资讯网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化