Artificial Intelligence Design Technology

Procedural Programming in Python

Pinterest LinkedIn Tumblr

Developers can structure their code in many different ways. That structure makes it easier to update the code and create new functionality within the code. Programming models or paradigms are used to structure code. Python allows for object oriented, procedural and functional programming models or as they are often called paradigms. In this article, we will discuss about the Procedural programming model.

What is procedural programming?

Procedural programming is like writing a step by step instruction the program executes. It’s important as a new developer to know how it works as it is an important stepping stone to object oriented programming. Procedural programming structures code into procedures, sometimes called subroutines or functional sections of code. Because of this approach, the code is made up of logical steps to perform a specific task. This model is very effective because procedures can be reused in other parts of the code which helps in abiding by the principle called DRY meaning Don’t Repeat Yourself. Code written using this model is also easy to understand because of how well the steps are logically broken down. Procedural programming though has certain disadvantages. It is hard to maintain and extend, data is exposed throughout the program and it doesn’t relate to real world objects. However, it is up to you as a developer to decide if its the best approach to a specific piece of coding or not.

Algorithms and their importance in programming.

The concept of an algorithm is quite similar to how procedural programming works. It can be said that steps are also broken down in both cases and then logical steps are followed to achieve a result. An algorithm is a series of steps to complete a given task or a series of problem. We use algorithms all the time to complete tasks in our day to day life. For example when following a recipe to make a particular dish, you are making use of an algorithm, following each step on the recipe to achieve an output.

ALSO READ  Building And Implementing A Blog App Using Django: Adding Forms

An algorithm in programming works in a similar way. In programming, algorithms are used to solve a multitude of problems ranging from very simple ones to very complex ones. When it comes to creating and understanding an algorithm, the key is to break down the problem into smaller parts. This way, you are able to build up the steps to complete the algorithm that will resolve the overall problem and once the steps of the algorithm are created, they will then execute the same way each time the algorithm is used. But then how do you go about writing an algorithm from scratch?

There are several approaches to this. One way is to use a pseudocode, an English like syntax that resembles code to explain the problem in a series of steps. Another approach is to use a flow chart, which provides a graphical representation of a series of steps. One very frequently used type of algorithm is recursion. Recursion refers to a method of function that will call itself. It is used to resolve problems by breaking the problems into sub problems. Some of the most common recursive algorithms are divide and conquer, dynamic programming and greedy algorithm.

Algorithmic complexity

As a developer, one of your main tasks will be to write code that suits business needs. This means that at times, you will have to rework or rewrite the code to make it easier to manage or run more efficiently. This process is called “refactoring”. Now to determine how to make your code faster or perform better, you must be able to measure the algorithmic complexity of the code. Algorithmic complexity is concerned about how fast or slow particular algorithms perform. This is measured based on the time and space of the code. The time is measured by how long it takes and space by how much memory it takes. The Big-O Notation is used to measure an algorithmic efficiency in terms of time and space. Therefore, what is a Big-O Notation?

ALSO READ  Meet Django...

Big-O Notation is a mathematical notation that describes the upper bound or worst case scenario for the time complexity of an algorithm. Big-O Notation is written as “O(f(n))”, where “f(n)” is a function that represents the relationship between the input size usually denoted as “n” and the algorithms runtime or resource usage.

Common examples of Big-O notation

1)O(1)-Constant time: In algorithms with constant time complexity, the run time does not depend on the size of the input data, it remains constant. This makes it the most efficient scenario. An example is when you are accessing an element in an array by its index.

2)O(n)-Linear time: They have a run time that grows linearly with the size of the input data. This means that if the input data doubles in size, the run time will also automatically double in time.

3)O(n^2)-Quadratic time: They have a run time that grows with the square of the input size meaning that as the input data size increases, the runtime increases quadratically.

4)O(log n)-Logarithmic time: Algorithms with logarithmic time have a complexity that has a runtime that grows logarithmically with the size of the input data. It is considered very efficient.

Here is a ranking of the speed of the various Big-O Notation examples:

1)Fastest: O(1)- Constant time.

2)Pretty fast: O(log n)- Logarithmic time

3)Moderate: O(n)- Linear time.

4)Slower: O(n log n)- Linear Arithmetic.

5)Slower still: O(n ^ 2)- Quadratic time.

6)Quite slow: O(2 ^ n)- Exponential time.

7)Incredibly slow: O(n!)- Factorial time.

It is important as developers to be able to adjust when it comes to building codes and improving them. The Big-O Notation is a fundamental concept in computer science that helps us to analyze and compare algorithms efficiency as well as complexity. Knowing this will greatly help us as developers know how best to maximize the efficiency of our codes. We have hereby come to the end of this article. In this article, we learnt about procedural programming which means the breaking down of code into sections or procedures, we also learnt about algorithms, their complexity and the Big-O Notation. In conclusion, making use of all the concepts discussed in this the article can overall make your code simpler to deduce and more easier to work with.

ALSO READ  Namespacing and Scoping in Python