Python官方最简单清晰教程(未完知识见主页)

Literal Constants 文本常量

文本常量的一个示例是 5、1.23 等数字,或者 ‘This is a string’ 或 “It’s a string!” 等字符串。

它被称为 Literals,因为它是 Literal - 您可以按字面意思使用它的值。数字 2 总是代表它自己,而不是其他任何东西 - 它是一个常数,因为它的值无法更改。因此,所有这些都称为 Literal 常量。

Numbers 数字
数字主要有两种类型 - 整数和浮点数。
整数的一个示例是 2,它只是一个整数。
浮点数(或简称浮点数)的示例是 3.23 和 52.3E-4。E 表示法表示 10 的幂。在这种情况下,52.3E-4 表示 52.3 * 10^-4。

给有经验的程序员的注意事项

没有单独的长型。int 类型可以是任意大小的整数。

Strings 字符串
字符串是字符序列。字符串基本上只是一堆单词。
您几乎会在编写的每个 Python 程序中使用字符串,因此请注意以下部分。

Single Quote 单引号
您可以使用单引号指定字符串,例如 ‘Quote me on this’。

引号内的所有空格(即空格和制表符)均按原样保留。

Double Quotes 双引号
双引号中的字符串与单引号中的字符串的工作方式完全相同。例如,“What’s your name?”。

Triple Quotes 三重语录
您可以使用三引号 - (“”“ 或 ‘’') 指定多行字符串。您可以在三引号内自由使用单引号和双引号。例如:

'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''

Strings Are Immutable 字符串是不可变的
这意味着,一旦创建了字符串,就无法更改它。虽然这似乎是一件坏事,但事实并非如此。我们将了解为什么这在我们稍后看到的各种程序中不是一个限制。

Note for C/C++ Programmers
C/C++ 程序员注意事项

Python 中没有单独的 char 数据类型。没有真正的必要,我相信你不会错过它。

Note for Perl/PHP Programmers
Perl/PHP 程序员注意事项

请记住,单引号字符串和双引号字符串是相同的 - 它们没有任何区别。

The format method format 方法
有时我们可能想从其他信息中构造字符串。这就是 format() 方法的用武之地。

将以下行另存为文件str_format.py:

age = 20
name = 'Swaroop'

print('{0} was {1} years old when he wrote this book'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
Output: 输出:

$ python str_format.py
Swaroop was 20 years old when he wrote this book

Why is Swaroop playing with that python?
How It Works 如何运作

字符串可以使用某些规范,随后可以调用 format 方法将这些规范替换为 format 方法的相应参数。

观察我们使用 {0} 的第一个用法,这对应于变量 name,它是 format 方法的第一个参数。同样,第二个规范{1}对应于 age,这是 format 方法的第二个参数。请注意,Python 从 0 开始计数,这意味着第一个位置在索引 0 处,第二个位置在索引 1 处,依此类推。

Notice that we could have achieved the same using string concatenation:
请注意,我们本可以使用字符串连接实现相同的效果:

name + ’ is ’ + str(age) + ’ years old’

但那要丑陋得多,也更容易出错。其次,到 string 的转换将通过 format 方法自动完成,而不是在这种情况下需要显式转换为 string。第三,当使用 format 方法时,我们可以更改消息而不必处理使用的变量,反之亦然。

Also note that the numbers are optional, so you could have also written as:
另请注意,数字是可选的,因此您也可以写成:

age = 20
name = 'Swaroop'

print('{} was {} years old when he wrote this book'.format(name, age))
print('Why is {} playing with that python?'.format(name))

which will give the same exact output as the previous program.
这将提供与上一个程序完全相同的输出。

We can also name the parameters:
我们还可以命名参数:

age = 20
name = 'Swaroop'

print('{name} was {age} years old when he wrote this book'.format(name=name, age=age))
print('Why is {name} playing with that python?'.format(name=name))

which will give the same exact output as the previous program.
这将提供与上一个程序完全相同的输出。

Python 3.6 introduced a shorter way to do named parameters, called “f-strings”:
Python 3.6 引入了一种更短的方法来执行命名参数,称为“f-strings”:

age = 20
name = 'Swaroop'

print(f'{name} was {age} years old when he wrote this book')  # notice the 'f' before the string
print(f'Why is {name} playing with that python?')  # notice the 'f' before the string

which will give the same exact output as the previous program.
这将提供与上一个程序完全相同的输出。

Python 在 format 方法中的作用是它将每个参数值替换为规范的位置。可以有更详细的规格,例如:

decimal (.) precision of 3 for float ‘0.333’

print(‘{0:.3f}’.format(1.0/3))

fill with underscores (_) with the text centered

(^) to 11 width ‘hello

print(‘{0:_^11}’.format(‘hello’))

keyword-based ‘Swaroop wrote A Byte of Python’

print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python'))
Output: 输出:

0.333
___hello___
Swaroop wrote A Byte of Python

由于我们正在讨论格式设置,请注意 print 总是以不可见的 “换行” 字符 (\n) 结尾,因此对 print 的重复调用都将分别打印在单独的行上。要防止打印此换行符,您可以指定它应以空格结尾:

print('a', end='')
print('b', end='')
Output is: 输出为:

ab
Or you can end with a space:
或者,您可以以空格结尾:

print('a', end=' ')
print('b', end=' ')
print('c')
Output is: 输出为:

a b c

Escape Sequences 转义序列

假设,你想要一个包含单引号 (') 的字符串,你将如何指定这个字符串?例如,字符串为 “What’s your name?”。你不能指定 ‘What’s your name?’,因为 Python 会混淆字符串的开始和结束位置。因此,您必须指定此单引号不表示字符串的结尾。这可以借助所谓的转义序列来完成。您将单引号指定为 ’ :注意反斜杠。现在,您可以将字符串指定为 'What’s your name?。

指定此特定字符串的另一种方法是 “What’s your name?”,即使用双引号。同样,您必须使用转义序列在双引号字符串中使用双引号本身。此外,您必须使用转义序列 \ 来指示反斜杠本身。

如果要指定一个两行字符串,该怎么办?一种方法是使用前面所示的三引号字符串,或者你可以对换行符 - \n 使用转义序列来指示新行的开头。例如:

另一个有用的转义序列是制表符:\t。还有更多的转义序列,但我在这里只提到了最有用的。

需要注意的一点是,在字符串中,行尾的单个反斜杠表示字符串在下一行继续,但不添加换行符。例如:

"This is the first sentence. \
This is the second sentence."
is equivalent to 等效于

"This is the first sentence. This is the second sentence."

Raw String 原始字符串

如果需要指定一些不处理特殊处理(如转义序列)的字符串,则需要通过在字符串前加上 r 或 R 前缀来指定原始字符串。例如:

r"Newlines are indicated by \n"
Note for Regular Expression Users
正则表达式用户注意事项

在处理正则表达式时,请始终使用原始字符串。否则,可能需要大量的反击。例如,反向引用可以称为 ‘\1’ 或 r’\1’。

Variable 变量

仅使用字面常量很快就会变得无聊 - 我们需要某种方法来存储任何信息并对其进行操作。这就是变量出现的地方。变量正如其名称所暗示的那样 - 它们的值可以变化,即您可以使用变量存储任何内容。变量只是计算机内存的一部分,用于存储一些信息。与文字常量不同,你需要一些方法来访问这些变量,因此你给它们命名。

Identifier Naming 标识符命名

变量是标识符的示例。标识符是用于识别某物的名称。命名标识符必须遵循一些规则:

标识符的第一个字符必须是字母表(大写 ASCII、小写 ASCII 或 Unicode 字符)或下划线 (_)。

标识符名称的其余部分可以由字母(大写 ASCII 或小写 ASCII 或 Unicode 字符)、下划线 (_) 或数字 (0-9) 组成。

标识符名称区分大小写。例如,myname 和 myName 就不一样了。请注意前者中的小写 n 和后者中的大写 N。

有效标识符名称的示例包括 i、name_2_3。无效标识符名称的示例是 2things,这是隔开的,my-name 和 >a1b2_c3。
Data Types 数据类型

变量可以保存不同类型的值,称为数据类型。基本类型是数字和字符串,我们已经讨论过了。在后面的章节中,我们将看到如何使用 class 创建自己的类型。

Object 对象
请记住,Python 将程序中使用的任何内容称为对象。这是一般意义上的意思。我们不是说 “the something”,而是 “the object”。

Note for Object Oriented Programming users:
面向对象编程用户注意事项:

Python 是很强的面向对象的,因为一切都是对象,包括数字、字符串和函数。

我们现在将看到如何将变量与文本常量一起使用。保存以下示例并运行程序。

How to write Python programs
如何编写 Python 程序
此后,保存和运行 Python 程序的标准过程如下:
For PyCharm 对于 PyCharm
Open PyCharm. 打开 PyCharm。
Create new file with the filename mentioned.
使用上述文件名创建新文件。
Type the program code given in the example.
键入示例中给出的程序代码。
Right-click and run the current file.
右键单击并运行当前文件。
NOTE: Whenever you have to provide command line arguments, click on Run -> Edit Configurations and type the arguments in the Script parameters: section and click the OK button:
注意:每当您必须提供命令行参数时,请单击 Run -> Edit Configurations,然后在 Script parameters: 部分中键入参数,然后单击 OK 按钮:

PyCharm command line arguments

For other editors 对于其他编辑者
Open your editor of choice.
打开您选择的编辑器。
Type the program code given in the example.
键入示例中给出的程序代码。
Save it as a file with the filename mentioned.
将其保存为带有上述文件名的文件。
Run the interpreter with the command python program.py to run the program.
使用命令 python program.py 运行解释器以运行该程序。
Example: Using Variables And Literal Constants
示例:使用变量和文本常量
Type and run the following program:
键入并运行以下程序:

Filename : var.py

i = 5
print(i)
i = i + 1
print(i)

s = '''This is a multi-line string.
This is the second line.'''
print(s)
Output: 输出:

5
6
This is a multi-line string.
This is the second line.

How It Works 如何运作

以下是该程序的工作原理。首先,我们使用赋值运算符 (=) 将文本常量值 5 分配给变量 i。这一行被称为 statement,因为它表明应该做某事,在这种情况下,我们将变量名称 i 连接到值 5。接下来,我们使用 print 语句打印 i 的值,不出所料,它只是将变量的值打印到屏幕上。

Then we add 1 to the value stored in i and store it back. We then print it and expectedly, we get the value 6.
然后我们将 1 添加到存储在 i 中的值并将其存储回去。然后我们打印它,预期会得到值 6。

Similarly, we assign the literal string to the variable s and then print it.
同样,我们将文本字符串分配给变量 s,然后打印它。

Note for static language programmers
静态语言程序员注意事项

Variables are used by just assigning them a value. No declaration or data type definition is needed/used.
只需为变量分配一个值即可使用变量。不需要/使用任何声明或数据类型定义。

Logical And Physical Line
逻辑和物理线

物理线是您在编写程序时看到的内容。逻辑行是 Python 视为单个语句的内容。Python 隐式假设每个物理行对应于一个逻辑行。

逻辑行的一个例子是像 print(‘你好世界’) 这样的语句 - 如果这本身就在一行上(就像你在编辑器中看到的那样),那么这也对应于物理行。

Implicitly, Python encourages the use of a single statement per line which makes code more readable.
隐式地,Python 鼓励每行使用一个语句,这使得代码更具可读性。

如果要在单个物理行上指定多个逻辑行,则必须使用分号 (;) 显式指定此行,该分号表示逻辑行/语句的结尾。例如:

i = 5
print(i)
is effectively same as 实际上与

i = 5;
print(i);
which is also same as
它也与

i = 5; print(i);
and same as ,与

i = 5; print(i)

但是,我强烈建议您坚持在每个物理行上最多编写一个逻辑行。这个想法是你永远不应该使用分号。事实上,我从未在 Python 程序中使用过,甚至从未见过分号。

在一种情况下,这个概念非常有用:如果你有一行很长的代码,你可以使用反斜杠将其分成多个物理行。这称为显式连接线:

s = 'This is a string. \
This continues the string.'
print(s)
Output: 输出:

This is a string. This continues the string.
Similarly, 同样地

i = \
5
is the same as 与

i = 5

有时,存在一个隐式假设,您不需要使用反斜杠。在这种情况下,逻辑行具有起始括号、起始方括号或起始大括号,但没有结束大括号。这称为隐式线连接。当我们在后面的章节中使用 list 编写程序时,你可以看到这一点。

Indentation 凹痕

空格在 Python 中很重要。实际上,行首的空格很重要。这称为缩进。逻辑行开头的前导空格(空格和制表符)用于确定逻辑行的缩进级别,而逻辑行的缩进级别又用于确定语句的分组。

This means that statements which go together must have the same indentation. Each such set of statements is called a block. We will see examples of how blocks are important in later chapters.
这意味着一起的语句必须具有相同的缩进。每组这样的语句都称为一个块。我们将在后面的章节中看到块如何重要的例子。

One thing you should remember is that wrong indentation can give rise to errors. For example:
您应该记住的一件事是错误的缩进会导致错误。例如:

i = 5

Error below! Notice a single space at the start of the line

 print('Value is', i)
print('I repeat, the value is', i)
When you run this, you get the following error:
当您运行此命令时,您会收到以下错误:

  File "whitespace.py", line 3
    print('Value is', i)
    ^
IndentationError: unexpected indent

请注意,第二行的开头只有一个空格。Python 指示的错误告诉我们程序的语法无效,即程序编写不正确。这对你来说意味着你不能任意地启动新的语句块(当然,除了你一直在使用的默认 main 块)。你可以使用新块的情况将在后面的章节中详细介绍,例如 控制流。

How to indent 如何缩进

使用 4 个空格进行缩进。这是官方的 Python 语言建议。优秀的编辑会自动为您执行此操作。请确保使用一致数量的空格进行缩进,否则程序将无法运行或出现意外行为。

Note to static language programmers
静态语言程序员注意事项

Python 将始终对块使用缩进,并且永远不会使用大括号。运行 from future import braces 以了解更多信息。
运算符和表达式
Most statements (logical lines) that you write will contain expressions. A simple example of an expression is 2 + 3. An expression can be broken down into operators and operands.
您编写的大多数语句(逻辑行)都将包含表达式。表达式的一个简单示例是 2 + 3。表达式可以分解为运算符和操作数。

Operators are functionality that do something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data is called operands. In this case, 2 and 3 are the operands.
运算符是执行某项操作的功能,可以用 + 等符号或特殊关键字表示。运算符需要一些数据才能操作,这些数据称为操作数。在本例中,2 和 3 是操作数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值