首页 > 其他分享 >基本数据类型的内置方法

基本数据类型的内置方法

时间:2023-12-03 16:22:57浏览次数:30  
标签:内置 res 数据类型 number list set dict print 方法

基本类型的内置方法

数字类型

(一)整型int

(二)浮点型float

(一)整型int

#整型
# number='111'
# print(number,type(number))#111 <class 'str'>
# #1.类型强转,  符合int类型格式的字符串强转为整型。
# print(int(number),type(int(number)))#111 <class 'int'>

# #2.十进制转换为其他进制
# #十进制转化为二进制
# print(bin(1000))#0b1111101000
# #十进制转化为八进制
# print(oct(1000))#0o1750
# #十进制转化为十六进制
# print(hex(1000))#0x3e8

# #3.其他进制转为十进制
# print(int('0b1111101000',2))#1000
# print(int('0o1750',8))#1000
# print(int('0x3e8',16))#1000

(二)浮点型float

#浮点型
# #1.类型强转
# number='12.0'
# print(number,type(number))#12.0 <class 'str'>
# #类型强转, 符合浮点数类型的字符串强转为浮点型
# print(float(number),type(float(number)))#12.0 <class 'float'>

# #2.取整  round
# #四舍五入
# print(round(6.6))#7
# print(round(6.5))#6
# print(round(6.4))#6

# #3.判断数字类型   isdigit 判断是否为数字类型
# number='66'             #数字
# number1=b'66'           #bytes
# number2='六十六'         #中文数字
# number3='Ⅳ'            #罗马数字
# print(number.isdigit())#True
# print(number1.isdigit())#True
# print(number2.isdigit())#False
# print(number3.isdigit())#False

(三)字符串str

字符串需要掌握的内置方法

# 字符串
# #1.索引取值
# res='hello world'
# print(res[0])#h
# print(res[-1])#d

# #字符串不允许索引修改
# res[0]='s'
r"""
Traceback (most recent call last):
  File "D:\old boy\python\python28基础\day07\03字符串.py", line 7, in <module>
    res[0]='s'
TypeError: 'str' object does not support item assignment
"""

# 2.字符串拼接
# # 第一种方法
# res='hello'
# res1='world'
# print(res + res1)#helloworld
# 第二种方法    join
# res='hello'
# print('.'.join(res))#h.e.l.l.o

# 3.字符串切片    切片骨头不顾尾
# res='hello world'
# print(res[0:5])#hello
# print(res[2:5])#llo
# #字符串切片支持步长   [起始位置:结束位置:步长]
# print(res[0:10:2])#hlowr
# #从指定位置切到结束
# print(res[0:])#hello world
# #从开始切到指定索引位置
# print(res[:10])#hello worl
# #从开始切到结束,指定步长
# print(res[::3])#hlwl

# res='hello world'
# #倒着取值
# # print(res[-1])#d

# # 翻转字符串
# res='hello'
# print(res[::-1])#olleh

# #4.计算长度
# res='hello world'
# print(len(res))#11

# #5.成员运算
# res='hello world'
# print('o' in res)#True
# print('s' not in res)#True
# print('s' in res)#False
# print('o'  not in res)#False

# # 6.strip去除空格  lstrip去除左边空格  rstrip去除右边空格
# res = '    hello world    '
#strip去除空格
# print(res.strip())   #hello world
#lstrip去除左边空格
# print(res.lstrip())  #hello world
#rstrip去除右边空格
# print(res.rstrip())  #     hello world

# #两边有字符,去除指定的字符
# res='!!!hello world!!!'
# #strip去除字符
# print(res.strip('!'))#hello world
# # lstrip去除左边字符
# print(res.lstrip('!'))#hello world!!!
# # rstrip去除右边字符
# print(res.rstrip('!'))#!!!hello world

# #7.切分 split
# res='hello world'
# #split默认是按照空格进行切分
# print(res.split())#['hello', 'world']
# #split按照指定的元素切分
# res='hello|world'
# print(res.split('|'))#['hello', 'world']
# res='hello wrold'
# # split从左往有进行切分
# #split在左往右的第一个o的位置切分
# print(res.split('o',1))#['hell', ' wrold']
# #rsplit从右往左切分
# res='hello wrold'
# print(res.rsplit('o'))#['hell', ' wr', 'ld']
# # rsplit从右往左 在第一个o的位置进行切分
# print(res.rsplit('o',1))#['hello wr', 'ld']

# #8.遍历
# res='hello world'
# for i in res:
#     print(i)
# h
# e
# l
# l
# o
#
# w
# o
# r
# l
# d

# #9.重复
# res='hello'
# print(res * 3)#hellohellohello

# #10.大小写转换  lower 小写     upper  大写
# res='hello WORLD'
# print(res.lower())#hello world
# print(res.upper())#HELLO WORLD

# # 11.首字母判断  startswith以什么开始     endswith以什么结尾
# res='hello world'
# print(res.startswith('hello'))#True
# print(res.startswith('ss'))#False
# print(res.endswith('world'))#True
# print(res.endswith('ss'))#False

# #12.格式化输出
# name='syh'
# age=23
# #%S %d
# print('my name is %s,my age is %s' % (name,age))
# #my name is syh,my age is 23
# #format
# print('my name is {},my age is {}'.format('syh',23))
# #my name is syh,my age is 23
# #按照索引取参数
# print('my name is {0},my age is {1}'.format('syh',23))
# #my name is syh,my age is 23
# print('my name is {0}{0}{0},my age is {1}{1}{1}'.format('syh',23))
# # my name is syhsyhsyh,my age is 232323
# #f + {}
# print(f"my name is {name},my age is {age}")
# #my name is syh,my age is 23

# #13.替换 replace
# res='hello world'
# #replace(旧的元素,新的元素)
# print(res.replace('o','s'))#hells wsrld

# #14.判断字符串类型是否符合数字类型
# input_number=input('请输入数字:')
# if input_number.isdigit():
#     print(input_number,type(input_number))#11 <class 'str'>
# else:
#     print('输入有误')

字符串需要了解的内置方法

# #字符串了解的内置方法
# #1.查找方法     find        顾头不顾尾
# res='hello world'
# #find   从左往右找,并切只能找到一个位置,且是第一个
# print(res.find('e'))#1
# #find 从左往右找,在指定的区间内找到的第一个,顾头不顾尾
# print(res.find('e',0,10))#1
# #rfind  从右往左找,找到的位置还是就是从左到右的正向元素的位置
# print(res.rfind('e'))#1

# #2.查找方法     index
# res='hello world'
# #index 能找到指定位置的元素
# print(res.index('d'))#10
# print(res.index('s'))#找不到的元素直接报错
r"""
Traceback (most recent call last):
  File "D:\old boy\python\python28基础\day07\03字符串.py", line 169, in <module>
    print(res.index('s'))
ValueError: substring not found
"""

# #3.计数count
# res='hello world'
# #统计o出现的次数
# print(res.count('o'))#2

# #4.填充
# res='syh'
# #格式center(字符串长度,填充内容)
# print(res.center(5,'-'))#-syh-
# #ljust      在字符串右边填充
# print(res.ljust(5,'-'))#syh--
# #rjust      在字符串左边填充
# print(res.rjust(5,'-'))#--syh
# #使用0进行填充,填充够指定的长度
# print(res.zfill(10))#0000000syh

# #5.制表符  \t
# res='hello\tworld'
# print(res)

# #6.首字母大写
# res='my name is syh'
# #capitalize
# #不论你是一句还 还是一个单词,只要是第一行,只有第一个单词的首字母大写
# print(res.capitalize())#My name is syh
# #title
# #可以让所有的单词首字母都大写,要符合英文语句的特点,每个单词之间要加空格
# print(res.title())#My Name Is Syh

# #7.大小写翻转    swapcase
# res='HELLO world'
# print(res.swapcase())#hello WORLD

#8.判断方法
"""
isalnum:字符串中既可以包含数字也可以包含字母
isalpha:字符串中只包含字母
islower:字符串是否是纯小写
isupper:字符串是否是纯大写
istitle:字符串中的单词首字母是否都是大写
isspace:字符串是否全是空格
isidentifier:字符串是否是合法标识符
"""
# res='Syh818'
# # 字符串中既可以包含数字也可以包含字母,True
# print(res.isalnum())
# # 字符串中只包含字母,False
# print(res.isalpha())
# # 字符串是否是合法标识符,True
# print(res.isidentifier())
# # 字符串是否是纯小写,True
# print(res.islower())
# # 字符串是否是纯大写,False
# print(res.isupper())
# # 字符串是否全是空格,False
# print(res.isspace())
# # 字符串中的单词首字母是否都是大写,False
# print(res.istitle())

(四)列表list

#列表list
#1.类型强转
"""字符串、元组、字典、集合都可以强转为列表"""
# #字符串类型强转列表类型,字符串的每个元素都是列表的每个元素
# res='hello'
# print(list(res))#['h', 'e', 'l', 'l', 'o']
# #元组强转列表,元组中的每一个元素就是列表的每一个元素
# number_tuple=(1,2,3,4,5)
# print(list(number_tuple))#[1, 2, 3, 4, 5]
# #字典强转列表,字典中的键就是列表的每个元素
# dict={'name':'syh','age':23}
# print(list(dict))#['name', 'age']
# #集合强转列表,集合中的每个元素就是列表的每个元素
# set={1,2,3,4,5}
# print(list(set))#[1, 2, 3, 4, 5]
"""整型、浮点型、布尔类型都不可以转换类型"""
## 特殊range
#range生成列表
# print(list(range(1,5)))#[1, 2, 3, 4]

# #2.索引取值
# list=[1,2,3,4,5]
# print(list[0])#1
# print(list[2])#3

# #3.切片   顾头不顾尾
# list=[1,2,3,4,5]
# print(list[0:3])##[1, 2, 3]
# #支持步长
# print(list[0:4:2])#[1, 3]

# #4.计算长度
# list=[1,2,3,4,5]
# print(len(list))#5

# #5.成员运算 in / not in
# list=[1,2,3,4,5]
# print(1 in list)#True
# print(1 not in list)#False

#6.增加
#append增加  每次只能在末尾添加一个元素,
# list=[1,2,3,4,5]
# list.append('555')
# print(list)#[1, 2, 3, 4, 5, '555']
#
# #extend可以添加多个元素到列表中,添加的都是可迭代元素
# list=[1,2,3,4,5]
# #在列表中添加列表
# list.extend([66,77,88])
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88]
# #在列表中添加元组
# list.extend((66,77,88))
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88, 66, 77, 88]
# #在列表中添加字典
# list.extend({'name':'syh','age':18})
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88, 66, 77, 88, 'name', 'age']
# #在列表中添加字符串
# list.extend('syh')
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88, 66, 77, 88, 'name', 'age', 's', 'y', 'h']
#
# #insert
# #按照索引位置,在索引的位置添加元素
# list=[1,2,3,4,5]
# list.insert(1,'syh')
# print(list)#[1, 'syh', 2, 3, 4, 5]

# #7.删除
# #del
# list=[1,2,3,4,5]
# # 删除列表索引位置的元素
# del list[2]
# print(list)#[1, 2, 4, 5]
#
# #pop 弹出元素
# list=[1,2,3,4,5]
# # 默认弹出最后一个元素
# list.pop()#[1, 2, 3, 4]
# print(list)
# list.pop(0)#弹出索引位置指定的元素[2, 3, 4]
# print(list)

# #8。改值
# # 按照索引位置对指定元素进行改值。
# list = [1, 2, 3, 4, 5]
# list[2]=666#[1, 2, 666, 4, 5]
# print(list)

# #9.颠倒元素reverse
# list = [1, 2, 3, 4, 5]
# list.reverse()#[5, 4, 3, 2, 1]
# print(list)

