List Operations
| Operation | Example | Result |
|---|---|---|
len() | len([1,2,3]) | 3 |
Concatenation + | [1,2] + [3,4] | [1,2,3,4] |
Repetition * | [1,2] * 3 | [1,2,1,2,1,2] |
in | 3 in [1,2,3] | True |
not in | 5 not in [1,2,3] | True |
max() | max([3,1,4,1,5]) | 5 |
min() | min([3,1,4,1,5]) | 1 |
sum() | sum([1,2,3]) | 6 |
all() | all([1,1,0]) | False |
any() | any([0,0,1]) | True |
a = [1, 2, 3]
b = [4, 5]
print(len(a)) # 3
print(a + b) # [1, 2, 3, 4, 5]
print(a * 2) # [1, 2, 3, 1, 2, 3]
print(3 in a) # True
print(10 not in a) # True
max(), min(), sum()
nums = [23, 5, 89, 12, 67]
print(max(nums)) # 89
print(min(nums)) # 5
print(sum(nums)) # 196
print(sum(nums) / len(nums)) # 39.2 (average)
all() and any()
all() returns True only if every element is truthy; any() returns True if at least one element is truthy.
marks = [45, 67, 89, 33]
print(all(m >= 40 for m in marks)) # False (33 < 40)
print(any(m >= 90 for m in marks)) # False (no one scored 90+)
print(any(m < 40 for m in marks)) # True (33 failed)
print(all([1, 2, 3])) # True -- all non-zero
print(all([1, 0, 3])) # False -- 0 is falsy
print(any([0, 0, 0])) # False
print(any([0, 0, 5])) # True
List Comprehension (bonus — concise list creation)
squares = [x ** 2 for x in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
evens = [x for x in range(20) if x % 2 == 0]
print(evens) # [0, 2, 4, ..., 18]