重拾python 三十二

来源:互联网 发布:安庆市民心声网络问政 编辑:程序博客网 时间:2024/04/29 11:54

开始学习循环语句,关键字for;学习一个基本的数据结构列表(list),用[]表示,上代码ex32.py:

#!/usr/bin/python# -*- coding: utf-8 -*-numbers = [1, 22, 333, 4444, 55555]fruits = ['apples', 'oranges', 'pears', 'apricots']unknown = [5, 'tom', [2,'funny'], 'jerry', 3, 'wang']for number in numbers:    print "This is number %d" % numberfor fruit in fruits:    print "A fruit of type: %s" % fruitfor i in unknown:    print "I got %r" % ielements = []for i in range(0, 6):    print "Adding %d to the list." % i    elements.append(i)for i in elements:    print "Element was: %d" % i

运行,看到:

This is number 1
This is number 22
This is number 333
This is number 4444
This is number 55555
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 5
I got ‘tom’
I got [2, ‘funny’]
I got ‘jerry’
I got 3
I got ‘wang’
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

可以看到,列表的元素可以是列表哦

0 0
原创粉丝点击