forloop:
基本:
C#中的foreach,Java中的增强forloop在python中的表示
example_list = [1,2,3,4,5,6,7,12,543,876,12,3,2,5] for i in example_list: print(i)
进阶:
python内置了工厂函数,range函数将返回一个序列,三种使用方法。
1. 返回数字1-9,但是不包括10
for i in range(1,10) print(i)
2.range(stop)
如果省略了start,那么将从0开始,相当于range(0,stop)
3.range(start,stop,step)
for i in range(0,10,3) print(i)
高级:
python共内置了list,tuple,dict,set四种集合,每个集合对象都能够迭代。
1.tuple类型
tuple=('python',3.5,64) for i in tuple: print(i) #程序将按行输出:python,3.5,64
2.dictionary类型
dic={} dic['lan']='python' dic['version']=3.5 dic['platform']=64 for key in dic: print(key,dic[key])
输出的结果为:platform 64,lan python, version 2.7
, 字典在迭代的过程 中将 key
作为可迭代的对象返回。注意字典中 key
是乱序的,也就是说和插入 的顺序是不一致的。如果想要使用顺序一致的字典,请使用 collections
模块 中的 OrderedDict
对象。
3.set类型
s = set(['python', 'python2', 'python3','python']) for item in s: print(item)
将会输出 python, python3, python2
set 集合将会去除重复项,注意输出的 结果也不是按照输入的顺序。
#多个迭代器
for a,b in [(1,2),(3,4)]
字符串的格式化:
'%.2f' % 3.14
返回当前工作路径:
cwd=os.getcwd()
python的数据结构:List、Tuple、Dictionaray、Set
List、Tuple(String)可以看成是序列模型,可以进行切片操作。List可以看成是数组,但是List的功能比Java中的数组要强大一些。
Tuple的特征和String很像,它们是不可变的你不能随意更改和编辑它们。Tuple同常用于保证某一句和某用户的函数能够采用一组安全的数值。
当集合中的项目的存在与否比起它们出现的次数和顺序更加重要的时候,我们就会采用集合。
列表List
# This is my shopping list shoplist = ['apple', 'mango', 'carrot', 'banana'] print('I have', len(shoplist), 'items to purchase.') print('These items are:', end=' ') for item in shoplist: print(item, end=' ') print('\nI also have to buy rice.') shoplist.append('rice') print('My shopping list is now', shoplist) print('I will sort my list now') shoplist.sort() print('Sorted shopping list is', shoplist) print('The first item I will buy is', shoplist[0]) olditem = shoplist[0] del shoplist[0] print('I bought the', olditem) print('My shopping list is now', shoplist)
元组Tuple
# 我会推荐你总是使用括号 # 来指明元组的开始与结束 # 尽管括号是一个可选选项。 # 明了胜过晦涩,显式优于隐式。 zoo = ('python', 'elephant', 'penguin') print('Number of animals in the zoo is', len(zoo)) new_zoo = 'monkey', 'camel', zoo print('Number of cages in the new zoo is', len(new_zoo)) print('All animals in new zoo are', new_zoo) print('Animals brought from old zoo are', new_zoo[2]) print('Last animal brought from old zoo is', new_zoo[2][2]) print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2]))
字典dict
# “ab”是地址(Address)簿(Book)的缩写 ab = { 'Swaroop': 'swaroop@swaroopch.com', 'Larry': 'larry@wall.org', 'Matsumoto': 'matz@ruby-lang.org', 'Spammer': 'spammer@hotmail.com' } print("Swaroop's address is", ab['Swaroop']) # 删除一对键值—值配对 del ab['Spammer'] print('\nThere are {} contacts in the address-book\n'.format(len(ab))) for name, address in ab.items(): print('Contact {} at {}'.format(name, address)) # 添加一对键值—值配对 ab['Guido'] = 'guido@python.org' if 'Guido' in ab: print("\nGuido's address is", ab['Guido'])
集合Set
>>> bri = set(['brazil', 'russia', 'india']) >>> 'india' in bri True >>> 'usa' in bri False >>> bric = bri.copy() >>> bric.add('china') >>> bric.issuperset(bri) True >>> bri.remove('russia') >>> bri & bric # OR bri.intersection(bric) {'brazil', 'india'}
python的标准库>解析xml
参考文献:http://blog.csdn.net/gingerredjade/article/details/21944675
python的IO和序列化有必要说以下
待续……