Peachpuff Makeup Photo Youtube Thumbnail

How to Become an Expert at Python For Security Professionals

How to Become an Expert at Python For Security Professionals

How to Become an Expert at Python For Security Professionals

Over the years, lots of programming languages have been on the rise, but Python is the most efficient in security-related stuff. It is a high-level programming language. It is also user-friendly because, unlike other programming languages, python syntax is intuitive, clear, and English-like, which is why it is mostly used by 80% of the population of programmers globally.

As Security professionals, knowing a scripting language would save time when dealing with manuals or repetitive tasks. Let’s say there has been a malware attack on your Company’s system; what and how would you approach it? Do you know malware attacks are written in codes that are mostly written in the python language to save you time? So this is why you need to be a step ahead of the hacker, which could be done by first knowing the nitty-gritty of Python programming.

But, I will love you to know something about programming. Learning Programming is like going to a destination for the first time with a bicycle…. it takes a bit of work to paddle, but keep this in mind the more you paddle, the closer you get to your destination.

This article aims to aid you as a security professional in becoming an expert with python programming, which also helps you know what’s needed and what’s not. How to become an expert in python for cybersecurity will be accomplished using the following outlines:

  • Installation
  • Running a python Code
  • Indentation
  • Data Types
  • Loops and Conditional Statements
  • Exploring Function
  • Access Files
  • Exception Handling
  • Modules


Recently, many PC’s(Windows/Linux) and Mac’s have already installed Python in their systems, but to check if you have it installed, open your terminal or command prompt type the following line of code:

           >> python – – version

If it replies “Python 3.x.x”, then it has been installed already but if the case is otherwise, click here to download and install it on your PC.

Running a Python Code

It can be run instantly from your CLI(command-line interface); try it out by the following :

          $ python -c “print(‘Hello World!’) “

The output would be in this form: Hello World. This is just a tip of what’s to come as you continue exploring how you could become a python expert.


Indentation is the space or tab at the beginning of a code line mostly used in a block statement. Indentation is significant because this is one of the major factors that make Python stand out from other programming languages.

Example of Python Indentation:

              if 5 > 2:

              ……..print(“Five is greater than two!”)

So from the above example, the dots(……..) signifies the indentation in the code.

Note: The dot(……..) is just an illustration, don’t Include while running the code.

Data Types

Data types in Python define any variable because each value is related to a data type. They are important because it helps you navigate through multiple lines of code successfully. It also helps you as a security professional manipulate variables of your choice, mostly when you begin writing complex codes.

It has numerous data types like integer, float, boolean, strings, list, tuple, dictionaries, etc. There is an in-built function called type(); it helps you know which data type a stored variable is, and it will be used in the examples below. 

Note: The following example would only explain three data types.

        Example 1 – Integer

            >>> a = 24

            >>> type(a)

            <class ‘int’>

From example 1, you could see the type function at work 

         Example 2 – String

             >>> message = ‘Hello world!’

             >>> type(message)

             <class ‘str’>

From example 2, you could see that from the variable message, the values start with apostrophe(‘) and ends with apostrophe(‘). You could see that strings in python starts and ends with apostrophe(‘).

        Example 3 – List

            >>> cases = [24, ‘Hello world!’]

            >>> type(message)

            <class ‘str’>

            >>> matters = list(24, ‘Hello world!’)

            >>> type(matters)

            <class ‘str’>


Example 3 is all about the Python list, and it starts and ends with a square bracket[ ]. In most cases, if the square brackets are not used, an in-built function is being initiated by typing ‘list(variables)’ as explained above.

Conditional Statements and Loops

       First, conditional statements are helpful when performing a series of actions that require if a condition is met or not. The following will only print “Search” if the site is equal to “” Let’s type the following lines of code to try it out:

            >>> site = “”


            >>> if (site == “”):

            >>> print(“Don’t Search”)

            >>> elif (site == “”):

            >>> print(“Search! “)

            >>> else:

            >>> print(“Don’t Search”)



       Secondly, Python loops are one of the easiest and fun to use when it comes to iteration. Loops in python are useful and helpful when iterating through a range of items or lists. Below lies the syntax on how loops are used in python.

             Example – Loops


                     >>> for port in range(8000,8005):

                     >>> print(” [+] port {0} is opened.”.format(port))



                     [+] port 8000 is opened

                     [+] port 8001 is opened

                     [+] port 8002 is opened

                     [+] port 8003 is opened

                     [+] port 8004 is opened

