Exploring Python and its Data Structures

Fortune Edema
3 min readFeb 2, 2023

--

As a cybersecurity enthusiast, I am always eager to learn and expand my knowledge. It was a Monday afternoon, and I had just finished watching a series with a friend. I decided to continue my journey in learning the Python programming language. In my last post, I wrote about setting up my laptop for Python and diving into some basic data types. Today, I am taking you through my journey of exploring the basic operations with these data types.

Assuming we have a list of temperatures on Monday:

monday_temperatures = [9.1,8.8,7.5]

and we want to add a float 8.1. A float is simply any integer with a decimal point. We can use the append method to add it to the list:

Along the way I also discovered the power of the Python interactive shell in Visual Studio Code’s terminal. By typing Python3,

I was able to access the shell and check the documentation of any built-in function and data type using the help(list) command and this appeared:

help(list)

To find the index of an item in a list, we use the index method:

This returns the position of 8.8, which is at the first index.

We can also access a portion of a list. For example, if we have:

monday_temperatures = [9.1,8.8,7.5,6.6,9.9]

We can access the last two items by using:

monday_temperatures[3:]

Or the first two items using:

monday_temperatures[:2]

I was also introduced to negative indexing, where we count from the end of the list. For example:

monday_temperatures = [9.1,8.8,7.5,6.6,9.9]
|-5|-4 |-3 |-2 |-1 |

So, monday_temperatures[-5]would output the first item in the list which is “9.1".

Along the way i learnt Lists can also contain a mixture of words and integers, for example:

monday_temperatures = ["Hello",1,2,3]

We can manipulate and access the string by chaining. For example,mystring[0] accesses the first item in the list, which is "hello".

We can chain it further to access the second letter in the word using mystring[0][1]. We can also slice and use Python indexing style on this.

Accessing items in a dictionary is different from accessing items in a list. A dictionary uses a “key” to access its “value”. For example, if we have a dictionary:

student_grades = {"mary":9.1,"sim":8.8,"john":7.5}

we can access the value “8.8” using

student_grades["sim"]

Similarly we can have key-value pair of strings also:

Location = {"mary":"lagos","sim":"abuja","john":"Delta"}

and accessed the same way as the previous example:

Location["john"]

One key takeaway from my instructor was that every problem you want to solve has its own data structure. This made me appreciate the versatility and importance of data structures in solving real-world problems.

I hope you enjoyed this journey with me, and I look forward to exploring more about cybersecurity and programming in the future. Happy reading!

--

--

Fortune Edema
Fortune Edema

Written by Fortune Edema

Information Security Associate ISO IEC 27001 ||Tech Enthusiast||InfoSec Researcher|| Jnr SOC Analyst ||Security Awareness||Computer Science Student

No responses yet