Understanding data structure is a key concept to writing code in any programming language. Data structure refers to the categorization of data into groups that best describe its purpose.
Data structure is the foundational building block of a programming language. They are the stepping stones to building functional and very complex applications and software.
While they can be grouped into primitive and non-primitive in some programming languages, Python inclusive, a simple breakdown of data structures in Python includes;
The primitive data structure which includes;
Integer,
Float,
String, and
Boolean
And the non-primitive data structure which is divided into;
Built-in type which is further divided into
List,
Tuple,
Set, and
Dictionary
User-defined type which is further divided into
Stack,
Queue,
Tree, and
Graph
A few of these data types will be discussed as well as algorithms.
They are the most fundamental kind of data structure, and non-primitive data structures start with them as a building block. They are regarded as immutable data structures.
The inability to modify an object’s value is referred to as immutability in Python. It is a characteristic that primitive objects have. These basic data types have this property because they are thought of as objects.
The only data structures that cannot have their values modified are the primitive data structures. All other data structures can have their values changed.
One of Python’s most basic data structures is an integer. They belong to Python’s numeric data type together with float and complex. When a value is assigned to a numeric data type variable, one of the built-in objects in Python is created: an integer.
Integers are unrounded, positive, or negative whole integers. Once it contains decimal places, it transforms into a floating-point, often known as a float.
For example;
Create numeric variables
1
2
3
a = 15;
b = -200;
c = 34667278
To check the type of this object, thetype() function
is called and the variable to be checked is passed into the parentheses as an argument. This function is then passed into theprint()
function to display it. See below;
1 2 3
print(type(a)); print(type(b)); print(type(c));
This displays int
which is how integers are represented in Python.
The same applies to the other numeric variables.
Strings are also one of the primitive data structures in Python. They are usually identified by the single or double quotation marks surrounding them. Strings are immutable as they are primitive objects.
String literals
This is the standard way used to create strings. It entails that a set of characters be enclosed in a single or double quote.
To display a string literal, the string literal can be passed as a parameter into the print() function
.
See example;print(‘Welcome’)
is the same as print(“Welcome”)
, where both of them are called string literals.
Assigning a string to a variable makes it a string variable.
See example;
1 2
a = ‘Welcome’ this turns the variable a into a string literal. print(a) //prints ‘Welcome’
There are several built-in techniques for manipulating, formatting, and escaping characters in strings.
The capitalize() method
, for instance, capitalizes the first character in a string.
The islower() method
determines whether lower case characters are present in a string and returns true or false depending on the situation.
The isupper() method
, among many others, determines if the characters in a string are uppercase and returns a true or false response depending on the situation.
An excellent resource for learning about strings is the Python documentation, which provides a thorough overview of the numerous methods on the string object and when to utilize each one.
They are a primitive data structure that returns a true or false value when an expression, variable, or a value is evaluated.
For example;print (10 > 15) //the expression 10 > 15
is not true so it returns false.
The bool() function
takes in a value or variable as its parameter and evaluates it.
For example;print bool(‘Welcome’) //the value ‘Welcome’
is a string which is a type of data structure in Python so it returns true.
1
2
3
4
a = ‘Welcome’
b = 10
print(bool(a)) //a is a string variable and strings are a type of data structure in Python so it returns true.
print(bool(b)) //a is an integer of the numeric data type structure.
Since integers are a type of data structure in Python, it returns true.
These exist so that data structures can become more organized as they grow larger. As a program becomes more complex, there becomes a pressing need to group various kinds of primitive data types so that code does not become too clumsy.
It is for this reason that the non-primitive data types were designed. They are considered objects in Python and since their values can be changed, they are regarded as mutable data structures.
They are one of the four built-in data types in the non-primitive data structure in Python. It can be illustrated as a basket that can be used to hold various items purchased from the grocery store. While the items could be fruits only or junk food only, this basket could hold a variety of different stuff.
List is a data type that helps to store multiple values in a single variable. It is often identified with a square bracket surrounding its values.
To create a list see the example below;
1 2
firstList = [‘Apples’, ‘Cherries’, ‘Oranges’] //creates the list print(firstList) //prints the values
Note: this list holds only string values, it can also hold only boolean values, only integers, or all the data types as its value.
This object’s feature includes mutability, which refers to the capacity to alter an object’s value. After a list has been formed, it is completely feasible to add, remove, or alter the values inside it.
The first item in a list has an index of 0, the second has an index of 1, and so on. Python lists are zero-indexed. The list becomes quite organized as a result. Consider the index as a signature that is added to each value at a certain location in a list.
Double-valued: Because a list is indexed, it is possible for the same values to appear at various points within the list. The reason for this is simple. Each individual value has its own.
insert()
- this method inserts a value at a specified position. It accepts the index position and the value to be inserted into its parameters as arguments.
For example;
1
2
firstList = [‘Apples’, ‘Cherries’, ‘Oranges’, ‘Pineapples’] //creates the list
firstList.insert(2, ‘Mangoes’) //inserts mangoes into the second index and third value.
append()
- this method adds an element to the end of the list. It takes the value to be appended as the parameter.
For example;
1 2
firstList = [‘Apples’, ‘Cherries’, ‘Oranges’, ‘Pineapples’] //creates the list firstList.append(‘cashew’) //appends cashew to the end of the list.
clear()
- this method removes all the values from a list. It takes no parameters.
For example;
1 2
firstList = [‘Apples’, ‘Cherries’, ‘Oranges’, ‘Pineapples’] //creates the list firstList.clear() //removes all the values from a list.
reverse()
- this method reverses the order of the values in a list. This changes the index value as well as the position of the values. It takes no parameters.
For example;
1 2
firstList = [‘Apples’, ‘Cherries’, ‘Oranges’, ‘Pineapples’] //creates the list firstList.reverse() //reverses all the values in a list thereby changing the index value and position.
This list goes on and the Python documentation is a great place to learn all about list methods.
They are one of the four built-in data types in the non-primitive data structure in Python. They are similar to lists but have some differences.
A tuple is a data type that helps to store multiple values in a single variable. It is often identified with a round bracket surrounding its values.
To create a tuple see the example below;
1 2
firstTuple = (‘Apples’, ‘Cherries’, ‘Oranges’) //creates the tuple print(firstTuple) //prints the values
Note: this tuple holds only string values, it can also hold only boolean values, only integers, or all the data types as its value.
Immutable - which refers to an object’s inability to have its value changed, is one of this object’s characteristics.
One of the main distinctions between tuples and lists is that once a tuple has been generated, it is completely impossible to add, remove, or change any of the values.
The first item in a list has an index of 0, the second has an index of 1, and so on. Tuples are zero-indexed in Python.
The tuple is now well organized, much like lists. Consider the index as a signature that is added to each value at a certain location in a tuple.
Tuples are indexed, therefore it’s feasible for the same values to appear twice.
count()
- this method returns the number of times a value occurs in a tuple. It takes the value to be evaluated as a parameter.
For example;
1 2 3
firstTuple = (‘Apples’, ‘Cherries’, ‘Oranges’) //creates the tuple a = firstTuple.count(‘Apples’) //saves the value of the count() method in a print(a) //prints out the count value stored in the variable.
index()
- this method searches through the values in a tuple at its first occurrence and then returns the value of its position. It takes the value to be scanned for as its parameter.
For example;
1 2 3
firstTuple = (‘Apples’, ‘Cherries’, ‘Oranges’) //creates the tuple a = firstTuple.index(‘Apples’) //saves the value of the index() method in a print(a) //prints out the index() value stored in the variable.
They are one of the four built-in data types in the non-primitive data structure in Python.
A set is a data type that helps to store multiple values in a single variable. It is often identified with a curly bracket surrounding its values.
To create a set see the example below;
1 2 3 4 5 6 7
firstSet = { ‘ Apples’, ‘Cherries’, ‘Oranges’ } //creates the set print(firstSet) //prints the values
Note: this set holds only string values, it can also hold only boolean values, only integers, or all the data types as its value.
Immutable as it describes the inability to change the value of an object. If we want to remove or add to the values within a set, after it has been created it is totally possible with sets, which is one of the key differences from lists. But values within a set cannot be changed or replaced.
Not indexed: Sets are not indexed in Python as compared with the previous non-primitive data types.
Not double-valued: Having duplicates of values is not allowed in sets since they are not indexed. This feature prevents confusion in code.
add()
- this method adds an element to a set. The parameter takes the value of the item to be added.
For example;
1 2 3
firstSet = (‘Apples’, ‘Cherries’, ‘Oranges’) //creates the set firstSet.add(‘Mangoes’) //adds the value mango to the set. print(firstSet) //prints out the values stored in the variable.
Note: If the value already exists, the duplicate will not be added.
remove()
- this method removes an element from a set. The parameter takes the value of the item to be removed.
For example;
1 2 3
firstSet = (‘Apples’, ‘Cherries’, ‘Oranges’) //creates the set firstSet.remove(‘Cherries’) //removes the value cherries from the set. print(firstSet) //prints out the values stored in the variable but does not include the removed item.
This list goes on and the Python documentation is a great place to learn all about set methods.
An algorithm is a term that cuts across various programming languages. It simply means a set of instructions that helps a program achieve an expected output after the processing of data to solve a problem. In other words, it involves the logical approach to using data structures to achieve a favorable outcome to a given problem.
While there are no standard rules guiding the creation of algorithms, there are some ways to construct their code.
They are important because they tell how to code a program.
The following categories help to solve different problems using the data structures as a base. Their name denotes their function.
Search algorithm
Sort algorithm
Delete algorithm
Update algorithm
Insert algorithm
Writing an algorithm is like writing down a to-do list. Below are the steps entailed;
step 1 − START MULTIPLY
step 2 − get values of a & b
step 3 − c ← a * b
step 4 − display c
step 5 − END
As simple as this is, it breaks down the problem of writing a program that multiplies two integers. Complex programs can also be broken down into steps like this for simplification and a better understanding of code in case something goes wrong.