简体   繁体   English

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

[英]What do square brackets mean after a dict variable

I just wanted to know what does the square bracket mean in check[] and what does this square bracket do, because all I know that {} are dictionaries and [] are lists.我只是想知道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)
])

check is a dictionary and check[k] looks up the key k in this dictionary and returns the associated value. check是一个字典, check[k]在这个字典中查找键k并返回相关的值。

This is a "clever" way of writing:这是一种“聪明”的写作方式:

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

if n%2==0 and (n in range(2,6) or n > 20):
    print("Not Weird")
else:
    print("Weird")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM