4 Cool Tips for Python Beginners
Small but useful bites
Python is the go-to language in the data science ecosystem. One of the reasons why Python is so popular among data scientists is the rich selection of libraries it offers.
In this article, we will not focus on any library though. Instead, we will go over 4 small but very useful tips for base Python. These tips are also important for the external libraries because they adapt features from base Python.
1. Sorting a list
List is a built-in data structure of Python. It is an ordered collection of items. The order of items is important because we use it for indexing and slicing a list.
We can sort a list using the sort method. However, we should be careful about how to use it. One common mistake is to try to assign the sorted list to a new variable.
Consider the following example.
a = [3, 1, 6, 2]
a_sorted = a.sort()
You may think that “a_sorted” is a list that contains the sorted elements of “a”. Let’s check.
a_sorted
It does not return anything because it does not hold any value. Actually, the value of “a_sorted” is None. The sort method works in place which means it sorts the list “a” but it does not return anything.
a
[1, 2, 3, 6]
If you want to sort a list and assign it to a new variable, you can use the sorted function.
a_really_sorted = sorted(a)
a_really_sorted
[1, 2, 3, 6]
2. Reverse a string
String is an ordered sequence of characters. Just like lists, we can index or slice a string. Python is pretty flexible in terms of slicing an ordered sequence. How slicing works is as follows:
[START:STOP:STEP]
We can indicate the beginning and end of a slice using the start and stop values, respectively. The step is used for specifying the step size.
mystring = "abcdefgh"
mystring[1:7:2]
'bdf'
We start from the index 1 which is the letter b. Then we take the every second character until the seventh item. It is important to note that the upper bound is not included.
We can use the slicing operation to reverse a string. You may encounter this question in a job interview. We start from the beginning and go until the end. The step size should be “-1” which means taking a step of size 1 backwards.
mystring = "Python is awesome"
mystring_reversed = mystring[::-1]
mystring_reversed
'emosewa si nohtyP'
If you leave the start and stop parts empty, the slice starts from the very beginning of the string and goes to the end.
3. Set vs List
Set is also a collection of items but it is unordered and contains only unique items. We can add new items to both lists and sets. There is a slight difference though.
Let’s first create a list and a set that contain the same items.
set_a = {1, 2, 3, 4}
list_a = [1, 2, 3, 4]
When we want to add a new item to a list, the append method is used because the order needs to be taking into consideration. On the other hand, the add method is used to add new items to a set.
set_a.add(5)
print(set_a)
{1, 2, 3, 4, 5}
list_a.append(5)
print(list_a)
[1, 2, 3, 4, 5]
The new item is appended at the end of the list. The new item is also displayed at the end in the set but it will not always be the case. We cannot really talk about the beginning or end for a set because it does not possess an order.
4. Be careful about dynamic typing
Python is a dynamically typed language which means the type of a variable is determined only during runtime. It is a really cool feature for developers. However, it may cause issues if not used carefully.
Let’s do an example to emphasize what dynamic typing means. For instance, the variable “my_number” holds the value 4 which is an integer.
my_number = 4
type(my_number)
int
We can change the value of “my_number” and it does not have to be an integer.
my_number = '4'
type(my_number)
str
The value of my_number is still 4 but it is a string. Python does not raise an error in such cases. The type of the variable is automatically updated. It is not the case with the statically-typed languages such as C++. You need to explicitly define the type of a variable.
I think dynamic typing is a cool feature but it may cause issues if not used carefully. For instance, we can add integers and strings using the “+” operator.
4 + 4
8
'4' + '4' '44'
Thus, if we use strings instead of integers and perform an addition, we will not get an error but we will end up having unexpected results.
Conclusion
We have covered 4 small but useful Python tips. You may not always need to use them but they come in handy in certain situations. Keep in mind that sometimes details are what make a difference.
Thank you for reading. Please let me know if you have any feedback.