[英]What do square brackets enclosed in normal brackets do?
For example in the computer variable, what purpose do the square brackets enclosed by the normal brackets server?比如在computer变量中,普通括号服务器里面的方括号有什么作用呢? I am new to python and confused about this.
我是 python 的新手,对此感到困惑。 Thanks in advance!
提前致谢!
import random
def play():
user = input(" 'r' for rock, 'p' for paper, 's' for scissors: ")
computer = random.choice(['r', 'p', 's'])
if user == computer:
return "tie"
whenever you use squared brackets you are defining a list (an array)每当您使用方括号时,您就是在定义一个列表(一个数组)
when you call a function (like random.choice
) need to use () to pass arguments当你调用一个函数时(比如
random.choice
)需要使用 () 来传递参数
so in this case you are calling a function over a list the equivalen should be:所以在这种情况下,你是在一个列表上调用一个函数,等价的应该是:
def play():
user = input(" 'r' for rock, 'p' for paper, 's' for scissors: ")
myList = ['r','p','s']
random.choice(myList)
if user == computer:
return "tie"
this is a paper scissor rock game where the only response is to tie.这是一个纸剪刀摇滚游戏,唯一的反应是打平。 the random.choice function select one element from an input List
random.choice 函数从输入列表中选择一个元素
The square brackets are actually a very simple term in python known as a list.方括号实际上是python中一个非常简单的术语,称为列表。
The code above could also translate to:上面的代码也可以翻译成:
import random #Import random module
userinput = input(" 'r' for rock, 'p' for paper, 's' for scissors: ") #ask user what they want to input
choicelist = ['r', 'p', 's'] #List of options
computer = random.choice(choicelist) #pick a random option from list
if user == computer: #checking if they are both the same
print("tie") #return tie if equal (Also this was a return which only works in functions, but i removed the function because it was not needed)
Please comment if you have any more questions!如果您有更多问题,请发表评论!
function(a)
means, call the function function
with the argument a
. function(a)
表示使用参数a
调用函数function
。
function([a])
means, call the function with the list [a]
as its argument. function([a])
表示以列表[a]
作为参数调用函数。 Equivalently, you could write function(list(a))
等效地,您可以编写
function(list(a))
Both characters have other uses;这两个字符都有其他用途; round parentheses can be used to group expressions, like in mathematics, which in Python also gets used to indicate a tuple.
圆括号可用于对表达式进行分组,就像在数学中一样,在 Python 中也用于表示元组。 So,
所以,
function((a,))
means, call the function with the tuple (a,)
as the argument. function((a,))
表示以元组(a,)
作为参数调用函数。 The comma is necessary to make it into a tuple;逗号是把它变成元组所必需的; just
(a)
is merely the mathematical grouping we mentioned before, saying evaluate a
before ... nothing. just
(a)
只是我们之前提到的数学分组,说之前评估a
......没有。 Equivalently, you could write function(tuple(a))
等效地,您可以编写
function(tuple(a))
Square brackets are also used in indexing, so listvar[a]
means the a
:th element of the list variable listvar
, and dictvar[a]
means get the value for the key a
from the dictionary dictvar
.方括号也用于索引,因此
listvar[a]
表示列表变量listvar
第a
个元素,而dictvar[a]
表示从字典dictvar
获取键a
的值。
For lists, the notation actually allows you to pull out sublists, called slices, in various ways, like listvar[:-a]
or listvar[::]
.对于列表,该符号实际上允许您以各种方式提取子列表,称为切片,例如
listvar[:-a]
或listvar[::]
。 This is complex enough that I'll defer the explanation of it to a separate question: Understanding slice notation这足够复杂,我将把它的解释推迟到一个单独的问题: 理解切片符号
Square brackets are also used for various matrix notations in Numpy and Pandas, but that's not part of the Python core.方括号也用于 Numpy 和 Pandas 中的各种矩阵符号,但这不是 Python 核心的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.