1.迭代器
迭代器的意思是重复做一些事很多次,在目前中只是在for循环中和对序列和字典进行迭代,但实际也能对其他的对象进行迭代,
_iter_ 方法返回一个迭代器,所谓的迭代器就是具有next方法的对象,在调用next方法是,迭代器会返回他的下一个值,如果next方法被调用,但迭代器没有值可以返回,就会报错。
In [32]: li = (1,2,34,5) //元组
In [33]: a = iter(li)
In [34]: a.next()
Out[34]: 1
In [39]: li = ['l','k',8,9,3] //列表
In [40]: b = iter(li)
In [41]: b.next()
Out[41]: 'l'
//集和
2.字典
In [18]: dic = {"name":"fentiao","age":5,"gender":"male"}
In [19]: type(dic)
Out[19]: dict
In [20]: s = {} //集合中没有元素也是字典类型
In [21]: type(s)
Out[21]: dict
In [22]: dic.keys() //列出属性
Out[22]: ['gender', 'age', 'name']
n [24]: dic.values() //列出属性值
Out[24]: ['male', 5, 'fentiao']
dic.get("name") //显示某个属性的值
In [25]: dic.items() //按元组方式列出
Out[25]: [('gender', 'male'), ('age', 5), ('name', 'fentiao')]
In [31]: for k,v in dic.items():
....: print "k=%s,v=%s" %(k,v)
....:
k=gender,v=male
k=age,v=5
k=name,v=fentiao
dic.get("name")
'fentiao'
dic.viewkeys() //显示字典里的keys
注意:元组可以同时多个赋值
In [26]: a,b = (1,2)
In [27]: a
Out[27]: 1
In [28]: b
Out[28]: 2
字典删除;
In [65]: dic = {"name":"fentiao","age":5,"gender":"male"}
In [69]: del dic["name"] //key值用方括号
In [70]: dic
Out[70]: {'age': 5, 'gender': 'male'}
In [71]: dic.pop("age") //弹出并删除
Out[71]: 5
In [72]: dic
Out[72]: {'gender': 'male'}
In [75]: dic.clear() //删除字典里的全部
In [76]: dic
Out[76]: {}
字典迭代:
In [52]: dic
Out[52]: {'age': 5, 'gender': 'male', 'name': 'fentiao'}
In [54]: q = iter(dic)
In [55]: q.next()
Out[55]: 'gender'
In [56]: q.next()
Out[56]: 'age'
dic.has_key("name")
“name” in dic.keys() //两种方法查看是否含有name属性,有则显示true,无则显示false
复制:
dic1 = dic.copy
id(dic1) //复制地址会变
dic2 = dic //赋值地址簿不变
In [91]: a
Out[91]: {'age': 6, 'name': 'fentiao'}
In [92]: id(a)
Out[92]: 49775200
In [93]: b = a.copy()
In [94]: b
Out[94]: {'age': 6, 'name': 'fentiao'}
In [95]: id(b)
Out[95]: 49267072
In [96]: c = a
In [97]: c
Out[97]: {'age': 6, 'name': 'fentiao'}
In [98]: id(c)
Out[98]: 49775200
dic.fromkeys(["a","b","c"])
{'a':none,'b',none,'c',none}
dic..fromkeys(["a","b","c"],"hello")
{'a':hello.'b':"hello",'c':"hello"} //给字典的多个key赋值
更新和修改:update会修改原有元组的值,原本没有的会添加,而set—default原有的不会修改,没有的也会添加
注意:字典格式不能通过索引来提取value值
dic[0]
应该是dic.get("name"),会输出对应的value值
练习;1,使用字典的方法将元组中的重复值删除掉(之前可以使用集和):
#!/usr/bin/python
li = [1,2,2,1,3]
d = {}.fromkeys(li)
li = d.keys()
print li
2,用字典实现四则运算
#!/usr/bin/evn python
a = int(raw_input("please input a number:"))
b = int(raw_input("please input a number:"))
x = raw_input("please input a operator:")
compute = {"+":a+b,"-":a-b,"*":a*b,"/":a/b}
compute.get(x)
print compute.get(x)
#!/usr/bin/env python
#coding:utf-8
a = float(raw_input("please input a num:"))
c = raw_input("please input a sysmbol(+-*/):")
b = float(raw_input("please reinput a num:"))
dic = {"+":a+b,"-":a-b,"*":a*b,"/":a/b}
print dic.get(c)
print "%s%s%s=%s"%(a,c,b,dic.get(c)
3.输出数的绝对值
#!/usr/bin/env python
a = input("please input number:")
if not isinstance(a,int): //如果不是int类型!!!!!
print "error type"
if a>=0:
print a
else:
print -a
下午:
1.vim break.py
#!/usr/bin/env python
#coding:utf-8
while 1:
a = raw_input("q or c:")
if a== "q":
break
elif a=="c":
continue
elif a=="n":
pass
else:
print "error string"
2.练习;
要求输入一个姓名列表,输入格式是“last name,first name”,即姓,逗号,名。编写程序处理输入,如果用户输入错误,比如“法irst name ,last name”,修改错误并通知用户
定义函数
局部变量和全剧变量
位置参数和关键字参数默认参数
#!/usr/bin/env python
#coding:utf-8
a = 1 //全局变量
def myadd(x,y):
if isinstance (x,(int,float)) and isinstance(y,(int,float)):
print x + y
else:
print "error type"
def func1():
# a = 2 //局部变量
global b
b = 3
print a
func1()
print b
def getInfo(name,age,gender,kind):
print name,age,gender,kind
myadd(3,4) //初始化参数和默认参数
getInfo("fentiao",5,"male","cat") //关键字参数和位置参数,所传参数乱序后,乱序之后的参数都需要加上关键字指定参数
~
~