我是靠谱客的博主 无情康乃馨,这篇文章主要介绍python 函数与字典,现在分享给大家,希望可以做个参考。

python 函数与字典

# -*- coding: utf-8 -*-
"""
目标:函数实参与任意数量自定义的字典
Created on Sat Nov 16 22:40:42 2019
@author: huzhenxing
"""
def car_profile(brands, car_type, **car_info):
    ''' 创建一个汽车简介的字典 可以添加自定义字典'''
    #创建空的汽车字典
    car_dct = {}
    #添加汽车品牌
    car_dct['brands'] = brands
    #添加汽车型号
    car_dct['car_type'] = car_type
    #循环遍历自定义字典
    for key, value in car_info.items():
        car_dct[key] = value
    #返回汽车字典
    return car_dct

#创建一个奔驰品牌的字典
car_chery = car_profile(
        'Benz', 'GLA', color = 'black', 
        automatic = True, level = 'SUV' )
print(car_chery)

#创建一个奇瑞品牌的字典
car_chery = car_profile('Chery', 'ARRIZO', color = 'white', automatic = True)
print(car_chery)

输出结果:
在这里插入图片描述

最后

以上就是无情康乃馨最近收集整理的关于python 函数与字典的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部