Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Billy Okeyo
Billy Okeyo

Posted on • Originally published atbillyokeyo.codes on

     

What's New in Python 3.9

Alt Text

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 Listmylist : list[int] = [1, 2, 3, 4]print(mylist)mylist = '1234'print(my;ist)
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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'}
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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'}
Enter fullscreen modeExit fullscreen mode

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 |= aprint(b)
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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']
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
gagande90 profile image
Gagan
UG student at Swinburne University Of Technology, Melbourne.Studying "Computer Science"
  • Location
    Melbourne, Australia
  • Work
    Developer
  • Joined

Great article thanks for sharing.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a software engineer building products in Python(Django), Flutter, JavaScript, TypeScript(Angular) and .NET.
  • Location
    Nairobi
  • Work
    Software Engineer at Innova Limited
  • Joined

More fromBilly Okeyo

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp