d = {}
d = {“key1”:”value1”}
d = {“key1”:”value1”,”key2”:”value3”}
d = {
"172.25.254.1":
{
"user":"root",
"password":"westos",
"command":"hostname"
},
"172.25.254.2":
{
"user":"westos",
"password":"westos1",
"command":"hostname"
}
} # 支持异构,嵌套
dict(zip(user,passwd))
“”.fromkeys([“a”,”b”],”1000”)
d.update()
d.setdefault(key[,value])
- 如果key存在,不更改;如果key不存在,添加key-value到字典中;
- 如果key值有,并不存在d字典中,默认value值为None;
d.pop(key) # 根据某个key值删除字典中的key-value值;
d.popitem() # 随机删除key-value值
d.clear() # 清空字典的所有内容,字典还存在
del(d[“key”]) # 删除指定的key值
del(d) # 删除字典d,d直接不存在
d[“key2”]=”value3”
d.get(“key”) # 如果key存在,返回value值;如果不存在,返回None;
d[“key”] # 如果key存在,返回value值;如果不存在,直接报错;
d.has_keys(“key”) # 如果key存在,返回True;如果不存在,返回False;
d.items()
d.keys()
d.values()
注意:如何遍历字典的所有key-value值:
for key,value in d.items():
print key, value
d.copy()
In [105]: d1 = d.copy()
In [106]: id(d)
Out[106]: 20314528
In [107]: id(d1)
Out[107]: 20338192
In [108]: d2 = d
In [109]: id(d2)
Out[109]: 20314528
** 不可变的数据类型一定可哈希;hash()内置函数可以判断某个类型是否可哈希;
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {1, 2}
s4 = {5}
print "s1,s2,s3的交集:", s1.intersection(s2, s3)
print "s1,s2,s3的交集:", s1 & s2 & s3
print "s1,s2,s3的并集:", s1.union(s2, s3)
print "s1,s2,s3的并集:", s1 | s2 | s3
print "s1-s2的差集:", s1.difference(s2)
print "s1-s2的差集:", s1 - s2
print "s1-s2-s3的差集:", s1.difference(s2, s3)
print "s1-s2-s3的差集:", s1 - s2 - s3
print "s2-s1的差集:", s2.difference(s1)
print "s1和s2的对等差分:", s1.symmetric_difference(s2)
print "s1和s2的对等差分:", s1 ^ s2
print "s2和s1的对等差分:", s2.symmetric_difference(s1)
# s.isdisjoint(s2) 如果有交集,返回False;否则,返回True;
print "s1和s4是否没有交集:", s1.isdisjoint(s4)
print "s3是否为s1的子集:", s3.issubset(s1)
print "s1是否为s3的父集:", s1.issuperset(s3)
min,max,sum,zip,abs,
int,float,str,list,tuple,bool,
In [56]: bool(1)
Out[56]: True
In [57]: bool("")
Out[57]: False
函数实质上是一个变量;函数名是一个指向函数对象的引用,完全可以把函数赋值给一个变量,相当于给这个函数起了个别名;
In [59]: a = abs # a变量指向abs函数
In [60]: a(-1) # 可以通过a实现abs的功能;
Out[60]: 1
In [61]: abs(-1)
Out[61]: 1
def 函数名([形式参数]):
函数体 # 函数要执行的程序
return 返回值 # 如果没有return返回,默认返回值为None;
函数名([实参])
注意:有返回值 的函数,必须print fun(),可以打印出返回值;
def maxSum(num1,num2,num3):
print num2
return max(num1,num2,num3),sum([num1,num2,num3])
num1 += 2
print num1
print type(maxSum(1,4,2))
print maxSum(1,4,2)
args接收的是一个元组(tuple);
如果四种函数参数同时存在,参数定义的顺序必须是: 必选参数,默认参数,可变参数,关键字参数.
解包:
- 把一个序列元素依次传入带有可变参数的函数时,可通过fun(*l)来解包;
- 把一个字典元素依次传入带有关键字参数的函数时,可通过fun(**d)来解包;
global name
name = "westos"
题目1:用户管理系统V2:
用户功能如下:
welcome to xxxx system
#!/usr/bin/env python
#coding:utf-8
d = {"name":"passwd"}
def register():
user = raw_input("User:")
if d.has_key(user):
print"%s 已经存在" %(user)
else:
passwd = raw_input("Passwd:")
d[user] = passwd
print "%s 注册成功!"
def info():
print "user\tpasswd"
for user,passwd in d.items():
print "%s\t%s\n" %(user,passwd)
def login():
user = raw_input("login username:")
passwd = raw_input("login passwd:")
if d.has_key(user)and d[user] == passwd:
print"登陆成功"
else:
print"%s不存在" %(user)
def delete():
while 1:
user = raw_input("which user?:")
if d.has_key(user):
del(d[user])
else:
print"%s不存在" %(user)
if raw_input("continue?(y/n)")!="y":
break
while 1:
pro="""
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:"""
choose = raw_input(pro)
if choose == "l":
login()
elif choose == "r":
register()
elif choose == "d":
delete()
elif choose == "i":
info()
elif choose == "e":
exit(1)
else:
print"error input !please reinput...\n"
print "\n\n"
测试
[root@foundation60 daima]# ./login.py
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:l
login username:root
login passwd:westos
root不存在
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:r
User:root
Passwd:westos
%s 注册成功!
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:l
login username:root
login passwd:westos
登陆成功
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:i
user passwd
root westos
name passwd
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:d
which user?:root
continue?(y/n)y
which user?:root
root不存在
continue?(y/n)n
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:i
user passwd
name passwd
welcome to user login system:
登陆(l)ogin:
注册(r)egister:
注销(d)elete:
显示(i)nfo:
退出(e)xit:
choose:e