python基础Ⅱ(字符串操作、列表操作、元组操作、字典操作、集合操作、共用方法、可变与不可变类型)

一、序列

所谓序列,指的是一块可存放多个值的连续内存空间,这些值按一定顺序排列,可通过每个值所在位置的编号(称为索引)访问它们

在 Python 中,序列类型包括字符串、列表、元组,他们支持索引、切片。第一个索引若为0,则指向左端;第一个索引若为负数,则指向右端

切片指截取序列中的其中一段内容。使用语法:[起始下标:结束下标:步长],截取的内容不包括结束下标对应的数据,步长是指隔几个下标截取一个项(默认为1)

二、字符串操作

字符串格式化操作

str_a = '{} {}'.format(111, 222)
str_b = '%d %d' % (111, 222)

print(str_a)
print(str_b)
# 输出结果
111 222
111 222

capitalize()

字符串第一个字母变成大写,其他字母变小写

str.capitalize()

test1 = 'i lovE python'

print(test1.capitalize())		# 输出 I love python

title()

返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写。还有istitle()函数可以检测字符串里面的每个单词的首字母是否为大写

str.title()

str1 = "this is string example....wow!!!"

print(str1.title())

lower()/upper()

转换字符串中所有大(小)写字符为小写(大)。

str.lower()
str.upper()

test1 = 'i LOVE python'
test2 = 'i love python6*'

print(test1.lower())		# i love python
print(test1.upper())		# I LOVE PYTHON
print(test2.lower())		# i love python6*
print(test2.upper())		# I LOVE PYTHON6*

swapcase()

对字符串的大小写字母进行转换。

str.swapcase();

test1 = 'i LOVE python'
test2 = 'i love python6*'

print(test1.swapcase())		# I love PYTHON
print(test1.swapcase())		# I love PYTHON
print(test2.swapcase())		# I LOVE PYTHON6*
print(test2.swapcase())		# I LOVE PYTHON6*


endswith()

用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

str.endswith(suffix[, start[, end]])

  • suffix – 该参数可以是一个字符串或者是一个元素。
  • start – 字符串中的开始位置,从0开始。
  • end – 字符中结束位置,最大为字符串长度。
test1 = 'i love python'				# 13个字符

print(test1.startswith('i', 0, 13))			# True,start从0开始
print(test1.startswith('i', 1, 13))			# False
print(test1.endswith('on', 0, 13))			# True,end为字符串长度,但不包括最后一个
print(test1.endswith('on', 0, 12))			# False

startswith()

用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

str.endswith(suffix[, start[, end]])

  • suffix – 该参数可以是一个字符串或者是一个元素。
  • start – 字符串中的开始位置,从0开始。
  • end – 字符中结束位置,默认为字符串的长度。
test1 = 'i love python'				# 13个字符

print(test1.startswith('i', 0, 13))			# True,start从0开始
print(test1.startswith('i', 1, 13))			# False
print(test1.endswith('on', 0, 13))			# True,end为字符串长度,但不包括最后一个
print(test1.endswith('on', 0, 12))			# False

lstrip()/rstrip()/strip()

截掉字符串左边(右边或头尾)的空格或指定字符。
注意:strip方法只能删除开头或是结尾的字符,不是删除中间部分的字符。

str.lstrip([chars]),默认截取的是空格
str.rstrip([chars]),默认截取的是空格
str.strip([chars]),默认截取的是空格

  • chars --指定截取的字符。
test1 = '  i LOVE python  '
test2 = '88888i love python88888'

print(test1.lstrip())			# i LOVE python  ,后面有三个空格
print(test1.rstrip())			#   i LOVE python,前面有三个空格
print(test1.strip())			# i LOVE python
print(test2.lstrip('88888'))			# i love python88888
print(test2.rstrip('88888'))			# 88888i love python
print(test2.strip('88888'))			# i love python

