




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Master arrays and lists in programming with this detailed guide. Learn how to store, access, and manipulate multiple values using indexing, iteration, and built-in methods. Includes syntax examples, diagrams, and real-world analogies.
Arrays and lists are fundamental data structures used to store collections of values. Instead of creating multiple variables, you can group related data in one structureโmaking your code cleaner, faster, and easier to manage. ๐ Sticky Note: Think of an array as a row of lockersโeach with a number and a value inside.
An array is a fixed-size, ordered collection of elements of the same type. Each element is accessed using an index , starting from 0. ๐ Example (Python): python marks = [85, 90, 78, 92] print(marks[2]) # Output: 78 ๐ Example (C): c int marks[4] = {85, 90, 78, 92}; printf("%d", marks[2]); // Output: 78
๏ท front(): Returns the value of the first element in the list. ๏ท back(): Returns the value of the last element in the list. ๏ท push_front(x): Adds a new element โxโ at the beginning of the list. ๏ท push_back(x): Adds a new element โxโ at the end of the list. ๏ท pop_front(): Removes the first element of the list, and reduces the size of the list by 1.
. pop_back(): Removes the last element of the list, and reduces the size of the list by 1.
๐ 1. Indexing Access elements using their position. python colors = ["red", "green", "blue"] print(colors[0]) # Output: red
๐ Index starts at 0 ๐ Accessing out-of-range index causes error ๐ 2. Iteration Loop through elements using for or while. Python for fruit in fruits: print(fruit) ๐ Real-Life Analogy: Like checking each item in a grocery list one by one. ๐ 3. Common Methods (Python Lists) Method Description Example append() Adds item to end fruits.append("kiwi") insert() (^) Adds item at fruits.insert(1,
Used to represent tables or grids. ๐ Example (2D Array in Python): python matrix = [[1, 2], [3, 4]] print(matrix[1][0]) # Output: 3 ๐ Analogy: Think of a spreadsheetโrows and columns.
๏ท ๐ Mixing data types in arrays (in statically typed languages) ๏ท ๐ Accessing out-of-bound indices ๏ท ๐ Forgetting to initialize array size (in C/C++) ๏ท ๐ Using list methods on arrays (language-specific)
๐ Quick Tip: Use try-except to handle index errors gracefully.
๐ Arrays store fixed-size, same-type data ๐ Lists are dynamic and flexible ๐ Use indexing to access, iteration to loop ๐ Master built-in methods for manipulation ๐ Practice with real-world examples like scores, names, and tables