溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python函數(shù)中的三大主要應用形式

發(fā)布時間:2021-08-25 15:41:43 來源:億速云 閱讀:133 作者:chen 欄目:編程語言

本篇內容主要講解“Python函數(shù)中的三大主要應用形式”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Python函數(shù)中的三大主要應用形式”吧!

Python函數(shù)在使用的時候有幾大分類,其中以int函數(shù),float函數(shù)和str函數(shù)的應用最為廣泛。下面我們就看看具體的環(huán)境中如何進行相關的函數(shù)代碼的編寫。

(1)把符合數(shù)學格式的數(shù)字型字符串轉換成整數(shù)

(2)把浮點數(shù)轉換成整數(shù),但是只是簡單的取整,而非四舍五入。

舉例:

aa = int("124") #Correct   print "aa = ", aa #result=124   bb = int(123.45) #correct   print "bb = ", bb #result=123   cc = int("-123.45") #Error,Can't Convert to int   print "cc = ",cc   dd = int("34a") #Error,Can't Convert to int   print "dd = ",dd   ee = int("12.3") #Error,Can't Convert to int  print ee

float函數(shù)將整數(shù)和字符串轉換成浮點數(shù)。

舉例:

aa = float("124") #Correct   print "aa = ", aa #result = 124.0   bb = float("123.45") #Correct   print "bb = ", bb #result = 123.45   cc = float(-123.6) #Correct   print "cc = ",cc #result = -123.6   dd = float("-123.34") #Correct   print "dd = ",dd #result = -123.34   ee = float('123v') #Error,Can't Convert to float  print ee

Python str函數(shù)將數(shù)字轉換成字符舉例:

aa = str(123.4) #Correct   print aa #result = '123.4'   bb = str(-124.a) #SyntaxError: invalid syntax   print bb   cc = str("-123.45") #correct   print cc #result = '-123.45'   dd = str('ddd') #correct   print dd #result = ddd   ee = str(-124.3) #correct  print ee #result = -124.3

到此,相信大家對“Python函數(shù)中的三大主要應用形式”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI