dict变量后面的方括号是什么意思?

问题描述 投票:-2回答:1

我只是想知道方括号在这里是什么意思?check[] 这个方括号有什么作用,因为我只知道 {} 是字典和 [] 是列表。

n = int(input().strip())
check = {True: "Not Weird", False: "Weird"}

print(check[
    n%2==0 and (
        n in range(2,6) or 
        n > 20)
])
python dictionary syntax
1个回答
2
投票

check 是一个字典,而 check[k] 寻章摘句 k 并返回相关的值。

这是一种 "聪明 "的写法。

n = int(input().strip())

if n%2==0 and (n in range(2,6) or n > 20):
    print("Not Weird")
else:
    print("Weird")
© www.soinside.com 2019 - 2024. All rights reserved.