#10.排序     sort升序    sort(reverse=True)降序   sorted()默认为升序排序
# list=[11,66,77,55,33,22,44]
# #按照升序排序
# list.sort()#[11, 22, 33, 44, 55, 66, 77]
# print(list)
# list=[11,66,77,55,33,22,44]
# #降序排序
# list.sort(reverse=True)#[77, 66, 55, 44, 33, 22, 11]
# print(list)

# # sorted()默认为升序排序
# list=[11,66,77,55,33,22,44]
# list1=sorted(list)#[11, 22, 33, 44, 55, 66, 77]
# print(list1)

# #11.迭代循环
# list=[11,22,33,44,55]
# #遍历取值
# for i in list:
#     print(i)
# 11
# 22
# 33
# 44
# 55
# #   通过结合range关键字取值
# list=[11,22,33,44,55]
# for i in range(len(list)):
#     print(i,end=' ')
#     print(len(list))
# 0 5
# 1 5
# 2 5
# 3 5
# 4 5

# #while 循环取值
# count=0
# while count<5:
#     print(count)
#     count+=1
# 0
# 1
# 2
# 3
# 4

# #12.步长
# list=[1,2,3,4,5,6,7]
# #从头取到末尾,步长为3
# print(list[::3])#[1, 4, 7]
# #翻转列表
# print(list[::-1])#[7, 6, 5, 4, 3, 2, 1]

(五)布尔类型bool

#布尔类型
#1.强转布尔类型
num=1
print(bool(num),type(bool))#True <class 'type'>
list=[1,2,3]
print(bool(list),type(bool))#True <class 'type'>
str='syh'
print(bool(str),type(bool))#True <class 'type'>
dict={'name':'syh','age':18}
print(bool(dict),type(bool))#True <class 'type'>
tuple=(1,2,3)
print(bool(tuple),type(bool))#True <class 'type'>
set={1,2,3}
print(bool(set),type(bool))#True <class 'type'>
"""
布尔类型:
        True:数字类型1 有值的数字类型 非空字符串、非空字典、非空列表、非空元组、非空集合都可以强转为布尔类型
        False:0  空字符串、空列表、空字典、空元组、空集合
"""

(六)元组类型tuple

# #元组tuple
# number_tuple=(1)
# print(type(number_tuple))#<class 'int'>
# number1_tuple=(1,)
# print(type(number1_tuple))#<class 'tuple'>
"""
    元组类型:
            元组中只有一个元素的时候,要在元素的后面加逗号
            否则会改变元组的数据类型。
"""

# #1.强转元组类型
# #字符串强转元组类型,字符串中的每个字符就是元组中的元素
# str='syh'
# print(tuple(str))#('s', 'y', 'h')
# #列表强转元组类型,列表中的每个元素,就是元组中的元素
# list=[1,2,3,4,5]
# print(tuple(list))#(1, 2, 3, 4, 5)
# # 字典强转元组类型,字典中的每个键就是元组中的元素。
# dict={'name':'syh','age':18}
# print(tuple(dict))#('name', 'age')
# # 集合强转元组,集合中的每个元素就是元组中的每个元素
# set={1,2,3}
# print(tuple(set))#(1, 2, 3)
# #数字类型不支持强转为元组类型
# print(tuple(1))#TypeError: 'int' object is not iterable

# #2.索引取值
# number_tuple=(1,2,3,4,5,6)
# #正向索引取值
# print(number_tuple[0])#1
# print(number_tuple[2])#2
# #反向索引取值
# print(number_tuple[-1])#6
# #元组类型不支持索引改值
# number_tuple[2]=666##TypeError: 'tuple' object does not support item assignment

# #3.切片
# #元组类型切片tuple[起始位置:终止位置]但是顾头不顾尾
# number_tuple=(1,2,3,4,5,6)
# #元组类型支持切片tuple[起始位置:终止位置:步长]
# print(number_tuple[0:5])#(1, 2, 3, 4, 5)
# # 元组类型支持步长
# print(number_tuple[0:5:2])#(1, 3, 5)

