Python学习笔记(五):数字、字符串、列表、元组

Python学习笔记

字符类型

1,可变、不可变 类型
		可变:值发生改变,但是内存地址不变。证明就是在改变原值,即原值可变。
		不可变:值发生改变,同时内存地址也发生改变。证明不是在改变原值,而是产生了新的值。即原值不可变。

2,int 数字类型
		age = int(18)
		一个值对应一个内存地址
		不可变

3,float 浮点类型
		price = float(3.33)
		一个值对应一个内存地址
		不可变

4,string 字符串类型
		str = string([1,2,3]) # "[1,2,3]"
		特点:有序、可变
		1)按索引取值:下标从0开始
				msg = "hello world"
				print(msg[4]) # o
				print(msg[-1]) # d -1表示最后一位
		2)切片:顾头不顾尾,步长
				msg = "hello world"
				res = msg[1:5:2] # el  获取msg中下标为1到5的数据,间隔2位,即获取下标为1和3的字符
				res = msg[1:4] # ell 获取msg中下标为1到4的数据,不取尾
				res = msg[1:]  # ello world
				res = msg[:]  # hello world
				res = msg[-1:-4:-1]  # dlr -1 -2 -3 
				res = msg[-1::-1]  # dlrow olleh
				res = msg[::-1]  # 将字符串颠倒
		3)len(msg) # 获取字符串长度
		4)成员运算
				in  和 not in
		5)移除空白。 string.strip()
				name = "   name   "
				print(name.strip())
		
				name = "**name to *"
				print(name.strip('*').replace(" ", ""))

				msg = "*(&%&%&_+)**&^e*gon*)(^%$$&$"
				print(msg.strip('*()&%_+^%$'))  # e*gon		

				x = 'a c d e'
				print(x.replace(' ','',1))  # ac d e
				print(x.replace(' ',''))   # acde
		6)切分  string.split('')
				把一个字符串按照某种分隔符切分成一个列表
						msg = "name:age:sex"
						res = msg.split(':') # ['name', 'age', 'sex']
						res = msg.split(':',1) # ['name', 'age:sex']
		7)把列表中的元素按照某种分隔符拼接成字符串
				list = ['name', 'age', 'sex']
				str = ':'.join(list)
		8)循环 for
		9)函数扩展:
				字符串移除:strip()、lstrip()、rstrip()
				转换大小写:lower()、upper()
				判断开头、结尾:startswith,endswith
					# print('hello world'.startswith('he'))		
					# print('hello world'.endswith('d'))
				切分:split,rsplit
					msg = 'egon:18:male'
					print(msg.split(':',1)) # ['egon', '18:male']
					print(msg.rsplit(':',1)) # ['egon:18', 'male']
				判断是否是数字:string.isdigit()
				
				find,rfind,index,rindex,count
					msg = 'hello egon ahahah egon xxx egon'
					# print(msg.find('egon1'))
					# print(msg.index('egon1'))
					# print(msg.rfind('egon'))
					
				center,ljust,rjust,zfill
					# print('egon'.center(50,'*'))
					# print('egon'.ljust(50,'*'))
					# print('egon'.rjust(50,'*'))
					# print('egon'.rjust(50,'0'))
					# print('egon'.zfill(50))
					
				captalize,swapcase,title
					# print('abcd'.capitalize())
					# print('AbCd'.swapcase())
					# print('my name is egon'.title())

				is数字系列
					# print('18'.isdigit())
					# print('Ⅳ'.isnumeric())

				is其他
					name='egon'
					print(name.isalnum()) #字符串由字母或数字组成
					# print(name.isalpha()) #字符串只由字母组成
					# print(name.islower())
					# print(name.isupper())
					name=' '
					# print(name.isspace())
					name = 'My Is Egon'
					# print(name.istitle())
				
		10)format 的三种用法
				print("my name is %s my age is %s" % ('egon', 18))
				print("my name is {name} my age is {age}".format(age=18,name='egon'))
				print("my name is {} my age is {}".format(18,'egon'))
				print("my name is {1} my age is {0}{0}{0}".format(18,'egon'))

				x = 'egon'
				y = 18
				print(f'my name is {x} ,my age is {y}')

5,list 列表
		特点:有序、可变
		arr = list("hello")
		print(arr)  # ['h', 'e', 'l', 'l', 'o']
	
		1)按索引取值
				print(arr[1])
				print(id(arr))
		2)切片:顾头不顾尾,步长
				print(arr[1:4:2)
		3)长度:len(arr)
		4)成员运算
				in  和  not in
		5)追加
				arr.append(88)
				arr.insert(99)
		6)删除
				1、万能删除
						del arr[1]
				2、arr.remove(‘o') # 移除列表arr中的数据 ‘o'
				3、res = arr.pop(1) # 取出列表arr中下标为1的元素
		7)循环 for
		8)相关函数
				l = [111,222,333,444,333,555,666,777]
				# print(l.index(333))
				# print(l.index(9999))
				# print(l.count(333))
				# l.reverse() # 倒序
					# print(l)
				# l.sort(reverse=True)
					# print(l)
				# l = [111, -3, 99, 27]
				# nums = [333,444,555]
				# # l.append(nums)
				# l.extend(nums)
					# print(l)
				# new_l = l.copy()

6,元组
		特点:有序、不可变
		1)类型转换:
				print(tuple("hello"))  # ('h','e','l','l','o')
		2)函数操作
				1、按索引取值:只能取值,不能赋值
				2,切片
				3,长度
				4,成员运算
						in  和 not in
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值