Python基础教程(第3版) 笔记(三)

1.9.1让脚本像普通程序一样
在UNIX中运行脚本,只需将下面的代码作为脚本的第一行, 就可在UNIX中轻松运行脚本:
#!/usr/bin/env python
要像普通程序一样运行脚本,还必须将其变成可执行的:
$ chmod a+x hello.py
现在,可以像下面这样来运行它(假定当前目录包含在执行路径中):
$ hello.py
如果这不管用,请尝试使用./hello.py
如何学会双击
当双击保存的文件hello.py,程序窗口会立即关闭,根本来不及看清结果,。这是因为程序结束后窗口将立即关闭。尝试修改脚本,在末尾添加如下代码行:
input("Press <enter>")
运行后按回车键后,窗口将会立即关闭,因为程序结束了。
1.9.2注释
井号(#),在代码中,井号后面的所有内容都将被忽略
例:#打印圆的周长
print(2*pi*radius)
注释是为了让人能够轻易的读懂,有些没有必要的注释就不用标出
1.10.1单引号字符串以及对引号转义
>>>"Hello,world!"
'Hello,world!'
这里使用的单引号与双引号是一样的,因为第一个字符串包含一个单引号(就这里而言,可能称之为撇号更合适),因 此不能用单引号将整个字符串括起,否则解释器将报错
>>> 'Let's go!'
SyntaxError: invalid syntax
字符串为'Let',因此Python不知道如何处理后面的s(更准确地说是当前行余下的 内容)
>>> '"Hello, world!" she said'
'"Hello, world!" she said'
字符串包含双引号,因此必须使用单引号将整个字符串括起,原因和前面一样。实 际上,并非必须这样做(这样做只是出于方便考虑)。
>>> 'Let\'s go!'
"Let's go!"
可使用反斜杠(\)对引号进行转义
>>> "\"Hello, world!\" she said"
'"Hello, world!" she said'
例如,在字符串同时包含单引号和双引号(如'Let\'s say "Hello, world!"')时,如果不使用反斜杠进行转义,可通过使用长字符串和原始字 符串(可结合使用这两种字符串)来避免使用反斜杠。
1.10.2 拼接字符串
>>> "Let's say " '"Hello, world!"'
'Let\'s say "Hello, world!"'

下面将他们相加:
>>> "Hello, " + "world!"
'Hello, world!'
>>> x = "Hello, "
>>> y = "world!"
>>> x + y
'Hello, world!'
1.10.3 字符串表示 str 和 repr
保留其在代码中的样子,而不是你希望用户看到的样子。但如果你使用 print,结果将不同。
>>> "Hello, world!"
'Hello, world!'
>>> print("Hello, world!")
Hello, world!
如果再加上表示换行符的编码\n,差别将更明显。
>>> "Hello,\nworld!"
'Hello,\nworld!'
>>> print("Hello,\nworld!")
Hello,
world!
使用函数str和repr
>>>print(repr("Hello,\nworld!")) 保留代码的样子
'Hello,\nworld!'
>>>print(str("Hello,\nworld!")) 转换为用户可视化
Hello,
world!
1.10.4 长字符串、原始字符串和字节
1.长字符串
使用三引号,或使用三个双引号
>>>print('''This is a very long string. It continues here.
And it's not over yet. "Hello, world!"
Still here.''')
This is a very long string. It continues here. And it's not over yet. "Hello, world!" Still here.
实现跨行还可以用在行尾加上反斜杠
例:
>>>print("Hello, \ world!")
Hello, world!
>>> 1 + 2 + \
4 + 5
12
>>> print \
('Hello, world')
Hello, world
2.原始字符串
方法①>>> print('C:\\nowhere')
C:\nowhere
方法②>>> print(r'C:\nowhere')
C:\nowhere
>>> print(r'Let\'s go!')
Let\'s go!

>>> print(r'C:\Program Files\foo\bar' '\\') (请注意,指定原始字符串时,可使用单引号或双引号将其括起,还可使用三引号将其括起。)
C:\Program Files\foo\bar\
3. Unicode、bytes和bytearray
1.11小结
     函 数                       描 述
abs(number)                  返回指定数的绝对值
bytes(string, encoding[, errors])        对指定的字符串进行编码,并以指定的方式处理错误
cmath.sqrt(number)           返回平方根;可用于负数
float(object)                将字符串或数字转换为浮点数
help([object])                 提供交互式帮助
input(prompt)                以字符串的方式获取用户输入
int(object)                   将字符串或数转换为整数
math.ceil(number)              以浮点数的方式返回向上圆整的结果
math.floor(number)                  以浮点数的方式返回向下圆整的结果
math.sqrt(number)                返回平方根;不能用于负数
pow(x, y[, z])                 返回x的y次方对z求模的结果
print(object, ...)                将提供的实参打印出来,并用空格分隔
repr(object)                   返回指定值的字符串表示
round(number[, ndigits])            四舍五入为指定的精度,正好为5时舍入到偶数
str(object)                     将指定的值转换为字符串。用于转换bytes时,可指定编码和错误处理方式

猜你喜欢

转载自www.cnblogs.com/nmlwh/p/10315928.html
今日推荐