一、字典:
1.字典的定义
#字典是一个无序的数据集合
#通常输出的顺序和定义的顺序不一致
s = {} ##空字典
print(type(s))
users = ['user1','user2']
passwd = ['123','456']
print(zip(users,passwd)) ##输出元组格式
print(list(zip(users,passwd))) ##输出列表格式
print(dict(zip(users,passwd))) ##输出字典格式
s = {
'westos':[190,521,231],
'mysql':[100,99,88]
} ##字典中多行数据的写法
print(s,type(s))
d = dict() ##定义一个空字典
print(d,type(d))
d = dict(a=1,b=2)
print(d,type(d))
#字典的嵌套
students = {
'03113009':{
'name':'yy',
'age':18,
'score':60
},
'03113010':{
'name':'westos',
'age':18,
'score':61
}
}
print(students['03113009']['name']) ##先找到字典中03113009的内容,再从其中找到name的内容
#所有的key的value值相同时,可以用这种方法快速定义字典
print({}.fromkeys({'1','2'},'000000'))
2.字典的特性
#字典的key值是唯一的,所以没有索引等特性
d = {
'1':'a',
'2':'b'
}
print(d['1'])
d = {
'1':'a',
'2':'b'
}
# 成员操作符
print('1' in d)
print('1' not in d)
d = {
'1':'a',
'2':'b'
}
#for循环
for key in d:
print(key)
for key in d:
print(key,d[key]) ##输出key与对应的value值
for k,v in d.items():
print(k,v)
3.字典的增加
services = {
'http':80,
'mysql':3306,
'smtp':25
}
services['ftp'] = 21 ##直接写上key值和value值就可以添加
print(services)
services['http'] = 443 ##添加已经存在的值时会根据key值直接修改对应的value值
print(services)
#添加多个key-value值
services = {
'http':80,
'mysql':3306,
'smtp':25
}
services_backup = {
'https':443,
'tomcat':8080,
'http':8080
}
services.update(services_backup) ##可以直接给一个字典中添加另一个字典的内容
print(services)
services.update(flask=9000,http=8000) ##当数值已经存在时,还是会根据key值修改对应的value值
print(services)
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#setdefault添加key值
#如果key值存在,不做修改
#如果key值不存在,添加对应的key-value
services.setdefault('http',9090)
print(services)
services.setdefault('oracle',44575)
print(services)
4.字典的删除
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#根据key值删除对应的数据
del services['http']
print(services)
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#pop删除指定的key的key-value
#如果key存在,删除,并返回删除key对应value
#如果不存在,报错
item = services.pop('http')
print(item)
print(services)
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#popitem删除最后一个key-value值对,并且可以返回最后一个key-value值对的内容
item = services.popitem()
print(item)
print(services)
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#清空字典内容(变成一个空字典)
services.clear()
print(services)
5.字典的查看
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#查看字典的key值
print(services.keys())
#查看字典的value值
print(services.values())
#查看字典的key-value值
print(services.items())
services = {
'http':80,
'mysql':3306,
'smtp':25
}
#查看key的value值
#key不存在,默认返回None
#key不存在,有default,则返回default值
print(services.get('https'))
#for循化
for k in services:
print(k,services[k])
services = {
'http':80,
'mysql':3306,
'smtp':25
}
# get方法
# 如果key值存在,返回
# 如果不存在,默认返回None,如果需要指定返回值,传值即可
print(services.get('https'))
print(services.get('https','key not exist'))
二、函数:
##函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
##函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。
1.函数的定义和调用
def hello():
print('hello1')
print('hello2')
print('hello3')
#如果不主动调用函数,函数是不会执行的
hello()
def hello(a):
print('hello',a)
#执行同一操作时可以反复调用函数,方便快捷
hello('laoli')
hello('yy')
2.函数的参数
参数:形参 实参
形参:位置参数 默认参数 可变参数 关键字参数
#位置参数
def studentInfo(name,age): ##安装位置传参
print(name,age)
studentInfo('westos',12)
studentInfo(12,'westos')
studentInfo(age=11,name='westos') ##通过位置名称可以不按顺序输入
#默认参数
def mypow(x,y=2):
print(x**y)
mypow(2,3)
mypow(4) ##当只输入一个参数且另一个参数有默认参数时,会自动使用默认参数
#可变参数
# *args 用来将多个参数打包成 tuple (元组)给函数体调用
def mysum(*args):
sum = 0
for item in args:
sum += item
print(sum)
mysum(1,2,3,4,5)
#关键字参数
# **kwargs 打包关键字参数成 dict (字典)给函数体调用
def studentInfo(name,age,**kwargs):
print(name,age)
print(kwargs)
print(studentInfo('westos','18',gender='female',hobbies=['coding','running']))
3.函数的返回值
#返回值:函数运算的结果,还需要进一步操作时,给一个返回值
#return用来返回函数的执行结果,如果没有返回值,默认返回None
#python可以间接返回多个值(返回一个元组)
#一旦遇到return,函数执行结束,后面代码不会执行
def mypow(x,y=2):
return x ** y,x + y
print('hello')
print(mypow(3))
a,b = mypow(3)
print(a,b)
4.变量的作用域
"""
局部变量:在函数内部定义的变量,只在函数内部起作用,函数执行结束,变量会自动删除
全局变量:在整个程序中都有效
"""
a = 1
print('out: ',id(a)) ##查看在内存中的地址
def fun():
global a ##global会把这个函数中的a值变成全局变量,如果这行生效就会输出5
a = 5
print('in: ',id(a))
fun()
print(a)
print(id(a))