Python(Dictionary,Function)

Dictionary

键(key),对应值(value)

结构介绍

# -*- coding: utf-8 -*-

#创建一个词典
phone_book = {'Tom': 123, "Jerry": 456, 'Kim': 789}

mixed_dict = {"Tom": 'boy', 11: 23.5} #键和值的类型无限制

#访问词典里的值
print("Tom's number is " + str(phone_book['Tom']))

print('Tom is a ' + mixed_dict['Tom'])


#修改词典
phone_book['Tom'] = 999
#添加元素
phone_book['Heath'] = 888

print("phone_book: " + str(phone_book)) 

phone_book.update({'Ling':159, 'Lili':247})

print("updated phone_book: " + str(phone_book)) 

#删除词典元素以及词典本身
del phone_book['Tom']
print("phone_book after deleting Tom: " + str(phone_book)) 

#清空词典所有内容,但词典还在
phone_book.clear()
print("after clear: " + str(phone_book))


#删除词典
del phone_book #

# print("after del: " + str(phone_book))

#不允许同一个键出现两次
rep_test = {'Name': 'aa', 'age':5, 'Name': 'bb'}
print("rep_test: " + str(rep_test)) 
#rep_test: {'Name': 'bb', 'age': 5}


#键必须不可变,所以可以用书,字符串或者元组充当,列表不行

list_dict = {['Name']: 'John', 'Age':13}
list_dict = {('Name'): 'John', 'Age':13}

六、字典内置函数&方法
Python字典包含了以下内置函数:

1、cmp(dict1, dict2):比较两个字典元素。
2、len(dict):计算字典元素个数,即键的总数。
3、str(dict):输出字典可打印的字符串表示。
4、type(variable):返回输入的变量类型,如果变量是字典就返回字典类型。

Python字典包含了以下内置方法:
1、radiansdict.clear():删除字典内所有元素
2、radiansdict.copy():返回一个字典的浅复制
3、radiansdict.fromkeys():创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
4、radiansdict.get(key, default=None):返回指定键的值,如果值不在字典中返回default值
5、radiansdict.has_key(key):如果键在字典dict里返回true,否则返回false
6、radiansdict.items():以列表返回可遍历的(键, 值) 元组数组
7、radiansdict.keys():以列表返回一个字典所有的键
8、radiansdict.setdefault(key, default=None):和get()类似, 但如果键不已经存在于字典中,将会添加键并将值设为default
9、radiansdict.update(dict2):把字典dict2的键/值对更新到dict里
10、radiansdict.values():以列表返回字典中的所有值


Function 函数1

函数:程序中可重复使用的程序段

给一段程程序起一个名字,用这个名字来执行一段程序,反复使用 (调用函数)

用关键字 ‘def’ 来定义,identifier(参数)

identifier

参数list

return statement

局部变量 vs 全局变量

Code:

#-*- coding: utf-8 -*-

#没有参数和返回的函数
# def say_hi():
#     print(" hi!")
#     
# say_hi()
# say_hi()
#    
#    
#有参数,无返回值
# def print_sum_two(a, b):
#     c = a + b
#     print(c)
#     
# print_sum_two(3, 6)

# def hello_some(str):
#     print("hello " + str + "!")
#         
# hello_some("China")
# hello_some("Python")


#有参数,有返回值
# def repeat_str(str, times):
#     repeated_strs = str * times
#     return repeated_strs
# 
# 
# repeated_strings = repeat_str("Happy Birthday!", 4)
# print(repeated_strings)


#全局变量与局部 变量
# x = 60
# 
# def foo(x):
#     print("x is: " + str(x)) #60
#     x = 3
#     print("change local x to " + str(x)) #3
# 
# foo(x)
# print('x is still', str(x)) #60

x = 60

def foo():
    global x
    print("x is: " + str(x)) #60
    x = 3
    print("change local x to " + str(x)) #3

foo()
print('value of x is' , str(x)) #3

Function 函数2

默认参数

关键字参数

VarArgs参数

Code:

#-*- coding: utf-8 -*-

# 默认参数
def repeat_str(s, times = 1):
    repeated_strs = s * times
    return repeated_strs


repeated_strings = repeat_str("Happy Birthday!")
print(repeated_strings) #只有1次

repeated_strings_2 = repeat_str("Happy Birthday!" , 4)
print(repeated_strings_2) #有4次

#默认参数放后面
#f(a, b =2)合法
#f(a = 2, b)非法

#关键字参数: 调用函数时,选择性的传入部分参数
def func(a, b = 4, c = 8):
    print('a is', a, 'and b is', b, 'and c is', c)

func(13, 17) #a13 b17 c8
func(125, c = 24) #a125 b4 c24
func(c = 40, a = 80) #a80 b4 c40


#VarArgs参数
def print_paras(fpara, *nums, **words):
    print("fpara: " + str(fpara)) #fpara: hello
    print("nums: " + str(nums)) #nums: (1, 3, 5, 7)
    print("words: " + str(words))
    #words: {'wo': 'python', 'anohter_wo': 'java'}


print_paras("hello", 1, 3, 5, 7, word = "python", anohter_word = "java")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值