Python3 & 基本数据类型(一)

Python提供的基本数据类型:数值(整型、浮点型、复数、布尔型等)、字符串、列表、元组、字典、集合等,将它们简单分类如下:


image.png

1、数值(Numbers)

(1)整数(Int)

通常被称为整型,数值为正或者负,不带小数点。
Python 3的整型可以当做Long类型使用,所以Python 3没有
Python 2的Long类型。
Python 初始化的时候会自动建立一个小整数对象池,方便我们调用,避免后期重复生成!这是一个包含 262个指向整数对象的指针数组,范围是 -5 到 256 。

(2)浮点数(Float)

Python的浮点数就是数学中的小数,类似C语言中的double。
浮点数 也就是小数,如 1.23 , 3.14 , -9.01 等等。但是对于很大或很小的浮点数,一般用科学计数法表示,把10用e替代, 1.23x10^9 就是 1.23e9 ,或者 12.3e8 , 0.000012 可以写成1.2e-5 等等。

(3) 复数(Complex)

复数 由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示,复数的实部a和虚部b都是浮点。

(4)布尔类型(Bool)

对 与 错 、 0 和 1 、 正 与 反 ,都是传统意义上的布尔类型。
但在Python语言中,布尔类型只有两个值, True 与 False 。请注意,是英文单词的对与错,并且首字母要大写。
在Python中,0、0.0、-0.0、None、空字符串“”、空元组()、空列表[]、空字典{}都被当作False,还有自定义类型,如果实现了nonzero()或len()方法且方法返回0或False,则其实例也被当作False,其他对象均为True

布尔值还可以用and、or和not运算。
1)、and 运算是 与 运算,只有所有都为 True , and 运算的结果才是 True ;
2)、or 运算是 或 运算,只要其中有一个为 True , or 运算结果就是 True ;
3)、not 运算是 非 运算,它是单目运算符,把 True 变成 False,False 变成 True。
例如:

print(True > False)
#输出:True
print(True < False)
#输出:False
print(True >=False)
#输出:True
print(True -1)
#输出:0
print(True + 1)
#输出:2
print(True *3)
#输出:3
print(False -1)
#输出:-1

由以上案例可以看出,在做四则运算的时候,明显把 True 看做 1 , False 看做 0 。

4)空值
空值不是布尔类型,只不过和布尔关系比较紧密。
空值是Python里一个特殊的值,用 None 表示(首字母大写)。None不能理解为0,因为0是整数类型,而None是一个特殊的值。None也不是布尔类型,而是NoneType。

在某些特定的情况下,需要对数字的类型进行转换。
Python提供了内置的数据类型转换函数:
int(x) 将x转换为一个整数。如果x是一个浮点数,则截取小数部分。
float(x) 将x转换成一个浮点数。
complex(x) 将x转换到一个复数,实数部分为 x,虚数部分为 0。
complex(x, y): 将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y。

2、字符串(String)

Python字符串即可以用单引号也可以用双引号括起来,甚至还可以用三引号括起来,字符串是以''或""括起来的任意文本。
例如:'abc',"xyz"等等。请注意,''或""本身只是一种表示方式,不是字符串的一部分,因此,字符串'abc'只有a,b,c这3个字符。如果'本身也是一个字符,那就可以用""括起来,比如"I'm OK"包含的字符是I,',m,空格,O,K这6个字符。
字符串中包括特殊字符,可以用转义字符\来标识
但是字符串里面如果有很多字符都需要转义,就需要加很多\,为了简化,Python还允许用r''表示''内部的字符串默认不转义
例如:
print r'\\\t\\' #输出:\\\t\\

字符串的一些常见操作

(1)切片

切⽚是指对操作的对象截取其中⼀部分的操作
语法:序列[开始位置下标:结束位置下标:步⻓]
a. 不包含结束位置下标对应的数据, 正负整数均可;
b. 步⻓是选取间隔,正负整数均可,默认步⻓为1。

name = "abcdefg"
print(name[2:5:1]) # 输出: cde
print(name[2:5]) # 输出: cde
print(name[:5]) #  输出:abcde
print(name[1:]) # 输出: bcdefg
print(name[:]) #  输出:abcdefg
print(name[::2]) #  输出:aceg
print(name[:-1]) #  输出:abcdef, 负1表示倒数第⼀个数据
print(name[-4:-1]) #  输出:def
print(name[::-1]) #  输出:gfedcba

