栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python的基本数据类型有哪些?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python的基本数据类型有哪些?

本篇文章给大家带来的内容是介绍python有哪些基本数据类型有哪些,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。

 

1、python的一切都是对象,对象是包含属性和方法的一个整体。

2、数据类型的组成:身份 (内存地址,通过id方法可看它的**标识符);类型(通过type方法查看);值(数据项)

3、常用基本数据类型

  • int 整型

  • bool 布尔

  • strintg 字符串

  • list 列表

  • tuple 元组

  • dict 字典

4、数据类型的可变和不可变

  • 不可变类型:int, string,tuple

  • 可变类型:list,dict

5、 转义字符

#转义字符
print('abcd
ef')#为转义字符
print(r'abcd
ef')#字符串前面加r表示不转义

运行结果:
abcd
ef
abcd
ef

6、切片

a = "abcde"
b = a[-1] #访问**后一个元素
c = a[0:4]#访问序列在0到4之间的元素不包括4
print(b)
print(c)

运行结果:
e
abcd

7、字符串替换

a = "abcd"
print(a[0])
b = a.replace('d','def')
print(b)
print(a.find('d'))#字符串查询

运行结果:
a
abcdef
3

8、字符串拼接

#【1】直接相加
a = 'my name is xiaobin'
b = 'tong'
c = a + b
print(c)

运行结果:
my name is xiaobintong

#【2】占位符
print('my name is %s xiaobin' % 'tong')#%s为字符串占位符,%d为数字占位符
print('my name is %s xiaobin,i'm %s years old' % ('tong',24))

print('my name is {1}, i'm {0} years old'.format('24','tongxiaobin'))#用format方法

运行结果:
my name is tong xiaobin
my name is tong xiaobin,i'm 24 years old
my name is tongxiaobin, i'm 24 years old

#【3】join
a = '123'
b = '456'
c = '789'
d = ''.join([a,b,c])
e = ';'.join([a,b,c])
print(d)
print(e)
运行结果:
123456789
123;456;789

9、文件操作:‘r’-read; 'w'-write;‘a’-append(在**后添加)

#写操作
d = open('1.txt','w')
d.write('hello world
my name is tongxiaobin')
d.close()

#读操作
e = open('1.txt','r')
print(e.readline())#按行读取
print(e.readline())

运行结果:
hello world
my name is tongxiaobin

#末尾添加操作
a = open('1.txt','a')
a.write('
come from anhui')
a.close()

打开文件结果为:
hello world
my name is tongxiaobinfdsd
come from anhui

10、linecache模块

import linecache
linecache.getline('1.txt',2)

运行结果:
'my name is tongxiaobin
'

linecache.getlines('1.txt')

运行结果:
['hello world
', 'my name is tongxiaobin
', 'come from anhui
']

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关视频教程请访问:Python视频教程,Python3视频教程,bootstrap视频教程!

以上就是python的基本数据类型有哪些?的详细内容,更多请关注考高分网其它

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/805716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号