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)
Python基础 之 数据类型的更多相关文章
- Python基础之数据类型
		
Python基础之数据类型 变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值 ...
 - 第二章:python基础,数据类型
		
"""第二章:python基础,数据类型2.1 变量及身份运算补充2.2 二进制数2.3 字符编码每8位所占的空间位一个比特,这是计算机中最小的表示单位.每8个比特组成一 ...
 - python基础一数据类型之字典
		
摘要: python基础一数据类型之一字典,这篇主要讲字典. 1,定义字典 2,字典的基础知识 3,字典的方法 1,定义字典 1,定义1个空字典 dict1 = {} 2,定义字典 dict1 = d ...
 - 第一节 Python基础之数据类型(整型,布尔值,字符串)
		
数据类型是每一种语言的基础,就比如说一支笔,它的墨有可能是红色,有可能是黑色,也有可能是黄色等等,这不同的颜色就会被人用在不同的场景.Python中的数据类型也是一样,比如说我们要描述一个人的年龄:小 ...
 - python基础一数据类型之集合
		
摘要: python基础一中介绍数据类型的时候有集合,所以这篇主要讲集合. 1,集合的定义 2,集合的功能 3,集合的方法 1,集合的定义 list1 = [1,4,5,7,3,6,7,9] set1 ...
 - python基础一数据类型之元祖
		
摘要: python基础一中写到数据类型元祖,那么这篇主要讲元祖. 1,元祖定义 tuple1 = (1,2,'a','b') 元祖是不可变数据,所以又名只读列表.那么如何让是元祖可变呢?可以在元祖中 ...
 - python基础一数据类型之列表
		
摘要: python基础一中写到列表,那么这篇主要讲列表. 1,定义列表 2,列表.元祖.字符串都属于序列,都可以用用索引和切片. 3,列表的方法 1,定义列表 list1 = ['a','b',1, ...
 - Python基础一数据类型之数字类型
		
摘要: python基础一中提到了数据类型,这里主要讲解的是数字类型. 数字类型: 1,整型 2,长整型 3,浮点型 4,复数型 1,整型(int) 定义a = 1 通过type函数查看数据类型,整型 ...
 - python基础(二)----数据类型
		
Python基础第二章 二进制 字符编码 基本数据类型-数字 基本数据类型-字符串 基本数据类型-列表 基本数据类型-元组 可变.不可变数据类型和hash 基本数据类型-字典 基本数据类型-集合 二进 ...
 - Python学习day04 - Python基础(2)数据类型基础
		
<!doctype html>day04 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...
 
随机推荐
- Mybatis resultMap灵活用法(使用子查询)
			
### 背景查询广州每个景点的总流量,和每个景点每日流量 #### 数据表 t_广州|唯一标识id|地点place|流量counts|日期date||:---:|:---:|:---:|:---:|| ...
 - MySQL数据库修改数据表类型(引擎)的方法
			
MySQL数据库使用事务,相关数据表必须为InnoDB引擎 查看数据表状态: SHOW TABLE STATUS FROM wawa WHERE NAME='ww_invite_code_temp'; ...
 - OAuth2.0 与 oauth2-server 库的使用
			
作者:baiyi链接:https://www.jianshu.com/p/83b0f6d82d6c來源:简书 OAuth2.0 是关于授权的开放网络标准,它允许用户已第三方应用获取该用户在某一网站的私 ...
 - Active Job 基础
			
开发中涉及到调用三方服务API,运行时间长,结果不需要实时反馈给用户这样的任务,都可以使用异步处理.常见的场景包括:发邮件和短信.图片处理.定时清理等.爬虫. 后端处理软件可以自行选择这里选择了sid ...
 - 从零开始一个http服务器(一)-开始
			
从零开始一个http服务器 (一) 代码地址 : https://github.com/flamedancer/cserver git checkout step1 一个简单的socket serve ...
 - C语言变量的初始化
			
关于C语言变量是否需要初始化的问题.以前西北工业大学的C语言老师说的是,需要初始化,如果不初始化就使用的话,变量的值是以前遗留在内存中的,是不确定的(这只是针对局部变量的).C语言全局变量如果没有初始 ...
 - R语言爬虫:CSS方法与XPath方法对比(代码实现)
			
CSS选择器和XPath方法都是用来定位DOM树的标签,只不过两者的定位表示形式上存在一些差别: CSS 方法提取节点 library("rvest") single_table_ ...
 - LeetCode初级算法的Python实现--数组
			
LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...
 - nmap教程(上)
			
一.nmap的主要功能 1.端口扫描 2.主机探测:查找目标网络的在线主机 3.服务/版本检测:发现开放端口后,进一步检测目标主机的检测服务协议.应用程序名称.版本号等信息 4.操作系统检测 5.网络 ...
 - 成都优步uber司机第五组奖励政策
			
7月14日,成都优步uber团队发布了第五组用户分组.在传言要推出第四组的时候,心想事不过三吧,意外,现在第五组都出来了.一起看看成都优步司机第五组的详细内容!滴滴快车单单2.5倍,注册地址:http ...