整数 & 浮点数 整数有两种,分别是 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'