zoukankan      html  css  js  c++  java
  • 【python实例】可变数据类型与不可变数据类型

     1 # 1. 判断gl_num和gl_list的值
     2 def demo(num, num_list):
     3     num += num
     4     num_list += num_list
     5 
     6     print(num)  # 18
     7     print(num_list)  # [1, 2, 3, 1, 2, 3]
     8 
     9     print("函数完成")
    10 
    11 
    12 gl_num = 9
    13 gl_list = [1, 2, 3]
    14 demo(gl_num, gl_list)
    15 print(gl_num)  # 9
    16 print(gl_list)  # [1, 2, 3, 1, 2, 3]
     1 # 2.请说出 list1,list2,list3 的值是什么,并且说明为什么
     2 def extendlist(val, lis=[]):
     3     lis.append(val)
     4     return lis
     5 
     6 list1 = extendlist(10)
     7 list2 = extendlist(123, [])
     8 list3 = extendlist('a')
     9 
    10 print(list1)  # [10, 'a']
    11 print(list2)  # [123]
    12 print(list3)  # [10, 'a']
     1 # 3.请说出acts[0](2)的值,并且说明为什么
     2 def makeActions():
     3     acts = []
     4     for i in range(5):
     5         acts.append(lambda x: i ** x)
     6     return acts
     7 
     8 
     9 acts = makeActions()
    10 print(acts[0](2))  # 16
    11 print(acts[1](2))  # 16
    12 print(acts[2](2))  # 16
    13 print(acts[3](2))  # 16
    14 print(acts[4](2))  # 16
    15 
    16 print("~"*50)
    17 
    18 def makeActions():
    19     acts = []
    20     for i in range(5):
    21         acts.append(lambda x, i=i: i ** x)
    22     return acts
    23 
    24 
    25 acts = makeActions()
    26 print(acts[0](2))  # 0
    27 print(acts[1](2))  # 1
    28 print(acts[2](2))  # 4
    29 print(acts[3](2))  # 9
    30 print(acts[4](2))  # 16
  • 相关阅读:
    python2.7学习记录之三
    编程题
    解题的小问题(C++)
    算法入门(C++)
    逻辑回归
    入门级(python)
    python2.7学习记录之二
    sql语句-排序后加入序号再运算判断取想要的项
    linux中c多线程同步方法
    进程间的通讯方式
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12110183.html
Copyright © 2011-2022 走看看