AnthemPython || Related



What Really Is Dsa (data Structure & Algorithms)

Photo by Richard Laxa on Unsplash

What Really Is Dsa (data Structure & Algorithms)

AnthemPython || Related's photo
AnthemPython || Related
·Mar 15, 2023·

5 min read

Play this article


DSA or Data Structures and Algorithms are essential concepts in computer science that enable developers to write efficient and optimized code. DSA concepts may be implemented in practically every programming language, however, in this article, we'll focus a lot on Python because it's user-friendly for beginners. Python is a popular programming language that has built-in support for many common data structures and algorithms, making it a great choice for implementing DSA concepts. In this article, we will explore some of the key data structures and algorithms in Python and how to use them effectively.

Data Structures in Python provides several built-in data structures, including lists which are donated as [], tuples (), sets {}, and dictionaries as {} and a colon : These data structures are used to store and organize data in a way that makes it easy to access and manipulate. Let's take a closer look at each of these data structures:

Lists: A list is a collection of values, which can be of any type. Lists are mutable, meaning that you can add, remove, and modify the elements in the list. In Python, lists are created using square brackets []. For example;

stuffs = ["Godspower", "Maurice", 21, "Love to Code"]

Additionally, we can add some stuff to it later in our codebase even without adding it manually from where it was created because list are mutable and we can do so using append built-in. For example;

stuffs = ["Godspower", "Maurice", 21, "Love to Code"]
stuffs.append("Sorry I missed something")

Here is the weirdness of lists;

name = list("hello") 

#OUTPUT: ['h', 'e', 'l', 'l', 'o']

This code provided uses list("hello") to convert the string "hello" to a list of individual characters.

Note: In Python, strings are iterable objects. That means you can iterate over any character in the string. When you pass a string to the list() function, it creates a new list and fills each character in the string as a separate item in the list.

So in this case the string "hello" is converted to a list of the individual characters "h", "e", "l", "l", and "o". It then uses the print() function to print the contents of the name list and prints ['h', 'e', ​​'l', 'l', 'o'].

Tuples: A tuple is a collection of values, very similar to a list. However, tuples are immutable (means ---> Unchangeable, Fixed, Permanent, Constant, Unalterable) meaning that once you create a tuple, you cannot change its contents and the beauty of tuples is that, they can be faster than lists. In Python, tuples are created using parentheses(). Let's create a variable number and use the built-in append it so we can add something to our tuple values. After this example, you will understand when we say tuples CANNOT BE CHANGED. For example;

#!/usr/bin/env python3
number = (13,12,13) 

If you run this program it will give you an AttributeError saying the Tuple object has no attribute append so this is why, tuples in python are constant, they cannot be changed later in the codebase except you manually change them from the value. But here is the weirdness of tuple

#!/usr/bin/env python3
number = 13, 
number_1 = 13

If you run this code it will donate number as tupleand number_1 as an integer I know we haven't talked about integers which we'll do as we progress and you also have questions about why number donates as a tuple. Well, we can also assign tuples with commas, even without the parenthesis() .

Sets: A set is an unordered collection of unique values. Sets are mutable, and you can add or remove elements from a set. In Python, sets are created using curly braces{}. But another weird thing about sets in python is that {} doesn't represent an empty set for some unknown reasons, it represents an empty dictionary instead set() produces an empty set. For example (and I will be coding some other examples);

#PRINT {'l', 'h', 'o', 'e'}, {'e', 'o', 'h', 'l'}, {'h', 'e', 'o', 'l'}, {'h', 'l', 'e', 'o'}, AND SO ON. IT DOES IT RANDOMLY 
name = set("hello")

#PRINT <class 'set'>, {'uyo', 'hello'}. SHOWING YOU THAT ITS A SET
name_1 = {"hello", "Hi"}

# PROVING THAT set() is an empty set
name_2 = set()

# PROVING that {} is not an empty set but an empty dictionary
name_3 = {}

I feel this article is getting too complex. So I'll stop here for now so that we can revise and try to understand the concept of data structures because they are complex. I will try to write part-2 of this article before this week runs out and if I can I'll also introduce some Algorithms in python making it a full DSA article.

Conclusion: Python provides built-in support for many common data structures, making it easy to write efficient and optimized code. By understanding the key data structures in Python, you can write code that performs well and is easy to maintain. Whether you are working on a small project or a large-scale application, the knowledge of Data Structure in Python will help you to improve the quality and performance of your code.

I am Godspower Maurice and I love y'all

Quotes of the Day:

  1. "The expert in anything was once a beginner." - Helen Hayes

  2. "Learning never exhausts the mind." - Leonardo da Vinci

  3. "It's not that I'm so smart, it's just that I stay with problems longer." - Albert Einstein (one of my favourite)

  4. "Success is no accident. It is hard work, perseverance, learning, studying, sacrifice and most of all, love of what you are doing or learning to do." - Pele

  5. "You don't have to be great to start, but you have to start to be great." - Zig Ziglar

  6. "Learning is a treasure that will follow its owner everywhere." - Chinese Proverb

Share this