# #4.计算长度
# number_tuple=(1,2,3,4,5,6)
# print(len(number_tuple))#6

# #5.成员运算
# number_tuple=(1,2,3,4,5,6)
# print(1 in number_tuple)#True
# print(88 in number_tuple)#False
# print(1 not in number_tuple)#False
# print(88 not in number_tuple)#True


# #6.遍历循环
# number_tuple=(1,2,3,4,5,6)
# for i in number_tuple:
#     print(i)
# # 1
# # 2
# # 3
# # 4
# # 5
# # 6

# #7.元组拼接 +
# number_tuple=(1,2,3,4,5)
# number1_tuple=(11,22,33,44)
# print(number_tuple+number1_tuple)#(1, 2, 3, 4, 5, 11, 22, 33, 44)

# #8.元组重复
# number_tuple=(1,2,3,4,5)
# print(number_tuple*3)#(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

(七)字典类型dict

#字典
#dict={'key':'value'}
#key:建议使用的是不可变类型:整型/字符串类型
# value:可以使用任意的数据类型

# #1.取值
#第一种
# user_dict={'name':'syh','age':23,'gender':'male'}
# #按照字典的key键取值
# print(user_dict['name'])#syh
# #按照字典的key取值时,如过字典中没有该key,那么就会报错
# print(user_dict['hobby'])#KeyError: 'hobby'

# #第二种
# #get取值
# user_dict={'name':'syh','age':23,'gender':'male'}
# #get取值,根据键key取值,取到的就是该键对应的值
# print(user_dict.get('name'))#syh
# #get取值,根据键key取值,如果字典中不存在该键kye那么就会返回None
# print(user_dict.get('hobby'))
# #get取值,根据键key取值,如果字典中不存在的该键key,那么也可以指定默认值,取不到的时候就返回默认值
# print(user_dict.get('hobby',666))
"""
    在字典中,如果确切的知道存在键key时,那么就看可以直接使用键取值[]
            如果不清楚是否存在该键key时,那么就可以使用get取值,还可以指定一个默认值
            而且使用get时还不会报错。
"""

# #2.计算长度。
# #在字典中计算长度时,计算的时键key的长度
# user_dict={'name':'syh','age':23,'gender':'male'}
# print(len(user_dict))#3

# #3.成员运算
# user_dict={'name':'syh','age':23,'gender':'male'}
# print('name' in user_dict)#True
# print('hobby' in user_dict)#False
# print('name' not in user_dict)#True
# print('hobby' not in user_dict)#False

# #4.增加值
# user_dict={'name':'syh','age':23,'gender':'male'}
# #在字典中,如果已经存在该键key了,那么就会直接修改该键的值
# user_dict['name']='susu'
# print(user_dict)#{'name': 'susu', 'age': 23, 'gender': 'male'}
# #在字典中,如果该键key不存在,那么就将该键值添加到字典中去
# user_dict['hobby']='music'
# print(user_dict)#{'name': 'susu', 'age': 23, 'gender': 'male', 'hobby': 'music'}

#update
#第一种update({'key':'value'})
# user_dict={'name':'syh','age':23,'gender':'male'}
# #update可以对字典进行批量的证据
# #在字典中如果没有存在该键,那么直接添加进字典中
# # user_dict.update({'hobby':'music'})#{'name': 'syh', 'age': 23, 'gender': 'male', 'hobby': 'music'}
# # print(user_dict)
# #在字典中如果该键存在,那么就将该键的值更新进字典中,改变原键的值
# user_dict.update({'name':'susu'})#{'name': 'susu', 'age': 23, 'gender': 'male'}
# print(user_dict)

# #第二种
# #update(key='value')
# user_dict={'name':'syh','age':23,'gender':'male'}
# #字典中,不存在该键的时候,在字典中加入该键值队
# user_dict.update(hobby='music')
# print(user_dict)#{'name': 'syh', 'age': 23, 'gender': 'male', 'hobby': 'music'}
# #字典中存在该键的时候,那么这个键对应的值就会被改变
# user_dict.update(name='susu')
# print(user_dict)#{'name': 'susu', 'age': 23, 'gender': 'male', 'hobby': 'music'}

#第三种
#setdefault
# user_dict={'name':'syh','age':23,'gender':'male'}
# #不支持关键字参数
# user_dict.setdefault(name='su')
# print(user_dict)#TypeError: dict.setdefault() takes no keyword arguments

# #支持setdefault(key,value)设置你的元素增加单个元素
# user_dict.setdefault('hobby','music')
# print(user_dict)#{'name': 'syh', 'age': 23, 'gender': 'male', 'hobby': 'music'}
# #有返回值,返回值就是你键对应的值
# print(user_dict.setdefault('hobby','music'))#music

#5.删除
# #第一种
# #del 根据键删除指定的键值
# user_dict={'name':'syh','age':23,'gender':'male'}
# del user_dict['gender']
# print(user_dict)#{'name': 'syh', 'age': 23}

#第二种
# #pop弹出
# user_dict={'name':'syh','age':23,'gender':'male'}
# #pop根据键弹出键值队,必须制定参数,参数就是键key
# res=user_dict.pop('name')#{'age': 23, 'gender': 'male'}
# print(user_dict)
# #pop弹出键有返回值,返回值就是键对应的value值。
# print(res)#syh

# #第三种
# #clear
# user_dict={'name':'syh','age':23,'gender':'male'}
# #clear 直接清楚字典中的所有元素。
# user_dict.clear()#{}
# print(user_dict)

# #第四种
# #popitem删除末尾的键值对
# user_dict={'name':'syh','age':23,'gender':'male'}
# user_dict.popitem()
# user_dict.popitem()
# print(user_dict)

# # #6.键队keys/值队values/键值队items
# user_dict={'name':'syh','age':23,'gender':'male'}
# #键队keys
# print(user_dict.keys())#dict_keys(['name', 'age', 'gender'])
# #值队values
# print(user_dict.values())#dict_values(['syh', 23, 'male'])
# #键值队items
# print(user_dict.items())#dict_items([('name', 'syh'), ('age', 23), ('gender', 'male')])

# #7.遍历循环
# #遍历循环的是键
# user_dict={'name':'syh','age':23,'gender':'male'}
# for i in user_dict:
#     print(i)
# # name
# # age
# # gender
#
# #遍历循环键队
# for i in user_dict.keys():
#     print(i)
# #name
# #age
# #gender
#
# #遍历循环值队
# for i in user_dict.values():
#     print(i)
# # syh
# # 23
# # male
#
# #遍历循环键值队
# for i in user_dict.items():
#     print(i)
# # ('name', 'syh')
# # ('age', 23)
# # ('gender', 'male')

#8.字典的排序
# number_dict={1:2,3:4,5:6,7:8}
# #字典中不存在sort排序
# number_dict.sort()

#sorted排序,但这是对字典的键进行排队
# number_dict={1:2,3:4,5:6,7:8}
# res=sorted(number_dict)#[1, 3, 5, 7]
# print(res)

# #sorted排序,对中文字符排序的话,是根据第一个字母的ASCII编码表进行大小排序的
# user_dict={'name':'syh','age':23,'gender':'male'}
# res=sorted(user_dict)#['age', 'gender', 'name']
# print(res)
"""记忆:
    	A-Z: 65-96
        a-z: 97-
        0-9: 48-
"""

(八)集合

#集合set
# #集合中只有一个元素的时候,可以不加入逗号,也不会改变集合的类型
# number_set={1}
# print(number_set,type(number_set))#{1} <class 'set'>

# #1.类型强转
# #字符串类型强转集合
# res='hello'
# print(set(res),type(set(res)))#{'o', 'h', 'l', 'e'} <class 'set'>
# #列表类型强转集合
# list=[1,2,3,4]
# print(set(list),type(set(list)))#{1, 2, 3, 4} <class 'set'>
# # 元组类型强转集合
# tuple=(1,2,3,4)
# print(set(tuple),type(set(tuple)))#{1, 2, 3, 4} <class 'set'>
# #字典类型强转集合,集合的元素就是字典的键key
# dict={'name':'syh','age':18}
# print(set(dict),type(set(dict)))#{'name', 'age'} <class 'set'>
# #数字类型不能强转为集合
# print(set(1))#TypeError: 'int' object is not iterable