(2)查找

find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返回-1。
index():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则报异常。
rfind(): 和find()功能相同,但查找⽅向为右侧开始。
rindex():和index()功能相同,但查找⽅向为右侧开始。
count():返回某个⼦串在字符串中出现的次数。

print(s.find('P'))          # 3, 返回第一次出现的子串的下标
print(s.find('h', 2))              # 6, 设定下标2开始查找
print(s.find('a'))          # -1, 查找不到返回-1,不会报错
print(s.index('y')) # 4, 返回第一次出现的子串的下标
print(s.index('a'))     # 不同与find(), 查找不到会抛出异常

mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1

(3)修改

replace():替换
split():按照指定字符分割字符串。
join():⽤⼀个字符或⼦串合并字符串,即是将多个字符串合并为⼀个新的字符串。
capitalize():将字符串第⼀个字符转换成⼤写。
title():将字符串每个单词⾸字⺟转换成⼤写。
lower():将字符串中⼤写转⼩写。
upper():将字符串中⼩写转⼤写。
lstrip():删除字符串左侧空⽩字符。
rstrip():删除字符串右侧空⽩字符。
strip():删除字符串两侧空⽩字符。
ljust():返回⼀个原字符串左对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串。
rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和
ljust()相同。
center():返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语
法和ljust()相同。

#字符串序列.replace(旧⼦串, 新⼦串, 替换次数)
print(s.replace('Python', 'Java'))  # '优雅的Java'

mystr = "hello world and itcast and itheima and Python"
print(mystr.replace('and', 'he', 2))
# 结果:hello world he itcast he itheima and Python

#字符串序列.split(分割字符, num)
#num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。
mystr = "hello world and itcast and itheima and Python"
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']

print(mystr.split('and', 2))
# 结果:['hello world ', ' itcast ', ' itheima and Python']

print(mystr.split(' '))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']

print(mystr.split(' ', 2))
# 结果:['hello', 'world', 'and itcast and itheima and Python']

# 字符或⼦串.join(多字符串组成的序列)
list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')

print('_'.join(list1))
# 结果:chuan_zhi_bo_ke
print('...'.join(t1))
# 结果:aa...b...cc...ddd

mystr = "hello world and itcast and itheima and Python"
print(mystr.capitalize())
# 结果:Hello world and itcast and itheima and python

print(mystr.title())
# 结果:Hello World And Itcast And Itheima And Python

print(mystr.lower())
# 结果:hello world and itcast and itheima and python

print(mystr.upper())
# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON

# 格式化
s1 = '%s %s' % ('Windrivder', 21)   # 'Windrivder 21'
print(s1)
s2 = '{}, {}'.format(21, 'Windridver')  # 推荐使用format格式化字符串
print(s2)
s3 = '{0}, {1}, {0}'.format('Windrivder', 21)
print(s3)
s4 = '{name}: {age}'.format(age=21, name='Windrivder')
print(s4)

(4)判断

所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。

startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开
始和结束位置下标,则在指定范围内检查。
endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开
始和结束位置下标,则在指定范围内检查。
isalpha():如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False。
isdigit():如果字符串只包含数字则返回 True 否则返回 False。
isalnum():如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回
False。

mystr = "hello world and itcast and itheima and Python "
print(mystr.startswith('hello'))
# 结果:True
print(mystr.startswith('hello', 5, 20))
# 结果False

print(mystr.endswith('Python'))
# 结果:True
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))
# 结果:False

mystr1 = 'hello'
mystr2 = 'hello12345'
print(mystr1.isalpha())
# 结果:True
print(mystr2.isalpha())
# 结果:False

mystr1 = 'aaa12345'
mystr2 = '12345'
print(mystr1.isdigit())
# 结果: False
print(mystr2.isdigit())
# 结果:False

print(mystr1.isalnum()) # 结果:True
print(mystr2.isalnum())# 结果:False

mystr1 = '1 2 3 4 5'
mystr2 = ' '
print(mystr1.isspace())# 结果:False
print(mystr2.isspace())# 结果:True

你可能感兴趣的:(Python3 & 基本数据类型(一))