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

2019-11-18 22:31:26

上级 426cd8a6
......@@ -3,16 +3,15 @@
> 原文: [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)
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.
The example below has two local variables: x and y.
局部变量只能在其作用域内达到。下面的示例有两个局部变量:`x``y`
```py
......@@ -25,7 +24,7 @@ print(sum(8,6))
```
变量 x 和 y 只能在函数 sum 之内使用,而在函数外部则不存在。
局部变量不能在其范围之外使用,此行将不起作用:
局部变量不能在其作用域之外使用,此行将不起作用:
```py
......@@ -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
......@@ -51,8 +50,7 @@ print(z)
```
全局变量 z 可以在整个程序中,函数内部或外部使用。
可以在函数内部修改全局变量,并为整个程序进行更改:
全局变量`z`可以在整个程序中,函数内部或外部使用。可以在函数内部修改全局变量,并为整个程序进行更改:
```py
......@@ -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
......
......@@ -9,7 +9,7 @@ Python 是一种通用的计算机编程语言。
## 下载 Python
To run Python code, you will need one of these programs:
要运行 Python 代码,您将需要以下程序之一:
* [适用于 Windows,Mac 或 Linux 的 PyCharm](https://www.jetbrains.com/pycharm/)
......@@ -19,14 +19,15 @@ To run Python code, you will need one of these programs:
* [其他程序](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 代码
A python program should be save as a file with a .py extension.
Try this code:
python 程序应保存为扩展名为`.py`的文件。试试这个代码:
```py
print("Hello World!")
......@@ -34,7 +35,7 @@ print("This is a Python program.")
```
预期产
预期产
```py
Hello World!
......
......@@ -4,7 +4,7 @@
![python-string](img/504bef57162c863b6a965fac9be10377.jpg)
a string, series of characters
字符串,一系列字符
字符串是一系列字符,它们通常用于显示文本。
......@@ -12,7 +12,7 @@ a string, series of characters
## 字符串输入和输出
To output text (string) to the screen:
要将文本(字符串)输出到屏幕:
```py
s = "hello world"
......@@ -43,7 +43,7 @@ _python –version_
## 字符串比较
To test if two strings are equal use the equality operator (==).
要测试两个字符串是否相等,请使用相等运算符(`==`)。
```py
#!/usr/bin/python
......
......@@ -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
#!/usr/bin/python
......@@ -28,7 +30,7 @@ print(s[1]) # prints "e"
## 字符串切片
Given a string s, the syntax for a slice is:
给定字符串`s`,切片的语法为:
```py
s[ startIndex : pastIndex ]
......
......@@ -2,11 +2,9 @@
> 原文: [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>
</figure>
Python 中的变量(`x``y``z`)。它们可以稍后在程序中使用
变量可以保存您可以使用一次或多次的数字。
......@@ -20,7 +18,7 @@
## 数值变量示例
Example of numeric variables:
数值变量示例:
```py
x = 1
......@@ -62,7 +60,7 @@ print(sum)
## Python 3
Use the input() function to get text input, convert to a number using int() or float().
使用`input()`函数获取文本输入,使用`int()``float()`转换为数字。
```py
#!/usr/bin/env python
......
......@@ -6,8 +6,7 @@
## Python 列表
We define lists with brackets []. To access the data, these same brackets are used.
Example list usage:
我们用方括号`[]`定义列表。 要访问数据,请使用这些相同的括号。列表用法示例:
```py
#!/usr/bin/python
......@@ -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
#!/usr/bin/python
......@@ -40,7 +39,7 @@ print(l) # print all elements.
## 排序列表
We can sort the list using the sort() function.
我们可以使用`sort()`函数对列表进行排序。
```py
#!/usr/bin/python
......
......@@ -7,7 +7,7 @@
## `if`语句
Consider this application, it executes either the first or second code depending on the value of x.
考虑这个应用程序,它根据`x`的值执行第一或第二个代码。
```py
......@@ -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:
| &gt; | 比...更棒 |
| &lt; | 小于 |
|
```py
a = 12
b = 33
if a &gt; 10:
if b &gt; 20:
if a > 10:
if b > 20:
print("Good")
```
|
考虑结合 4 或 6 个条件,这可能很快变得难以阅读。 幸运的是,Python 有一个解决方案,我们可以使用 _ 和 _ 关键字组合条件。
```py
......
......@@ -8,7 +8,7 @@
## 函数定义
We use this syntax to define as function:
我们使用以下语法来定义函数:
```py
def function(parameters):
......@@ -21,7 +21,7 @@ def function(parameters):
## 实际例子
We can call the function using function(parameters).
我们可以使用`function(parameters)`来调用函数。
```py
#!/usr/bin/python
......@@ -44,7 +44,7 @@ print(f(3))
## 参数
 We can pass multiple variables:
您可以传递多个变量:
```py
#!/usr/bin/python
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册