目录
1、基础数据类型概览
1)int 数字型:主要用于计算
2)str 字符串:存少量数据
3)bool:反馈真、假
4)list 列表:[ ] 可存各种数据类型,操作方便
5)tuple 元组 :( )可读列表
6)dic 字典:{ } 配对键值对
7)set 集合:{ } 无配对键值对,主要用于关系测试,天然去重
2、int 数字型
# 1、十进制数转换成二进制位后,有效位有几位
n = 2
print(n.bit_length())
<< 2
3、bool
bool <---> str : 空为Flase,(空格也不为空),有字符串为True
bool <---> int : 假为0,真为 1
4、str
4.1 索引、切片、步长
str1 = 'hello,everybody,he'
str1[-1:-5:-1]
<<eh,y
# 1、切片顾头不顾尾
# 2、要想倒切,步长需要为负数,否者不显示
4.2 字符串常用方法
str1 = 'the fact'
# 1、capitalize :首字母大写
print(str1.capitalize())
<< The fact
# 2、center: 居中,长度为10居中,其余位置用-填满
print(str1.center(10,'-'))
<< -the fact-
# 3、upper、lower 全大写、全小写,主要用于验证码
# 4、startswith、endswith:是否以什么开头或结尾,支持切片
# 5、swapcase:大小写翻转
# 6、title:非字母隔开的每个开头字母都大写
# 7、find:找到第一个就返回位置,支持切片,返回的位置是在整个字符串中的位置,不存在返回-1
index:同find,不存在报错
# 8、strip: 1、默认去除前后空格,强制转义特殊字符串\n之类 为普通字符串
2、也可去除制定内容,夹在中间不去除
# 9、split: 默认按空格分割,也可制定分割符和次数
# 10、join:定制连接符
print('-'.join(str1))
<< t-h-e- -f-a-c-t
# 11、replace:替换,可指定替换次数
# 12、format
# 1)
'hello,{}'.format('world')
# 2)
'{0}says:hello,{1}'.format('Alex','Zara')
# 3)
name1 = 'Alex'
name2 = 'computer'
age1 = 23
print('{it} says:hello {name},your age is {age},welcome back'.format(it=name2, name=name1, age=age1))
<< computer says:hello Alex,your age is 23,welcome back
# 13、is系列
isdigit :是否是数字
5、for循环
for i in 对象:
print(i)
6、详细博客内容
太白:python基础数据类型
https://www.cnblogs.com/jin-xin/articles/7562422.html