python 数据类型内置方法

今日内容概要

  • 数据类型的内置方法理论
  • 整形相关操作
  • 浮点型相关操作
  • 字符串相关操作
  • 列表相关操作

今日内容详细

数据类型内置方法理论

我们之前所学习的每一种数据类型本身都含有一系列的操作方法,内置方法是其中最多的(自带的功能)
在python中数据类型调用内置方法的统一句式为>>>:句点符
	'guts'.字符串内置方法
 	绑定字符串的变量名.字符串内置方法
  	str.字符串内置方法
ps:数据类型的内置方法比较的多 我们如果想要掌握 不要光靠死记硬背 更多时候靠的是熟能生巧

整型内置方法与操作

1. 类型转换(将其他数据类型转换成整形)
	int(其他类型数据)
    ps:浮点型可以直接转换 字符串必须满足内部是纯数字才可以
2.进制数转换
	十进制转其他进制
    	print(bin(100))  #0b1100100
        print(oct(100))  #0o144
        print(hex(100))  #0x64
'''
数字的开头如果是0b则为二进制 0o则为八进制 0x则为十六进制
'''        
	其他进制转十进制
    	print(int(0b1100100))
        print(int(0o114))
        print(int(0x64))
        
        print(int("0b1100100", 2))
        print(int("0o144", 8))
        print(int("0x64", 16))

3.python自身对数字的敏感度较低(精确度低)
	python这门语言其实真的一点都不厉害 主要是因为他背后有太多的大佬
    如果需要精准的计算需要借助与模块numpy....

字符串内置方法与操作

1.类型转换
	str(其他数据类型)
    ps:可以转任意数据类型(只需要在前后加引号即可)
2.必须要掌握的方法
	1.索引取值(起始位置0开始 超出范围直接报错)
    s1 = 'hello word'
    print(s1[0])  #h
    print(s1[-1])  #d  支持复数 从末尾开始
    2.切片操作
    print(s1[1:5])  # 顾头不顾尾 从索引1一直切到索引4
    print(s1[-1:-5])  # 默认的顺序是从左往右 所以不能这么输入
    print(s1[-5:-1])  # 默认的顺序是从左往右 
    3.修改切片方向(间隔)
    print(s1[1:5:1])  # 默认是1
    print(s1[1:5:2])  # 默认是1
    print(s1[-1:-5:-1]) # dlro
    print(s1[:])  # 不写数字就默认都要
    print(s1[2:])  # 从索引2开始往后都要
    print(s1[:5])  # 从索引0开始要到4
    print(s1[::2])  # 每隔两个拿一个
    4.统计字符串中字符的个数
    print(len(s1))  
    5.移除字符串首尾指定的字符
    username = input('username:').strip()
    username = username.strip()
    if username == 'guts'
    	print('登陆成功')
    res = '   guts   '
    print(len(res))
    print(len(res.strip()))  # 括号内不写 默认一处首位空格
    res1 = '$$guts$$'
    print(res1.strip('$'))   # guts
    print(real.lstrip('$'))  # guts$$
    print(real.rstrip('$'))  # $$guts
    6.切割字符串中的指定字符
    res = 'guts|123|study'
    print(res.split('|'))  # ['guts','123'.'study']  该方法的处理结果是一个列表
    name, pwd, hobby = res.split('|')
    print(res.split('|', maxsplit=1))  #['guts'.'123|study']  # 从左往右切指定个数
    print(res.rsplit('|', maxsplit=1))  #['guts|123', 'study']  # 从右往左切指定个数
    7.字符串格式花输出
    format玩法1:等价于占位符
    res = 'my name is {} my age is {}'.format('guts',123)  
	print(res)    # 大括号中的数字为.format()中的索引位置
    format玩法2:索引取值并支持反复使用
    res = 'my name is {0} my age is {1} {0} {0} {1}'.format('guts', 123)
    print(res)
    format玩法3:占位符见名知意
    res = 'my name is {name1} my age is {age1} {name1} {age1} {name1}'
    .format(name1='guts',age1=123)
    print(res)
    format玩法4:推荐使用(**********)
    name = input('username:')
    age = input('age:')
    res = f'my name is {name} my age is {age)'
    print(res)
    3.字符串需要了解的方法
    	1.大小写相关
    res = 'hElLo WorLd 666'
    print(res.upper())  # HELLO WORLD 666
    print(res.lower())  # hello world 666
    '''图片验证码:生成没有大小写统一的验证码 展示给用户看
    获取用户输入的验证码 讲用户输入的验证码和当初生成的验证码统一转大写或小写再比对
    '''
    code = 'Bja6Cc'
    print('展示给用户看到图片验证码',code)
    confirm_code = input('请输入验证码').strip()
    if confirm_codde.upper() == code.upper():
        	print('验证码正确')
    res = 'hello world'
    print(res.isupper())  # 判断字符串是否是纯大写 Flase
    print(res.islower())  # 判断字符串是否是纯小写 True
    2.判断字符串是否是纯数字
    res = ''
    print(res.isdigit())  # False
    guess_age = input('guess_age>>>:').strip()
    if guess_age.isdigit():
        guess_age = int(guess_age)
    else:
        print('年龄都不知道怎么输吗???')
    3.替换字符串中指定的内容
    res = 'my name is guts guts guts guts'
    print(res.replace('guts','tonySB'))  # my name is tonySB
    print(res.replace('guts','tonySB', 1))  # my name is tonySB guts guts guts 从左往右替换指定个数的内容
    4.字符串的拼接
    ss1 = 'hello'
    ss2 = 'world'
    print(ss1 + '$$$' + ss2)  # hello$$$world
    print(ss1 * 10)  # hellohellohellohellohellohellohellohellohellohello
    print('|'.join(['guts','123','read','DBJ']))  # guts|123|read|DBJ
    print('|',join(['guts',123]))  # 参与拼接的数据值必须是字符串
    5.统计指定字符出现的次数
    res = 'hello world'
    print(res.count('l'))  # 3
    6.判断字符串开头或者结尾
    res = 'guts say hello'
    print(res.startswith('guts'))  #  Ture
    print(res.startswith('g'))
    print(res.startswith('gut'))
    print(res.startswith('s'))  # False
    print(res.startswith('uts'))  # False
    print(res.endswith('o'))  # Ture
    print(res.endswith('llo')) 
	print(res.endswith('hello'))
    print(res.endswith('he'))  # False
    7.其他方法补充
    res = 'hello woRLd HeLLo worLD'
    print(res.title())  # Hello World Hello World
    print(res.capitalize())  # Hello world hello world
    print(res.swapcase())  # HELLO WOrlD hEllo WORld
    print(res.index('o'))  # 所选字符在字符串中的数量
    print(res.find('o'))
    print(res.index('c'))  # 找不到直接报错
    print(res.find('c'))  # 找不到默认返回-1
    print(res.find('lo'))  # 索引字符串在字符串中的位置

