-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path10-membership-operators.py
47 lines (37 loc) · 1.33 KB
/
10-membership-operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# HEAD
# Membership Operators
# DESCRIPTION
# Describe the usage of membership operators
# RESOURCES
#
# 'in' operator checks if an item is a part of
# a sequence or iterator
# 'not in' operator checks if an item is not
# a part of a sequence or iterator
lists = [1, 2, 3, 4, 5]
dictions = {"key": "value", "a": "b", "c": "d"}
# Usage with lists for
# 'not in' AND 'in'
# 'in' checks for item in sequence
if (1 in lists):
print("in operator - 1 in lists:", (1 in lists))
# 'in' checks for absence of item in sequence
if (1 not in lists):
print("not in operator - 1 not in lists:", (1 not in lists))
# 'in' checks for absence of item in sequence
if ("b" not in lists):
print("not in operator - 'b' not in lists:", ("b" not in lists))
# Usage with dictions for
# 'not in' AND 'in'
# 'in' checks for item in sequence
# diction.keys() return a sequence
if ("key" in dictions.keys()):
print("in operator - 'key' in dictions.keys():", ("key" in dictions.keys()))
# 'not in' checks for absence of item in sequence
# diction.keys() return a sequence
if ("key" not in dictions.keys()):
print("not in operator - 'key' in dictions.keys():",
("key" not in dictions.keys()))
if ("somekey" not in dictions.keys()):
print("not in operator - 'somekey' in dictions.keys():",
("somekey" not in dictions.keys()))