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


本文摘自php中文网,作者青灯夜游,侵删。

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

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

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

3、常用基本数据类型

  • int 整型

  • bool 布尔

  • strintg 字符串

  • list 列表

  • tuple 元组

  • dict 字典

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

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

  • 可变类型:list,dict

5、 转义字符

1

2

3

4

5

6

7

8

#转义字符

print('abcd\nef')#\为转义字符

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

 

运行结果:

abcd

ef

abcd\nef

6、切片

1

2

3

4

5

6

7

8

9

a = "abcde"

b = a[-1] #访问最后一个元素

c = a[0:4]#访问序列在0到4之间的元素不包括4

print(b)

print(c)

 

运行结果:

e

abcd

7、字符串替换

1

2

3

4

5

6

7

8

9

10

a = "abcd"

print(a[0])

b = a.replace('d','def')

print(b)

print(a.find('d'))#字符串查询

 

运行结果:

a

abcdef

3

8、字符串拼接

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#【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(在最后添加)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#写操作

d = open('1.txt','w')

d.write('hello world\nmy 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('\ncome from anhui')

a.close()

 

打开文件结果为:

hello world

my name is tongxiaobinfdsd

come from anhui

10、linecache模块

1

2

3

4

5

6

7

8

9

10

import linecache

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

 

运行结果:

'my name is tongxiaobin\n'

 

linecache.getlines('1.txt')

 

运行结果:

['hello world\n', 'my name is tongxiaobin\n', 'come from anhui\n']

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

以上就是python的基本数据类型有哪些?的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python gpu 什么意思

Python一定要学爬虫么

如何Python判断字符串是否为回文?

Python中logging的详细介绍(附示例)

举例说明Python中空格是属于字符

Python能做什么?是什么意思?

Python能写桌面程序吗

Python可以写病毒吗

Python输出怎么保留两位小数

Python可以开发软件吗

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...