Python变量进阶

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33248299/article/details/82788562

一.变量的引用

1.变量引用

在Python中

  • 变量和数据是分开存储的
  • 数据保存在内存中的一个位置
  • 变量中保存着数据在内存中地址
  • 变量中记录数据的地址,就叫做引用
  • 使用id()函数可以查看变量中保存数据所在的内存地址

注意:如果变量已经被定义,当给一个变量赋值的时候,本质上是修改数据引用

  • 变量不再对之前的数据引用
  • 变量改为对新赋值的数据引用
a = 1
print(id(a))
print(id(1))
b = a
print(id(b))

a = 2
print(id(a))
print(id(2))
print(id(b))
print(id(1))
/*
a = 1
print(id(a))
print(id(1))
b = a
print(id(b))

a = 2
print(id(a))
print(id(2))
print(id(b))
print(id(1))
*/

2.函数实参引用

def test(num):
    print("%d代表的地址为%d" % (num,id(a)))

a = 1
print("%d代表的地址为%d" %(a,id(a)))
#调用test函数 本质上传递的是实参保存数据的引用 而不是实参保存的数据
test(a)
test(1)

/*
1代表的地址为140711981077536
1代表的地址为140711981077536
1代表的地址为140711981077536
*/

3.函数返回值传递的引用

def test(num):
    print("%d代表的地址为%d" % (num,id(a)))
    result="hello"
    print("字符串%s的地址为%d" %(result,id(result)))
    return  result

a = 1
print("%d代表的地址为%d" %(a,id(a)))


str = test(a)
print("地址为%d" %(id(str)))
/*
1代表的地址为140711981077536
1代表的地址为140711981077536
字符串hello的地址为799051230992
地址为799051230992
*/

二.可变与不可变类型

1.不可变类型

  • 内存中数据不允许被修改
    • 数字类型:int,bool,float,complex,long(2.x)
    • 字符串(str)
    • 元组(tuple)

2.可变类型

  • 内存中数据可以被修改
    • 列表list
    • 字典dict

列表

a = [1,2,3]

print(id(a))

a.append(4)

print(a,end="")
print(id(a))

a.remove(2)

print(a,end="")
print(id(a))

a.clear()
print(a,end="")
print(id(a))

a = []
print(id(a))

/*
[1, 2, 3, 4]542229160520
[1, 3, 4]542229160520
[]542229160520
542233199432
*/

字典

  • 字典的key只能使用不可变类型的数据
student_info = {"username":"小张"}
student_info["password"]="123"
print(student_info,end="")
print(id(student_info))
student_info.pop("password")
print(student_info,end="")
print(id(student_info))

student_info.clear()
print(student_info,end="")
print(id(student_info))

student_info = {}
print(student_info,end="")
print(id(student_info))

/*
{'username': '小张', 'password': '123'}505698221440
{'username': '小张'}505698221440
{}505698221440
{}505698221944
*/

三.函数参数

  • 无论可变还是不可变
    • 在函数内部对参数赋值不会影响外部实参
  • 对可变来说
    • 在函数内部使用方法对参数修改实参会影响外部实参
def test(num,str):
    num=100
    str=[1,2,3]
    print("内部参数为%d %s" %(num,str))

num=99
str=[4,5,6]
test(num,str)
print("外部参数为%d %s" % (num, str))
/*
内部参数为100 [1, 2, 3]
外部参数为99 [4, 5, 6]
*/
def test(str):

    str.append(7)
    print(str)


str=[4,5,6]
test(str)
print(str)
/*
[4, 5, 6, 7]
[4, 5, 6, 7]
*/

四.多值参数

  • 有时可能需要一个函数能够处理的参数个数是不确定的,这个时候,就可以使用多值参数
  • 在python中有两种多值参数
    • 参数名前增加一个*,可以接收元组
    • 参数名前增加两个*,可以接收字典

    def test(num1,*num2,**num3):
            print(num1)
            print(num2)
            print(num3)


test(1,2,3,4,5,name="小张",age=18)
/*
1
(2, 3, 4, 5)
{'name': '小张', 'age': 18}
*/
  • 元组和字典的拆包
def test(num1,*num2,**num3):
            print(num1,end=" ")
            print(num2,end=" ")
            print(num3)


num1=80
num2=(1,2,3)
num3={"name":"小张","age":18}
test(num1,num2,num3) #这样是不对的
test(num1,*num2,**num3) #要进行拆包
test(80,1,2,3,name="小张",age=18)
/*
80 ((1, 2, 3), {'name': '小张', 'age': 18}) {}
80 (1, 2, 3) {'name': '小张', 'age': 18}
80 (1, 2, 3) {'name': '小张', 'age': 18}
*/

猜你喜欢

转载自blog.csdn.net/qq_33248299/article/details/82788562