Python 中的類型轉換

在本教程中,我們將學習如何在 Python 中進行類型轉換。

我們遇到不同類型的算術運算,其中涉及多種數據類型,然後相應地獲得結果。

在這裡我們將討論兩者,

  1. 隱式類型轉換
  2. 顯式類型轉換

讓我們藉助程序來理解它們-

隱式類型轉換

在隱式類型轉換期間,用戶不應該在轉換期間提及任何特定的數據類型。

下面的程序說明了如何用 Python 來實現。


#program to demonstrate implicit type conversion
#initializing the value of a
a = 10
print(a)
print("The type of a is ", type(a))
#initializing the value of b
b = 4.5
print(b)
print("The type of b is ", type(b))
#initializing the value of c
c = 4.0
print(c)
print("The type of c is ", type(c))
#initializing the value of d
d = 5.0
print(d)
print("The type of d is ", type(d))
#performing arithmetic operations
res = a * b
print("The product of a and b is ", res)
add = c + d
print("The addition of c and d is ", add)

輸出:

10
The type of a is <class 'int'>
4.5
The type of b is <class 'float'>
4.0
The type of c is <class 'float'>
5.0
The type of d is <class 'float'>
The product of a and b is  45.0
The addition of c and d is  9.0

解釋-

讓我們看一下這個節目的解說。

  1. 為了檢查這些值在執行操作時是如何轉換的,我們初始化了 a、b、c 和 d 的值。
  2. 在此之後,我們檢查了它們每個的數據類型。
  3. 最後,我們對變量 a 和 b 執行加法,對變量 c 和 d 執行乘法。
  4. 在執行上述程序時,我們可以觀察到,在產品的情況下,最終結果是一個浮點值(a 是整數,b 是浮點)。

現在,是時候繼續下一件事了,那就是顯式類型轉換。

顯式類型轉換

在這種類型轉換中,用戶應該在函數中傳遞值以獲得所需的數據類型。

int()、float()和 str()主要用於顯式類型轉換。

考慮下面給出的程序,


#program to demonstrate explicit type conversion
#initializing the value of a
a=10.6
print("The type of 'a' before typecasting is ",type(a))
print(int(a))
print("The type of 'a' after typecasting is",type(a))
#initializing the value of b
b=8.3
print("The type of 'b' before typecasting is ",type(b))
print(int(b))
print("The type of 'b' after typecasting  is",type(b))
#initializing the value of c
c=7
print("The type of 'c' before typecasting is ",type(c))
print(float(c))
print("The type of 'c' after typecasting is",type(c))

輸出:

The type of 'a' before typecasting is  <class 'float'>
10
The type of 'a' after typecasting is <class 'float'>
The type of 'b' before typecasting is  <class 'float'>
8
The type of 'b' after typecasting  is <class 'float'>
The type of 'c' before typecasting is  <class 'int'>
7.0
The type of 'c' after typecasting is <class 'int'>

解釋-

讓我們了解一下我們在這個項目中做了什麼。

  1. 我們已經初始化了 a、b 和 c 的值。
  2. 我們在這個程序中使用了 int()和 float()來查看顯式類型轉換是如何發生的。
  3. 在執行這個程序時,我們可以驗證數據類型是如何變化的。

最後,讓我們看一下本文的最後一個程序,它涵蓋了顯式類型轉換的可能類型。


#program to demonstrate explicit type conversion
#initializing the value of a
a=10
print("The type of 'a' before typecasting is ",type(a))
print(str(a))
print("The type of 'a' after typecasting is",type(a))
#initializing the value of b
b='8'
print("The type of 'b' before typecasting is ",type(b))
print(int(b))
print("The type of 'b' after typecasting  is",type(b))
#initializing the value of c
c='7.9'
print("The type of 'c' before typecasting is ",type(c))
print(float(c))
print("The type of 'c' after typecasting is",type(c))

輸出:

The type of 'a' before typecasting is  <class 'int'>
10
The type of 'a' after typecasting is <class 'int'>
The type of 'b' before typecasting is  <class 'str'>
8
The type of 'b' after typecasting  is <class 'str'>
The type of 'c' before typecasting is  <class 'str'>
7.9
The type of 'c' after typecasting is <class 'str'>

解釋-

該方法類似於上面的程序,在這個程序中,我們包含了使用所有三個 int()、str()和 float()的轉換。

結論

在本教程中,我們學習了什麼是 python 中的類型轉換,它的類型是什麼,以及如何在 Python 中執行。


原創文章,作者:HEYWN,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127529.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
HEYWN的頭像HEYWN
上一篇 2024-10-03 23:15
下一篇 2024-10-03 23:16

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • int類型變量的細節與注意事項

    本文將從 int 類型變量的定義、聲明、初始化、範圍、運算和類型轉換等方面,對 int 類型變量進行詳細闡述和講解,幫助讀者更好地掌握和應用 int 變量。 一、定義與聲明 int…

    編程 2025-04-29

發表回復

登錄後才能評論