python常见八大数据类型

一:整型(int): 

二:浮点型(flost)

三:布尔(bool)

True - 真 False - 假 用于条件判断

四:元组(tuple)

创建的元组中只有一个字符串类型的元素时,该元素后面必须要加一个逗 号 , ,否则 Python 解释器会将它视为字符串。

url = tuple("https://www.baidu.com/")
#使用索引访问元组中的某个元素
print(url[3]) #使用正数索引
print(url[-4]) #使用负数索引
#使用切片访问元组中的一组元素
print(url[9: 15]) #使用正数切片
print(url[9: 15: 3]) #指定步长
print(url[-6: -1]) #使用负数切片

特性:元组是有序的:有索引,切片,步长        元组不可变 

书写格式  tu = ('nihao',1)      #逗号隔开
tu = ("哈哈")       # 小括号中只有元素,没有逗号,结果为元素本身
print(tu)  结果 :哈哈
tu = (1,)    # 小括号中有一个元素后有逗号的就是元组
tu = ()     # 空的小括号也是元组   print 结果是 : 0

元组可以查询,不能增删改 

tuple1 = (1,2,3,[1,2,3])
data = tuple1[0]
print(data)
#结果:1  
tuple1[-1][0] = 123
print(tuple1)
#结果:(1, 2, 3, [123, 2, 3])  #元组里面是一个列表,列表可以修改,新词修改的是里面的列表而不是元组 

五:字符串(str) 

1.字符串大小写转换

strs = 'HELLO world Ni Hao'
#全部变成小写
print(strs.lower())
#小写转成大写
print(strs.upper())
#首字母转大写
print(strs.title())
#大小写互相转
print(strs.swapcase())

结果

 

2.字符串替换

strs = '  HELLO world Ni Hao  *&'
#字符串替换
print(strs.replace('HELLO','你好'))
# 字符串拆分
print(strs.split(' '))
# 字符串去除左右特殊符号或空格
print(strs.strip().rstrip('*&'))

结果

3.字符串三种格式化方法:

#第一种
strs = '今天天气%s,我们一起%s' %('晴朗','打篮球')
print(strs)
# 第二种
strs = '今天天气%{},我们一起{}'.format('大暴雨','watch TV')
print(strs)
# 第三种
# f-string
user = '清凉'
activity = '去爬山'
strs = f'今天天气{user},我们一起{activity}'
print(strs)

结果 

4.字符串转义

 对含有特殊含义的符号进行转义,例如对引号进行转义,只需在引号前面加反斜杠 \ ,就可以对引号进行转义。

str1='I\'m a good man'
print(str1)  #I'm a good man

原始字符串:

在在普通字符串或者长字符串前面加r前缀,就变成原始字符串。

5. 字符串拼接

用+运算符来拼接

s1 ='python'
s2 ='http://www.python.org/'
s3=s1+'的网址是:'+s2
print(s3)  #python的网址是:http://www.python.org/

6.切片

url = '人生苦短,我用python,不掉丢发哈'
#获取索引从4处到10(不包含10)的子串
print(url[4: 10]) # 输出 ,我用pyt
#获取索引从7处到-6的子串
print(url[7: -6]) # 输出 python
#获取索引从-8到2的子串
print(url[-8: -2]) # 输出 on,不掉丢
#从索引0开始,每隔2个字符取出一个字符,直到索引10为止
print(url[0: 10: 2])  # 输出 人苦,用y
print(url[:: -1])  # 输出  哈发丢掉不,nohtyp用我,短苦生人
print(url[::])  # 输出  人生苦短,我用python,不掉丢发哈

 7.获取字符串长度或字节数

要想知道一个字符串有多少个字符(获得字符串长度),或者一个字符串占用多少个字节,可以使 用 len 函数。

url = '人生苦短,我用python,不掉丢发哈'
print(len(url))  #输出 19

在 Python 中,不同的字符所占的字节数不同,数字、英文字母、小数点、下划线以及空格,各占 一个字节,而一个汉字可能占 2~4 个字节,具体占多少个,取决于采用的编码方式。例如,汉字在 GBK/GB2312 编码中占用 2 个字节,而在 UTF-8 编码中一般占用 3 个字节。

使用 encode() 方法,将字符串进行编码后再获取它的字节数

str1 = "人生苦短,我用Python"
print(str1.encode('gbk'))
# 输出 b'\xc8\xcb\xc9\xfa\xbf\xe0\xb6\xcc\xa3\xac\xce\xd2\xd3\xc3Python'
print(len(str1.encode('gbk'))) # 输出20

 8.字符串拆分,和并

split() 方法可以实现将一个字符串按照指定的分隔符切分成多个子串

join() 方法它是 split() 方法的逆方法,可以将字符串合并,也可以将列表(或元组)中包含 的多个字符串连接成一个字符串。

str1 = "人生苦短,我用Python"
print(str1.split(','))  # 输出 ['人生苦短', '我用Python']
print(','.join(str1.split(',')))  # 输出 人生苦短,我用Python
print(''.join(str1.split(',')))  # 输出 人生苦短我用Python

9.统计字符串出现的次数

count 方法用于检索指定字符串在另一字符串中出现的次数,如果检索的字符串不存在,则返回 0,否则返回出现的次数。 

str1 = "https://www.baidu.com"
str2 = str1.count('w')
print(str2)  # 输出 3

10.检测字符串中是否包含某个元素 

find() 方法用于检索字符串中是否包含目标字符串,如果包含,则返回第一次出现该字符串的索 引;反之,则返回 -1。

index()方法也可以用于检测字符串中是否包含某个元素,不同之处在于,当指定的字符串不存在 时,index() 方法会报错。

str1 = "人生苦短,我用Python"
str2 = str1.find('P')
str3 = str1.find('m')
print(str2,str3)  # 输出 7 -1
str4 = str1.index('短') # 输出3
str5 = str1.index('g') # 直接报错
print(str4,str5)

六:列表(list)

1.列表的增删改查:

  特性:有序  可变

lists = [1,2,3,4]
# 增
# 方法一:
lists.append(5)
print(lists)
#结果:[1, 2, 3, 4, 5]
# 方法二:
print(lists+[6])
#结果:[1, 2, 3, 4, 6]
# 方法三:
lists.extend([7])
print(lists)
#结果:[1, 2, 3, 4, 7]
# 方法四:
lists.insert(2,'hello 你好')  #根据下表增
print(lists)
#结果:[1, 2, 'hello 你好', 3, 4]
# 删
# 方法一:
data = lists.pop(2)
print(data,lists)
#结果:3 [1, 2, 4]
# 方法二:
del lists[2]
print(lists)
#结果:[1, 2, 4]
# 方法三
lists.clear()
print(lists)
#结果:[]
# 方法四
lists[:] = ''
print(lists)
#结果:[]
# 方法五
# lists.remove(3)
# print(lists)
#结果:[1, 2, 4]
# 改
lists[2] = 'hello world'
print(lists)
#结果:[1, 2, 'hello world', 4]
# 查
#方法一:
print(lists[1:3])
#结果:[2, 3]
方法二: index
nums = [40, 36, 89, 2, 36, 100, 7, -20.5, -999]
#检索列表中的所有元素
print( nums.index(2) )  #输出 3
#检索3~7之间的元素
print( nums.index(100, 3, 7) ) #输出 5
方法三:count
nums = [40, 36, 89, 2, 36, 100, 7, -20.5, 36]
#统计列表中元素出现的次数
print(nums.count(36)) #输出 3
for i in lists:
    print(i)
#结果:
1
2
3
4
for i in range(len(lists)):
    print(i,lists[i])
#结果:
0 1
1 2
2 3
3 4

#range介绍
# for i in range(0,10,2): # (起始位置,终止位置,步长)
#     print(i)

2.列表删除方法pop的运用

lists = ['https://www.baidu.com|0','https://www.taobao.com|0','https://www.jd.com|0']
finish_list = []
error_list = []
while True:
    网址 = lists.pop()
    # print(网址)
    true_website = 网址.split('|')[0]
    # print(true_website)
    error_num = int(网址.split('|')[1])
    data = requests.get(true_website)
    if data:
        finish_list.append(true_website)
    else:
        error_num = error_num+1
        if error_num>3:
            error_list.append(true_website)
            continue
        lists = [true_website+'|'+str(error_num)]+lists
    if len(lists)==0:
        break
print(finish_list,error_list)

 3.列表切片

