- int: Integer numbers
x = 10 - float: Floating-point numbers
pi = 3.14 - complex: Complex numbers
z = 2 + 3j - bool: Boolean values
is_active = True - str: Strings (text)
name = "Alice" - NoneType: Represents no value
value = None
Ordered, mutable collection
fruits = ["apple", "banana", "cherry"]
Common Methods:
append(x)remove(x)pop([i])sort()reverse()
Ordered, immutable collection
coordinates = (10, 20)
Common Methods:
count(x)index(x)
Unordered collection of unique items
unique_numbers = {1, 2, 3}
Common Methods:
add(x)remove(x)union(other_set)intersection(other_set)
Unordered collection of key-value pairs
person = {"name": "Alice", "age": 25}
Common Methods:
keys()values()items()get(key, default)update(other_dict)
Immutable set
frozen = frozenset([1, 2, 3])
lower():"Hello".lower()→'hello'upper():"Hello".upper()→'HELLO'strip():" hi ".strip()→'hi'replace(old, new):"hello world".replace("world", "Python")→'hello Python'split(sep):"a,b,c".split(",")→['a', 'b', 'c']join(list):",".join(['a', 'b', 'c'])→'a,b,c'
append(x)extend(iterable)insert(i, x)remove(x)pop([i])sort()reverse()
get(key, default)keys()values()items()update(other_dict)pop(key)
add(x)remove(x)union(other_set)intersection(other_set)difference(other_set)
int(x),float(x),str(x),list(x),set(x),dict(x)
len(x)type(x)range(start, stop, step)enumerate(iterable)zip(a, b)
person = {"name": "Alice", "age": 25}
print(person.get("name")) # Alice
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # ['apple', 'banana', 'cherry']
text = "Hello, World!"
print(text.lower()) # 'hello, world!'def search_insert(nums, target):
lo = 0
hi = len(nums) - 1
while lo <= hi:
mid = (hi + lo) // 2
mid_val = nums[mid]
if target == mid_val:
return mid
elif target > mid_val:
lo = mid + 1
else:
hi = mid - 1
return -1
res = search_insert([1, 2, 3, 4, 5], 10)
print(res)The standard way to calculate mid in binary search is:
mid = (left + right) // 2This is efficient and works well in Python because Python integers can grow arbitrarily large, so integer overflow is not an issue.
In some other languages (like C, C++, or Java), using (left + right) // 2 can cause integer overflow if left and right are very large. To avoid this, a safer way is:
mid = left + (right - left) // 2In Python, both methods are equally efficient and safe.
There is no more efficient way than this for calculating mid in binary search. The main efficiency comes from the binary search algorithm itself, not from how mid is calculated.