Python基础 之 数据类型,,一、运算符算数运算a
Python基础 之 数据类型,,一、运算符算数运算a
一、运算符
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1
布尔值:True 真
False 假
if True: pass while True: pass v = not Trure #False
成员运算
a = "文" in "伊文尧"
"伊文尧" 字符串"伊" 字符"伊文" 建文 子字符串,子序列# ctrl + ? 在Pychram中使用该操作进行整体注释
name = "伊文尧"if "伊文" in name: print(‘OK‘)else: print(‘Error‘)if "伊尧" not in name: print(‘1‘)else: print(‘2‘)#"伊尧"字符不在name中,print输出为1
if "伊尧" not in name:if 1 == 1:if 2 > 2:# 以上三条均是真假布尔值关于真假布尔值
v = 1 == 2print(v)#直接对真假进行输出
比较运算
a = 1 > 5
逻辑运算
a = 1 > 6 or 1==1
user = "root"pwd = "root!23"v = user == "root" and pwd == "root!23" or 1 == 1 and pwd == "99854" and 1==2print(v)#先计算括号内的#遇到如上情况,从前到后执行#结果True,后面还有or,直接为True#如果True,后面还有and,则继续运算#如果False,后面还有or,则继续运算#如果False,后面还有and,直接为False
二、基本数据类型
数字 int
a1 = 123
a1 = 456
将字符串转换为数字
a = "123"print(type(a),a) #输出一个值,并表示类型b = int(a)print(type(b),b) #输出一个值,并表示类型num = "B" v = int(num, base=16)print(v)#<class ‘str‘> 123#<class ‘int‘> 123#11转换
capitalize
首字母大写
test = "evEn"v = test.capitalize() print(v)#Evencapitalize
casefold 和 lower
casefold使用功能更强大,很多未知的语言对应关系也可以变小写
test = "evEn"v1 = test.casefold()print(v1)v2 = test.lower()print(v2)#even#evencasefold 和 lower
swapcase
将小写转成大写,大写转成小写
test = "eVen"v = test.swapcase()print(v)#EvENswapcase
center 和 just
设置宽度 center
test = "even"v = test.center(20,"=")# 20 代指总长度# = 代表填充,只能填一个字符,可有可无print(v)#========evEn========center
填充功能 ljust
test = "even"v = test.ljust(20,"=") #把内容放置到左侧,右侧为填充print(v)#even================ljust
填充功能 rjust
test = "even"v = test.rjust(20,"=") #把内容放置到右侧,左侧为填充print(v)#================evenrjust
count
去字符串中寻找,寻找子序列的出现次数
test = "evEnevenr"v = test.count(‘ev‘)print(v)#2test = "evEnevenr"v = test.count(‘ev‘,5,6)# 从5开始,直至6结束,在这之间寻找子序列的出现次数print(v)#0count
encode 和 decode 暂不讨论
expandtabs
断句,制表
test = "username\temail\tpassword\nroot\troot@!23\t123\nroot\troot@!23\t123\nroot\troot@!23\t123"v = test.expandtabs(20) #test数字符20个,不足则补全20个print(v)#username email password#root root@!23 123#root root@!23 123#root root@!23 123
find
从开始往后找,找到第一个之后,获取其位置
test = "evenEven"# 未找到 -1v = test.find(‘en‘)print(v)#2
format
格式化,将一个字符串中的占位符{ }替换为指定的值
test = ‘i am {name}, age {a}‘print(test) v = test.format(name=‘even‘,a=20)print(v)#i am {name}, age {a}#i am even, age 20format
按照位置替换为指定的值
test = ‘i am {0}, age {1}‘print(test)v = test.format(‘even‘,20)print(v)#i am {name}, age {a}#i am even, age 20format
格式化,传入的值 {"name": ‘alex‘, "a": 19},字典相关
test = ‘i am {name}, age {a}‘v1 = test.format(name=‘Q‘,a=10)v12 = test.format_map({"name": ‘even‘, "a": 20})print(v1)print(v2)#i am Q, age 10#i am even, age 20format_map
判断类型
isalnum
判断字符串中是否只包含 字母和数字
test = "123"v = test.isalnum()print(v)#Trueisalnum
isalpha
判断是字母或汉字
test1 = "as2df"test2 = "asdf"v1 = test1.isalpha()v2 = test2.isalpha()print(v1)print(v2) #False#Trueisalpha
isdecimalisdigitisnumeric
判断当前输入是否是数字
test = "二" # 1,②v1 = test.isdecimal() #常用v2 = test.isdigit()v3 = test.isnumeric()print(v1,v2,v3)#False False Trueisdecimal
isprintable
判断是否存在不可显示的字符
# \t 制表符# \n 换行test = "oiuas\tdfkj"v = test.isprintable()print(v)#Falseisprintable
isspace
判断是否全部是空格
test1 = ""test2 = "eve n"test3 = " "v1 = test1.isspace()v2 = test2.isspace()v3 = test3.isspace()print(v1,v2,v3)#False False Trueisspace
istitle 和title
istitle判断是否是标题(每个单词首字母大写),title负责将其转换成标题
test = "Even Is A good boy!"v1 = test.istitle()print(v1)v2 = test.title()print(v2)v3 = v2.istitle()print(v3)#False#Even Is A Good Boy!#Trueistitle title
islowerisupper 和 lower upper
islower lower 判断是否全部是小写 和 全部转换小写
test = "Even"v1 = test.islower()v2 = test.lower()print(v1, v2)#False even
isupper upper判断是否全部是大写 和 全部转换大写
test = "Even"v1 = test.isupper()v2 = test.upper()print(v1,v2)#False EVEN
startswith 和endswith
判断是否以xxx开头,以xxx结尾
test = "ip 127.0.0.1"v1 = test.startswith(‘i‘)v2 = test.endswith(‘255‘)print(v1)print(v2)#True#Falsestartswith endswith
isidentifier
判断是否是有效标识符
a = "def"b = "123"v1 = a.isidentifier()v2 = b.isidentifier()print(v1,v2)#True Falseisidentifier
join
将字符串中的每一个元素按照指定分隔符进行拼接
test = "伊文尧"print(test)v1 = ‘ ‘.join(test)v2 = "_".join(test)print(v1)print(v2)#伊文尧#伊 文 尧#伊_文_尧
striplstrip rstrip
移除空白字符 (除此之外还能去除\t \n)
test = " even " v1 = test.lstrip() #去除左空白v2 = test.rstrip() #去除右空白v3 = test.strip() #去除左右空白 print(v1)print(v2)print(v3)#even # even#even
移除指定字符串(优先最多匹配)
test = "even"v1 = test.lstrip(‘ev‘)v2 = test.rstrip(‘mnvzev‘)v3 = test.strip(‘ev‘)print(v1)print(v2)print(v3)#n##n
maketrans 和translate
构建对应关系 和 替换
test = "aeiou"test1 = "12345"v = "asidufkasd"m = str.maketrans("aeiou", "12345") #构建对应关系new_v = v.translate(m) #根据对应关系进行替换print(new_v)#1s3d5fk1sdmaketrans translate
分割
partitionrpartition
分割为三部分
test = "testasdsddfg"v = test.partition(‘s‘) #按照s,做分割print(v)v = test.rpartition(‘s‘) #按照s,从右开始做分割print(v)(‘te‘, ‘s‘, ‘tasdsddfg‘)(‘testasd‘, ‘s‘, ‘ddfg‘)
splitrsplit
分割为指定个数 但匹配值会丢失
test = "testasdsddfg"v = test.split(‘s‘,2) #按照s,做两刀分割print(v)v = test.rsplit(‘s‘,2) #按照s,从右做两刀分割print(v)#[‘te‘, ‘ta‘, ‘dsddfg‘]#[‘testa‘, ‘d‘, ‘ddfg‘]
splitlines
只能根据True,False:选择是否保留换行
test = "abc\nabc\nabc"v = test.splitlines(False)print(v)#[‘abc‘, ‘abc‘, ‘abc‘]
replace
将指定字符串替换为指定字符串
test = "rootrootroot"v = test.replace("oo",‘xx‘) #将oo替换成xxprint(v)#rxxtrxxtrxxtv = test.replace("oo",‘xx‘,2) #将oo替换成xx,只限于前2个print(v)#rxxtrxxtroot
总而言之
7个 十分重要的基本数据类型
# join # split# find# strip# upper# lower# replace
三、其他补充
1.索引
获取字符串中的某一个字符,0为第一个字符
test = "even"v = test[2] #获取第3个字符print(v)#e
2.切片
获取字符串中的范围
test = "evenyao"v = test[1:-1] #获取第2个字符至倒数第2个字符中的所有字符print(v)#venya
3.获取长度
Python3: len获取当前字符串中由几个字符组成
test = "even"v = len(test)print(v)#4
4.for循环
for 变量名 in 字符串:
一样支持break 和 continue
使用while循环对一条语句进行纵向单个输出
test = "今天天气不错"index = 0while index < len(test): #len获取长度 v = test[index] #获取索引 print(v) index = index + 1print(‘=======‘)#今#天#天#气#不#错#=======
使用for循环进行一样的实现
test = "今天天气不错"for item in test: print(item)print(‘=======‘)#今#天#天#气#不#错#=======
for中的break和continue
test = "今天天气不错"for item in test: print(item) break#今#break
test = "今天天气不错"for item in test: continue print(item)#continue
5.获取连续或不连续的数字
Python2中立即创建在内容中
Python3中只有for循环时,才一个一个创建
r1 = range(10)r2 = range(1,10)r3 = range(1,10,2)#帮助创建连续的数字,通过设置步长来指定不连续
设置步长为5,并输出0-99的数字
v = range(0, 100, 5) #0-100连续输出,设置步长为5for item in v: print(item)# 0# 5# 10# 15# 20# 25# 30# 35# 40# 45# 50# 55# 60# 65# 70# 75# 80# 85# 90# 95range(0,100,5)
Python基础 之 数据类型
相关内容
- python---基础知识回顾(九)图形用户界面,,主要使用
- python 命名元组(namedtuple),,我们知道c/c++语
- python调用操作系统的库函数,,调用操作系统的库函数
- python解析PDF文档,,1.安装pip in
- python字典操作方法详解,,前言字典是一种通过名
- 【python】Python3中出现'gbk' codec can't
- Python vue坏境搭建及项目创建,,vue项目坏境搭建n
- 如何为 Python 基本类型自定义方法?,
- Python绘制语谱图+时域波形,, 1 """Pyth
- Python对JSON数据的解析,什么是大数据分析,1.python与j
评论关闭