Python中的簡單類型

整數 & 浮點數

整數有兩種,分別是 int 和 long。其中 int 最大值是 2147483647 (sys.maxint),而 long 長度僅受內存大小限制。
>>> a = 123
>>> b = 123L
>>> type(a)
<type 'int'>
>>> type(b)
<type 'long'>

浮點數基本上也沒有什麼特別之處,不過要注意下面寫法不同。
>>> a = 1
>>> b = 1.0
>>> type(a)
<type 'int'>
>>> type(b)
<type 'float'>

和數字有關的函數有:

1. abs(x) 取絕對值。
>>> abs(-10)
10

2. coerce(x, y) 將兩個數字轉換成相同類型。
>>> a = 1.0
>>> b = 2
>>> s = coerce(a, b)
>>> type(s)
<type 'tuple'>
>>> s
(1.0, 2.0)

3. divmode(a, b) 獲取商和餘數。返回一個 tuple,如 (2, 1) 分別是商和餘數。
>>> s = divmod(5, 2)
>>> s
(2, 1)

4. pow(x, y) 取冪,和 ** 操作符意義相同。
>>> pow(2, 3)
8
>>> 2 ** 3
8

5. round(x, [n]) 四捨五入
>>> round(2.4567, 2)
2.46

6. min(x [, y, z...]) 返回最小的一個數。
>>> min(5, 4, 3, 2, 1)
1

7. max(x [, y, z...]) 返回最大的一個數。
>>> min(5, 4, 3, 2, 1)
5

8. cmp(x, y) 比較數字。x > y 返回 1, x == y 返回 0, x < y 返回 -1。
>>> cmp(2, 1)
1
>>> cmp(1, 1)
0
>>> cmp(1, 2)
-1

字符串

Python 中沒有字符(char)類型,而且和 C# 一樣,字符串是不可以更改的。字符串可以使用單引號(')也可以使用雙引號("),或者使用三引號使其跨越多行。
>>> s = """a
b
c
d"""
>>> s
'a\nb\nc\nd'

字符串同樣支持轉義符。還記得 C# 字符串前面那個常用的 "@" 嗎?Python 也有類似的東西,就是 "r"。
// C#
string s = @"c:\windows\notepad.exe";

// Python
s = r"c:\windows\notepand.exe"

比較有意思的是,Python 中的字符串支持使用乘號來創建一個連續字符串。如:
>>> s = "abc" * 6
>>> s
'abcabcabcabcabcabc'

儘管沒有字符類型,但依然可以使用索引號來獲取字符串中的字符。
>>> s = "abc"
>>> s[0]
'a'

Python 擁有非常方便的切片處理能力,我們可以使用負索引號從字符串結尾進行索引。
>>> s = "abcdefg"
>>> s[1:-2]
'bcde'

這裏需要提一下,Python 比較古怪的多變量賦值方式。
>>> a, b, c = (1, 2, 3)
>>> a
1
>>> b
2
>>> c
3
>>> a, b, c = "123"
>>> a
'1'
>>> b
'2'
>>> c
'3'

Python 同樣支持格式化字符串,類似 C# 中的 String.Format。包括各種類型以及固定寬度輸出。
>>> s = "string = [%-5s], int = [%05d], float = [%.2f]" % ("a", 5, 3.1415)
>>> s
'string = [a ], int = [00005], float = [3.14]'

Python 使用如下方式支持 Unicode。
>>> s = u"abc"
>>> type(s)
<type 'unicode'>
>>> s += "sss"
>>> s
u'abcsss'
>>> a = str(s)
>>> a
'abcsss'
>>> unichr(97)
u'a'

和字符串相關的常用函數有:

1. lstrip() / rstrip() / strip() 好像多數語言都命名爲 LTrim() / RTrim() / Trim()。
>>> " abc ".strip()
'abc'

2. expandtabs() 將 TAB 替換成指定數量的空格。
>>> "\tabc".expandtabs(2)
' abc'

3. lower() / upper() 大小寫轉換。
>>> "ABC".lower()
'abc'
>>> "abc".upper()
'ABC'

4. swapcase() / title() / capitalize() 分別將全部字符,每單詞首字符,短語首字符轉成大寫。
>>> "hello, world!".swapcase()
'HELLO, WORLD!'
>>> "hello, world!".title()
'Hello, World!'
>>> "Hello, World!".capitalize()
'Hello, world!'

5. isxxxx 判斷字符串... 沒啥好說的。
>>> "abcd".isalpha()
True
>>> "abcd".isalnum()
True
>>> "abcd".isdigit()
False
>>> "1abc".isdigit()
False
>>> "123".isdigit()
True
>>> " ".isspace()
True
>>> " ".isupper()
False

6. find() 查找子串,類似的還有 index() / rindex() / rfind()。rxxx 表示找最後一個子串, index 在找不到時會觸發異常。
>>> "abcdefg".find("d", 1, -1)
3
>>> "abcdefg".find("d", 1, -4)
-1
>>> "aa1111aaa".rfind("aaa")
6
>>> "aa1111aaa".index("b")

Traceback (most recent call last):
 File "<pyshell#87>", line 1, in <module>
 "aa1111aaa".index("b")
ValueError: substring not found

7. startwith() / endwith() 判斷是否以某個子串開始或結束。

8. count() 統計子串數量。

9. replace() 替換子串
>>> "abc".replace("b", "1")
'a1c'

10. splite() 分解字符串。
>>> "a b c d".split(" ")
['a', 'b', 'c', 'd']
>>> "a b c ".split(" ", 2)
['a', 'b', 'c d']

11. join() 連接字符串。
>>> "|".join(["a", "b", "c"])
'a|b|c'

類型轉換

轉換函數和多數編程語言類似。
>>> int("123")
123
>>> long("123")
123L
>>> float("123.45")
123.45
>>> float(123)
123.0
>>> float(123L)
123.0
>>> ord("a")
97
>>> chr(97)
'a'
>>> hex(97)
'0x61'
>>> str(123)
'123'
發佈了18 篇原創文章 · 獲贊 1 · 訪問量 3466
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章