|
布尔类型(Boolean)
在Python中,None、任何数值类型中的0、空字符串“”、空元组()、空列表[]、空字典{}都被当作False,还有自定义类型,如果实现了__nonzero__()或__len__()方法且方法返回0或False,则其实例也被当作False,其他对象均为True
布尔值和布尔代数的表示完全一致,一个布尔值只有True、False两种值,要么是True,要么是False,在Python中,可以直接用True、False表示布尔值(请注意大小写),也可以通过布尔运算计算出来。布尔值也可以用来计算
>>>True +True
2
>>>True * False
0
字符串(String)
Python字符串即可以用单引号也可以用双引号括起来,甚至还可以用三引号括起来
字符串是以''或""括起来的任意文本,比如'abc',"xyz"等等。请注意,''或""本身只是一种表示方式,不是字符串的一部分
s为字符串
s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False。
s.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False。
s.isdigit() 所有字符都是数字,为真返回 Ture,否则返回 False。
s.islower() 所有字符都是小写,为真返回 Ture,否则返回 False。
s.isupper() 所有字符都是大写,为真返回 Ture,否则返回 False。
s.istitle() 所有单词都是首字母大写,为真返回 Ture,否则返回 False。
s.isspace() 所有字符都是空白字符,为真返回 Ture,否则返回 False。
例如:
>>> s = 'I LOVE FISHC'
>>> s.isupper()
>>> True
下面的程序演示了一些类型的输出:
http://www.169it.com/article/3220303488.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
#http://www.169it.com 测试代码
class test1:
pass
class test2(test1):
pass
if __name__=="__main__":
x=123 #int
print(type(x))
y=123.1 #float
print(type(y))
z=test1() # test1
print(type(z))
zz=test2() #test2
print(type(zz))
j='sdf' #str
print(type(j))
jj=True #bool
print(type(jj))
j3=['s'] #list
print(type(j3))
j4=('sdf',) #tuple
print(type(j4))
j5={'sdf':132} #dict
print(type(j5)) |
输出结果:

那么在程序中如何判断类型并根据判断结果进行相应的处理呢?首先可以通过type和is来进行,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
#http://www.169it.com 测试代码
class test1:
pass
class test2(test1):
pass
if __name__=="__main__":
x=123 #int
print(type(x) is int)
y=123.1 #float
print(type(y) is float)
z=test1() # test1
print(type(z) is test1)
zz=test2() #test2
print(type(zz) is test2)
j='sdf' #str
print(type(j) is str)
jj=True #bool
print(type(jj) is bool)
j3=['s'] #list
print(type(j3) is list)
j4=('sdf',) #tuple
print(type(j4) is tuple)
j5={'sdf':132} #dict
print(type(j5) is dict) |
输出结果:

其次,python还提供了另外一种方法来进行变量类型的测试即isinstance函数,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
#http://www.169it.com 测试代码
class test1:
pass
class test2(test1):
pass
if __name__=="__main__":
x=123 #int
print(isinstance(x,int))
y=123.1 #float
print(isinstance(y,float))
z=test1() # test1
print(isinstance(z,test1))
zz=test2() #test2
print(isinstance(zz,test2))
j='sdf' #str
print(isinstance(j,str))
jj=True #bool
print(isinstance(jj,bool))
j3=['s'] #list
print(isinstance(j3,list))
j4=('sdf',) #tuple
print(isinstance(j4,tuple))
j5={'sdf':132} #dict
print(isinstance(j5,dict)) |
输出结果:

那么isinstance和type到底有什么区别呢?最重要的一个区别为:type只能对类型作直接判断,而isinstance功能比type更强,可以对子类
的类型做出推理判断。具体代码如下:
|
1
2
3
4
5
6
7
8
9
10
11 |
#http://www.169it.com 测试代码
class test1:
pass
class test2(test1):
pass
if __name__=="__main__":
zz=test2() #test2
print(isinstance(zz,test1))
print(isinstance(zz,test2))
print(type(zz) is test1)
print(type(zz) is test2) |
输出结果:

2.Python中的除法
Python中分为3种除法:传统除法、精确除法、地板除。
传统除法
如果是整数除法则执行地板除,如果是浮点数除法则执行精确除法。
精确除法
除法总是会返回真实的商,不管操作数是整形还是浮点型。执行from __future__ import division 指令就可以做到这一点。
地板除
从Python2.2开始,增加了一个操作符 // ,以执行地板除://除法不管操作数为何种数值类型,总是会舍去小数部分,返回数字序列中比真正的商小的最接近的数字。
内建函数divmod()
divmod (a,b),返回(a//b,a%b)
3.python判断是否整除
小黑屋|手机版|Archiver|鱼C工作室
( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)
GMT+8, 2026-3-16 06:13
Powered by Discuz! X3.4
© 2001-2023 Discuz! Team.