import math
print(math.log2(2)) # 1.0
print(math.log2(4)) # 2.0
print(math.log2(5)) # 2.321928094887362
Using negative number import math
print(math.log2(-2))
Above code will generate error. print(math.log2(1024) + 1) # Output: 11.0
import math
try:
print(math.log2(-4))
except ValueError as e:
print("Error:", e)
Output
Error: math domain error
import math
nodes = 8
depth = math.log2(nodes + 1)
print("Tree depth:", int(depth))
Output
Tree depth: 3
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.