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时,可指定编码和错误处理方式

Python基础教程(第3版) 笔记(三)的更多相关文章

  1. Python基础教程(第3版) 笔记(二)

    1.8模块Python提供了完成(某人的年 龄为32.9,并想将这个值向下圆整为32,因为他还没有满33岁)这种任务的函 数floor.导入模块,可以使用特殊命令import.函数floor包含在模块 ...

  2. Python基础教程(第3版) 笔记(一)

    1.1 数和表达式: 除法运算的结果为小数,即浮点数 >>>1/2 0.5 除法运算为整数,使用双斜杠 >>>1//2 0 >>>5.0//2.4 ...

  3. 《python基础教程(第二版)》学习笔记 列表/元组(第2章)

    <python基础教程(第二版)>学习笔记 列表/元组(第2章)序列中的下标从0开始x='ABC' ==> x[0]='A', x[1]='B', x[2]='C'负数索引从右边开始 ...

  4. 《python基础教程(第二版)》学习笔记 文件和素材(第11章)

    <python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...

  5. 《python基础教程(第二版)》学习笔记 类和对象(第7章)

    <python基础教程(第二版)>学习笔记 类和对象(第7章) 定义类class Person:    def setName(self,name):        self.name=n ...

  6. 《python基础教程(第二版)》学习笔记 函数(第6章)

    <python基础教程(第二版)>学习笔记 函数(第6章) 创建函数:def function_name(params):  block  return values 记录函数:def f ...

  7. 《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)

    <python基础教程(第二版)>学习笔记 语句/循环/条件(第5章) print 'AB', 123 ==> AB 123 # 插入了一个空格print 'AB', 'CD' == ...

  8. 《python基础教程(第二版)》学习笔记 字典(第4章)

    <python基础教程(第二版)>学习笔记 字典(第4章)创建字典:d={'key1':'value1','key2':'value2'}lst=[('key1','value1'),(' ...

  9. 《python基础教程(第二版)》学习笔记 字符串(第3章)

    <python基础教程(第二版)>学习笔记 字符串(第3章)所有的基本的序列操作(索引,分片,乘法,判断成员资格,求长度,求最大最小值)对字符串也适用.字符串是不可以改变的:格式化输出字符 ...

随机推荐

  1. xhprof 安装详解

    准备工作1.xhprof不支持php7,需要php7以下版本2.php扩展模块xhprof下载地址: http://pecl.php.net/get/xhprof-0.9.4.tgz xhprof安装 ...

  2. [Solution] 950. Reveal Cards In Increasing Order

    Difficulty: Medium Problem In a deck of cards, every card has a unique integer. You can order the de ...

  3. python中assert详解

    assert基础 官方解释:"Assert statements are a convenient way to insert debugging assertions into a pro ...

  4. python 模块定义导入

    1.定义模块:用来从逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能),本质就是:.py结尾的python文件(文件名:test.py,对应的模块名:test)包:本质就是一个目录(必须 ...

  5. tp3.2 phpexcel 简单导出多个sheet(execl表格)

    参考链接:https://blog.csdn.net/u011341352/article/details/70211962 以下是公共类PHPExcel.php文件: // 开始 <?php/ ...

  6. C++ 实现分数的四则运算

    对分数求加减乘除,以及化简 #include<iostream> #include<math.h> using namespace std; struct Fraction{ ...

  7. go语言 http学习

    net/http库学习 概念 处理器 处理器:拥有ServeHTTP方法的接口(任何类型) 签名:ServeHTTP(http.ResponseWriter, *http.Request) Respo ...

  8. Django模型层(2)

    <!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...

  9. 微软URLRewriter.dll的url重写在目标框架.Net Framework2.0、4.0和应用程序池经典模式、集成模式下的配置

    大家参考几篇园子里面的这篇文章: 文章1:微软URLRewriter.dll的url重写的简单使用 (讲解了使用UrlReWriter.dll的下载.web.config如何在目标框架2.0应用程序池 ...

  10. background-clip 和 background-origin 的区别

    background-origin:指定绘制背景图片的起点. background-clip:是对背景图片的剪裁,指定背景图片的显示范围. 1.background-origin:padding  | ...