Python programming language has been with us for quite a long time and now there is version 3.9 and we are asking ourselves what is this that has been added into this new version.
In this article we will see three new things that have been added into python 3.9, I bet there are more others but I will only talk about the three I have interacted with. So let's get started.
** 1. Type Hints**
in previous versions of python if you wanted to use type hints you had to import a library to use that for, for example
from typing import List
mylist : list[int] = [1, 2, 3, 4]
print(mylist)
mylist = '1234'
print(my;ist)
If you run this, nothing will happen, in that it won't break despite having a list of integers and changing it to a string later, this is because type hints doesn't affect the running of your code but it affects everything that uses those typehints. For instance if you use mypy it will raise an issue saying there is incompatible types in assignment.
in python 3.9, we don't have to import the type hints to use it, instead we will have:
mylist: list[int] = [1, 2, 3, 4]
print(mylist)
mylist = '1234'
print(my;ist)
The above will run well in python 3.9 without any issues.
2. Merging and Updating Dictionaries
This new feature is a proper and a shorter way to merge and update dictionaries in python. Let's look at an example below:
a = {'id' : 1, 'username' : 'Billy', 'email' : 'myemail@gmail.com'}
b = {'id' : 1, 'username' : 'Billy', 'email' : 'hisemail@gmail.com'}
In the above dictionary we have two dictionaries with the same id and username but different email, so if we want to combine this two dictionaries we can use the below syntax;
a = {'id' : 1, 'username' : 'Billy', 'email' : 'myemail@gmail.com'}
b = {'id' : 1, 'username' : 'Billy', 'email' : 'hisemail@gmail.com'}
print({ **a,** b})
b.update(a)
print(b)
So what this does is it updates everything that is in b with everything that is in a and the output will be
{'id' : 1, 'username' : 'Billy', 'email' : 'myemail@gmail.com'}
{'id' : 1, 'username' : 'Billy', 'email' : 'myemail@gmail.com'}
So in python 3.9 we can rewrite the above code with
a = {'id' : 1, 'username' : 'Billy', 'email' : 'myemail@gmail.com'}
b = {'id' : 1, 'username' : 'Billy', 'email' : 'hisemail@gmail.com'}
print(b | a)
b |= a
print(b)
The above code will have the exact output as the code we tried earlier but now the code looks clean and attractive and easier to write.
*3. Removing Prefix - Suffix *
Here we try to remove a prefix or a suffix from a string. Let's consider the example below
doctors = ['Dr. John Doe', 'Dr. Jane Dean', 'My Doctor', 'Dr. James Smith', 'Dr. Mary Jean']
names = []
for doctor in doctors:
if doctor.startswith('Dr. '):
names.append(doctor[4:1])
else:
names.append(doctor)
print(names)
If we run this it will look for every name that has a "Dr." and strip that off and return only the name, output will be
['John Doe', 'Jane Dean', 'My Doctor', 'James Smith', 'Mary Jean']
The above example was how we can implement the logic in python 3.8 and below and now in python 3.9 we can implement it as below:
doctors = ['Dr. John Doe', 'Dr. Jane Dean', 'My Doctor', 'Dr. James Smith', 'Dr. Mary Jean']
names = []
for doctor in doctors:
names.append(doctor.removeprefix('Dr. '))
print(names)
If we run the above code the output will be same, "Dr. " will be stripped off. In python 3.9 we can see it's much easier because we now don't have to write the if else statement. Alternatively we can have a much simpler way to do that also as below:
doctors = ['Dr. John Doe', 'Dr. Jane Dean', 'The Doctor', 'Dr. James Smith', 'Dr. Mary Jean']
names = [doctor.removeprefix('Dr. ') for doctor in doctors]
print(names)
This will also print the exact output.
That's all I had for now, I might do another article to show more other features in python 3.9. In the comments section you can say what other features you find interesting in python 3.9 and I'll be happy to check them out. Till then, see you in the next article.
Top comments (1)
Great article thanks for sharing.