Tuples represent an immutable data structure that can be used to store an ordered sequence of data.
Once you initialize them, you cannot change their values.
Let’s see an example of a tuple:
Since it can be quite challenging to keep track of each of the elements in a tuple, we can also use named tuples.
To do that, we are going to use namedtuple function from the collections library.
Let’s say that we need to declare a day as a tuple that has 2 attributes.
We can use more than just 2 attributes, but let’s keep it…
If you have solved some sort of coding challenges online that have to do with lists, you know that you usually need to have access to indices and also elements in it.
You may also have to do that while you are working on your tasks.
Let us assume that you want to iterate through a list of strings that has the names of days of the week.
In Python, you may have had the tendency to do that using ranges and also the length of the list, like the following:
As usual, there is more than one way…
Working with a list in Python can be one of the most common tasks.
However, not everything comes to you in the form of a list as input.
There can be times when you are interacting with an external API, or reading from a legacy database, or formatting the data in a CSV file and for whatever reason, a piece of data in there is not in the desired format that you would like for it to be.
Let us assume that it is a string and you are interested in converting it into a list.
It can be helpful…
It can be quite common that you have to work with a list:
The list goes on.
There can be cases when you may need to randomly shuffle these elements. and Python already got your back.
There can be many paths that you can take to randomly shuffle these lists, but in…
When you create a new array in Java, you can set up the length of it. If you are programming mostly in Python these days, then you may not be aware that you can also do that in Python.
Let’s say that we have a fixed number of students and we want to save their heights in a list.
We can of course just declare an empty list and save their heights by appending new elements to the empty list.
However, there can be cases when you may need to have a fixed length and not want to allow…
Strings are pretty common in our day-to-day programming lives. One common task that we may need to do is perform a switch from one case to another one.
Of course, it’s not supposed to be a standalone task in Jira. Rather, it can be just a tiny portion of a larger task.
Since your time is valuable and you can invest it in more useful things, it is worth knowing that you can do such switches quickly in Python with built-in functions without having you do any implementation.
Let’s jump in.
This method, as you can guess, converts a string…
One of the first algorithms that we are usually taught is finding either the smallest or the largest element in a list of elements by iterating through every element in a list.
Let’s assume that we are trying to find the smallest element in a list.
The algorithm is quite straightforward and intuitive even for a 5-year-old: You assume that the first element is the smallest and save its value in a variable.
You then keep on comparing this value with all the elements in the list from the first one until the last one checking whether there is another…
It’s inevitable that you need to work with lists in your day-to-day job.
A list of users. A list of store items. A list of objects. A one potentially rare task that you may need to do is get a random element in a list.
There can be many ways that you can use to do that, but in this article, I am going to share 2 ways that can hopefully be helpful.
You have probably used methods from the random library already, trying to get a randomized numerical value that you may need to do some initializations, or adding…
In one way or another, you are probably constantly using lists in your day-to-day job.
This means that you may at least need to find their sum which is part of a task.
You probably know a sort-of universal way of finding the sum of elements in a list is pretty much all programming languages out there:
Iterate through a list of elements and then keep on adding the value of that element to a variable in which you have initialized before the start of the iteration.
In other words, it is just a general algorithm that resembles the way…
When you see famous developers in the industry, you tend to think highly of them.
Somehow you think that you are the only person who is facing job rejections.
They are always able to get whichever job they want, and it was always like that. When we take a closer look, it cannot be further from the truth.
First of all, it can happen to anyone. There can be coding challenges that you may not be able to solve at the time of the interview due to stress, or that you made a mistake, or forgot something as you haven’t…