163.md 9.2 KB
Newer Older
W
wizardforcel 已提交
1
# Python 基础知识 - 又称 Hello World 以及如何实现
W
init  
wizardforcel 已提交
2 3 4

> 原文: [https://javabeginnerstutorial.com/python-tutorial/python-basics-a-k-a-hello-world-and-how-to-achieve-it-2/](https://javabeginnerstutorial.com/python-tutorial/python-basics-a-k-a-hello-world-and-how-to-achieve-it-2/)

W
wizardforcel 已提交
5
在介绍并安装 Python 之后,让我们继续前进,并使用 Python 3 编写第一行代码。
W
init  
wizardforcel 已提交
6

W
wizardforcel 已提交
7
第一步是使用 python3 命令启动交互式解释器。 三个箭头(或更大的符号)指示提示,我们可以在其中键入解释器的命令。 我们将输入以下文本:“`print('We want a shrubbery!')`”,然后按回车键。
W
init  
wizardforcel 已提交
8 9 10

以下代码段显示了您应该看到的内容:

W
wizardforcel 已提交
11
```py
W
init  
wizardforcel 已提交
12 13 14 15 16 17 18
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('We want a shrubbery!')
We want a shrubbery!
```

W
wizardforcel 已提交
19
这是基本的 Python 应用,它将给定的字符串输出到控制台。 基本上,我们将使用打印功能将所有消息写入控制台。
W
init  
wizardforcel 已提交
20 21 22

## 基本语法

W
wizardforcel 已提交
23
Python 的基本语法与其他编程语言非常相似。 但是,有些差异会在开发应用时引起麻烦。
W
init  
wizardforcel 已提交
24

W
wizardforcel 已提交
25
因此,让我们研究 Python 应用的基本语法。
W
init  
wizardforcel 已提交
26 27 28

### 缩进

W
wizardforcel 已提交
29
这是您可能遇到的最令人不安的事情。 如果您没有正确配置 IDE 或使用其他格式设置扩展由他人编写的代码,这可能会在以后让您彻夜难眠。
W
init  
wizardforcel 已提交
30 31 32

**但是缩进是什么意思?**

W
wizardforcel 已提交
33
在执行分组在一起的语句时,使用最广泛的编程语言会使用大括号(`{}`)。 Python 利用缩进,因为这样代码更易读。 让我们用一个小示例来演示一下:
W
init  
wizardforcel 已提交
34

W
wizardforcel 已提交
35
**Java 代码**
W
init  
wizardforcel 已提交
36

W
wizardforcel 已提交
37
```py
W
init  
wizardforcel 已提交
38 39 40
if(a == 42){a = 25; System.out.println(a);}else{a = 42;} 
```

W
wizardforcel 已提交
41
**Python 代码**
W
init  
wizardforcel 已提交
42

W
wizardforcel 已提交
43
```py
W
init  
wizardforcel 已提交
44 45 46 47 48 49 50
if a == 42:
   a = 25
   print(a)
else:
   a = 42
```

W
wizardforcel 已提交
51
如您所见,Python 代码更具可读性(尽管我也是一个有良好基础的 Java 开发人员,但我认为这是我的看法,因此我可以覆盖前面的示例以使其可读)。 如果您有**缩进行(请仔细查看 python 代码中的缩进)**,则表示它属于同一执行组。 例如,在 Java 中,它将不会:
W
init  
wizardforcel 已提交
52

W
wizardforcel 已提交
53
```py
W
init  
wizardforcel 已提交
54 55 56 57 58 59 60
if(a == 42)
   a = 25;
   System.out.println(a); // Indention will not work here. And it will give compilation error
else
   a = 42;
```

W
wizardforcel 已提交
61
如果不使用花括号,则会出现`SyntaxError`,因为在上面的情况下`System.out.println(a);`不属于`if`语句。
W
init  
wizardforcel 已提交
62

W
wizardforcel 已提交
63
**但是**这不是一篇有关 Java 或这两种编程语言之间的差异的文章,因此我从这件事回到*缩进*的主题。
W
init  
wizardforcel 已提交
64

W
wizardforcel 已提交
65
为什么会导致不眠之夜? 因为您有两种缩进方式:**空格****TAB****而且您不能将两者混为一谈!** 如果这样做,则会出现`TabError`
W
init  
wizardforcel 已提交
66

W
wizardforcel 已提交
67
```py
W
init  
wizardforcel 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
>>> a = 33
>>> if a == 42:
...     a = 25  
...     print(a)
File "<stdin>", line 3
   print(a)
           ^
TabError: inconsistent use of tabs and spaces in indentation
```

在上面的示例中不可见,但是首先我使用空格,然后使用制表符使行缩进。 这导致异常。

如果我切换线路,则会收到另一个错误:

W
wizardforcel 已提交
82
```py
W
init  
wizardforcel 已提交
83 84 85 86 87
File "<stdin>", line 3
   a = 25
         ^
```

W
wizardforcel 已提交
88
`IndentationError`:缩进与任何外部缩进级别都不匹配
W
init  
wizardforcel 已提交
89 90 91 92 93 94 95

这就是我不眠之夜的意思。 如果您混淆了所有内容,则必须浏览文件,找到在每个可能的地方都用制表符交换空格,反之亦然。

### 注释

注释代码有时是必要的(尽管一些开发人员说,如果必须编写注释来描述代码,则应重新编写代码)。

W
wizardforcel 已提交
96
您可以使用井号(# )。 这告诉解释器,井号后面的同一行上的文字仅是注释,它们无意执行:
W
init  
wizardforcel 已提交
97

W
wizardforcel 已提交
98
```py
W
init  
wizardforcel 已提交
99 100 101 102
>>> print(1+2) # results in 3 : this is comment
3
```

W
wizardforcel 已提交
103
**注意**与其他编程语言一样,**没有**多行注释。 如果要禁用代码块,则必须在每行的开头放置`#`
W
init  
wizardforcel 已提交
104

W
wizardforcel 已提交
105
### 标识符
W
init  
wizardforcel 已提交
106 107 108

标识符是用于标识变量,类,函数,模块或其他对象的名称。

W
wizardforcel 已提交
109
在 Python 中,您可以以字母(大写或小写无关紧要)或下划线(`_`)开头,并在其后跟随零个或多个字母,数字和下划线。 标识符名称中不允许使用标点符号(例如`@``$``%`)。
W
init  
wizardforcel 已提交
110

W
wizardforcel 已提交
111
该语言区分大小写,这意味着您可以拥有一个名为`my_variable`的变量和另一个名为`My_variable`的变量,它们将被视为不同的变量。 因此**要小心**,因为这在开发时也会引起问题。
W
init  
wizardforcel 已提交
112 113 114 115 116 117 118

### 保留字

高级编程语言几乎是自由形式的代码编写。 但是,有一些内部字词是为特殊用例保留的,当解释器遇到它们时,它们对解释器具有意义。

这意味着您不能将这些关键字用作标识符名称。 在其他情况下,您会根据关键字获得错误。

W
wizardforcel 已提交
119 120 121
```py
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
```
W
init  
wizardforcel 已提交
122

W
wizardforcel 已提交
123
当然,它们是区分大小写的。 这意味着您可以使用`false`作为变量名,但不能使用`False`
W
init  
wizardforcel 已提交
124 125 126

### 用户输入

W
wizardforcel 已提交
127
当您可以编写交互式应用时,编程会变得很有趣。 交互性意味着用户可以向应用输入一些输入,然后他/她将成为应用的答案。
W
init  
wizardforcel 已提交
128

W
wizardforcel 已提交
129
我现在不会从这里开始编写带有用户输入的应用,但是我将告诉您如何要求用户提供一些东西。
W
init  
wizardforcel 已提交
130 131 132

它带有输入功能,并且使用非常简单:

W
wizardforcel 已提交
133
```py
W
init  
wizardforcel 已提交
134 135 136 137 138 139 140 141 142 143 144 145
>>> entered = input("Enter some text: ")
Enter some text: 42
>>> entered
'42'
```

如您所见,您可以将参数传递给输入函数,并且该参数将显示为用户的命令提示符。 他/她输入内容后,将其作为字符串存储在输入的变量中。

稍后,我们将更全面地检查输入功能。

## 基本运算符

W
wizardforcel 已提交
146
现在是时候看看 Python 中可用的基本运算符了。 我知道在不了解基本类型的情况下并没有什么用,但是,嘿! 我们已经知道语言中至少有数字和字符串,这对运算符来说是一个很好的起点。
W
init  
wizardforcel 已提交
147 148 149

### 算术运算符

W
wizardforcel 已提交
150
每个人都知道它们,它们是数学的基础。 因此,Python 也必须了解它们。 如果它们来自正确的类型,它们也可以使用变量。 这意味着您不能在数字和字符串上使用加法。
W
init  
wizardforcel 已提交
151

W
wizardforcel 已提交
152
让我们快速列举一些示例。 我希望这些示例能说明自己。 如果不是这种情况,请随时给我写一封信息,我还将在示例中添加说明性文字。
W
init  
wizardforcel 已提交
153

W
wizardforcel 已提交
154
```py
W
init  
wizardforcel 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
>>> 6 + 5 # the + sign adds numbers up
11
>>> 6 - 5 # the - sign subtracts the second number from the firs
1
>>> 6 * 5 # the * sign multiplies the numbers
30
>>> 6.0 / 5 # the / sign divides the numbers
1.2
>>> 6.0 // 5 # the // sign makes an integer division
1.0
>>> 6 % 5 # the % calculates the modulo
1
>>> 6 ** 5 # the ** sign raises the first number to the power of the second
7776
```

### 比较运算符

W
wizardforcel 已提交
173
这些运算符比较值(变量或值本身不再重要)。 结果来自布尔类型,可以为`True``False`
W
init  
wizardforcel 已提交
174

W
wizardforcel 已提交
175
```py
W
init  
wizardforcel 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
>>> 5 == 4 # == is the equality operator, returns True when both sides are equal
False
>>> 4 == 4
True
>>> 5 != 5 # != is the inequality operator, returns False when both sides are equal
False
>>> 5 > 5 # > is the greater than operator
False
>>> 5 < 5 # < is the less than operator
False
>>> 5 >= 5 # >= is the greater or equal operator
True
>>> 5 <= 5 # is the less or equal operator
True
```

### 赋值运算符

W
wizardforcel 已提交
194
我们已经知道 **Python 中的**赋值运算符,即等号(`=`)。 但是,这些运算符中有相当一部分具有不同的用法,但所有这些运算符都会减少一些编写所需的代码。
W
init  
wizardforcel 已提交
195 196 197

在下面的示例中,我将一遍又一遍地使用相同的变量来说明这些运算符的工作方式。 同样,如果您想知道示例的含义,请随时给我写邮件,并在这些示例中添加说明。

W
wizardforcel 已提交
198
仅输入变量名(在本例中为`a`)并按回车键,便可以在交互式解释器中看到变量的值。
W
init  
wizardforcel 已提交
199

W
wizardforcel 已提交
200
```py
W
init  
wizardforcel 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
>>> a = 5
>>> a += 3 # a has the value of 8
>>> a -= 2 # a has the value of 6
>>> a *= 3 # a has the value of 18
>>> a /= 4 # a has the value of 4.5
>>> a //= 1 # a has the value of 4.0
>>> a %= 6 # a has the value of 4.0
>>> a **= 2 # a has the value of 16.0
>>> a
16.0
```

### 逻辑运算符

它们并不多:和,或不。 当我们看看爱是什么时,我们已经在复活节蛋中遇到了它们。 它们是逻辑运算符,因此最好是在使用它们的地方具有布尔值。

W
wizardforcel 已提交
217
但是,这里有一点例外。 0,空对象和`None`被视为`False`,因此您可以在条件表达式中使用它们(稍后将在后面的文章中使用)。 数字和非空对象当然被视为`True`
W
init  
wizardforcel 已提交
218

W
wizardforcel 已提交
219
现在,让我们看一些示例:
W
init  
wizardforcel 已提交
220

W
wizardforcel 已提交
221
```py
W
init  
wizardforcel 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
>>> a = True
>>> b = False
>>> a and b
False
>>> a or b
True
>>> not a
False
>>> not b
True
>>> not 0
True
>>> not 1
False
>>> not [] # this is an empty list
True
```

W
wizardforcel 已提交
240
### 成员运算符
W
init  
wizardforcel 已提交
241

W
wizardforcel 已提交
242
当我们想查看某物是否为集合的成员时,它会派上用场。 例如,数字 4 在前 5 个质数的列表中。
W
init  
wizardforcel 已提交
243

W
wizardforcel 已提交
244
成员运算符是`in`关键字。 有些书也没有添加到此列表中,但据我们了解,不是一个逻辑运算符,它否定了所使用的值。
W
init  
wizardforcel 已提交
245

W
wizardforcel 已提交
246
结果是布尔值`True``False`
W
init  
wizardforcel 已提交
247

W
wizardforcel 已提交
248
```py
W
init  
wizardforcel 已提交
249 250 251 252 253 254 255 256 257
>>> primes = [2,3,5,7,11]
>>> 4 in primes
False
>>> 4 not in primes
True
>>> 5 in primes
True
```