int() 函數用來將字串會數字轉換為整數型。接下來透過本文來跟大家介紹python 中的int()函數的相關知識,有興趣的朋友一起看看吧
int(x, [base])
#功能:
函數的作用是將一個數字或base類型的字串轉換成整數。
函數原型:
int(x=0)
int(x, base=10),base缺省值為10,也就是說不指定base的值時,函數將x以十進位處理。
適用Python版本:
Python2.x
Python3.x
注意:
#1. x 可以是數字或字串,但是base被賦值後x 只能是字串
2. x 作為字串時必須是base 類型,也就是說x 變成數字時必須能用base 進位表示
Python英文文件解釋:
class int(x=0)
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.##If x x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by spaceor - with no base. Optionally, the literal can be preceded by spaceor - withen 產品whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base -2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a literals in code. Base 0 means to interpret exactly as a ualcode the , that ual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
The integer type is described in Numeric Types — int, float, complex.
Changed in version 3.4: If base is not an instance of int and the base object has a base.__ for the base. Previous versions used base.__int__ instead of base.__index__.
Changed in version 3.6: Grouping digits with underscores as in code literals is allowed.
################################################### #########1. x 是數字的情況:############
int(3.14) # 3 int(2e2) # 200 int(100, 2) # 出错,base 被赋值后函数只接收字符串############2. x 是字串的情況:############
int('23', 16) # 35 int('Pythontab', 8) # 出错,Pythontab不是个8进制数#########3. base 可取值範圍是2~36,囊括了所有的英文字母(不區分大小寫),十六進位中F表示15,那麼G將在二十進位中表示16,依此類推....Z在三十六進位中表示35#########
int('FZ', 16) # 出错,FZ不能用十六进制表示 int('FZ', 36) # 575###### ###4. 字串0x 可以出現在十六進位中,視為十六進位的符號,同理0b 可以出現在二進位中,除此之外視作數字0 和字母x##### ####
int('0x10', 16) # 16,0x是十六进制的符号 int('0x10', 17) # 出错,'0x10'中的 x 被视作英文字母 x int('0x10', 36) # 42804,36进制包含字母 x#########相關推薦:############################################################################################################################ python函數之bin()函數詳解########################
以上是python 中的int()函數怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

Dreamweaver CS6
視覺化網頁開發工具