#2.添加元素
# #add  只能添加一个元素
# number_set={1,2,3}
# number_set.add(666)
# print(number_set)

# #update 可以在集合中添加多个元素  放的是可迭代的数据类型(字符串,列表,元组,字典)
# number_set={1,2,3}
# number_set.update([11,22,33])#{1, 2, 3, 33, 11, 22}
# print(number_set)
# #集合 有去重性质,如果添加重复的元素会去重,不重复的元素会直接添加进集合中
# number_set.update([11,22,33,44,])#{1, 2, 3, 33, 11, 44, 22}
# print(number_set)

#3.删除元素
#第一种
# #remove  删除指定元素
# number_set={1,2,3,4,5,6}
# number_set.remove(3)#{1, 2, 4, 5, 6}
# print(number_set)
# #remove只能删除集合中存在的元素,删除不存在的元素时会报错。
# number_set.remove(66)##KeyError: 66

# 第二种
# #discard  删除指定元素,无返回值
# number_set={1,2,3,4,5,6}
# number_set.discard(3)#{1, 2, 4, 5, 6}
# print(number_set)
# #discard 删除指定元素,删除的元素如果不存在 也不会报错
# number_set.discard(66)
# print(number_set)

#第三种
# # pop删除
# number_set={1,2,3,4,5,6}
# #pop删除的是集合的第一个元素
# number_set.pop()
# print(number_set)

#4.集合的运算方法
# #1.并集/合集 |  //  union
# number_set={1,2,3,4,5,6}
# number1_set={3,4,5,}
# print(number_set|number1_set)#{1, 2, 3, 4, 5, 6}
# print(number_set.union(number1_set)){1, 2, 3, 4, 5, 6}

# # 2.交集  & //intersection
# number_set={1,2,3,4,5,6}
# number1_set={3,4,5,}
# print(number_set&number1_set)#{3, 4, 5}
# print(number_set.intersection(number1_set))#{3, 4, 5}

# # 3.差集 -  // difference
# number_set={1,2,3,4,5,6}
# number1_set={3,4,5,}
# print(number_set-number1_set)#{1, 2, 6}
# print(number_set.difference(number1_set))#{1, 2, 6}

# #4.对称差集 ^  // symmetric_difference
# number_set={1,2,3,4,5,6}
# number1_set={3,4,5,}
# print(number_set^number1_set)#{1, 2, 6}
# print(number_set.symmetric_difference(number1_set))#{1, 2, 6}

# # 5.判断是否相等==
# print({1,2,3}=={1,2})#False
# print({1,2,3}=={1,2,3})#True

# # 6.计算长度
# number_set={1,2,3,4,5}
# print(len(number_set))#5

# #7.遍历循环
# number_set={1,2,3,4,5}
# for i in number_set:
#     print(i)
# # 1
# # 2
# # 3
# # 4
# # 5

# # 8.成员运算
#集合是无序的,但是在成员运算中 不影响判断元素是否在集合当中。
# number_set={1,2,3,4,5}
# print( 1 in number_set)#True
# print( 1 not in number_set)#False
# print(111 in number_set)#False
# print(111 not in number_set)#True

(小结)

#按找存入的值的个数分为:
	#只能存放一个:字符串/数字类型/布尔型
    #可以存放多元素:列表/字典/元组/集合
#按访问当时区分:
	#能索引取值的:字符串/列表/元组
    #不能索引取值:数字类型/布尔类型/集合
    #按键取值的:字典
#按可变不可变类型区分:
	#不可变:数字类型/字符串/集合/元组/布尔
    #可变类型:列表/字典

标签:内置,res,数据类型,number,list,set,dict,print,方法
From: https://www.cnblogs.com/suyihang/p/17873317.html

相关文章

  • 精通C#要点:解析委托、匿名方法与事件
    文章目录委托(Delegate)委托的特性声明委托实例化委托委托的多播(MulticastingofaDelegate)委托的用途匿名方法委托实际应用场景事件(Event)声明事件事件实例1事件实例2事件实例3委托和事件的区别总结 委托(Delegate)委托是对具有特定参数列表和返回类型的方法的......
  • 公众号文章中添加附件的方法
    微附件的作用是给公众号添加附件的,但是由于公众号本身不提供这种服务,所以微附件发挥了重要的传媒功能,他能够将许多不同类型的附件上传。要添加附件首先要知道如何打开微附件的官网:可以利用下方图片中的网址,在浏览器中打开。其次要知道通过正确的方法上传文件,具体有三个方法,都在下方......
  • 数据类型内置方法
    数据类型内置方法介绍八大基本数据类型数字类型整形(int)浮点型(float)字符串(str)列表(list)元组(tuple)布尔(bool)字典(dict)集合(set)【一】整型(int)(1)类型强转可以将由纯整数构成的字符串直接转换成整型符合int类型格式的字符串可以强转成整数类型num='123'print......
  • 学c笔记归纳 第二篇——基本数据类型
    基本数据类型告诉编译器,变量是什么类型,不同类型占内存大小不同,单位:字节char字符型 1short短整型2int整型  4long长整型 4longlong更长的整型 ......
  • 极语言3-13任务栏对象、链接对象、存储对象、自动化对象——方法表
    英文名字中文名称作用解释ITaskbarList任务栏对象公开控制任务栏的方法。它允许动态添加、删除和激活任务栏上的项。任务栏对象——方法表QueryInterface接口(标识,@指针)检索指向对象上支持的接口的指针。AddRef计数递增对象上接口的引用计数。对于指向对象上接口的指针的每个新......
  • 极语言3-14网页框——对象使用,浏览对象可执行命令表,新快捷对象——方法表
    网页框——对象使用对象浏览=浏览器对象; 申请一个浏览器对象的变量程序段窗体启动; 在程序段内控制网页框对象  浏览=控件对象网页框1; 获取网页框的浏览器对象  浏览.改静默(1); 控制网页框的浏览器对象不显示对话提示  浏览.连接("`https://www.baidu.com/",0,0,0,......
  • 极语言3-16公用绘图对象——方法表
    英文名字中文名称作用解释IDirectDraw公用绘图对象使用驱动显示接口的方法创建绘图对象并使用系统级变量。公用绘图对象——方法表QueryInterface接口(标识,@指针)检索指向对象上支持的接口的指针。AddRef计数递增对象上接口的引用计数。对于指向对象上接口的指针的每个新副本,应调......
  • 【面试攻略】Oracle中blob和clob的区别及查询修改方法
    大家好,我是小米,欢迎来到小米的技术小屋!今天我们要一起来聊聊一个在面试中常常被问到的问题——“Oracle中Blob和Clob有啥区别,在代码中怎么查询和修改这两个类型的字段里的内容?”别急,跟着小米一步步揭开这个技术的神秘面纱!Blob和Clob是什么?首先,让我们来了解一下Blob和Clob是什么。......
  • CentOS7 无法执行systemctl status ntpd的原因及解决方法
    在CentOS7中,NTP的服务名为ntpd.service,如果出现Unitntpd.servicecouldnotbefound的错误,可能是由于NTP没有安装或未正确启动所致。你可以尝试以下步骤来解决此问题:首先,检查系统是否已经安装NTP。可以运行以下命令:rpm-qntp如果返回packagentpisnotinstall......
  • 数据类型
    数据类型数据类型:强类型语言+弱类型语言强类型语言:要求变量的使用严格符合规定,所有变量都必须先定义后才能使用 Java的数据类型分为基本数据类型和引用数据类型八大基本数据类型:整型byte[1],short[2],int[4],long[8]浮点型float[4],double[8]字符型ch......