Definition
An
algorithm is a list of steps or statements that can be converted into code and
executed by a computer.
Purpose
Algorithms
are the foundation of programming and are used to solve problems by taking in
input, processing it, and providing an answer.
Characteristics
Algorithms
are not language-specific and can be implemented in several programming
languages. They are resource- and problem-dependent, but share some common code
constructs, such as flow-control (if-else) and loops (do, while, for).
Examples
Some
well-known Python algorithms include tree traversal, sorting, and searching.
Real
time Example
If we are
going to prepare a coffee,
Preparation
of coffee – Algorithm ( Set of instructions )
Sugar,
Milk, Coffee Powder – Data ( It is stored in Memory which requires some Space )
Data
Structure
·
Data
Structures are a way of organizing data so that it can be accessed more
efficiently depending upon the situation.
·
Data
Structures are fundamentals of any programming language around which a program
is built.
·
Python
helps to learn the fundamental of these data structures in a simpler way as
compared to other programming languages.
·
Any
program has no meaning without the data.
For
example, we have the program to send an email, then this program is of no use
if we don’t have any email address or the body of the email.
And it
is very critical how well we organize or store the data.
For the
purpose of data management we have the data structures.
·
To
store any data, we use variables – if we have a large amount of data and we
want to store it so for this purpose we have to use data structures.
·
Suppose
if we have some mobile numbers and we want to send messages on them, so for
sorting the mobile numbers we cannot use multiple variables for each mobile
number.
·
If
we use multiple variables then all data will be allocated to the random
locations on the RAM
·
It
will take a lot of space in the RAM.
Arrays
·
Arrays
are used to store multiple values in a single variable.
·
Arrays
are used to store the data in a sequential manner.
·
Another
Example –
·
An
array can hold many values under a single name, and you can access the values
by referring to an index number.
·
In
python, there are two ways to create the array – one is using the tuple and
other one is using list.
Tuple
·
Tuple
is a built-in data type in python used to store collection of data, the other 3
are list, set and dictionary
·
A
tuple is a collection of data which is ordered and immutable – it allow duplicate
values
·
Tuples
are written with rounded brackets “()”.
·
Immutable
means that we cannot change the data-
add or remove data from the tuple.
Accessing
element in array
·
To
access the elements in a array, you have to get the position of the element and
call like below example
List
·
List
is built-in data types in Python used to store collections of data.
·
List
is a mutable, or changeable, ordered sequence of elements.
·
They
can hold a various of data types, including integers, floats, strings, and even
other lists.
·
We
can create a list in Python using square brackets [] or by using the list()
constructor.
·
In
list we can change data at any position as it is mutable.
·
In
python we can do row-wise operations with lists and tuples.
·
The
same can be done using the python list slicing.
In the
above example,
1:4 means
it starts from the 1st index and will go to the 3rd, it excludes 0th
element in the list.
This is
known as slicing in python
·
To
pick the last data of the list you can use -1. ( Negative Indexing )
·
To
get all elements in the list you have to do like below,
Using [:]
& [::] without specifying any start, end, or step returns all elements of
the list.
·
To
get all items before/after a specific position
To get all
the items from a specific position to the end of the list, we can specify the
start index and leave the end blank.
And to get
all the items before a specific index, we can specify the end index while
leaving start blank.
·
To
get all items in between two specific positions
·
To
get items at specified intervals,
In this
example,
# Get every
second element from the list starting from the beginning
# Get every
second element from the list starting from index 0 to 3(exclusive)
·
Out of bound slicing, list slicing allows out-of-bound indexing without raising errors.
If we
specify indices beyond the list length then it will simply return the available
items.
In this
example, even if we have five elements if we try to access 3rd till
6th element, if we have only 5 elements in the list – it will get
only the items till the end without any error
·
To
reverse a list using slicing,
·
Suppose
if we want to store the data like this we will be using Nested list.
Note : If
we want to retrieve only the mobile number from the above list, will it be
possible ? without using any libraries or for loop ?
Answer :
Not Possible
·
In
this case we can use numpy or for loop ( list comprehension ) as a solution
Nested
Lists
·
A
list within another list is referred to as a nested list in Python.
·
We
can also say that a list that has other lists as its elements is a nested list.
·
To
access the elements in this nested list we use indexing.
·
The
above list elements can be accessed by the below methods.
1D Array
vs 2D Array
·
Arrays
can be created in 1D or 2D.
·
1D
arrays are just one row of values, while 2D arrays contain a grid of values
that has several rows/columns.
List
Methods
·
Below
are some widely used list methods,
ü append(): adds an element to the end
of the list.
ü insert(): adds an element at a
specified position in the list.
ü remove(): removes the first
occurrence of a specified element from the list.
ü pop(): removes the element at a
specified position in the list and returns it.
ü sort(): sorts the elements in the
list in ascending order.
ü reverse(): reverses the order of the
elements in the list.
ü count(): returns the number of times
a specified element appears in the list.
ü index(): returns the index of the
first occurrence of a specified element in the list.
ü extend(): extends the list by
appending elements from another iterable.
To access count() in list
To access
count() in nested list
Lists
vs Tuples
Tuple
Methods
·
Tuple
has only limited number of built-in methods
ü count()
ü Index()
No comments:
Post a Comment