Software engineering and personal development

Category: Science & Tech (Page 9 of 35)

How to Quickly Round Numbers in Python

A MacBook with lines of code on its screen on a busy desk
Photo by Christopher Gower on Unsplash

If you are working with many numbers in Python, you probably need to do some rounding to cut off some digits that you may not need at all.

For example, let us say that you are calculating an average salary in a company and the average number that you have found is around 54,334.218

This may not be a number that can look good in a final report you want to submit to someone. You should instead round it.

Let us do it in Python:

As you can see, the number 2 after the comma represents the number of digits we want to keep. The thing is, we can also use negative numbers to specify the number of digits we want to keep.

If we use -1, we are doing the rounding to the nearest ten:

When we need to do the rounding to the nearest hundert, we use -2:

If we want to do the rounding to the nearest thousand, we use -3:

This may seem something quite trivial, but I also got to learn it just recently.

I hope you find this useful.

How to Use Methods that you have not Implemented yet in Python

aerial view of zigzag road on mountain
Photo by Johannes Hofmann on Unsplash

If you are implementing a new class, but do not have time now, or simply want to postpone it for later, then you can use the pass statement.

I have seen it being used all over the place, and I think it is a good idea to use it when you are writing a class that does not have any bodies of any methods, or when you are implementing a class that is not yet complete.

You can use it to do test-driven development when you initially write a failing test that calls a method that is not implemented yet, which you can then implement and fix the failing test.

Another scenario when I saw it being used was in coding assignments that are part of homework.

Continue reading

How to Quickly Check Whether a List Contains Duplicate Elements in Python

white and yellow daisy in bloom
Photo by Stefan Cosma on Unsplash

Sometimes, you may need to check whether a list has any duplicate elements. This can be a common task when you are given a list of numbers or a list of strings. This can also be something that you may need to do both at work, or it can also be part of a coding challenge that you are implementing when being asked at a programming interview.

Continue reading

How to Find the Index of a Substring in a string

file cabinet
Photo by Maksym Kaharlytskyi on Unsplash

Many programming interview questions have something to do with working with indexes. You may also be dealing with tasks at work that can have something to do with indexes.

One such task can be doing different splits and formattings of text files based on an index of a specific character.

Let us assume that you are asked to remove part of a string based on a certain index in a string.

Continue reading

How to Quickly Generate Fake Data in Python using Faker

person using phone and laptop computer
Photo by Austin Distel on Unsplash

Testing your code is a great way to check whether you have implemented everything correctly. No matter how much you try, no matter how hard you analyze all the cases, sometimes, just the smallest mistake can lead to a big problem which could have maybe been avoided writing tests.

When you write tests, you are obviously not going to use data from production. We need to use some dummy data that does not belong to anyone who is actually using the project. This is called faking the data.

Continue reading

How to Quickly Check Whether a Variable is a Number in Python

photo of man walking near LED signage
Photo by Ryoji Iwata on Unsplash

Since you don’t have a predefined type for a variable in Python, you may notice a lot of bugs that can arise. As such, it is very important that you pay a lot of attention in cases when you have to make sure that you are using the correct type of variable.

This can also be the case when you expect a variable to be a number, but you get a string or any other type instead.

Continue reading

How to Quickly Convert 2 Lists Into a Dictionary in Python

Get a driver's-eye view of other cars driving along the 110 Freeway in Los Angeles as they pass under the soaring interchange with the 105 Freeway. This is quintessential L.A. architecture and a typical view for thousands of people every commute.  Another sunny day in paradise.
Photo by Chris Linnett on Unsplash

Two of the most common data structures in Python are lists and dictionaries. If you take a look back at your code, you may notice that you have a lot of lists and dictionaries used all over the place.

They may look quite similar, but they have a key difference. You can think of a list as a collection of items, and a dictionary as a collection of key-value pairs.

There can be cases when you may need to do a conversion of 2 lists into a single dictionary. This is something that you can easily do in Python with the help of zip() function.

Continue reading
« Older posts Newer posts »

© 2024 Fatos Morina

Theme by Anders NorenUp ↑