首页 > 代码库 > Python 复制与赋值

Python 复制与赋值

用‘b=a’复制,对变量a,b的操作都会对a指向的对象起作用。

用‘b=a[]’赋值,则只是赋值。

 

#!/usr/bin/env python# -*- coding: utf-8 -*-# Filename:reference.py__author__ = ‘JerryQiu‘print ‘Simple Assignment‘shoplist = [‘apple‘,‘mango‘,‘carrot‘,‘banana‘]mylist = shoplistdel mylist[0]print ‘shoplist is:‘,shoplistprint ‘mylist is:‘,mylistprint ‘Copy by making a full slice‘mylist= shoplist[:]del shoplist[0]print ‘shoplist is:‘,shoplistprint ‘mylist is:‘,mylist

 

$python reference.pySimple Assignmentshoplist is: [‘mango‘, ‘carrot‘, ‘banana‘]mylist is: [‘mango‘, ‘carrot‘, ‘banana‘]Copy by making a full sliceshoplist is: [‘carrot‘, ‘banana‘]mylist is: [‘mango‘, ‘carrot‘, ‘banana‘]

  

Python 复制与赋值