nums = [40, 36, 89, 2, 36, 100, 7]
#修改第 1~4 个元素的值(不包括第4个元素)
nums[0: 3] = [45.25, -77, -52.5]
print(nums)  #输出 [45.25, -77, -52.5, 2, 36, 100, 7]
nums = [40, 36, 89, 2, 36, 100, 7]
#在4个位置插入元素
nums[4: 4] = [-77, -52.5, 999]
print(nums)  #输出 [40, 36, 89, 2, -77, -52.5, 999, 36, 100, 7]
nums = [40, 36, 89, 2, 36, 100, 7]
#步长为2,为第1、3、5个元素赋值
nums[1: 6: 2] = [0.025, -99, 20.5]
print(nums)  #输出 [40, 0.025, 89, -99, 36, 20.5, 7]

2.列表生成式

list1 = [x for x in range(10) if x%2==0]
print(list1)
#结果:[0, 2, 4, 6, 8]
list2 = [x if x%2!=0 else -x for x in range(10)]
print(list2)
#结果:[0, 1, -2, 3, -4, 5, -6, 7, -8, 9]
list3 = [[1,2],[3,4],[5,6]]  #;两层列表嵌套
list4 = [y for x in list3 for y in x ]  #把list3中两层列表转换成一层列表
print(list4)
#结果:[1, 2, 3, 4, 5, 6]

七:字典(dict)

无序,可变

1.增删改查

dicts = {'a':'b','c':'d'}
# 增
# 方法一:
dicts['hello'] = 'world'
print(dicts)
#结果:{'a': 'b', 'c': 'd', 'hello': 'world'}
# 方法二
dicts.update({'nihao':'大海'})
print(dicts)
#结果:{'a': 'b', 'c': 'd', 'nihao': '大海'}

# 删
# 方法一:
data = dicts.pop('a')
print(data,dicts)
#结果:b {'c': 'd'}
#方法二:
del dicts['c']
print(dicts)
#结果:{'a': 'b'}

# 改
dicts['a'] = 'BB'
print(dicts)
#结果:{'a': 'BB', 'c': 'd'}
# 查
# 方法一
for i in dicts:
    print(i,dicts[i])
#结果:
a b
c d
# 方法二
for key,values in dicts.items():
    print(key,values)
print(dicts.items())
#结果
a b
c d
dict_items([('a', 'b'), ('c', 'd')])

2.get() 方法

获取指定键对应的值,当指定的键不存在时,get() 方法不会抛出异常

dictname.get(key,default)
# 其中,dictname 表示字典变量的名字;key 表示指定的键;default 用于指定要查询的键不存在
#时,此方法返回的默认值,如果不手动指定,会返回 None。
a = dict(two=0.65, one=88, three=100, four=-59)
print( a.get('one') )  #输出 88
# 当键不存在时,get() 返回空值 None,如果想明确地提示用户该键不存在,那么可以手动设置
3#get() 的第二个参数,例如:
a = dict(two=0.65, one=88, three=100, four=-59)
print( a.get('five', '该键不存在') ) #输出 改键不存在

3.keys()、values() 和 items() 方法

keys() 方法用于返回字典中的所有键(key); values() 方法用于返回字典中所有键对应的值(value); items() 用于返回字典中所有的键值对(key-value)。 

scores = {'数学': 95, '语文': 89, '英语': 90}
print(scores.keys())  #输出dict_keys(['数学', '语文', '英语'])
print(scores.values())  #输出dict_values([95, 89, 90])
print(scores.items())  #输出dict_items([('数学', 95), ('语文', 89), ('英语', 90)])

八.集合(set)

特性:无序  可变  天然去重  元素唯一

1.增删改查

sets= {1,2,3,4,5,3,5,6,6}
print(sets)
#结果:{1, 2, 3, 4, 5, 6}  可以天然去重
#增
sets.add(123)
print(sets)
#结果:{1, 2, 3, 4, 5, 6, 123}
# 删
# 方法一
sets.pop()
print(sets)
#结果:{2, 3, 4, 5, 6}
# 方法二
sets.remove(3)
print(sets)
#结果:{1, 2, 4, 5, 6}
set1 = {1,2,3,4}
set2 = {3,4,5,6}
# 查
# 差集
print(set1-set2)
#结果:{1, 2}
# 并集
print(set1|set2)
#结果:{1, 2, 3, 4, 5, 6}
# 补集
print(set1^set2)
#结果:{1, 2, 5, 6}
# 交集
print(set1&set2)
#结果:{3, 4}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值