用英语学Python | Day3 Python 变量
我前几天看了一段美国物理学家费曼的讲课视频,他用了这样一个有趣的例子展现数学家和物理学家对待同一问题的不同思维方式:
物理学家:“三维空间是什么状况?”
数学家:“给你n维空间的结果”。
物理学家:“可我只想了解三维空间”。
数学家:“那你取n=3就行了”。
这个“n维空间”里和“n”,就是一个变量。这个例子很有趣,有助于理解共性与个性,变量与常量,变量的作用与赋值。
Variables (变量)
Variables are containers for storing data values. 变量是存放数据值的容器。
Creating Variables (创建变量)
Python has no command for declaring a variable. 与其他编程语言不同,Python 没有声明变量的命令。
A variable is created the moment you first assign a value to it. 首次为其赋值时,才会创建变量。
x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they have been set. 变量不需要使用任何特定类型声明,甚至可以在设置后更改其类型。
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
String variables can be declared either by using single or double quotes. 字符串变量可以使用单引号或双引号进行声明。
x = "John"
# is the same as
x = 'John'
Variable Names (变量名称)
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:
A variable name must start with a letter or the underscore character 变量名必须以字母或下划线字符开头
A variable name cannot start with a number 变量名称不能以数字开头
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) 变量名只能包含字母数字字符和下划线(A-z、0-9 和 _)
Variable names are case-sensitive (age, Age and AGE are three different variables) 变量名称区分大小写(age、Age 和 AGE 是三个不同的变量)
Variable names are case-sensitive. 变量名称区分大小写。
Assign Multiple Values (向多个变量赋值)
Python allows you to assign values to multiple variables in one line. Python 允许您在一行中为多个变量赋值。
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
And you can assign the same value to multiple variables in one line. 您可以在一行中为多个变量分配相同的值。
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables (输出变量)
The Python print statement is often used to output variables. Python 的 print 语句通常用于输出变量。
To combine both text and a variable, Python uses the + character. 如需结合文本和变量,Python 使用 + 字符。
x = "awesome"
print("Python is " + x)
You can also use the + character to add a variable to another variable. 您还可以使用 + 字符将变量与另一个变量相加。
x = "Python is "
y = "awesome"
z = x + y
print(z)
For numbers, the + character works as a mathematical operator. 对于数字,+ 字符用作数学运算符。
x = 5
y = 10
print(x + y)
If you try to combine a string and a number, Python will give you an error. 如果您尝试组合字符串和数字,Python 会提示错误。
Global Variables (全局变量)
Variables that are created outside of a function (as in all of the examples above) are known as global variables. 在函数外部创建的变量(如上述所有实例所示)称为全局变量。
Global variables can be used by everyone, both inside of functions and outside. 全局变量可以被函数内部和外部的每个人使用。
Create a variable outside of a function, and use it inside the function. 在函数外部创建变量,并在函数内部使用它:
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value. 如果在函数内部创建具有相同名称的变量,则该变量将是局部变量,并且只能在函数内部使用。具有相同名称的全局变量将保留原样,并拥有原始值。
Create a variable inside a function, with the same name as the global variable. 在函数内部创建一个与全局变量同名的变量。
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
The global Keyword (global 关键字)
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. 通常,在函数内部创建变量时,该变量是局部变量,只能在该函数内部使用。
To create a global variable inside a function, you can use the global keyword. 要在函数内部创建全局变量,您可以使用 global 关键字。
If you use the global keyword, the variable belongs to the global scope. 如果您用了 global 关键字,则该变量属于全局范围。
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Also, use the global keyword if you want to change a global variable inside a function. 另外,如果要在函数内部更改全局变量,请使用 global 关键字。
To change the value of a global variable inside a function, refer to the variable by using the global keyword. 要在函数内部更改全局变量的值,请使用 global 关键字引用该变量。
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
注:英文和中文内容来自www.w3cschool.com。如果想看费曼的这个视频,可以搜微博博主“费曼Bogo”。
我还看到一篇文章,博主把Python的变量类比为标签贴和起名字。下面两张图截取自博客园可优的博文。
喜欢的话,欢迎关注我的微信公众号“英语三年之约”。
个人微信:carolxiang88