Exploring Function

Functions in Python are essential parts of programming, and its use is highly recommended for security professionals. It is leveraged to have a code snippet that performs an action and returns an output.

In Python, we have two types of functions: the in-built function and the other is the one newly created by you, starting with “def. “

There are different types of in-built functions like type, input, int, str, and soon, but an example will be given using the input function.


                     >>> name = input(‘Name: ‘)

                     Name: Joe Mark 

                     >>> print(name)

                     Joe Mark

From the example, it is seen that the in-built function there accepts a variable. What the input function does is that it asks the user for input.

Now over to the function you are to create yourself; the example below would help you create your own Python function.


                     >>> def Myfunction(name):

                     >>>      return name


                     >>> name = input(‘Name: ‘)

                     Name: Joe Mark 

                     >>> MyFunction(name)

                     Joe Mark 

From the example above, when creating a function in Python, you start with def which defines it as a function then followed by the function name.

Access Files in Python

Python makes programming easy when accessing files using the open() function. It provides the ability to create and read files in Python.

The example below should put you through how you access files in Python:


                    >>> file1 = open(‘file.txt’, ‘w’)

                    >>> file1.write(‘Hello World!’)

                    >>> file.1close()



                    >>> file_2 = open(”file.txt’, ‘r’)

                    >>> file_2.readlines()

                    [‘Hello World!’]


Line 1 of the example explains how a new file is being created and stores the file as file_1 and moving down to line 3, the file had to be closed because it’s going to be useless; it is just like accessing a file in your office cabinet and leaving the file opened which is not proper.

The main difference between line 1 and line 6 is that line 3: the ‘w’ is used to create(write) a new file while line 6 is used for reading(‘r’) through a file.

Exception Handling

As you go further, you will begin to encounter one or more errors; through this, you receive an error message; this is where exception handling comes in.

Exception handling helps to handle various types of errors encountered during programming. Let’s try a Try/Except loop example.


                                 >>> try:

                                 ……. s.connect((‘’,23))

                                 ……. except:

                                 ……. pass


In the first line of the example above, try the next code if the next line of code, the third line of code ‘except’ the error is none it should pass by the error.


Modules are one of the most powerful features you would come across. 

It can be referred to as a code library or a file containing a set of functions you want to include in your script.

Just like functions, you either create a module or use an inbuilt module. Some in-built modules come with the Python software; examples are the OS module, Urllib module, Subprocess Module, Math Module, Socket Module, and soon.

Creating a module requires you to save your code in a file with the file extension ‘.py.’

            Example – Module

                        Note: Open a file and write the following lines of code, then save the file with

                        >>> def greeeting(name):

                        >>> print(‘Hello, ‘ + name)

                        Save the file as said earlier.

                        Open your CLI


                        >>> import myModule

                        >>> myModule.greeting(‘Ben Mark’)

                        Ben Mark


From the example, it is clear that after writing your code, save it, and use the import function to import your previous code as a module to the current code. So Modules in python are reusability of preexisting code into an existing one.


In conclusion, So far, you have been able to lay hands on some vital information on how you could become an expert in python as a security professional. To stay ahead of the curve, you need always to keep practicing and also be able to create your own quick scripts to automate your own tasks. Take this with you. Learning a scripting language like python is fun.

There are references below for further studies on becoming an expert in python as a security professional.

          – Primal Security Tutorial Series:


          – Books (Violent Python, Black Hat Python, Gray Hat Python)

          – Free Online:



          – Python Courses:

                     • Google’s Free Python course:


                     •’s Python Scripting Expert course:



Artificial Intelligence And Machine Learning In Cybersecurity

Comments (2)

  1. Thiago Pereira

    The first question of Variables Quiz’s “Python3 For InfoSec Pros” course said that the right answer is “x=33”, but when I write the code (x=18 x +15 print (x)) in Google Colab, it says that the right answer is 18. Why?
    Thank you.

    December 31, 2020 at 3:49 pm
    1. pythonforcybersecurity

      Go through it again, I’m sure the answer is the same as google colab.

      January 1, 2021 at 2:09 pm

Leave your thought here

Your email address will not be published. Required fields are marked *