新手必看的Python整型的例項操作

2021-10-25 15:16:18 字數 2325 閱讀 2818

說到python整型,對於初學者可能有點陌生,今天就給大家推薦python整型的例項操作。

1、整型、

【例子】通過 print() 可看出 a 的值,以及類 (class) 是int。

a = 1031print(a, type(a))# 1031

python 裡面萬物皆物件(object),整型也不例外,只要是物件,就有相應的屬性 (attributes) 和方法(methods)。

【例子】

b = dir(int)print(b)# [『abs』, 『add』, 『and』, 『bool』, 『ceil』, 『class』,# 『delattr』, 『dir』, 『divmod』, 『doc』, 『eq』,# 『float』, 『floor』, 『floordiv』, 『format』, 『ge』,# 『getattribute』, 『getnewargs』, 『gt』, 『hash』,# 『index』, 『init』, 『init_subclass』, 『int』, 『invert』,# 『le』, 『lshift』, 『lt』, 『mod』, 『mul』, 『ne』,# 『neg』, 『new』, 『or』, 『pos』, 『pow』, 『radd』,# 『rand』, 『rdivmod』, 『reduce』, 『reduce_ex』, 『repr』,# 『rfloordiv』, 『rlshift』, 『rmod』, 『rmul』, 『ror』,# 『round』, 『rpow』, 『rrshift』, 『rshift』, 『rsub』,# 『rtruediv』, 『rxor』, 『setattr』, 『sizeof』, 『str』,# 『sub』, 『subclasshook』, 『truediv』, 『trunc』, 『xor』,# 『bit_length』, 『conjugate』, 『denominator』, 『from_bytes』, 『imag』,# 『numerator』, 『real』, 『to_bytes』]

對它們有個大概印象就可以了,具體怎麼用,需要哪些引數 (argument),還需要查文件。看個bit_length()的例子。

【例子】找到乙個整數的二進位制表示,再返回其長度。

a = 1031print(bin(a)) # 0b10000000111print(a.bit_length()) # 11

以上就是關於python整型的例項操作的全部介紹了,大家可以通過以上方法進行學習操作。

文章部分內容源於網路,聯絡侵刪*

文章**:

C C 呼叫python(新手必看)

目錄 1.背景介紹與程式設計環境 2.完整例項演示 3.問題集錦 4.總結 5.參考鏈結 背景介紹 近年來,python語言憑藉著自身語言的簡單易學 相對於其他程式語言來說 及其功能強大的函式庫 還有其他優點 受到許多程式設計愛好者的青睞,同時十分廣泛的用於人工智慧演算法程式設計中。c c 語言是基...

xpath的使用(新手必看)

步驟 1.從lxml中匯入etree 兩種方式 第一種 from lxml import etree注意 第一種方式,可能etree下方會出現紅線,但是不影響使用 第二種 from lxml import html etree html.etreestr harry potter 29.99 lea...

CSS 的簡寫 新手必看

1.顏色的簡寫 十六進製制形式 如 rrggbb rgb函式值形式 如 rgb x,x,x x 有兩種寫法,一種是0程式設計客棧 255之間整數,另一種是百分號形式 如 20 顏色名稱 如 red green 使用者系統色盤值 如windowtext background 2.單位值的省略 當數值為...