Demo - Static vs Dynamic Array

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 79:-Demo - Static vs Dynamic Array

 

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:

  1. The static array is declared with a fixed size of 5. Values are assigned to specific indices of the array using indexing.
  2. The dynamic array is initialized as an empty list. Values are added to the dynamic array using the append() method, which dynamically increases the size of the array.
  3. Both the static and dynamic arrays are printed to observe the contents.

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.

6. Arrays Representations

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support