If you are or plan to be working in the field of Data Science, Python will likely be your best buddy.
Although you will mostly be using third-party libraries, it is very important to have a comprehensive understanding of the base Python. Besides, those libraries are using the great features of base Python as well.
In this article, we will go over 5 simple Python tricks that I think will make your scripts more efficient and appealing.
Trick 1
The first one is about f-strings which is the newest string interpolation method in Python. By using f-strings, we can place values of variables in a string.
Here is a simple example:
name = "Jane"
print(f"Hello {name}!")
Hello Jane!
The f-strings is a highly flexible method. Consider we are placing the value of a very large number in an f-string.
number = 12000000
print(f"The value of the company is ${number}")
The value of the company is $12000000
The output might be easier to read with thousand separators. We can easily do this modification in the f-string.
print(f"The value of the company is ${number:,d}")
The value of the company is $12,000,000
Trick 2
Assume you have several strings stored in a list.
mylist = ["Data", "Science", "Machine", "Learning"]
You want to combine all the items in this list into a single string. There are many options for solving this task. For instance, you can iterate over the list in a for loop to add all the strings into a single one.
myword = ""
for word in mylist:
myword += word + " "
print(myword)
Data Science Machine Learning
This is, of course, not the optimal solution. The join method is much simpler.
print(" ".join(mylist))
Data Science Machine Learning
You can use any character to separate individual strings.
print("-".join(mylist))
Data-Science-Machine-Learning
Trick 3
Let’s say you need to find the most frequent character in a string.
mystring = "aaaabbbccd"
One way is to use a Counter object from the built-in collections module of Python.
from collections import Counter
cnt = Counter(mystring)
The cnt object contains each character along with the number of times they appear in the string. We can get the most frequent one using the most_common method.
cnt.most_common(1)
[('a', 4)]
This is not an extremely complex method but there is even a simple one. The built-in max function is able to find the most frequent character. We just need to use the key parameter properly.
max(mystring, key=mystring.count)
'a'
By default, the max function returns the maximum value. In case of letters, it is the last one in the alphabet.
We can use the same method to find the most frequent element in a list.
mylist = ["foo", "foo", "bar"]
max(mylist, key=mylist.count)
'foo'
Trick 4
This one is about randomness which is a fundamental concept in data science. We will not be going in detail about randomness here. However, we will see a quick method to pick a random item from a list.
The built-in random module of Python has several functions and methods. One of them is the choice method which can be used for selecting a random element from an indexable (or subscriptable) collection.
Let’s do an example.
import random
mylist = ["Ashley", "Jane", "John", "Matt", "Jenny"]
random.choice(mylist)
'Matt'
We can also use the choice method on a tuple.
mytuple = ("a","b","c","d","e")
random.choice(mytuple)
'c'
However, it will not work on a collection that does not allow for indexing (i.e. not subscriptable) such as sets.
myset = {"a","b","c","d","e"}
random.choice(myset)
TypeError: 'set' object is not subscriptable
Trick 5
A dictionary is one of the most commonly used data structures in Python. It consists of key-value pairs. The keys in a dictionary are unique so we can use them to access the values easily.
For instance, a dictionary is a great fit to store the ages of people. The names will be the keys and the ages will be stored as the values.
Please note that it is not the best way to use names as dictionary keys because it is quite possible to have two or more people with the same name. For the sake of clarity, we will keep going with the names. However, in a real-life scenario, we can assign a unique id number to each person and use these numbers as dictionary keys.
Consider the names and ages are stored in separate lists. We can easily combine two lists into a dictionary as follows:
names = ["Jane", "John", "Adam", "Ashley"]
ages = [25, 22, 27, 33]
mydict = dict(zip(names, ages))
mydict
{'Adam': 27, 'Ashley': 33, 'Jane': 25, 'John': 22}
The zip function creates tuples of pairs.
for pair in zip(names, ages):
print(pair)
('Jane', 25)
('John', 22)
('Adam', 27)
('Ashley', 33)
Then, the dict function converts this collection of tuples to a dictionary.
Conclusion
The tricks we have covered are simple but functional. I believe that there will be times when these tricks will simplify your code.
Last but not least, if you are not a Medium member yet and plan to become one, I kindly ask you to do so using the following link. I will receive a portion from your membership fee with no additional cost to you.
Thank you for reading. Please let me know if you have any feedback.