简体   繁体   English

如何在 Python 中使用 if else 检查数据类型?

[英]How to check Datatype with if else in Python?

I want to check my Datatype in python using if else我想使用if else检查 python 中的数据类型

Output I want: If user Enter float Datatype so it print It is float or if user enter another Datatype so it print it is not float Output 我想要:如果用户输入 float 数据类型所以它打印It is float或者如果用户输入另一个Datatype所以它打印it is not float

Code:代码:

c=(input("Enter the value\n"))
if type (c) == float:
    print('it is float')
else:
   print("it is not float")

Output I want: Output 我要:

Enter the value
12.1
it is float

Output I'm getting: Output 我得到:
If I enter float datatype it is still printing it is else如果我输入float datatype ,它仍在打印it is else

Enter the value
12.1
it is else

You should have something like this你应该有这样的东西

c = (input("Enter the value\n"))
try:
    f = float(c)
    is_float = True
except ValueError:
    is_float = False
c=input("Enter the value\n")
try:
    float(c)
    print('it is float')
except ValueError:
   print("it is not float")

your problem is that input always give you str value so by trying to convert it to float you know if its a float or not你的问题是输入总是给你 str 值所以通过尝试将它转换为 float 你知道它是否是 float

Check if object is int or float: isinstance()检查 object 是整数还是浮点数: isinstance()

The type of an object can be obtained with the built-in function type().可以使用内置的 function type() 获取 object 的类型。

i = 100
f = 1.23

print(type(i))
print(type(f))
# <class 'int'>
# <class 'float'>

The built-in function isinstance(object, type) can be used to determine whether an object is of a particular type.内置的 function isinstance(object, type) 可用于确定 object 是否为特定类型。

Get / determine the type of an object in Python: type() , isinstance()获取/确定 Python 中 object 的类型: type() , isinstance()

print(isinstance(i, int))
# True

print(isinstance(i, float))
# False

print(isinstance(f, int))
# False

print(isinstance(f, float))
# True

In this case, since only the type is checked, it cannot be determined whether the value of float is an integer (the fractional part is 0).在这种情况下,由于只检查类型,无法判断float的值是否为integer(小数部分为0)。

f_i = 100.0

print(type(f_i))
# <class 'float'>

print(isinstance(f_i, int))
# False

print(isinstance(f_i, float))
# True
source: check_int_float.py
Check if float is integer: is_integer()
float has is_integer() method that returns True if the value is an integer, and False otherwise.

f = 1.23

print(f.is_integer())
# False

f_i = 100.0

print(f_i.is_integer())
# True

For example, a function that returns True for an integer number (int or integer float) can be defined as follows.例如,对于 integer 数字(整数或 integer 浮点数)返回 True 的 function 可以定义如下。 This function returns False for str.这个 function 为 str 返回 False。

def is_integer_num(n):
    if isinstance(n, int):
        return True
    if isinstance(n, float):
        return n.is_integer()
    return False

print(is_integer_num(100))
# True

print(is_integer_num(1.23))
# False

print(is_integer_num(100.0))
# True

print(is_integer_num('100'))
# False

Credits: https://note.nkmk.me/en/python-check-int-float/积分: https://note.nkmk.me/en/python-check-int-float/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM