怎样用python 判断是整数判断整数

在接收raw_input方法后,判断接收到的字符串是否为数字
str = raw_input("please input the number:")
if str.isdigit():
为True表示输入的所有字符都是数字,否则,不是全部为数字
str为字符串 str.isalnum()&所有字符都是数字或者字母 str.isalpha()&所有字符都是字母 str.isdigit()&所有字符都是数字 str.islower()&所有字符都是小写 str.isupper()&所有字符都是大写 str.istitle()&所有单词都是首字母大写,像标题 str.isspace()&所有字符都是空白字符、\t、\n、\r
上述的主要是针对整型的数字,但是对于浮点数来说就不适用了,那么浮点数怎么判断呢,一直在纠结这个问题,为什么非要区分整型和浮点数呢,既然都是参与运算的,全部适用浮点数不是一样吗,在得到结果后,直接转换为int型不是一样吗,为什么非要纠结在前期去判断是否整型或者浮点数呢,有了这样的思路,下面就好做了,例如:
我们可以通过异常来判断,异常语法如下:
try: &&&&{statements} exception: {Exception Objects} &&&&{statements}
str = raw_input("please input the number:")
try: & & f&= float(str) exception ValueError: & & print("输入的不是数字!")
==========================================================
还有一种纯粹判断是否为浮点数的方法,使用正则表达式:
#引用re正则模块
float_number&=&str(input("Please&input the number:"))
value&=&pile(r'^[-+]?[0-9]+\.[0-9]+$')
result = value.match(float_number)
if result:
&&&&print&"Number&is&a&float."
&&&&print&"Number&is&not&a&float."
2.&关于这个正则表达式,解释一下:
^[-+]?[0-9]+\.[0-9]+$&&
^表示以这个字符开头,也就是以[-+]开头,[-+]表示字符-或者+之一,
?表示0个或1个,也就是说符号是可选的。
同理[0-9]表示0到9的一个数字,+表示1个或多个,也就是整数部分。
\.表示的是小数点,\是转义字符因为.是特殊符号(匹配任意单个除\r\n之外的字符),
所以需要转义。
小数部分同理,$表示字符串以此结尾。
才开始学正则,有错误的地方请大家指正。
阅读(...) 评论()用户名:_Mr_Computer_
文章数:169
访问量:52443
注册日期:
阅读量:1297
阅读量:3317
阅读量:448076
阅读量:1133064
51CTO推荐博文
mport types&type(x) is types.IntType # 判断是否int 类型&type(x) is types.StringType #是否string类型&.........&--------------------------------------------------------超级恶心的模式,不用记住types.StringTypeimport types&type(x) == types(1) # 判断是否int 类型&type(x) == type('a') #是否string类型&------------------------------------------------------使用内嵌函数:isinstance&(object, classinfo&)Return true if the&object&argument is an instance of the&classinfo&argument, or of a (direct or indirect) subclass thereof. Also return true if&classinfo&is a type object and&object&is an object of that type. If&objectis not a class instance or an object of the given type, the function always returns false. If&classinfo&is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). If&classinfo&is not a class, type, or tuple of classes, types, and such tuples, a&TypeError&exception is raised.&Changed in version 2.2: Support for a tuple of type information was added.&Python可以得到一个对象的类型 ,利用type函数:&&&lst = [1, 2, 3]&&&type(lst)&type 'list'&不仅如此,还可以利用isinstance函数,来判断一个对象是否是一个已知的类型。isinstance说明如下:&&& isinstance(object, class-or-type-or-tuple) -& bool&&&&&&& Return whether an object is an instance of a class or of a subclass thereof.&&& With a type as second argument, return whether that is the object's type.&&&&The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for&&&& isinstance(x, A) or isinstance(x, B) or ... (etc.).其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。若对象的类型与参数二的类型相同则返回True。若参数二为一个元组, 则若对象类型与元组中类型名之一相同即返回True。&&&isinstance(lst, list)Trueisinstance(lst, (int, str, list))True&&&isinstance(lst, (int, str, list))True本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)姹傚姪:鍏ラ棬闂??,濡備綍鍒ゆ柇瀛楃?鏄?笉鏄?暣鏁

我要回帖

更多关于 python 判断整数相等 的文章

 

随机推荐