为什么可以';Python是否打印一个整数并将其串在一起而不进行转换?

为什么可以';Python是否打印一个整数并将其串在一起而不进行转换?,python,Python,在这段代码中,Python将返回一个错误,即它无法像这样打印整数和字符串-我知道可以使用str将int转换为字符串,但Python为什么不自动执行此操作?它可以: num = 4 print(num + " other words") 它可以: num = 4 print(num + " other words") 作者 类型转换 将一种数据类型(整数、字符串、浮点等)的值转换为另一种数据类型的过程称为类型转换。Python有两种类型的类型转换 隐式类型转换 显式类型转换 隐式类型转换 在隐

在这段代码中,Python将返回一个错误,即它无法像这样打印整数和字符串-我知道可以使用str将int转换为字符串,但Python为什么不自动执行此操作?

它可以:

num = 4
print(num + " other words")
它可以:

num = 4
print(num + " other words")
作者

类型转换 将一种数据类型(整数、字符串、浮点等)的值转换为另一种数据类型的过程称为类型转换。Python有两种类型的类型转换

  • 隐式类型转换
  • 显式类型转换
  • 隐式类型转换 在隐式类型转换中,Python自动将一种数据类型转换为另一种数据类型。此过程不需要任何用户参与

    让我们看一个例子,Python促进了从低级数据类型(整数)到高级数据类型(浮点)的转换,以避免数据丢失

    num = 4
    print(f"{num} other words")
    
    
    当我们运行上述程序时,输出将为:

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))
    
    当我们运行上述程序时,输出将为:

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))
    
    当我们运行上述程序时,输出将为:

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))
    
    num\u int的数据类型: 类型转换前num_str的数据类型: 类型转换后num_str的数据类型: num_int和num_str之和:579 总和的数据类型: 在上述节目中,

    我们添加num_str和num_int变量。 我们使用int()函数将num_str从字符串(较高)类型转换为整数(较低)类型来执行加法。 将num_str转换为整数值后,Python可以添加这两个变量。 我们将num_sum值和数据类型设置为整数

    作者

    类型转换 将一种数据类型(整数、字符串、浮点等)的值转换为另一种数据类型的过程称为类型转换。Python有两种类型的类型转换

  • 隐式类型转换
  • 显式类型转换
  • 隐式类型转换 在隐式类型转换中,Python自动将一种数据类型转换为另一种数据类型。此过程不需要任何用户参与

    让我们看一个例子,Python促进了从低级数据类型(整数)到高级数据类型(浮点)的转换,以避免数据丢失

    num = 4
    print(f"{num} other words")
    
    
    当我们运行上述程序时,输出将为:

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))
    
    当我们运行上述程序时,输出将为:

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))
    
    当我们运行上述程序时,输出将为:

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)
    
    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))
    
    num\u int的数据类型: 类型转换前num_str的数据类型: 类型转换后num_str的数据类型: num_int和num_str之和:579 总和的数据类型: 在上述节目中,

    我们添加num_str和num_int变量。 我们使用int()函数将num_str从字符串(较高)类型转换为整数(较低)类型来执行加法。 将num_str转换为整数值后,Python可以添加这两个变量。 我们将num_sum值和数据类型设置为整数


    print(num,“其他单词”)
    因为。Python是动态但强类型的。它将在哪个方向进行自动转换?
    1+“1”
    应该是
    2
    还是
    “11”
    打印(num,即其他单词)
    。Python是动态但强类型的。它将在哪个方向进行自动转换?应该
    1+“1”
    2
    还是
    “11”
    ?您似乎是从一个链接引用中抄袭了这篇文章。。。推荐人?请注意,您未撰写的任何内容也应被阻止引用。您似乎是从一个链接引用中抄袭了此批发。。。推荐人?请注意,您未编写的任何内容也应被块引用。