163.md 9.3 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 11 12 13 14 15 16 17 18

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

```java
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 37 38 39 40

```java
if(a == 42){a = 25; System.out.println(a);}else{a = 42;} 
```

W
wizardforcel 已提交
41
**Python 代码**
W
init  
wizardforcel 已提交
42 43 44 45 46 47 48 49 50

```java
if a == 42:
   a = 25
   print(a)
else:
   a = 42
```

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

```java
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

```java
>>> a = 33
>>> if a == 42:
...     a = 25  
...     print(a)
File "<stdin>", line 3
   print(a)
           ^
TabError: inconsistent use of tabs and spaces in indentation
```

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

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

```java
File "<stdin>", line 3
   a = 25
         ^
```

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

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

### 注释

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

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

```java
>>> print(1+2) # results in 3 : this is comment
3
```

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

### 保留字

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

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

|假|类|最后|是|返回| |无|继续|对于| lambda |尝试| |真| def |来自|非本地| while | |和| del | global |不| with | |为| ifif |如果|或| yield | |声明|其他|导入|通过|| |中断|除了|提高||

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

### 用户输入

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

W
wizardforcel 已提交
127
我现在不会从这里开始编写带有用户输入的应用,但是我将告诉您如何要求用户提供一些东西。
W
init  
wizardforcel 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

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

```java
>>> entered = input("Enter some text: ")
Enter some text: 42
>>> entered
'42'
```

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

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

## 基本运算符

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

### 算术运算符

W
wizardforcel 已提交
148
每个人都知道它们,它们是数学的基础。 因此,Python 也必须了解它们。 如果它们来自正确的 tpye,它们也可以使用变量。 这意味着您不能在数字和字符串上使用加法。
W
init  
wizardforcel 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

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

```java
>>> 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 已提交
171
这些运算符比较值(变量或值本身不再重要)。 结果来自布尔类型,可以为 True 或 False。
W
init  
wizardforcel 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

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

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

W
wizardforcel 已提交
196
仅输入变量名(在本例中为 a)并按回车键,便可以在交互式解释器中看到变量的值。
W
init  
wizardforcel 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

```java
>>> 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 已提交
215
但是,这里有一点例外。 0,空对象和 None 被视为 False,因此您可以在条件表达式中使用它们(稍后将在后面的文章中使用)。 数字和非空对象当然被视为 True。
W
init  
wizardforcel 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

现在,让我们看一些例子:

```java
>>> 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
当我们想查看某物是否为集合的成员时,它会派上用场。 例如,数字 4 在前 5 个质数的列表中。
W
init  
wizardforcel 已提交
241

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

W
wizardforcel 已提交
244
结果是布尔值 True 或 False。
W
init  
wizardforcel 已提交
245 246 247 248 249 250 251 252 253 254 255

```java
>>> primes = [2,3,5,7,11]
>>> 4 in primes
False
>>> 4 not in primes
True
>>> 5 in primes
True
```