# 故事背景

``````requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:

# 出现转折

``````requeseted_toppings = ['mushrooms', 'onions', 'pineapple']
a = 'mushrooms', 'onions' in requeseted_toppings
print(a)
print(type(a))``````

``````('mushrooms', True)
<class 'tuple'>``````

# WHY?

``'mushrooms', 'onions' in ['mushrooms', 'onions', 'pineapple']``

1. 于 () 符号中输入资料，并且以逗号区隔。
2. 不加 () 符号，直接指派资料，并且以逗号区隔，Python会预设为Tuples(元组)资料型态，不过要注意的是如果只有一个资料，一定要再加一个逗号。
3. 使用tuple()方法，传入Iterable(可叠代的)物件来建立Tuples(元组)。
4. 使用 * 符号来建立Tuples(元组)。

``````def all_in(subset, lst):
return all(map(lambda x:x in lst, subset))

subset1 = ('mushrooms', 'onions')
subset2 = ('mushrooms', 'onions', 'apple')
lst     = ['mushrooms', 'onions', 'pineapple']

print(all_in(subset1, lst))
print(all_in(subset2, lst))``````
``````True
False``````

``````subset1 = {'mushrooms', 'onions'}
subset2 = {'mushrooms', 'onions', 'apple'}
fullset = {'mushrooms', 'onions', 'pineapple'}

print(subset1.issubset(fullset))
print(subset2.issubset(fullset))``````
``````True
False``````
1个月前 评论
txmsuper （楼主） 1个月前

1个月前 评论

`a = 'mushrooms', 'onions' in requeseted_toppings` 只不过是省略了括号而已。其实你应该也见过，`return a, b` 这种形式的返回值，这也是 `tuple`

1个月前 评论
txmsuper （楼主） 1个月前

1天前 评论