2021-02-23 COMP9021 Quize中遇到的问题

文章目录

Quize 1

  1. python中的赋值问题
    在python中将一个变量赋值给另一个变量时,并不是创建了一个新的变量,而是创建了一个引用,对该引用进行修改时,原变量内容将同时被改变。
    在这里插入图片描述
    如果不希望原变量被改变,应该使用List.copy()函数,检验程序如下:
    L = [5, 7, 3, 9]
    print("The original list of L: ", L);
    print("-"*50)
    # ------------------------------------------------- copy of L
    L_copy = L.copy(); L_copy[0] = 0
    
    print("After L_copy = L.copy(), and let L_copy[0] = 0")
    print(' '*5, f"L: {L}")
    print(' '*5, f"L_reference: {L_reference}")
    
    print("-"*50)
    # ------------------------------------------------ reference of L
    L_reference = L; L_reference[0] = 0
    
    print("After L_reference = L, and let L_reference[0] = 0")
    print(' '*5, f"L: {L}")
    print(' '*5, f"L_reference: {L_reference}")
    
    print("-"*50)
    # ------------------------------------------------ therefor
    if L is L_reference:    print("L_reference is the reference of L")
    else:                   print("L_reference is not the reference of L")
    
    if L is L_copy:    print("L_copy is the reference of L")
    else:              print("L_copy is not the reference of L")
    
    输出结果:
    在这里插入图片描述
    问题来源: kaini.busy同学
    参考:https://my.oschina.net/u/4505693/blog/3232657
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值