整数 & 浮点数整数有两种分别是 和 long其中 最大值是 2147483647 (sys.max)而 long 长度仅受内存大小限制
>>> a = 123
>>> b = 123L
>>> type(a)
<type ''>
>>> type(b)
<type 'long'>
浮点数基本上也没有什么特别的处不过要注意下面写法区别
>>> a = 1
>>> b = 1.0
>>> type(a)
<type ''>
>>> 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]) 4舍 5入
>>> 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# 样串是不可以更改串可以使用单引号(')也可以使用双引号(")或者使用 3引号使其跨越多行
>>> s = """a
b
c
d"""
>>> s
'a\nb\nc\nd'
串同样支持转义符还记得 C# 串前面那个常用 "@" 吗?Python 也有类似东西就是 "r"
// C#
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 = " = [%-5s], = [%05d], float = [%.2f]" % ("a", 5, 3.1415)
>>> s
' = [a ], = [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. swap / title / capitalize 分别将全部每单词首短语首转成大写
>>> "hello, world!".swap
'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 / rfindrxxx 表示找最后个子串, 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: sub 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'
类型转换
转换和多数编程语言类似
>>> ("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'
最新评论