Python 学习笔记(一)
Aug 24, 2016
以首字母大写的方式显示每个单词:
1
2name = "ada lovelace"
print(name.title()) # Ada Lovelace
删除字符串开头和末尾的空白:
1
2
3
4
5
6
7favorite_language1 = ' Python'
print(favorite_language1.lstrip()) # 'Python'
print(favorite_language1) # ' Python'
favorite_language2 = 'Python '
print(favorite_language2.rstrip()) # 'Python'
print(favorite_language2) # 'Python 'Python 使用两个乘号表示乘方运算:
1
2print(3 ** 2) # 27
print(10 ** 6) # 1000000Python 不能精确表示
0.3
:1
2print(0.2 + 0.1) # 0.30000000000000004
print(3 * 0.1) # 0.30000000000000004使用函数
str()
避免类型错误:1
2
3
4
5
6
7
8
9# wrong
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
# right
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)python 2 与 python 3 之间整数除法的差别:
1
2
3
4
5
6
7
8
9
10
11# python 2
print(3 / 2) # 1
print(3.0 / 2) # 1.5
print(3 / 2.0) # 1.5
print(3.0 / 2.0) # 1.5
# python 3
print(3 / 2) # 1.5
print(3.0 / 2) # 1.5
print(3 / 2.0) # 1.5
print(3.0 / 2.0) # 1.5