数据类型内置方法
数字类型内置方法
整型
- 1.用途:年龄、身份证号码、手机号。。。
- 2.定义方式:
age1 = 18
age2 = int(18)
print(age1)
print(id(age1))
print(type(age1))
print(type(age2))
18
1723559936
<class 'int'>
<class 'int'>
- 3.常用操作和内置方法:+ - * / // ** % ;无内置方法
- 4.存一个值or多个值:一个值
- 5.有序or无序(有索引为有序,无索引为无序):根本没有有序或无序的说法
- 6.可变or不可变:不可变
#不可变类型:值变则ID变
age = 18
print(id(age))
age += 1
print(id(age))
1723559936
1723559968
#可变类型:值变ID则不变
list1 = ['name','height','weight']
print(list1)
print(id(list1))
list1[1] = 185
print(list1)
print(id(list1))
['name', 'height', 'weight']
1701728445128
['name', 185, 'weight']
1701728445128
浮点型
- 1.用途:身高、体重、薪水、存款
- 2.定义方式:
salary1 = 1.5
salary2 = float(1.5)
print((salary1))
print(id(salary1))
print(type(salary1))
print(type(salary2))
1.5
1701727543560
<class 'float'>
<class 'float'>
- 3.常用操作和内置方法:+ - * / ** // % :无内置方法
- 4.存一个值or多个值: 一个值
- 5.有序or无序:根本没有有序无序一说
- 6.可变or不可变:不可变
字符串类型内置方法
- 1.用途:名字、单个爱好
- 2.定义方式:
name1 = 'william'
name2 = "william"
name3 = '''
william1
william2
'''
name4 = """
william3
william4
"""
print(type(name1))
print(type(name4))
<class 'str'>
<class 'str'>
- 3.常用操作和内置方法:
**1.优先掌握
***1.按索引取值
my_info_list = ['name','height', 'weight','hobby',['reading','running']]
my_info = "name height weight hobby ['reading','running']"
print(my_info_list[2])
print(my_info[2])
print(my_info_list[4])
print(my_info[4])
print(my_info[5])
weight
m
['reading', 'running']
h
***2.切片(顾头不顾尾),步长的正负控制行进的方向,左行进还是右行进。
my_info = "name height weight hobby ['reading','running']"
print(my_info[:])
print(my_info[0:6])
print(my_info[0:8:2])
print(my_info[-1:-4:-1])
print(my_info[-4:-1])
print(my_info[-4:])
name height weight hobby ['reading','running']
name h
nm e
]'g
ng'
ng']
***3.长度len
my_info = "name height weight hobby ['reading','running']"
print(len(my_info))
46
***4.成员运算 in not in
my_info = "name height weight hobby ['reading','running']"
print('height' in my_info)
print('hobbies'in my_info)
True
False
***5.移除空白strip,默认为空格
test = ' a abc a'
print(test.strip())
print(test.strip('a'))
a abc a
a abc
***6.切分split
my_info = "name height weight hobby ['reading','running']"
print(my_info.split())
print(my_info.split(' ',2))
print(my_info.split('i'))
['name', 'height', 'weight', 'hobby', "['reading','running']"]
['name', 'height', "weight hobby ['reading','running']"]
['name he', 'ght we', "ght hobby ['read", "ng','runn", "ng']"]
***7.循环
my_info = "name height weight hobby ['reading','running']"
for i in my_info :
print(i,end=' ')
n a m e h e i g h t w e i g h t h o b b y [ ' r e a d i n g ' , ' r u n n i n g ' ]
**2.需要掌握
***1.lstrip & rstrip
test = ' a abc a'
print(test.lstrip())
print(test.lstrip('a'))
print(test.rstrip('a'))
a abc a
a abc a
a abc
***2.lower & upper
my_info = "Name Height Weight hobby ['reading','running']"
print(my_info.lower())
print(my_info.upper())
name height weight hobby ['reading','running']
NAME HEIGHT WEIGHT HOBBY ['READING','RUNNING']
***3.startswith & endswith
my_info = "name height weight hobby ['reading','running']"
print(my_info.startswith('name'))
print(my_info.startswith('he'))
print(my_info.endswith('running'))
True
False
False
***4.rsplit
my_info = "name height weight hobby ['reading','running']"
print(my_info.rsplit(' '))
print(my_info.rsplit(' ',2))
['name', 'height', 'weight', 'hobby', "['reading','running']"]
['name height weight', 'hobby', "['reading','running']"]
***5.join
my_info = "name height weight hobby ['reading','running']"
my_info_split = my_info.split()
print(my_info)
print(my_info_split)
print(''.join(my_info_split))
print('.'.join(my_info_split))
name height weight hobby ['reading','running']
['name', 'height', 'weight', 'hobby', "['reading','running']"]
nameheightweighthobby['reading','running']
name.height.weight.hobby.['reading','running']
***6.replace
my_info = "name height weight hobby ['reading','running']"
print(my_info.replace('name','william').replace('weight','150'))
william height 150 hobby ['reading','running']
***7.isdigit
print('10dsf'.isdigit())
print('10'.isdigit())
False
True
**3.了解
# find rfind index rindex count
his_info = 'Name 1 WHEIGHT reading running reading 1 height'
print(his_info.find('1'))
print(his_info.find('18')) #记住不是False,而是-1
print(his_info.rfind('1'))
print(his_info.rfind('z')) #记住不是False,而是-1
5
-1
39
-1
his_info = 'Name 1 WHEIGHT reading running reading 1 height'
print(his_info.index('WHEIGHT'))
#print(his_info.index('18')) #会报错
7
his_info = 'Name 1 WHEIGHT reading running reading 1 height'
print(his_info.count('reading'))
2
# center ljust rjust zfill
print('SS'.center(20,'*'))
print('SS'.center(20,'+'))
print('SS'.ljust(20,'*'))
print('SS'.rjust(20,'+'))
print('SS'.zfill(20))
*********SS*********
+++++++++SS+++++++++
SS******************
++++++++++++++++++SS
000000000000000000SS
# expandtabs
print(' Tom'.expandtabs(4))
Tom
#capitalize swapcase title
print('tom and jerry'.capitalize())
print('Tom and Jerry'.swapcase())
print('tom and jerry'.title())
Tom and jerry
tOM AND jERRY
Tom And Jerry
# is系列
print('tom'.isdigit())
print('tom'.isalpha())
print('10'.isalpha())
False
True
False
- 4.存一个值or多个值:一个值
- 5.有序or无序:有序
- 6.可变or不可变:不可变
name = 'William'
print(name)
print(id(name))
name += '2'
print(name)
print(id(name))
William
1701731277152
William2
1701731197360