Python数据类型补充1
一、可变和不可变类型
可变类型: 值变了,但是id没有变,证明没有生成新的值而是在改变原值,原值是可变类型
不可变类型:值变了,id也跟着变,证明是生成了新的值而不是在改变原值,原值是不可变
# x=10
# print(id(x))
# x=11
# print(id(x)) y=['a','b','c']
print(id(y))
y[0]='A'
print(y)
print(id(y))
二、数字类型
# 其他进制=>十进制
# 十进制: 0-9
# 11 = 1*10^1 + 1*10^0 # 二进制: 0 1
# 11 = 1*2^1 + 1*2^0 # 八进制: 0-7
# 11 = 1*8^1+1*8^0 # 十六进制:0-9 A-F
# 11 = 1*16^1+1*16^0 # 十进制=>其他进制
print(bin(13)) # 十进制=>二进制 ob1101 print(oct(13)) # 十进制=>八进制 Oo15
print(hex(13)) # 十进制=>十六进制 Oxd
三、字符串
# 可以将任意类型转换成字符串
# str(1)
# str(1.3)
# x=str([1,2,3])
# print(x,type(x))
常用操作+内置的方法
优先掌握的操作:
1、按索引取值(正向取+反向取) :只能取
msg='hello world'
# print(msg[0])
# print(msg[5])
# print(msg[len(msg)-1])
# print(msg[-1])
# msg[0]='H'
2、切片(顾头不顾尾,步长): 想要从一个大字符串中切出一个小字符串
# msg='hello world'
# print(msg[0:5])
# print(msg)
# print(msg[0:5:2])
3、长度len
# msg='你好啊a'
# print(len(msg))
4、成员运算in和not in
msg='yangyuanhu 老师是一个非常虎的老师'
# print('yangyuanhu' in msg)
# print('虎' not in msg)
# print(not '虎' in msg)
5、移除字符串左右两边的字符strip:默认去空格
# pwd=' 1 23 '
# res=pwd.strip(' ')
# print(res) # pwd=input('>>: ').strip() #pwd='123'
#
# if pwd == '123':
# print('密码输入正确') # pwd='******12*3****'
# print(pwd.strip('*'))
6、切分split:针对有规律字符串按照某个字符切成列表
# info='yyhdsb|18|female'
# li=info.split('|',1) #只按照第一个分隔符切
# print(li)
7、循环
# msg='hello'
#
# for item in msg:
# print(item)
Conclusion:
存一个值 有序 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
s1='hello'
print(id(s1))
s1='world'
print(id(s1)) 需要掌握的操作(****)
1、strip,lstrip,rstrip
print('****egon***'.strip('*'))
print('****egon***'.lstrip('*'))
print('****egon***'.rstrip('*'))
2、lower,upper
print('AAAbbbb'.lower())
print('AAAbbbb'.upper()) 3、startswith,endswith
print('alex is sb'.startswith('alex'))
print('alex is sb'.endswith('sb')) 4、format的三种玩法
print('my name is %s my age is %s' %('egon',18))
print('my name is %s my age is %s' %(18,'egon')) 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 {0} my age is {1} '.format(18,'egon'))
print('my name is {1} my age is {0} '.format(18,'egon')) 5、split,rsplit
msg='a:b:c:d:e'
print(msg.split(':',1))
print(msg.rsplit(':',1)) 6、join
msg='a:b:c:d:e'
list1=msg.split(':')
msg1=':'.join(list1)
print(msg1) info='egon:123:male'
list1=info.split(':')
print(list1) print(':'.join(list1)) 7、replace
msg='alex is alex alex is hahahaha'
print(msg.replace('alex','SB',1)) 8、isdigit
print(''.isdigit()) # 只能判断纯数字的字符串
print('12.3'.isdigit()) age_of_db=30
inp_age=input('>>>: ').strip()
if inp_age.isdigit():
inp_age=int(inp_age)
if inp_age > age_of_db:
print('too big')
elif inp_age < age_of_db:
print('too small')
else:
print('you got it') 其他操作1、find,rfind,index,rindex,count
msg='hello worldaa'
print(msg.index('wo'))
print(msg.index('wo',0,3))
print(msg.find('wo',0,3))
print(msg.find('xxxxxxx'))
print(msg.index('xxxxxxx'))
print(msg.count('l')) 2、center,ljust,rjust,zfill
name=input('>>: ').strip()
print('egon'.center(50,'='))
print(('%s' %name).center(50,'-')) print('egon'.ljust(50,'='))
print('egon'.rjust(50,'='))
print('egon'.zfill(50)) 3、expandtabs
print('hello\tworld'.expandtabs(5)) 4、captalize,swapcase,title
print('hello world'.capitalize())
print('Hello world'.swapcase())
print('Hello world'.title()) 5、is数字系列
num1=b'' #bytes
num2=u'' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字 isdigit: bytes,str
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit()) isdecimal:str
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal()) isnumberic:str,中文\罗马
print(num2.isnumeric())
print(num3.isnumeric())
print(num4.isnumeric()) 6、is其他
print('aaasdfaA'.isalpha()) # 纯字母组成的字符串
print('aaasdfaA123'.isalnum()) # 字母或数字组成
print('aaasdfaA'.isalnum()) # 字母或数字组成
Python数据类型补充1的更多相关文章
- PYTHON 100days学习笔记007-2:python数据类型补充(2)
目录 day007:python数据类型补充(2) 1.Python3 元组 1.1 访问元组 1.2 删除元组 1.3 元组运算符 1.4 元组索引,截取 1.5 元组内置函数 2.python3 ...
- PYTHON 100days学习笔记007-1:python数据类型补充(1)
目录 day007:python数据类型补充(1) 1.数字Number 1.1 Python 数字类型转换 1.2 Python 数字运算 1.3 数学函数 1.4 随机数函数 1.5 三角函数 1 ...
- python数据类型补充
四.元组 #为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,3],'sss',(1,2)) #t=tuple((1,[1,3],'sss',(1,2))) # print(t ...
- Python数据类型补充2
四.列表 常用操作+内置的方法: 1.按索引存取值(正向存取+反向存取):即可存也可以取 # li=['a','b','c','d'] # print(li[-1]) # li[-1]='D' # p ...
- 7.Python初窥门径(数据类型补充,操作及注意事项)
python(数据类型补充,转换及注意事项) 数据类型补充 str str.capitalize() 首字母大写 str.title() 每个单词首字母大写 str.count() 统计元素在str中 ...
- python之数据类型补充、集合、深浅copy
一.内容回顾 代码块: 一个函数,一个模块,一个类,一个文件,交互模式下,每一行就是一个代码块. is == id id()查询对象的内存地址 == 比较的是两边的数值. is 比较的是两边的内存地址 ...
- python基础数据类型补充
python_day_7 一. 今日主要内容: 1. 补充基础数据类型的相关知识点 str. join() 把列表变成字符串 列表不能再循环的时候删除. 因为索引会跟着改变 字典也不能直接循环删除.把 ...
- Python基础数据类型补充及深浅拷贝
本节主要内容:1. 基础数据类型补充2. set集合3. 深浅拷贝主要内容:一. 基础数据类型补充首先关于int和str在之前的学习中已经讲了80%以上了. 所以剩下的自己看一看就可以了.我们补充给一 ...
- python基础(9):基本数据类型四(set集合)、基础数据类型补充、深浅拷贝
1. 基础数据类型补充 li = ["李嘉诚", "麻花藤", "⻩海峰", "刘嘉玲"] s = "_&qu ...
随机推荐
- eclipse xml 文件添加注解快捷键
eclipse xml 文件注解快捷键: <!-- --> Ctrl + shift + / 添加注解 Ctrl + shift + \ 取消注解
- The Little Prince-12/12
The Little Prince-12/12 双十二,大家有没有买买买呢?宝宝双十一之后就吃土了,到现在,叶子都长出来了!!! 当你真的喜欢一个人的时候 就会想很多 会很容易办蠢事 说傻话 小王子要 ...
- word2vec原理(一) CBOW与Skip-Gram模型基础——转载自刘建平Pinard
转载来源:http://www.cnblogs.com/pinard/p/7160330.html word2vec是google在2013年推出的一个NLP工具,它的特点是将所有的词向量化,这样词与 ...
- Spring 注入的两种方式
Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...
- 注册页面的JSON响应方式详细分析(与前端页面交互方式之一)
控制器层 需求分析: 访问路径:`/user/reg.do` //自己根据功能需求设定的请求参数:`username=xx&password=xx&&phone=xx& ...
- Python调用大漠插件
Python版本要用32位的?我去官网下载,太慢了,就在腾讯软件里面下载了一个,结果实验成功 import win32com.client dm = win32com.client.Dispatch( ...
- php 接收blob数据流,base64数据流 转为 blob二进制数据流
php正常接收参数的方式如下:$_GET$_POST$_REQUEST 但是如果跨语言接收请求参数的话,可能会出现一系列的问题,其他语言的http请求可能是基于数据流的概念来传递参数的,如果按照常规处 ...
- Mysql安装错误:Install/Remove of the Service Denied!解决办法
Mysql安装错误:Install/Remove of the Service Denied!解决办法 在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld -- ...
- ACM札记
1. 逗号表达式 在“计蒜客“的ACM教程中,看到这样一段很好的代码: int n; while (scanf("%d", &n), n) { //do something ...
- 一图解释PHPstorm代码片段设置---附官方文档(转)
参考:https://blog.csdn.net/thinkthewill/article/details/81145106 资料 设置片段[官方设置教程] 设置变量对话框[官方设置教程] phpst ...