Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python tell if a variable is a number?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "python how to judge whether a variable is a number", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python how to judge whether a variable is a number" bar!

Title:

Determine whether a variable is a number or not?

Str_list1 = [1234, '1341414,' 134.151,'- 134respectively, '123,',' positive 345', 'negative 456', 'positive 3.1415'] str_list2 = ['-','13','34. 421', '.134'] title requirements:

Variables like str_list1 internal elements are judged to be numeric, and variables similar to str_list2 internal elements are judged to be non-numeric!

Code answer:

You'd better try to write it yourself before you read the answer, and then compare it with what I wrote:

Def isnumber (str_number): # determine whether the string is a number if type (str_number) is int or type (str_number) is float: # if the parameter is numeric or floating-point, return Truereturn Trueif type (str_number)! = str or len (str_number)

< 1: # 如果参数不是字符串型或长度小于1,返回Falsereturn Falsesymbol1 = ('+', '-', '正', '负') # 正负号symbol2 = ('.', '点') # 小数点if str_number[0] in symbol1: # 如果首字符是符号,那么整个字符串去除符号str_number = str_number[1:]if len(str_number) < 1: # 如果去除符号后长度为0,判断不是数字return Falseelif str_number[0] in symbol2: # 首字符不能为小数点return Falsenumber = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '零', '一', '二', '三', '四', '五', '六', '七', '八', '九') # 匹配模板result = True # 预设结果是Truepoint_times = 0 # 小数点次数str_len = len(str_number) # 计算整个字符串长度for i in range(str_len): # 遍历整个字符串if str_number[i] in symbol2: # 如果当前字符是小数点point_times += 1 # 小数点次数+1if point_times >

1: # the number of decimal point is more than 1 result = False # determine that the string is not a number breakelif str_ number [I] not in number: # if the current character is not in the template result = False # determine that the string is not a number breakreturn resultstr_list1 = [1234, '134', '134.151','- 134', 'one two three', 'positive three four five', 'negative four five six' 'positive 3.1415'] str_list2 = ['-','- 13','34.. 421', '.134'] for i in str_list1:print (f "current parameter {I} {'is a number!' If isnumber (I) else 'not a number!' } ") print ('-'* 50) for i in str_list2:ret ='is a number!' If isnumber (I) else 'not a number!' Print (f "current parameter {I} {'is a number!'if isnumber (I) else'is not a number!'}") out: the current parameter 1234 is a number! The current parameter 134 is a number! The current parameter 134.151 is a number! The current parameter-134 is a number! The current parameter one, two, three is a number! The current parameter is positive three, four, five is a number! The current parameter minus four, five, six is a number! The current parameter positive 3.1415 is a number! -current parameter-not a number! The current parameter-13 is not a number! The current parameter 34. 421 is not a number! The current parameter .134 is not a number! Thank you for your reading, the above is the content of "python how to judge whether a variable is a number". After the study of this article, I believe you have a deeper understanding of how python judges whether a variable is a number or not, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report