If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Certainly! Here's a demo comparing static and dynamic arrays in Python:
Static Array:
pythonCopy code
# Static array (fixed size)
static_array = [0, 0, 0, 0, 0]
# Assign values to static array elements
static_array[0] = 1
static_array[1] = 2
static_array[2] = 3
# Print the static array
print("Static Array:", static_array)
Dynamic Array:
pythonCopy code
# Dynamic array (list)
dynamic_array = []
# Append values to dynamic array
dynamic_array.append(1)
dynamic_array.append(2)
dynamic_array.append(3)
# Print the dynamic array
print("Dynamic Array:", dynamic_array)
In this demo:
append()
method, which dynamically increases the size of the array.Output:
sqlCopy code
Static Array: [1, 2, 3, 0, 0]
Dynamic Array: [1, 2, 3]
As shown in the output, the static array has a fixed size, so even if not all indices are assigned values, the remaining indices retain their default value (0). On the other hand, the dynamic array dynamically grows as new elements are added, and it doesn't have any unused indices.
This demo illustrates the difference between static and dynamic arrays, showcasing their distinct behaviors in terms of size flexibility and memory allocation.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform