Python基础—数据类型

本文介绍了Python的基础数据类型,包括数字(整型、浮点型、复数)、字符串、列表、元组、字典和集合。详细讲解了每种类型的定义、创建、操作以及它们之间的区别。还特别提到了Python中的数字表示(如二进制、八进制、十六进制)和列表的深拷贝与浅拷贝概念。
摘要由CSDN通过智能技术生成


前言

1 什么是数据

a = 10,10就是我们需要存储的数据

2 为什么数据需要分类型

数据是用来表示状态的,不同状态就应该使用不同的数据类型的数据去表示

3 Python数据有那些类型

数字、字符串、列表、元组、字典、集合


一、数字

整形int

表示整数,包含正数、负数、0。如: -5, 100, 0

字面值:

十进制:5

二进制:0b开头,后跟0或者1

二进制(0 1 10 11 100 101 …)
num02 = 0b11
print(num02)
#num02=3

八进制:0o开头,后跟0,1,2,3,4,5,6,7,

八进制(0 1 … 7 10…)
num03 = 0o10
print(num03)
#num03=8

十六进制:0x开头,后跟0,1,2,3,4,5,6,7,8,9,a(10),b(11),c(12),d(13),e(14),f(15)

十六进制(0 1 …9 a(10) – f(15) 10(16))
num04 = 0xa
print(num04)
#num04=10

小整数对象池:CPython 中整数 -5 至 256,永远存在小整数对象池中,不会被释放并可重复使用。

浮点型float
表示小数,包含正数、负数,0.0。

字面值:

小数:1.0 2.5

科学计数法:e/E (正负号) 指数

例如:

1.23e-2 (等同于0.0123)

1.23456e5(等同于123456.0)

1.0000002科学计数法表达(1+0.2e-6)

f01 = 0.0
f02 = 0.2e2
print(f02)
#f02=20.0
print(type(f01))
#<class ‘float’>

其他数据类型(了解)
int(整型)

在32位机器上,整数的位数为32位,取值范围为-231~231-1,即-2147483648~2147483647
在64位系统上,整数的位数为64位,取值范围为-263~263-1,即-9223372036854775808~9223372036854775807

long(长整型)

跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
注意:在Python3里不再有long类型了,全都是int

>>> a= 2**64
>>> type(a)  #type()是查看数据类型的方法
<type 'long'>
>>> b = 2**60
>>> type(b)
<type 'int'>

complex复数型

>>> x=1-2j
>>> x.imag
-2.0
>>> x.real
1.0

二、字符串

定义: 在单引号/双引号/单引号内,由一些列字符组成的不可变序列容器

作用: 用于名字、性别、国籍、地址等描述信息。
如:name = ‘PPPsych’

重复输出字符串:

print('hello'*2)

切片:

print('helloworld'[2:0])

in判断:

print('el' in 'hello')

拼接:

  • c=a+b
  • c=’ ‘.join([a,b])

重要内置方法:
strip:

name='*PPPsych**'
print(name.strip('*')) #去掉字符串外的*
print(name.lstrip('*')) #去掉字符串左边的*
print(name.rstrip('*')) #去掉字符串右边的*
————————————————————————————————————————————————
输出:
PPPsych
PPPsych**
*PPPsych

lower,upper:

name='PPPsych'
print(name.lower()) #大写变小写,小写不变
print(name.upper()) #小写变大写,大写不变
————————————————————————————————————————
输出:
pppsych
PPPSYCH

startswith,endswith:

name='python_SB'
print(name.endswith('SB')) #判断是否以SB结尾
print(name.startswith('python')) #判断是否以python开头
——————————————————————————————————————————————————————
输出:
True
True

format的三种玩法

res='{} {} {}'.format('PPPsych',18,'male')
print(res)
res='{1} {0} {1}'.format('PPPsych',18,'male')
print(res)
res='{name} {age} {sex}'.format(sex='male',name='PPPsych',age=18)
print(res)
——————————————————————————————————————————————————————————————————
输出:
PPPsych 18 male
18 PPPsych 18
PPPsych 18 male

split:

name='root:x:0:0::/root:/bin/bash'
print(name.split(':')) #以:分割字符串,默认分隔符为空格
name='C:/a/b/c/d.txt' #只想拿到顶级目录
print(name.split('/',1))
name='a|b|c'
print(name.rsplit('|',1)) #从右开始切分
——————————————————————————————————————————————————————
输出:
['root', 'x', '0', '0', '', '/root', '/bin/bash']
['C:', 'a/b/c/d.txt']
['a|b', 'c']

join:

tag=' '
print(tag.join(['PPPsych','say','hello','world'])) #可迭代对象必须都是字符串
————————————————————————————————————————————————————————————————————————————
输出:
PPPsych say hello world

replace:

name = 'python say :i have one tesla,my name is Tom'
print(name.replace('python', 'SB', 1))
msg = 'my title title'
print(msg.replace('itle', 'lesson', 1))
# .replace('被替换的内容','新内容','第几次出现')
————————————————————————————————————————————————————————
输出:
SB say :i have one tesla,my name is Tom
my tlesson title

isdigit:
可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法

age=input('>>: ')
print(age.isdigit())

其他操作(了解):


# find,rfind,index,rindex,count
name = 'PPPsych say hello'
print(name.find('o', 1, 3))  # 顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# print(name.index('e',2,4)) #同上,但是找不到会报错
print(name.count('e', 1, 3))  # 统计e出现的次数,顾头不顾尾,如果不指定范围则查找所有

# center,ljust,rjust,zfill
name = 'PPPsych'
print(name.center(30, '-'))
print(name.ljust(30, '*'))
print(name.rjust(30, '*'))
print(name
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值