一入python深似海--range()与for

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

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print "This is count %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

# now we can print them out too
for i in elements:
    print "Element was: %d" % i

输出

This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

一入python深似海--range()与for

时间: 2024-08-02 07:02:09

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

一入python深似海--while-loop与for-loop

while loops 定义与实例 i = 0 numbers = [] while i < 6: print "At the top i is %d" % i numbers.append(i) i = i + 1 print "Numbers now: ", numbers print "At the bottom i is %d" % i print "The numbers: " for num in numbe

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

下面是python中字典的一种实现,用list数据结构实现字典.具体是这样的:[[(key1,value1),(key2,value2),...],[],[],...] 内部每一个hash地址是一个list,存放hash地址相同的(key,value)对. dict代码 def Map(num_buckets=256): """Initializes a Map with the given number of buckets.""" aMap

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

一.基本原理 Python中一切都是对象,变量是对象的引用.这是一个普遍的法则.我们举个例子来说,Python是如何来处理的. x = 'blue' y = 'green' z = x 当python执行上面第一句的时候,会在heap中首先创建一个str对象,其文本内容为blue,同时还创建一个名为x的对象引用,x引用的就是这个str对象.第二句也是类似:第三条创建了一个名为z的新对象引用,并将其设置为对象引用x所指向的相同对象.如下图的变化. 所以看出在Python中赋值操作符号"="

一入python深似海--list

几个实例展示python中数据结构list的魅力! list变量申明 the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] 访问list元素 array= [1,2,5,3,6,8,4] #其实这里的顺序标识是 (0,1,2,3,4,5,6) (-7,-6,-5,-4,-3,-2,-1)#有

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

Python中一切皆是对象,每个对象都可以有多个属性.Python是如何管理这些属性呢?我们来探讨一下. 属性的__dict__系统 对象的属性包含两部分:类属性和对象属性.对象的属性可能来自于其类的定义,叫做类属性.类属性可能来自于类的定义自身,也可能来自父类.一个对象的属性还可能是该对象实例定义的,叫做对象属性. 对象的属性存储在对象的__dict__属性中.__dict__为一个字典,键为属性名,对应的值为属性本身.下面是一个例子. class bird(object): feather

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

python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝.要理解浅拷贝,必须先弄清楚python中的引用. 引用 Python中一切都是对象,变量中存放的是对象的引用.这是一个普遍的法则.可以说 Python 没有赋值,只有引用.如,a=1,变量a只是整数对象1的引用. 可变对象与不可变对象及其引用 一.不可变对象 不可变对象包括:数字,字符串,元组. 由于Python中的变量存放的是对象引用,所以对于不可变对象而言,尽管对象本身不可变,但变量的对象引用是可变的.

一入python深似海--函数与文件

先上写干货,几个开源网站: github.com launchpad.net gitorious.org sourceforge.net freecode.com 今天介绍一下python函数和文件读写的知识. 函数 def print_two(*args):#That tells Python to take all the arguments to the function and then put them in args as a list arg1,arg2=args print "a

一入python深似海--split

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

一入python深似海--class

python class 分为三个部分:class and object(类与对象),inheritance(继承),overload(重载)and override(覆写). class and object 类的定义,实例化,及成员访问,顺便提一下python中类均继承于一个叫object的类. class Song(object):#definition def __init__(self, lyrics): self.lyrics = lyrics#add attribution def