0% found this document useful (0 votes)
4 views2 pages

Understanding Arrays: Types & Examples

Uploaded by

skarmur31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Understanding Arrays: Types & Examples

Uploaded by

skarmur31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Array

=============================
What is Array :

-> An array is a collection of elements (items). All elements must be of the same
data type.

Why use Array :

-> Used to store multiple values in a single variable, without declaring separate
variables.

Where use Array :

-> Used in loops, sorting, searching, and when you need to store a fixed-size list
of elements.

Example:
================

numbers = [10, 20, 30, 40, 50]

print(numbers[2]) # Output: 30

-> Array work with index number you have to pass index number.

Types of Arrays
========================

-> One-dimensional Array (1D)


-> Two-dimensional Array (2D)
-> Multi-dimensional Array

-> One-dimensional Array (1D) :

-> Single row or column of elements.

example:

arr = [1, 2, 3, 4]
print(arr[0]) # Output: 1 # pass index number

-> Two-dimensional Array (2D) :

-> Array of arrays (like a table or matrix). create array inside array.

example:

matrix = [[1, 2], [3, 4]]


print(matrix[1][0]) # Output: 3
-> Multi-dimensional Array :

-> More than two levels.

example:

arr3D = [ [ [1, 2], [3, 4] ], [ [5, 6], [7, 8] ] ]

print(arr3D[1][0][1]) # Output: 6

String Arrays
====================

-> array is collections of elements but it's strings types elements.

example:

names = ["A", "B", "C"]


print(names[1]) # Output: B

You might also like