0% found this document useful (0 votes)
18 views

Arrays in Python a Beginners Guide

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

Arrays in Python a Beginners Guide

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

Arrays in Python:

A Beginner's
Guide
Welcome! Today we'll explore the fundamental concepts of arrays in
Python, from their creation to efficient searching. These skills will lay a solid
foundation for your programming journey.
Declaration of Arrays
Defining Arrays Code Examples
An array stores a collection of elements of the same data type Let's see how arrays are declared in Python.
in contiguous memory locations.

Python Lists: Dynamic and versatile for holding different numbers = [10, 20, 30] # Array-like list with three
elements
data types.
print(numbers) # Output: [10, 20, 30]
Python Array Module: Provides fixed-type arrays for
specific needs.
Indexing in Arrays
Accessing Elements Example
Each element in an array has a unique index, starting from 0 We can use indexing to retrieve specific elements from an
for the first element. array.

Positive Indexing: Access elements from the beginning


numbers = [10, 20, 30, 40]
(e.g., `numbers[2]`).
print(numbers[2]) # Output: 30
Negative Indexing: Access elements from the end (e.g.,
print(numbers[-1]) # Output: 40
`numbers[-1]`).
Appending to
Arrays
Adding Elements
We can append elements to the end of an array, expanding its size.

`append()` method: Adds a single element to the end.

Code Examples
Here's how to use the `append()` method

numbers = [10, 20, 30]


numbers.append(40)
print(numbers) # Output: [10, 20, 30, 40]
Linear Search
Algorithm
1 Introduction
Linear search is a simple algorithm for finding an element in
an array by examining each element sequentially.

2 Procedure
The algorithm compares the target with each element in the
array.

3 Result
If a match is found, the index is returned; otherwise, -1 is
returned.
Linear Search:
Implementation
Code Example

This Python function demonstrates the linear search algorithm.

numbers = [10, 20, 30, 40] # The array to search in


target = 30 # The element to find

# Linear search implementation


found_index = -1 # Default value if not found
for i in range(len(numbers)):
if numbers[i] == target:
found_index = i
break # Exit the loop once the target is found

# Output the result


print(found_index) # Output: 2 if the element is found, -1 if not found
Linear Search: Advantages &
Disadvantages

Advantages Disadvantages
Simple to understand and implement. Inefficient for large datasets.

No need for pre-sorted data. Time complexity is O(n), which means search time increases
with the size of the array.
Conclusion
Congratulations! You've learned the essential concepts of arrays in Python,
from their declaration and indexing to appending elements and the core
linear search algorithm.

Practice using arrays in different programming scenarios.


Explore more efficient search algorithms, like binary search, for sorted
arrays.
Build upon this foundation to tackle complex data structures in Python.

You might also like