range使用方法

使用python的人都知道range()函数非常方便,今天再用到他的时候发现了非常多曾经看到过可是忘记的细节。

这里记录一下:

range(1,5)#代表从1到5(不包括5)
[1,2,3,4]
range(1,5,2)#代表从1到5,间隔2(不包括5)
[1,3]
range(5)#代表从0到5(不包括5)
[0,1,2,3,4]

再看看list的操作:

array= [1,2,5,3,6,8,4]
#事实上这里的顺序标识是
[1,2,5,3,6,8,4]
(0123456)
(-7,-6,-5,-4,-3,-2,-1)
 
array[0:]#列出0以后的
[1,2,5,3,6,8,4]
array[1:]#列出1以后的
[2,5,3,6,8,4]
array[:-1]#列出-1之前的
[1,2,5,3,6,8]
array[3:-3]#列出3到-3之间的
[3]

for
loop

  1. the_count = [1, 2, 3, 4, 5]
  2. fruits = ['apples', 'oranges', 'pears', 'apricots']
  3. change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
  4.  
  5. # this first kind of for-loop goes through a list
  6. for number in the_count:
  7. print "This is count %d" % number
  8.  
  9. # same as above
  10. for fruit in fruits:
  11. print "A fruit of type: %s" % fruit
  12.  
  13. # also we can go through mixed lists too
  14. # notice we have to use %r since we don't know what's in it
  15. for i in change:
  16. print "I got %r" % i
  17.  
  18. # we can also build lists, first start with an empty one
  19. elements = []
  20.  
  21. # then use the range function to do 0 to 5 counts
  22. for i in range(0, 6):
  23. print "Adding %d to the list." % i
  24. # append is a function that lists understand
  25. elements.append(i)
  26.  
  27. # now we can print them out too
  28. for i in elements:
  29. print "Element was: %d" % i

输出

  1. This is count 1
  2. This is count 2
  3. This is count 3
  4. This is count 4
  5. This is count 5
  6. A fruit of type: apples
  7. A fruit of type: oranges
  8. A fruit of type: pears
  9. A fruit of type: apricots
  10. I got 1
  11. I got 'pennies'
  12. I got 2
  13. I got 'dimes'
  14. I got 3
  15. I got 'quarters'
  16. Adding 0 to the list.
  17. Adding 1 to the list.
  18. Adding 2 to the list.
  19. Adding 3 to the list.
  20. Adding 4 to the list.
  21. Adding 5 to the list.
  22. Element was: 0
  23. Element was: 1
  24. Element was: 2
  25. Element was: 3
  26. Element was: 4
  27. Element was: 5

一入python深似海--range()、list与for的更多相关文章

  1. 一入python深似海--dict(字典)的一种实现

    以下是python中字典的一种实现.用list数据结构实现字典.详细是这种:[[(key1,value1),(key2,value2),...],[],[],...] 内部每个hash地址是一个lis ...

  2. 一入python深似海--浅拷贝与深拷贝

    python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝. 要理解浅拷贝,必须先弄清楚python中的引用. 引用 Python中一切都是对象,变量中存放的是对象的引用 ...

  3. 一入python深似海--class

    python class 分为三个部分:class and object(类与对象),inheritance(继承),overload(重载)and override(覆写). class and o ...

  4. 一入python深似海--变量和对象

    一.基本原理 Python中一切都是对象,变量是对象的引用. 这是一个普遍的法则.我们举个样例来说.Python是怎样来处理的. x = 'blue' y = 'green' z = x 当pytho ...

  5. 一入python深似海--python之道

    python社区不乏幽默.先来看"python之道"这首诗. 导入this包: import this 输出是一首诗,这首诗总结了Python的风格,能够指导Python程序猿的编 ...

  6. 一入python深似海--对象的属性

    Python中一切皆是对象,每一个对象都能够有多个属性.Python是怎样管理这些属性呢?我们来探讨一下. 属性的__dict__系统 对象的属性包括两部分:类属性和对象属性.对象的属性可能来自于其类 ...

  7. 一入Python深似海--print

    先给大家来个干货^~^,学习Python的一个好站点,http://learnpythonthehardway.org/book/ 经典样例 以下是几个老经典的样例喽,刚接触Python的能够敲一敲, ...

  8. 「一入 Java 深似海 」系列课程

    第一期 「一入 Java 深似海 」系列课程 - 第一期 第一节:Java 语言基础

  9. 一入爬虫深似海,从此游戏是路人!总结我的python爬虫学习笔记!

    前言 还记得是大学2年级的时候,偶然之间看到了学长在学习python:我就坐在旁边看他敲着代码,感觉很好奇.感觉很酷,从那之后,我就想和学长一样的厉害,就想让学长教我,请他吃了一周的饭,他答应了.从此 ...

随机推荐

  1. Android坐标getLeft,getRight,getTop,getBottom,getLocationInWindow和getLocationOnScreen

    Android中获取坐标点的一些方法解释 一.getLocationInWindow和getLocationOnScreen的区别 // location [0]--->x坐标,location ...

  2. jmeter+ANT+Jekins性能自动生成测试报告脚本(模板),加入:Median TIme、90%、95%、99%、QPS、以及流量显示

    <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/T ...

  3. IO Streams:对象流

    简介 正如数据流支持原始数据类型的I / O一样,对象流支持对象的I / O.标准类中的大多数但不是全部都支持对象的序列化.那些实现标记接口Serializable的那些. 对象流类是ObjectIn ...

  4. UVALive 5099 Nubulsa Expo 全局最小割问题

    B - Nubulsa Expo Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  5. P2412 查单词

    题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...

  6. [luogu2044][NOI2012] 随机数生成器 [矩阵快速幂]

    题面: 传送门 思路: 看一眼这个公式: $x\left[n+1\right]=\left(a\ast x\left[n\right]+c\right) mod m$ 递推,数据范围$n\leq 10 ...

  7. drools6

    <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artif ...

  8. BestCoder 2nd Anniversary/HDU 5718 高精度 模拟

    Oracle Accepts: 599 Submissions: 2576 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/26 ...

  9. redux使用需要注意的地方

    1. react和redux没有直接联系,当react需要结合redux使用的时候,需要引入 react-redux ,该插件提供了connet等方法使得react可以注入redux属性. 2. re ...

  10. USACO 刷题记录bzoj

    bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...