Python基础 之 数据类型
数据类型
一、运算符
算数运算
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('')
else:
print('') #"伊尧"字符不在name中,print输出为1
if "伊尧" not in name:
if 1 == 1:
if 2 > 2:
# 以上三条均是真假布尔值
关于真假布尔值
v = 1 == 2
print(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 == "" and 1==2
print(v) #先计算括号内的
#遇到如上情况,从前到后执行
#结果True,后面还有or,直接为True
#如果True,后面还有and,则继续运算
#如果False,后面还有or,则继续运算
#如果False,后面还有and,直接为False
二、基本数据类型
数字 int
a1 = 123
a1 = 456
将字符串转换为数字
a = ""
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
#
转换
capitalize
首字母大写
test = "evEn"
v = test.capitalize()
print(v)
#Even
capitalize
casefold 和 lower
casefold使用功能更强大,很多未知的语言对应关系也可以变小写
test = "evEn"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
#even
#even
casefold 和 lower
swapcase
将小写转成大写,大写转成小写
test = "eVen"
v = test.swapcase()
print(v)
#EvEN
swapcase
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)
#================even
rjust
count
去字符串中寻找,寻找子序列的出现次数
test = "evEnevenr"
v = test.count('ev')
print(v)
# test = "evEnevenr"
v = test.count('ev',5,6)
# 从5开始,直至6结束,在这之间寻找子序列的出现次数
print(v)
#
count
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"
# 未找到 -1
v = test.find('en')
print(v)
#
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 20
format
按照位置替换为指定的值
test = 'i am {0}, age {1}'
print(test)
v = test.format('even',20)
print(v)
#i am {name}, age {a}
#i am even, age 20
format
格式化,传入的值 {"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 20
format_map
判断类型
isalnum
判断字符串中是否只包含 字母和数字
test = ""
v = test.isalnum()
print(v)
#True
isalnum
isalpha
判断是字母或汉字
test1 = "as2df"
test2 = "asdf"
v1 = test1.isalpha()
v2 = test2.isalpha()
print(v1)
print(v2)
#False
#True
isalpha
isdecimal isdigit isnumeric
判断当前输入是否是数字
test = "二" # 1,②
v1 = test.isdecimal() #常用
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
#False False True
isdecimal
isprintable
判断是否存在不可显示的字符
# \t 制表符
# \n 换行
test = "oiuas\tdfkj"
v = test.isprintable()
print(v)
#False
isprintable
isspace
判断是否全部是空格
test1 = ""
test2 = "eve n"
test3 = " "
v1 = test1.isspace()
v2 = test2.isspace()
v3 = test3.isspace()
print(v1,v2,v3)
#False False True
isspace
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!
#True
istitle title
islower isupper 和 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('')
print(v1)
print(v2)
#True
#False
startswith endswith
isidentifier
判断是否是有效标识符
a = "def"
b = ""
v1 = a.isidentifier()
v2 = b.isidentifier()
print(v1,v2)
#True False
isidentifier
join
将字符串中的每一个元素按照指定分隔符进行拼接
test = "伊文尧"
print(test)
v1 = ' '.join(test)
v2 = "_".join(test)
print(v1)
print(v2)
#伊文尧
#伊 文 尧
#伊_文_尧
strip lstrip 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 = "" v = "asidufkasd"
m = str.maketrans("aeiou", "") #构建对应关系
new_v = v.translate(m) #根据对应关系进行替换
print(new_v)
#1s3d5fk1sd
maketrans translate
分割
partition rpartition
分割为三部分
test = "testasdsddfg"
v = test.partition('s') #按照s,做分割
print(v)
v = test.rpartition('s') #按照s,从右开始做分割
print(v)
('te', 's', 'tasdsddfg')
('testasd', 's', 'ddfg')
split rsplit
分割为指定个数 但匹配值会丢失
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替换成xx
print(v)
#rxxtrxxtrxxt v = 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.for循环
for 变量名 in 字符串:
一样支持break 和 continue
使用while循环对一条语句进行纵向单个输出
test = "今天天气不错"
index = 0
while index < len(test): #len获取长度
v = test[index] #获取索引
print(v)
index = index + 1
print('=======')
#今
#天
#天
#气
#不
#错
#=======
使用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连续输出,设置步长为5 for item in v:
print(item)
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
range(0,100,5)
最新文章
- 欧几里德算法 GCD
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
- C# chart,有关如何在鼠标移动到Series上时显示节点及数据 (有待继续更新)
- Yii::app()方法详解
- Android 之 SharedPreferences
- javaweb中去除某个get方式的参数,并且返回路径
- google base 之MessagePumpForUI
- js登录滑动验证,不滑动无法登陆
- 封装Thread的两种方法 via C++ in Linux
- MIME 类型
- 模式PK:命令模式VS策略模式
- [转帖] mysql 用户 权限 密码等操作记录
- 关于C#线程,线程池和并行运算的简单使用和对比
- java设计模式(五)责任链模式
- Dubbo 管理控制台安装
- Java虚拟机(一)之开篇
- TMS320C6657双核DSP的图像处理系统开发(1):硬件相关tips
- webservice 创建及调用
- [NOI2017]蔬菜 贪心
- win7 VC6.0 安装 fatal error C1083: Cannot open include file: &#39;Iphlpapi.h&#39;: No such file or directory