Data Structures in Python

This article is about data structures in python.

This is my Networking book on Amazon, if you would take a look at it I would appreciate it.



The two most commonly used data structures in python are the integer and float.
Integers deal with whole numbers while a float handles portions of an integer.
Either can be positive or negative. 

a,b = 3,5
print(a+b)
print(a-b)
print(a*b)
print(a/b)

Booleans are also very important. They can be either true or false. You use
booleans to test conditions. This means testing some condition to see if it is
true or false.

a = 15 > 10
print(a)

a = 10 > 15
print(a)

For the first expression, you should get true and then false for the second
expression. You can also use "and", "or", and "not" for more complex
expressions. The expressions "a and b" evaluates to true if both "a" and "b" are
true. Similarly, the expression "a or b" evaluates to true if either or both "a"
or "b" are true. The "not" keyword evaluates to true if the expression is false.
Otherwise, it is true.

Here are some boolean examples.

a,b = True, False
print((a or b) == True)

This will evaluate to True. You will see that booleans have a priority. In
order, they are "not", "and", then "or".

Next, I want to talk about strings. Strings are a combination of characters.
Once a string is created, it cannot be changed. This is called being immutable.
You can make string by using single quotes, double quotes, triple quotes, string
method, and concatenation.

a = 'I am Wally'
a = "I am Wally"
a = '''I am Wally'''

There are several methods for strings.

This method removes beginning and ending white space if you wanted to.
a = '   I am Wally   '
print(a.strip())

This will make everything lower case.
print(a.lower())

This makes everything upper case.
print(a.upper())

Matches the first part of a string
print('I am Wally'.startswith('I'))

You can match any length of the string the same way.
print('I am Wally'.startswith('I am'))

You can check the end of the string:
print('I am Wally'.endswith('Wally'))

By the way, case matters so make sure you have that right.

You can check the index:
print('Wally'.find('all'))

You can find and replace characters:
print('Wally'.replace('W', 'S'))

We can check the length of a word or string:
print(len('Wally'))
print(len('I am Wally'))

Finally, we can test if a sequence of letters is in our string:
print('all' in 'Wally')

There are many more methods for working with strings. Check out the python user
manual for everything else.

Python has a keyword called "none". It means there is not a value. It does not
equal zero. There is no value at all. 

There are several types of container data structures. Some of them are lists,
stacks, sets, and dictionaries.

A list is a container data type. It can store a sequence of values. You create a
list by using square brackets. Lists are mutable, which means they can be
changed. 

list_a = [3,4,5,6,7,8,9]
print(len(list_a))

Python has an "is" keyword. It can be quite handy. It will check two variables
and see if they reference the same object in memory. 

a=b=10
print(a is b)


Different objects have different memory addresses. So, two lists, no matter the
contents, will have different memory locations. 

I mentioned above that lists are mutable, meaning their contents can be changed.
To change a list, we can append, insert, or join them together.

list_a = [3,4,5]
list_a.append(6)
print(list_a)

list_a = [5,7]
list_a.insert(1,6)
print(list_a)

list_a = [5,6,7]
list_b = [8,9,10]
print([5,6,7] + [8,9,10])

list_c = list_a + list_b
print(list_c)

Removing elements can be done too. Use the "remove()" method to do so. 

list_a = [2,3,3,3,4,5]
list_a.remove(3)
print(list_a)

One thing to note, removing an item from a list does not work the same way as
inserting an element to a list. When inserting, we use an index position.
However, when removing an element from a list, you supply the value and the
first instance of that value is removed.

We can reverse a list. Use the "reverse()" method.

list_a = [2,3,3,3,4,5]
list_a.reverse()
print(list_a)

We can also sort lists using the "sort()" method.

list_a = [5,4,3,6,7,8]
list_a.sort()
print(list_a)

Indexing elements in a list is a common task. We use the "index()" method. You
supply the value and the method gives the index position. Indexing starts at
zero.

list_a = [3,4,5,6,7,8]
print(list_a.index(4))

A set is another type of data structure. It is an unordered collection of unique
values. The data types in a set must be unique. We can check the uniqueness of
an element by using the "hash()" method. It will give a unique code that is
called the hash.

easy = 'algebra'
medium = 'calculus'
hard = 'number_theory'
print(hash(easy))
print(hash(medium))
print(hash(hard))

We can then create a set of strings.

math_classes = {easy,medium,hard}
print(math_classes)

Dictionaries are another useful data structure. They store pairs of values.
The first is a key and then comes the value. 

First, we create a dictionary called "grades". We give the key followed by a
colon and then the numerical value. The class names are string so must be
surrounded by some sort of quotes. Then we compare if the Calculus grade is less
than the Algebra grade. We are told it is True.

grades = {'Algebra' : 98, 'Calculus' : 91, 'Number_Theory' :84}
print(grades['Calculus'] < grades['Algebra'])      
True

Next, we ask the numerical grade of the Number_Theory class. We got an 84.

print(grades['Number_Theory'])
84

Here, we ask if the Calculus class is included in the grades dictionary. It says
True.

print('Calculus' in grades.keys())
True

Then, we ask if we got a 98 in one of our classes. Again, it is True.
print(98 in grades.values())
True

Here, we use the "items()" method to ask what classes we got above an 80. It
reports all of them. You can see the useful features here.

for k,v in grades.items(): print(k) if v > 80 else None
Algebra
Calculus
Number_Theory