by PALAKOLLU SRI MANIKANTA
通過PALAKOLLU SRI MANIKANTA
在五分鐘內學習使用Python進行類型轉換 (Learn typecasting in Python in five minutes)
以非常詳盡的方式介紹了Python中的類型轉換和類型轉換的速成課程 (A crash course on Typecasting and Type conversion in Python in a very non-verbose manner)
鑄件 (TypeCasting)
The process of converting one data type to another data type is called Typecasting or Type Coercion or Type Conversion.
將一種數據類型轉換為另一種數據類型的過程稱為Typecasting或Type Coercion或Type Conversion 。
The topics that I’ll be focusing on in this article are:
我將在本文中重點討論的主題是:
- Implicit Type Conversion 隱式類型轉換
- Explicit Type Conversion 顯式類型轉換
- Advantages 優點
- Disadvantages 缺點
隱式類型轉換 (Implicit Type Conversion)
When the type conversion is performed automatically by the interpreter without the programmer’s intervention, that type of conversion is referred to as implicit type conversion.
當解釋器自動執行類型轉換而無需程序員干預時,該類型的轉換稱為隱式類型轉換 。
示例程序: (Example Program:)
myInt = 143 # Integer value.myFloat = 1.43 # Float value.
myResult = myInt + myFloat # Sum result
print("datatype of myInt:",type(myInt))print("datatype of myFloat:",type(myFloat))
print("Value of myResult:",myResult)print("datatype of myResult:",type(myResult))
輸出: (Output:)
The output for the above program will be:
上面程序的輸出將是:
datatype of myInt: <class 'int'>datatype of myFloat: <class 'float'>Value of myResult: 144.43datatype of myResult: <class 'float'>
In the above program,
在上面的程序中,
- We add two variables myInt and myFloat, storing the value in myResult. 我們添加兩個變量myInt和myFloat,將值存儲在myResult中。
- We will look at the data type of all three objects respectively. 我們將分別查看所有三個對象的數據類型。
In the output, we can see the datatype of myInt is an
integer
, the datatype of myFloat is afloat
.在輸出中,我們可以看到myInt的數據類型是
integer
,myFloat的數據類型是float
。Also, we can see the myFloat has
float
data type because Python converts smaller data type to larger data type to avoid the loss of data.此外,我們可以看到myFloat具有
float
數據類型,因為Python會將較小的數據類型轉換為較大的數據類型,以避免數據丟失。
This type of conversion is called Implicit Type conversion (or) UpCasting.
這種類型的轉換稱為隱式類型轉換 (或) UpCasting 。
顯式類型轉換 (Explicit Type Conversion)
In Explicit Type Conversion, users convert the data type of an object to the required data type. We use predefined in-built functions like:
在“顯式類型轉換”中,用戶將對象的數據類型轉換為所需的數據類型。 我們使用預定義的內置函數,例如:
- int() int()
- float() 浮動()
- complex() 復雜()
- bool() bool()
- str() str()
The syntax for explicit type conversion is:
顯式類型轉換的語法為:
(required_datatype)(expression)
This type of conversion is called Explicit Type conversion (or) DownCasting.
這種類型的轉換稱為顯式 類型轉換 (或DownCasting) 。
整數轉換 (Int Conversion)
We can use this function to convert values from other types to int.
我們可以使用此函數將其他類型的值轉換為int。
For example:
例如:
>>> int(123.654)123
>>>int(False)0
>>> int("10")10
>>> int("10.5")ValueError: invalid literal for int() with base 10: '10.5'
>>> int("ten")ValueError: invalid literal for int() with base 10: 'ten'
>>> int("0B1111")ValueError: invalid literal for int() with base 10: '0B1111'
>>> int(10+3j)TypeError: can't convert complex to int
注意: (Note:)
- You can’t convert complex datatype to int 您不能將復雜的數據類型轉換為int
- If you want to convert string type to int type, the string literal must contain the value in Base-10 如果要將字符串類型轉換為int類型,則字符串文字必須包含Base-10中的值
浮點轉換 (Float Conversion)
This function is used to convert any data type to a floating point number.
此函數用于將任何數據類型轉換為浮點數。
For example:
例如:
>>> float(10) 10.0
>>> float(True)1.0
>>> float(False)0.0
>>> float("10")10.0
>>> float("10.5")10.5
>>> float("ten")ValueError: could not convert string to float: 'ten'
>>> float(10+5j)TypeError: can't convert complex to float
>>> float("0B1111")ValueError: could not convert string to float: '0B1111'
注意: (Note:)
- You can convert complex type to float type value. 您可以將復雜類型轉換為浮點類型值。
- If you want to convert string type to float type, the string literal must contain the value in base-10. 如果要將字符串類型轉換為浮點類型,則字符串文字必須包含以10為底的值。
復合轉換 (Complex Conversion)
This function is used to convert real numbers to a complex (real, imaginary) number.
該功能 用于將實數轉換為復數(實數,虛數)。
表格1:復數(x) (Form 1: complex (x))
You can use this function to convert a single value to a complex number with real part x and imaginary part 0.
您可以使用此函數將單個值轉換為具有實部x和虛部0的復數。
For example:
例如:
>>> complex(10)10+0j
>>> complex(10.5)10.5+0j
>>> complex(True)1+0j
>>> complex(False)0+0j
>>> complex("10")10+0j
>>> complex("10.5")10.5+0j
>>> complex("ten")ValueError: complex() arg is a malformed string
形式2:復數(x,y) (Form 2: complex (x, y))
If you want to convert X and Y into complex number such that X will be real part and Y will be imaginary part.
如果要將X和Y轉換為復數,使得X將是實部而Y將是虛部。
For example:
例如:
>>> complex(10,-2)10-2j
>>> complex(True, False)1+0j
布爾轉換 (Boolean Conversion)
This function is used to convert any data type to boolean data type easily. It is the most flexible data type in Python.
此函數用于輕松地將任何數據類型轉換為布爾數據類型。 它是Python中最靈活的數據類型。
For example:
例如:
>>> bool(0)False
>>> bool(1)True
>>> bool(10)True
>>> bool(0.13332)True
>>> bool(0.0)False
>>> bool(10+6j)True
>>> bool(0+15j)True
>>> bool(0+0j)False
>>> bool("Apple")True
>>> bool("")False
Note: With the help of bool function, you can convert any type of datatype into boolean and the output will be - For all values it will produce True except 0, 0+0j and for an Empty String.
注意:借助bool函數,您可以將任何類型的數據類型轉換為布爾值,并且輸出將為-對于所有值,除0、0 + 0j和空字符串外,它將生成True。
字符串轉換 (String Conversion)
This function is used to convert any type into a string type.
此函數用于將任何類型轉換為字符串類型。
For example:
例如:
>>> str(10)'10'
>>> str(10.5)'10.5'
>>> str(True)'True'
>>> str(False)'False'
>>> str(10+5j)'10+5j'
>>> str(False)'False'
示例程序: (Example Program:)
integer_number = 123 # Intstring_number = "456" # String
print("Data type of integer_number:",type(integer_number))print("Data type of num_str before Type Casting:",type(num_str))
string_number = int(string_number)print("Data type of string_number after Type Casting:",type(string_number))
number_sum = integer_number + string_number
print("Sum of integer_number and num_str:",number_sum)print("Data type of the sum:",type(number_sum))
輸出: (Output:)
When we run the above program the output will be:
當我們運行上面的程序時,輸出將是:
Data type of integer_number: <class 'int'>Data type of num_str before Type Casting: <class 'str'>Data type of string_number after Type Casting: <class 'int'>Sum of integer_number and num_str: 579Data type of the sum: <class 'int'>
In the above program,
在上面的程序中
- We add string_number and integer_number variable. 我們添加string_number和integer_number變量。
We converted string_number from string(higher) to integer(lower) type using
int()
function to perform addition.我們使用
int()
函數將string_number從string(higher)轉換為integer(lower)類型以執行加法。- After converting string_number to an integer value Python adds these two variables. 將string_number轉換為整數值后,Python會添加這兩個變量。
- We got the number_sum value and data type to be an integer. 我們獲得了number_sum值和數據類型為整數。
類型轉換的優點 (Advantages Of Typecasting)
- More convenient to use 使用更方便
類型轉換的缺點 (Disadvantages Of Typecasting)
- More complex type system 更復雜的類型系統
- Source of bugs due to unexpected casts 由于意外的強制轉換導致的錯誤來源
I covered pretty much everything that is required to perform any type of typecasting operation in Python3.
我介紹了在Python3中執行任何類型的類型轉換操作所需的幾乎所有內容。
Hope this helped you learn about Python Typecasting in a quick and easy way.
希望這可以幫助您以快速簡便的方式了解Python類型轉換。
If you liked this article please click on the clap and leave me your valuable feedback.
如果您喜歡本文,請單擊拍手,并留下寶貴的反饋給我。
翻譯自: https://www.freecodecamp.org/news/learn-typecasting-in-python-in-five-minutes-90d42c439743/