列表内置方法及操作

1.类型转换
	list(其他数据类型)
    ps:能够被for循环的数据类型都可以转成列表
    print(list('hello'))
    print(list({'name':'guts','pwd':123}))
    print(list((1,2,3,4)))
    print(list({1,2,3,4,5}))
    
2.需要掌握的方法
	l1 = [111, 222, 333, 444, 555, 666, 777]
    1.索引取值(正负数)
    print(l1[0])  # 111
    print(l1[-1])  # 777
    2.切片操作   与字符串讲解操作一致
    print(l1[0:5])  # [111, 222, 333, 444, 555]
    print(l1[:])  
    3.间隔数 方向   与字符串讲解操作一致
    print(l1[::1])
    4.统计列表中数据值的个数
    print(len(l1))  # 7
    5.数据值修改
    l1[0] = 123  # 按照索引位置    
    print(l1)
    6.列表添加数据值
    方式1:尾部追加数据值
    l1.append('干饭')
    print(l1)  # [123, 222, 333, 444, 555, 666, 777, '干饭']
    l1.append(['guts','jason','kevin','jerry'])
    print(l1) # [123, 222, 333, 444, 555, 666, 777, ['guts', 'jason', 'kevin', 'jerry']]
    方式2:扩展列表 合并列表
    ll1 = [11,22,33]
    ll2 = [44,55,66]
    print(ll1+ll2)  # [11, 22, 33, 44, 55, 66]
    ll1,extend(112)  # for循环+append
    print(ll1)  # [11, 22, 33, 44, 55, 66]
    for i in ll2:
    	ll1.append(i)
    print(ll1)  # [11, 22, 33, 44, 55, 66]
    7.删除列表数据
    方式1:通用的删除关键字del
    del l1[0]
    print(l1)
    方式2:remove
    l1.remove(444)  # 括号内填写数据值
    print(l1)
    方式3:pop
    l1.pop(3)  # 括号内填写索引值
    print(l1)
    l1.pop()  # 默认尾部弹出数据值
    print(l1)
    res = l1.pop(3)
    print(res)  # 444
    res1 = l1.remove(444)  # 直接销毁
    print(resl)  #None
    8.排序
    ss = [54, 99, 55, 76, 12, 43, 76, 88, 99, 100, 33]
    ss.sort()  # 默认是升序
    print(ss)
    ss.sort(reverse = True)
    print(ss)  # 改为降序
    9.统计列表中某个数据值出现的次数
    print(l1.count(111))
    10.颠倒列表顺序
    l1.reverse()
    print(l1)

可变类型与不可变类型

	s1 = '$$guts$$'
	l1 = [11,22,33]
	s1.strip('$')
	print(s1)  # $$guts$$
	'''字符串在调用内置方法之后并不会修改自己 而是产生了一个新的结果
	如何查看调用方法之后有没有新的结果 可以在调用该方法的代码左侧添加变量名和赋值符号
	res = s1.strip('$')
	'''
	ret = l1.append(44)
    print(l1)  # [11,22,33,44]
    print(ret) # None
    '''列表在调用内置方法之后修改的就是自身 并没有产生一个新的结果'''
   
    可变类型:值改变 内存地址不变
    l1 = [11, 22, 33]
    print(l1)  # [11, 22, 33]
    print(id(l1))  # 1470055892864
    l1.append(44)
    print(l1)  # [11, 22, 33, 44]
    print(id(l1))  # 1470055892864
    
    不可变类型:值改变 内存地址肯定变
    res = '$$hello world$$'
    print(res)  # $$hello world$$
    print(id(res))  # 2089978428912
    res1 = res.strip('$')
    print(res1)  # hello 
    print(id(res1))  # 2089979738160
    
    
    
    

作业

image
image
image

posted @ 2022-09-29 21:45  理塘丁真1!5!  阅读(25)  评论(0编辑  收藏  举报