python学习笔记一:基本数据类型

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

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

3、常用基本数据类型

  • Number  数字
  • Strintg   字符串
  • Tuple   元组
  • List   列表
  • Dict  字典
  • Set  集合

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

  • 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);
  • 可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。

5、 转义字符

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

运行结果:
abcd
ef
abcd\nef

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\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模块

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']

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值