msg = '   22   '
print(msg.rstrip())				# 默认截取的是空格
print(msg.lstrip())
print(msg.strip())

find()

是从字符串左边开始查询子字符串匹配的第一个索引 ,如果指定 start(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回第一个索引值,否则返回-1。
rfind()从字符串右边开始查询字符串匹配到的第一个索引,不过没有lfind()

str.find(str, beg, end)

  • str – 指定检索的字符串
  • start – 开始索引,默认为0。
  • end – 结束索引,默认为字符串的长度。
test1 = 'i love python'				# 13个字符

print(test1.find('i', 0, 13))		# 0
print(test1.find('i', 1, 13))		# -1
print(test1.find('on', 0, 13))		# 11,为查询到的字符串的首个索引
print(test1.find('on', 0, 12))		# -1

index()

用于从字符串中找出某个值第一个匹配项的索引位置,它与find()一样,只不过如果str找到就会爆出异常
rindex()从字符串右边开始查询字符串匹配到的第一个索引,不过 没有lindex()

str.index(x[, start[, end]])

  • x-- 查找的对象。
  • start-- 可选,查找的起始位置。
  • end-- 可选,查找的结束位置。
str1 = 'hello world'

print(str1.index('e'))			# 1
print(str1.index('e', 1, 4))	# 1
print(str1.index('o', 1, 5))	# 4
print(str1.index('o', 1, 4))	# 报出异常

replace()

把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

str.replace(old, new[, max])

  • old – 将被替换的子字符串。
  • new – 新字符串,用于替换old子字符串。
  • max – 可选字符串, 替换不超过 max 次
str1 = "this is string example....wow!!! this is really string"

print(str1.replace("is", "was"))		# thwas was string example....wow!!! thwas was really string
print(str1.replace("is", "was", 3))		# thwas was string example....wow!!! thwas is really string


isalnum()

检测字符串(不能有空格)是否由字母或数字组成。

str.isalnum()

test1 = 'i love python666'
test2 = 'python666'
test3 = 'python'
test4 = '666'
test5 = '---'

print(test1.isalnum())		# False,有空格
print(test2.isalnum())		# True
print(test3.isalnum())		# True
print(test4.isalnum())		# True
print(test5.isalnum())		# False

isalpha()

检测字符串是否只由字母组成。

str.isalpha()

test1 = 'i love python666'
test2 = 'python666'
test3 = 'python'
test4 = '666'
test5 = '---'

print(test1.isalpha())		# False
print(test2.isalpha())		# False
print(test3.isalpha())		# True
print(test4.isalpha())		# False
print(test5.isalpha())		# False

isdigit()

检测字符串是否只由数字组成。

str.isdigit()

test1 = 'i love python666'
test2 = 'python666'
test3 = 'python'
test4 = '666'
test5 = '---'

print(test1.isdigit())		# False
print(test2.isdigit())		# False
print(test3.isdigit())		# False
print(test4.isdigit())		# True
print(test5.isdigit())		# False

islower()

检测字符串是否由小写字母组成。

str.islower()

test1 = 'i love python6'
test2 = 'i love python-'
test3 = 'i love python'
test4 = 'I LOVE PYTHON'
test5 = 'i LOVE python'

print(test1.islower())		# True
print(test2.islower())		# True
print(test3.islower())		# True
print(test4.islower())		# False
print(test5.islower())		# False


encode()与decode()

encode函数以 encoding指定的编码格式编码字符串而decode函数则是与encode函数相反。errors参数可以指定不同的错误处理方案。encode函数返回编码后的bytes类型对象,decode函数则是返回解码后的字符串。

str.encode([encoding][,errors])
bytes.decode([encoding][,errors])

  • encoding – 要使用的编码,默认为"UTF-8"。
  • errors – 设置不同错误的处理方案。默认为 ‘strict’。其他可能得值有 ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error() 注册的任何值。
msg = '你好'

res = msg.encode()				# 默认为utf-8
res2 = msg.encode('utf-8')
print(type(res2))				# <class 'bytes'>
print(res == res2)				# True
print(res.decode())				# # 默认为utf-8

join()

用于将序列中的元素以指定的字符连接生成一个新的字符串。

str.join(sequence)

  • equence – 要连接的元素序列。
str1 = '-'

seq = ("a", "b", "c")
print(str1.join(seq))		# a-b-c

split()

通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔成 num+1 个子字符串

str.split(str, num)

  • str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • num – 分割次数。默认为 -1, 即分隔所有。
str1 = "Line1-abcdef \nLine2-abc \nLine4-abcd"
str2 = "Line1-abcdef\nLine2-abc\nLine4-abcd"
str3 = "Line1-abcdef\tLine2-abc\tLine4-abcd"
str4 = "Line1-abcdef Line2-abc Line4-abcd"

print(str1.split())		# ['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
print(str2.split())		#['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
print(str3.split())		#['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
print(str4.split())		#['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
print(str1.split(' ', 1))		#['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

count()

用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

str.count(sub, start,end)

  • sub – 搜索的子字符串
  • start – 字符串开始搜索的位置。默认第一个字符索引值为0。
  • end – 字符串中结束搜索的位置。默认为字符串的长度。
test1 = '88888i lov888e pyth8on88888'		# 27个字符,14个8

print(test1.count('8', 0, 27))			# 14
print(test1.count('8', 1, 27))			# 13
print(test1.count('8', 0, 26))			# 13
print(test1.count('8', 1, 26))			# 12

切片

str1 = 'hello world'

print(str1[1:7])		# ello w
print(str1[1::2])		# el ol
print(str1[::-1])		# 倒序,dlrow olleh

三、列表

列表内数据可以增删查改;当列表内数据改变时,其内存地址不会改变;列表内的数据可以是任何的数据类型;并且它也支持索引和切片

切片

li = ['abcd', 123, True, 12.3]

print(li)				# ['abcd', 123, True, 12.3]
print(li[0])			# abcd
print(li[-1])			# 12.3
print(li[1:2])			# [123],切片
print(li * 2)			# ['abcd', 123, True, 12.3, 'abcd', 123, True, 12.3],列表的复制

append()

用于在列表末尾添加新的对象。

list.append(obj)

li = ['abcd', 123, True, 12.3]

print(li)			# ['abcd', 123, True, 12.3]
li.append([1, 2])
print(li)			# ['abcd', 123, True, 12.3, [1, 2]]

insert()

用于将指定对象插入列表的指定位置。

list.insert(index, obj)

  • index – 对象 obj 需要插入的索引位置。
  • obj – 要插入列表中的对象。
aList = [123, 'xyz', 'zara', 'abc', 123]
aList.insert(1, 123)
print(aList)			# [123, 123, 'xyz', 'zara', 'abc', 123]

extend()

用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。

list.extend(seq)

  • seq – 元素列表。
aList = [123, 'xyz', 'zara', 'abc', 123]
bList = [2009, 'manni']

aList.extend(bList)
print(aList)			# [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']


count()

用于统计某个元素在列表中出现的次数。

list.count(obj)

aList = [123, 'xyz', 'zara', 'abc', 123]

print(aList.count(123))			# 2
print(aList.count('zara'))		# 1

index()

用于从列表中找出某个值第一个匹配项的索引位置,当找不到的时候会报错。

list.index(x[, start[, end]])

  • x-- 查找的对象。
  • start-- 可选,查找的起始位置。
  • end-- 可选,查找的结束位置。
aList = [123, 'xyz', 'zara', 'abc', 123]

print(aList.index(123))			# 0
print(aList.index(777))			# 当找不到的时候会报错

注意列表没有rindex方法,只要字符串才有‘r’方法


pop()

用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

list.pop([index])

  • index-- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
aList = [123, 'xyz', 'zara', 'abc', 123]

aList.pop()
print(aList)			# [123, 'xyz', 'zara', 'abc']
aList.pop(0)
print(aList)			# ['xyz', 'zara', 'abc']

remove()

用于移除列表中某个值的第一个匹配项,没有找到的话就报出错误。

list.remove(obj)

aList = [123, 'xyz', 'zara', 'abc', 'xyz']

aList.remove('xyz')
print(aList)			# [123, 'zara', 'abc', 'xyz']

clear()

用于删除列表中所有值。

list.clear()

li = ['abcd', 123, True, 12.3]

li.clear()				# []

del

删除列表内的某项

listA = list(range(10, 20))

print(listA)				# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
del listA[0]	
print(listA)				# [11, 12, 13, 14, 15, 16, 17, 18, 19]
del listA[1:4]
print(listA)				# [11, 15, 16, 17, 18, 19]


sort()

用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

list.sort( key=None, reverse=False)

  • key – 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse – 排序规则,reverse = True 降序, reverse = False 升序(默认)。
aList = ['Google', 'Runoob', 'Taobao', 'Facebook']

aList.sort()
print("List : ", aList)				# List :  ['Facebook', 'Google', 'Runoob', 'Taobao']

reverse()

用于反向列表中元素。

list.reverse()

aList = [123, 'xyz', 'zara', 'abc', 'xyz']

aList.reverse()
print(aList)			# ['xyz', 'abc', 'zara', 'xyz', 123]

四、元组

元组于列表类似,不同之处在于元组的一级元素不能增、删、改;它也可以进行索引和切片;元组内可以是任何类型;当元组中只有一个元素时,要加上逗号,不然解释器会当做整形来处理

tupleA = ('abcd', 89, 12.3, 'peter', [11, 22], 'peter')

print(tupleA.count('peter'))		# 2,用来计算'peter'在元组内出现的次数

print(tupleA[0])			# abcd
print(tupleA[1:3])			# (89, 12.3)
print(tupleA[-1:-3:-1])		# ('peter', [11, 22])
print(tupleA[-2:-1:])		# ([11, 22],),注意当元组内只有一个元素时有逗号的

tupleA[4][0] = 22
print(tupleA)				# ('abcd', 89, 12.3, 'peter', [22, 22], 'peter'),不可以修改元组的值,但可以修改元组值得值

tupleB = (1)
print(type(tupleB))			# <class 'int'>

tupleC = (1,)
print(type(tupleC))			# <class 'tuple'>

tupleA[0] = 1				# 元组不能修改,在这里会爆出error
print(tupleA)

count()

用于统计某个元素在元组中出现的次数。

list.count(obj)

name = (0, 1, 2, 3, 4, 5, 0)

print(name.count(1))		# 1
print(name.count(7))		# 0

index()

用于从元组中找出某个值第一个匹配项的索引位置,当找不到的时候会报错。

list.index(x[, start[, end]])

  • x-- 查找的对象。
  • start-- 可选,查找的起始位置。
  • end-- 可选,查找的结束位置。
name = (0, 1, 2, 3, 4, 5, 0)

print(name.index(0))				# 0
print(name.index(0, 1))				# 6
print(name.index(0, 1, 6))			# 当找不到的时候会报错

拆包和装包

装包就是把未命名的参数放到元组中,把命名参数放到字典中,拆包将一个结构中的数据拆分为多个单独变量
参考案例

五、字典

字典是以键值对的形式存在的 ,字典不可使用索引和切片,它支持添加、修改、删除

dictA = dict([('name', 'ming'), ('age', 20)])		# 列表
dictB = dict((('name', 'ming'), ('age', 21)))		# 元组
print(dictA)					# {'name': 'ming', 'age': 20}
print(dictB)					# {'name': 'ming', 'age': 21}

dictA = {'name': '李华'}
dictA['age'] = 30				# 添加值

print(len(dictA))				# 获得字典长度
print(dictA['age'])

dictA['age'] = 40				# 修改值
print(dictA['age'])

print(dictA.keys())
print(dictA.values())
print(dictA.items())

dictA.update({'name': '李明'})
print(dictA)

for (key, value) in dictA.items():
    print('{}为{}'.format(key, value))
    pass

keys()

获得字典的所有键

dictA = {'name': '李华', 'age': 40}

print(dictA.keys())			# 结果为dict_keys(['name', 'age'])
for value in dictA.keys():
    print(value)
    pass

values()

获得字典的所有键值

dictA = {'name': '李华', 'age': 40}

print(dictA.values())			# 结果为dict_values(['李华', 40])
for value in dictA.values():
    print(value)
    pass

items()

获得字典的所有键对,结果里面的item为tuple类型

dictA = {'name': '李华', 'age': 40}

print(dictA.items())			# 结果为dict_items([('name', '李华'), ('age', 40)])
for (key, value) in dictA.items():	# key为键,value为键值
    print('{}为{}'.format(key, value))
    pass

update()

若更新的内容在字典内有则更改,若没有则添加

dictA = {'name': '李华', 'age': 40}

dictA.update({'name': '李明'})		# 修改
dictA.update({'class': 1})			# 添加

print(dictA)

del、pop()与clear()

删除操作pop() 方法删除字典给定键 key及对应的值,返回值为被删除的值,若没有找值到则返回 default 值,字典没有remove方法

pop(key[,default])

dictA = {'name': '李华', 'age': 40}

del dictA['name']			# 删除
dictA.pop('age')
print(dictA)

dictA.clear()				# 清空字典
print(dictA)				# {}

get()

函数返回指定键的值

dict.get(key, default=None)

  • key – 字典中要查找的键。
  • default – 如果指定键的值不存在时,返回该默认值。
dictA = {'name': '李华', 'age': 40}

print(dictA.get('gender', 'male'))			# male
print(dictA.get('age', 0))					# 40
print(dictA)					# {'name': '李华', 'age': 40}

六、集合操作

set(集合)也是python里面的一种数据类型,是一无序不重复元素集合,它不支持索引与切片

add() – 添加值,不能为重复的值
clear() – 清空set
difference() – 取差集, -也可以;而 difference_update() 为取差集并赋值
intersection() – 取交集,&也可以;而 intersection_update() 为取交集并赋值
union() – 取并集,|也可以
symmetric_difference() – 取对称差集,^也可以;而 symmetric_difference_update() 为取对称差集并赋值
pop() – 从集合中随机移除某个元素并获取那个参数,集合pop没有参数,建议使用discard()
discard() – 移除指定元素
remove() – 集合中删除指定的元素。此方法与 discard() 不同,因为如果指定项目不存在,remove() 会引发错误,而 discard() 不会。
update() – 更新集合,用于把两个集合合并,并去重

set1 = {1, '2'}          	# 集合的创建
set2 = {}					# 这样不能创建集合
set3 = set()				# 集合的创建
list1 = [1, 2, 3]
set4 = set(list1)			# 使用set方法创建集合

print(type(set1))			# <class 'set'>
print(type(set2))			# <class 'set'>
print(type(set3))			# <class 'set'>
print(type(set4))			# <class 'set'>

set1.add((5, 6))			# 添加一个值
print(set1)					# {1, (5, 6), '2'}
set1.update((5, 6))			# 把元组拆开再添加值
print(set1)					# {1, 5, 6, (5, 6), '2'}

print(set1.difference(set4))	# 差集,{'2', (5, 6), 5, 6}
print(set1.union(set4))			# 并集,{1, 2, 3, 5, 6, (5, 6), '2'}
print(set1.intersection(set4))	# 交集,{1}
print(set1.symmetric_difference(set4))		# {2, 3, 5, 6, (5, 6), '2'}

set1.pop()					# 随机删除一个值
print(set1)					# {5, 6, (5, 6), '2'}
set1.discard('2')			# 在集合里面删除对应值
print(set1)					# {5, 6, (5, 6)}
set1.remove(5)				# 在集合里面移除值为5的值
print(set1)					# {5, 6, (5, 6)}
set1.remove(7)				# 在集合里面移除值为7的值
# print(set1)				# 因为值不存在,所以报错

set4.clear()				# 删除集合里面所有值
print(set4)					# set()

七、公用方法

sorted()

对所有可迭代的对象进行排序操作。

sorted(iterable, cmp=None, key=None, reverse=False)

  • iterable – 可迭代对象。
  • cmp – 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。
  • key – 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse – 排序规则,reverse = True 降序 , reverse = False 升序(默认)。

注意Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。list 的 sort 方法是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

dictA = {'name': '李华', 'age': 40}

print(sorted(dictA.items(), key=lambda d: d[0]))	# 排序

+合并操作

适用于字符串,列表,元组

strA = '123'
strB = '456'
listA = list(range(10))
listB = list(range(11, 21))
tupleA = (1,)
tupleB = (2, 3, 4)
dictA = {'name': '李明'}
dictB = {'age': 30}

print(strA + strB)		# 123456
print(listA + listB)		# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print(tupleA + tupleB)		# (1, 2, 3, 4)
print(dictA + dictB)		# 报错,字典不可使用+

*复制

适用于字符串,列表,元组

strA = '123'
listA = list(range(10))
tupleA = (1,)
dictA = {'name': '李明'}

print(strA * 3)		# 123123123
print(listA * 3)		# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(tupleA * 3) 	# (1, 1, 1)
print(dictA * 3)	# 报错,字典不可使用*

in判断元素是否存在

适用于字符串,列表,元组,字典;而not in则于它相反

strA = '123'
listA = list(range(10))
tupleA = (1,)
dictA = {'name': '李明'}

print('1' in strA)			# True
print('0' in strA)			# False
print(1 in listA)			# True
print(-1 in listA)			# False
print(1 in tupleA)			# True
print(0 in dictA)			# False
print('name' in dictA)			# True
print('age' in dictA)			# False

八、其他

id( )

用于查找在内存的物理地址

str1 = 'hello'
str2 = str1

print(id(str1))			# 1970902101296
print(id(str2))			# 1970902101296

九、可变与不可变类型

可变类型

可变类型为对象引用指向值为可变的,这就意味着当对象重新赋值时对象的引用不会改变,可变的类型有列表、字典

import ctypes				# 该库用于查找内存地址对应的值

value = [1, 2]				# 定义一个列表
value1_loc = id(value)		# 获得当前列表的内存地址
print(value1_loc)			# 输出地址,2075718505536

value[0] = 3  				# 列表里面的值重新赋值
value2_loc = id(value)		# 获得当前列表的内存地址
print(value2_loc)			# 输出地址,2075718505536
get_value = ctypes.cast(value1_loc, ctypes.py_object).value  # 读取对应地址的值
print(get_value)			# 输出对应地址的值,[3, 2]

不可变类型

不可变类型为对象引用指向的值为不可变的,这就意味着当对象重新赋值时对象的引用也改变了值,而原来的值还在内存池里面,不可变的类型有int、str、float、元组,下面就可以说明这个问题

import ctypes			# 该库用于查找内存地址对应的值

value = 'hello world'	# 定义一个字符串变量
value1_loc = id(value)	# 获得当前变量的内存地址
print(value1_loc)		# 输出地址,1475453744688

value = '你好'			# 字符串变量重新赋值
value2_loc = id(value)	# 获得当前变量的内存地址
print(value2_loc)		# 输出地址,1475458315248
get_value = ctypes.cast(value1_loc, ctypes.py_object).value  # 读取对应地址的值
print(get_value)		# 输出对应地址的值,hello world

本文只用于个人学习与记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值