Python——字典的相關特性及函數的簡單用法

一、字典:

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))

在這裏插入圖片描述

發佈了74 篇原創文章 · 獲贊 6 · 訪問量 5632
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章