提交 277148ca 编写于 作者: W wizardforcel

2019-11-18 22:31:26

上级 426cd8a6
...@@ -3,16 +3,15 @@ ...@@ -3,16 +3,15 @@
> 原文: [https://pythonspot.com/global-local-variables/](https://pythonspot.com/global-local-variables/) > 原文: [https://pythonspot.com/global-local-variables/](https://pythonspot.com/global-local-variables/)
变量有两种类型:**全局变量****局部变量** 变量有两种类型:**全局变量****局部变量**
全局变量可以在代码中的任何位置访问,局部变量只能在[范围](https://pythonspot.com/scope/)中访问。 全局变量可以在代码中的任何位置访问,局部变量只能在[作用域](https://pythonspot.com/scope/)中访问。
![global-local-variable](img/b096b6df86baa4af3f1d9b86a8b57af2.jpg) ![global-local-variable](img/b096b6df86baa4af3f1d9b86a8b57af2.jpg)
A **global variable** (x) can be reached and modified anywhere in the code, **local variable** (z) exists only in block 3. 可以在代码中的任何位置访问和修改**全局变量**`x`),**局部变量**`z`)仅存在于块 3 中。
## 局部变量 ## 局部变量
Local variables can only be reached in their scope. 局部变量只能在其作用域内达到。下面的示例有两个局部变量:`x``y`
The example below has two local variables: x and y.
```py ```py
...@@ -25,7 +24,7 @@ print(sum(8,6)) ...@@ -25,7 +24,7 @@ print(sum(8,6))
``` ```
变量 x 和 y 只能在函数 sum 之内使用,而在函数外部则不存在。 变量 x 和 y 只能在函数 sum 之内使用,而在函数外部则不存在。
局部变量不能在其范围之外使用,此行将不起作用: 局部变量不能在其作用域之外使用,此行将不起作用:
```py ```py
...@@ -35,8 +34,8 @@ print(x) ...@@ -35,8 +34,8 @@ print(x)
## 全局变量 ## 全局变量
A global variable can be used anywhere in the code. 全局变量可以在代码中的任何位置使用。
In the example below we define a global variable z 在下面的示例中,我们定义了全局变量z
```py ```py
...@@ -51,8 +50,7 @@ print(z) ...@@ -51,8 +50,7 @@ print(z)
``` ```
全局变量 z 可以在整个程序中,函数内部或外部使用。 全局变量`z`可以在整个程序中,函数内部或外部使用。可以在函数内部修改全局变量,并为整个程序进行更改:
可以在函数内部修改全局变量,并为整个程序进行更改:
```py ```py
...@@ -71,8 +69,7 @@ print(z) ...@@ -71,8 +69,7 @@ print(z)
## 练习 ## 练习
Local and global variables can be used together in the same program. 局部变量和全局变量可以在同一程序中一起使用。尝试确定该程序的输出:
Try to determine the output of this program:
```py ```py
......
...@@ -9,7 +9,7 @@ Python 是一种通用的计算机编程语言。 ...@@ -9,7 +9,7 @@ Python 是一种通用的计算机编程语言。
## 下载 Python ## 下载 Python
To run Python code, you will need one of these programs: 要运行 Python 代码,您将需要以下程序之一:
* [适用于 Windows,Mac 或 Linux 的 PyCharm](https://www.jetbrains.com/pycharm/) * [适用于 Windows,Mac 或 Linux 的 PyCharm](https://www.jetbrains.com/pycharm/)
...@@ -19,14 +19,15 @@ To run Python code, you will need one of these programs: ...@@ -19,14 +19,15 @@ To run Python code, you will need one of these programs:
* [其他程序](https://pythonspot.com/python-ides/) * [其他程序](https://pythonspot.com/python-ides/)
For terminal only: [Apple Mac OS X](https://www.python.org/downloads/mac-osx/), [Microsoft Windows](https://www.python.org/downloads/windows/), [Linux/UNIX](https://www.python.org/downloads/source/) 仅用于终端:[Apple Mac OS X](https://www.python.org/downloads/mac-osx/)[Microsoft Windows](https://www.python.org/downloads/windows/)[Linux/UNIX](https://www.python.org/downloads/source/)
<标题 ID =“ attachment_3478” align =“ alignnone” width =“ 300”] [<picture><source srcset="/wp-content/uploads/2016/03/pycharm-300x223.png.webp" type="image/webp"> <source srcset="/wp-content/uploads/2016/03/pycharm-300x223.png" type="image/jpeg"> ![pycharm](img/0e5e1650a644a2fdcbb8890c2461f664.jpg) </picture> ](https://www.jetbrains.com/pycharm/) PyCharm, 流行的 Python 编辑器 ![pycharm](img/0e5e1650a644a2fdcbb8890c2461f664.jpg) </picture>
[PyCharm,流行的 Python 编辑器](https://www.jetbrains.com/pycharm/)
## 运行 Python 代码 ## 运行 Python 代码
A python program should be save as a file with a .py extension. python 程序应保存为扩展名为`.py`的文件。试试这个代码:
Try this code:
```py ```py
print("Hello World!") print("Hello World!")
...@@ -34,7 +35,7 @@ print("This is a Python program.") ...@@ -34,7 +35,7 @@ print("This is a Python program.")
``` ```
预期产 预期产
```py ```py
Hello World! Hello World!
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
![python-string](img/504bef57162c863b6a965fac9be10377.jpg) ![python-string](img/504bef57162c863b6a965fac9be10377.jpg)
a string, series of characters 字符串,一系列字符
字符串是一系列字符,它们通常用于显示文本。 字符串是一系列字符,它们通常用于显示文本。
...@@ -12,7 +12,7 @@ a string, series of characters ...@@ -12,7 +12,7 @@ a string, series of characters
## 字符串输入和输出 ## 字符串输入和输出
To output text (string) to the screen: 要将文本(字符串)输出到屏幕:
```py ```py
s = "hello world" s = "hello world"
...@@ -43,7 +43,7 @@ _python –version_ ...@@ -43,7 +43,7 @@ _python –version_
## 字符串比较 ## 字符串比较
To test if two strings are equal use the equality operator (==). 要测试两个字符串是否相等,请使用相等运算符(`==`)。
```py ```py
#!/usr/bin/python #!/usr/bin/python
......
...@@ -8,13 +8,15 @@ ...@@ -8,13 +8,15 @@
## 字符串索引 ## 字符串索引
Python indexes the characters of a string, every index is associated with a unique character. For instance, the characters in the string ‘python’ have indices: Python 为字符串的字符建立索引,每个索引都与一个唯一字符相关联。 例如,字符串`"python"`中的字符具有索引:
[<picture><source srcset="/wp-content/uploads/2016/03/python-string.png.webp" type="image/webp"> <source srcset="/wp-content/uploads/2016/03/python-string.png" type="image/jpeg"> ![String](img/9faa714fe3700fc2441dae01484b73c3.jpg) </picture>](/wp-content/uploads/2016/03/python-string.png) 字符串编号 ![String](img/9faa714fe3700fc2441dae01484b73c3.jpg) </picture>](/wp-content/uploads/2016/03/python-string.png)
字符串编号
## 字符串中的字符 ## 字符串中的字符
The <mark>0th index</mark> is used for the first character of a string. Try the following: 第 0 个索引用于字符串的第一个字符。 请尝试以下操作:
```py ```py
#!/usr/bin/python #!/usr/bin/python
...@@ -28,7 +30,7 @@ print(s[1]) # prints "e" ...@@ -28,7 +30,7 @@ print(s[1]) # prints "e"
## 字符串切片 ## 字符串切片
Given a string s, the syntax for a slice is: 给定字符串`s`,切片的语法为:
```py ```py
s[ startIndex : pastIndex ] s[ startIndex : pastIndex ]
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
> 原文: [https://pythonspot.com/python-variables/](https://pythonspot.com/python-variables/) > 原文: [https://pythonspot.com/python-variables/](https://pythonspot.com/python-variables/)
<figure aria-describedby="caption-attachment-4776" class="wp-caption alignright" id="attachment_4776" style="width: 300px">![python-variables](img/6f6aa6594bc5ce80d77b48a2abe7c75e.jpg) ![python-variables](img/6f6aa6594bc5ce80d77b48a2abe7c75e.jpg)
<figcaption class="wp-caption-text" id="caption-attachment-4776">Variables in Python (x,y,z). They can be used later in the program</figcaption> Python 中的变量(`x``y``z`)。它们可以稍后在程序中使用
</figure>
变量可以保存您可以使用一次或多次的数字。 变量可以保存您可以使用一次或多次的数字。
...@@ -20,7 +18,7 @@ ...@@ -20,7 +18,7 @@
## 数值变量示例 ## 数值变量示例
Example of numeric variables: 数值变量示例:
```py ```py
x = 1 x = 1
...@@ -62,7 +60,7 @@ print(sum) ...@@ -62,7 +60,7 @@ print(sum)
## Python 3 ## Python 3
Use the input() function to get text input, convert to a number using int() or float(). 使用`input()`函数获取文本输入,使用`int()``float()`转换为数字。
```py ```py
#!/usr/bin/env python #!/usr/bin/env python
......
...@@ -6,8 +6,7 @@ ...@@ -6,8 +6,7 @@
## Python 列表 ## Python 列表
We define lists with brackets []. To access the data, these same brackets are used. 我们用方括号`[]`定义列表。 要访问数据,请使用这些相同的括号。列表用法示例:
Example list usage:
```py ```py
#!/usr/bin/python #!/usr/bin/python
...@@ -22,7 +21,7 @@ print(l[1]) # prints second element ...@@ -22,7 +21,7 @@ print(l[1]) # prints second element
## 添加/删除 ## 添加/删除
We can use the functions append() and remove() to manipulate the list. 我们可以使用`append()``remove()`函数来操作列表。
```py ```py
#!/usr/bin/python #!/usr/bin/python
...@@ -40,7 +39,7 @@ print(l) # print all elements. ...@@ -40,7 +39,7 @@ print(l) # print all elements.
## 排序列表 ## 排序列表
We can sort the list using the sort() function. 我们可以使用`sort()`函数对列表进行排序。
```py ```py
#!/usr/bin/python #!/usr/bin/python
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
## `if`语句 ## `if`语句
Consider this application, it executes either the first or second code depending on the value of x. 考虑这个应用程序,它根据`x`的值执行第一或第二个代码。
```py ```py
...@@ -42,13 +42,13 @@ else: ...@@ -42,13 +42,13 @@ else:
## 条件运算符 ## 条件运算符
A word on conditional operators 关于条件运算符
Do not confuse the assignment operator (=) with the equals operator (==). 不要将赋值运算符(`=`)与相等运算符(`==`)混淆。
## 嵌套 ## 嵌套
The most straightforward way to do multiple conditions is nesting: 执行多个条件的最直接的方法是嵌套:
| **运算符** | **说明** | | **运算符** | **说明** |
| != | 不相等 | | != | 不相等 |
...@@ -56,20 +56,16 @@ The most straightforward way to do multiple conditions is nesting: ...@@ -56,20 +56,16 @@ The most straightforward way to do multiple conditions is nesting:
| &gt; | 比...更棒 | | &gt; | 比...更棒 |
| &lt; | 小于 | | &lt; | 小于 |
|
```py ```py
a = 12 a = 12
b = 33 b = 33
if a &gt; 10: if a > 10:
if b &gt; 20: if b > 20:
print("Good") print("Good")
``` ```
|
考虑结合 4 或 6 个条件,这可能很快变得难以阅读。 幸运的是,Python 有一个解决方案,我们可以使用 _ 和 _ 关键字组合条件。 考虑结合 4 或 6 个条件,这可能很快变得难以阅读。 幸运的是,Python 有一个解决方案,我们可以使用 _ 和 _ 关键字组合条件。
```py ```py
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
## 函数定义 ## 函数定义
We use this syntax to define as function: 我们使用以下语法来定义函数:
```py ```py
def function(parameters): def function(parameters):
...@@ -21,7 +21,7 @@ def function(parameters): ...@@ -21,7 +21,7 @@ def function(parameters):
## 实际例子 ## 实际例子
We can call the function using function(parameters). 我们可以使用`function(parameters)`来调用函数。
```py ```py
#!/usr/bin/python #!/usr/bin/python
...@@ -44,7 +44,7 @@ print(f(3)) ...@@ -44,7 +44,7 @@ print(f(3))
## 参数 ## 参数
 We can pass multiple variables: 您可以传递多个变量:
```py ```py
#!/usr/bin/python #!/usr/bin/python
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册