python基础入门----类型转换

原文链接:https://www.programiz.com/python-programming/type-conversion-and-casting

Table of Contents

Before learning Type Conversion in Python, you should have knowledge about Python Data Types.

表格内容

  • 类型转换
  • 隐式转换
  • 显式转换
  • 要记住的关键点

之前在python中学习了类型转换,你应该已经了解python的数据类型。


Type Conversion:

The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

类型转换:

一种数据类型(整数,字符串,浮点数,等)的值转换成另外一种数据类型的过程叫类型转换。python有2种类型的数据转换。

  1. 隐式转换
  2. 显示转换

Implicit Type Conversion:

In Implicit type conversion, Python automatically converts one data type to another data type. This process doesn't need any user involvement.

Let's see an example where Python promotes conversion of lower datatype (integer) to higher data type (float) to avoid data loss.

Example 1: Converting integer to float

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))

When we run the above program, the output will be

datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>

Value of num_new: 124.23
datatype of num_new: <class 'float'>

隐式类型的转换:

在隐式类型转换中,python自动转换一种数据类型到另外一种数据类型。整个过程我们不需要参与。

让我们看一个例子,python促进低数据类型(整数)到高数据类型(float)的转换可以避免数据丢失。

例子1:整数转换到浮点数

当我们执行以上程序,结果输出为

>>> num_int = 123
>>> num_flo = 1.23
>>> num_new = num_int + num_flo
>>> print("datatype of num_int:",type(num_int))
datatype of num_int: <class 'int'>
>>> print("datatype of num_flo:",type(num_flo))
datatype of num_flo: <class 'float'>
>>> print("Value of num_new:",num_new)
Value of num_new: 124.23
>>> print("datatype of num_new:",type(num_new))
datatype of num_new: <class 'float'>

In the above program,

  • We add two variables num_int and num_flo, storing the value in num_new.
  • We the data type of all three objects respectively.
  • In the output we can see the datatype of num_int is an integer, datatype of num_flo is a float.
  • Also, we can see the num_new has float data type because Python always converts smaller data type to larger data type to avoid the loss of data.

在以上程序,

  • 我们添加2个变量num_int 和num_flo,值存储在num_new.
  • 我们分别看这3个数据类型
  • 在输出中我们看到num_int的数据类型是一个整数,num_flo数据类型是一个浮点数。
  • 还有,我们看到num_new是浮点数据类型,因为总是把小的数据类型转换到大的数据类型为了避免数据丢失。

Now, let's try adding a string and an integer, and see how Python treats it.

Example 2: Addition of string(higher) data type and integer(lower) datatype

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)

When we run the above program, the output will be

Data type of num_int: <class 'int'> 
Data type of num_str: <class 'str'> 

Traceback (most recent call last): 
  File "python", line 7, in <module> 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

现在,让我们尝试添加一个字符串和整数,看python是如何对待它的。

例子2:字符串(高)数据类型和整数(低)数据类型相加

当我们执行以上程序,结果输出为

>>> num_int = 123
>>> num_str = "456"
>>> print("Data type of num_int: ",type(num_int))
Data type of num_int:  <class 'int'>
>>> print("Data type of num_str:",type(num_str))
Data type of num_str: <class 'str'>
>>> print(num_int+num_str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In the above program,

  • We add two variable num_int and num_str.
  • As we can see from the output, we got typeerror. Python is not able use Implicit Conversion in such condition.
  • However Python has the solution for this type of situation which is know as Explicit Conversion.

以上程序,

  • 我们添加2个变量num_int 和num_str。
  • 当我们看到这个输出,我们获取到typeerror(类型错误)。python不能使用显示转换像这样的条件。
  • 然而python有解决这类类型,当我们了解了隐式转换。

Explicit Type Conversion:

In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int()float()str(), etc to perform explicit type conversion.

This type conversion is also called typecasting because the user casts (change) the data type of the objects.

Syntax :

(required_datatype)(expression)

Typecasting can be done by assigning the required data type function to the expression.

隐式类型转换:

在隐式类型转换中,我们转换的一类数据类型是需要的数据类型。我们需要预订函数,像int(),float(),str()等去执行隐式转换。

这种类型转换也叫类型转换,因为我们的强制转换的(改变)的对象数据类型。

语法:

(required_datatype)(expression)

强制转换所需要的数据类型函数需要分配表达式完成。


Example 3: Addition of string and integer using explicit conversion

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))

When we run the above program, the output will be

Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>

Data type of num_str after Type Casting: <class 'int'>

Sum of num_int and num_str: 579
Data type of the sum: <class 'int'>

例子3:字符串和整数需要隐式转换相加

当我们执行以上程序,输出结果

>>> num_int = 123
>>> num_str = "456"
>>> print("Data type of num_int:",type(num_int))
Data type of num_int: <class 'int'>
>>> print("Data type of num_str before Type Casting:",type(num_str))
Data type of num_str before Type Casting: <class 'str'>
>>> num_str = int(num_str)
>>> print("Data type of num_str after Type Casting:",type(num_str))
Data type of num_str after Type Casting: <class 'int'>
>>> num_sum = num_int + num_str
>>> print("Sum of num_int and num_str:",num_sum)
Sum of num_int and num_str: 579
>>> print("Data type of the sum:",type(num_sum))
Data type of the sum: <class 'int'>

In above program,

  • We add num_str and num_int variable.
  • We converted num_str from string(higher) to integer(lower) type using int() function to perform the addition.
  • After converting num_str to a integer value Python is able to add these two variable.
  • We got the num_sum value and data type to be integer.

以上程序,

  • 我们添加num_str 和 num_int变量。
  • 我们转换num_str从字符串(高)转换到整数(低)类型使用int()函数去执行加法。
  • 在python中转换num_str为一个整数值后是能将这2个变量相加。
  • 我们将获得num_sum值和数据类型变成的整数。

Key Points to Remember:

  1. Type Conversion is the conversion of object from one data type to another data type.
  2. Implicit Type Conversion is automatically performed by the Python interpreter.
  3. Python avoids the loss of data in Implicit Type Conversion.
  4. Explicit Type Conversion is also called Type Casting, the data types of object are converted using predefined function by user.
  5. In Type Casting loss of data may occur as we enforce the object to specific data type.

需要记住的关键点:

  1. 类型转换是一个对象从一个数据类型转换到另外一个数据类型。
  2. 在python解释器中显示类型是自动被执行转换的。
  3. 在显示类型转换中python避免数据丢失。
  4. 隐式类型转换也叫强制类型转换,用户在把一个对象的数据类型转换时需要预定义。
  5. 在类型强制转换可能发生数据丢失,当我们的对象执行转换为制定的数据类型时。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值