DEV Community

loading...
Cover image for Python 3.9 is here ! Top 3 cool features for you to try.

Python 3.9 is here ! Top 3 cool features for you to try.

siddheshshankar profile image Siddhesh Shankar ・6 min read

Python 3.9 is out! Volunteers from all over the world have been working on improvements to Python, While beta versions have been available for some time, the first official version of Python 3.9 was released on October 5, 2020.
It’s clear that this version marks a breaking point from the old route of Python’s evolution, onto a new path. We’ll cover:

  • Merging Dictionaries
  • Removing prefix and suffix
  • Time zone support

Merging Dictionaries

Dictionaries are one of the fundamental data structures in Python. They’re used everywhere in the language and have gotten quite optimized over time.
"|" can be used to merge dictionaries in python.

>>> dict1 = {'Asia': ['India', 'Singapore'], 
             'Europe': ['Austria', 'Belgium']}
>>> dict2 = {'Africa': ['Egypt', 'Kenya'], 
             'North America': ['USA', 'Canada']}
>>> dict1|dict2
{'Asia': ['India', 'Singapore'], 
'Europe': ['Austria', 'Belgium'], 
'Africa': ['Egypt', 'Kenya'], 
'North America': ['USA', 'Canada']}

Removing prefix and suffix from a string

We have two new methods for removing string prefixes and suffixes. .removeprefix() and .removesuffix() remove at most one copy of the affix. If you want to be sure to remove all of them, then you can use a while loop

>>> str1 = "Mr. Donald Trump"
>>> str1.removeprefix("Mr. ")
'Donald Trump'

>>> str2 = "Martin Luther King Jr."
>>> str2.removesuffix(" Jr.")
'Martin Luther King

Proper Time Zone Support

Python has extensive support for working with dates and times through the datetime module in the standard library. However, support for working with time zones has been somewhat lacking. Here is zoneinfo module. zoneinfo uses an IANA time zone database residing on your local computer.
A Python implementation of the IANA Time Zone Database is available on PyPI as tzdata. You can install it with pip:

$ python -m pip install tzdata

Accessing Time Zones

Get a set containing all the valid keys for IANA time zones available anywhere on the time zone path. This is recalculated on every call to the function.

>>> from datetime import datetime, timezone
>>> import ZoneInfo

>>> zoneinfo.available_timezones()
>>> {'Asia/Shanghai', 'Asia/Karachi', 'Europe/Madrid', 'Pacific/Saipan', 'Europe/Zaporozhye', 'Asia/Harbin', 'Portugal', 'Asia/Pyongyang', 'Europe/Stockholm', 'Pacific/Funafuti', 'Europe/Tirane', 'Australia/Queensland', 'America/Rankin_Inlet', 'Europe/Gibraltar', 'America/Coral_Harbour', 'Africa/Bissau', 'Asia/Choibalsan', 'US/Michigan', 'Australia/Perth', 'America/Sao_Paulo', 'America/Mexico_City', 'Asia/Kabul', 'America/Mazatlan', 'Etc/GMT+2', 'Asia/Kolkata', 'America/Fort_Wayne', 'CET', 'Etc/Zulu', 'US/Samoa', 'Europe/Kaliningrad', 'Etc/GMT-11', 'Brazil/West', 'Europe/Vatican', 'EET', 'Asia/Sakhalin', 'Antarctica/Rothera', 'Pacific/Kosrae', 'Etc/GMT-7', 'America/Santa_Isabel', 'Pacific/Wallis', 'Africa/Monrovia', 'Australia/Sydney', 'Asia/Tokyo', 'Africa/Ceuta', 'America/Santiago', 'Indian/Kerguelen', 'Atlantic/Azores', 'Pacific/Yap', 'Antarctica/Casey', 'Australia/Broken_Hill', 'America/Guadeloupe', 'Canada/Pacific', 'Europe/Budapest', 'America/Scoresbysund', 'America/Moncton', 'Africa/Lagos', 'America/Guatemala', 'America/Adak', 'Europe/Guernsey', 'Etc/GMT-4', 'Asia/Ulan_Bator', 'Indian/Maldives', 'America/Winnipeg', 'US/Central', 'Pacific/Kiritimati', 'Antarctica/DumontDUrville', 'Europe/Simferopol', 'America/Barbados', 'Africa/Sao_Tome', 'America/Whitehorse', 'America/Goose_Bay', 'America/North_Dakota/Beulah', 'Etc/GMT-3', 'Asia/Bishkek', 'Australia/LHI', 'Mexico/BajaNorte', 'Navajo', 'Australia/Yancowinna', 'America/Atikokan', 'Libya', 'PST8PDT', 'Asia/Baku', 'Asia/Chongqing', 'MET', 'Australia/Lindeman', 'America/Denver', 'America/Halifax', 'Australia/Brisbane', 'Europe/Mariehamn', 'GMT', 'Etc/GMT-10', 'Europe/Oslo', 'America/Detroit', 'Pacific/Pitcairn', 'Iran', 'Europe/Malta', 'Africa/Addis_Ababa', 'Asia/Seoul', 'Asia/Dushanbe', 'America/Costa_Rica', 'Brazil/Acre', 'America/Punta_Arenas', 'Africa/Maputo', 'America/Thule', 'Africa/Bujumbura', 'Europe/Astrakhan', 'ROK', 'America/Argentina/Ushuaia', 'EST5EDT', 'Antarctica/Mawson', 'America/Tegucigalpa', 'America/North_Dakota/New_Salem', 'Europe/Kiev', 'America/Curacao', 'America/Porto_Velho', 'America/Yakutat', 'Asia/Ashkhabad', 'Europe/Dublin', 'America/Buenos_Aires', 'Pacific/Enderbury', 'Europe/Helsinki', 'America/Nassau', 'America/Campo_Grande', 'America/Eirunepe', 'America/Jujuy', 'America/Boise', 'Asia/Ulaanbaatar', 'Poland', 'Africa/Ouagadougou', 'Pacific/Fiji', 'America/Chicago', 'America/Catamarca', 'Africa/Maseru', 'America/Cayman', 'Etc/GMT-9', 'America/Pangnirtung', 'Asia/Gaza', 'Asia/Qyzylorda', 'Europe/Prague', 'Etc/UCT', 'Asia/Pontianak', 'America/Lima', 'US/Eastern', 'Etc/GMT+4', 'Antarctica/Syowa', 'Africa/Kigali', 'America/Juneau', 'America/Miquelon', 'Asia/Singapore', 'America/Sitka', 'Etc/GMT+5', 'Europe/Bucharest', 'America/Cancun', 'America/St_Thomas', 'Pacific/Palau', 'America/Paramaribo', 'Atlantic/Bermuda', 'America/Shiprock', 'Africa/Libreville', 'Indian/Comoro', 'Etc/GMT+1', 'Asia/Qatar', 'Etc/GMT', 'Asia/Kuching', 'Etc/GMT-2', 'Asia/Istanbul', 'Etc/GMT+10', 'America/Montevideo', 'Asia/Kuwait', 'US/Arizona', 'Africa/Niamey', 'Asia/Novosibirsk', 'Pacific/Kwajalein', 'Antarctica/Davis', 'America/Dominica', 'PRC', 'Africa/Lubumbashi', 'Africa/Dakar', 'America/Puerto_Rico', 'Europe/Vaduz', 'Asia/Thimphu', 'US/Aleutian', 'America/Martinique', 'Africa/Luanda', 'Pacific/Nauru', 'Europe/Ljubljana', 'Pacific/Chuuk', 'Africa/Mogadishu', 'America/Rio_Branco', 'Asia/Aqtobe', 'EST', 'Kwajalein', 'Atlantic/Faroe', 'Europe/Zagreb', 'Etc/GMT+6', 'Africa/Kampala', 'Asia/Calcutta', 'GMT+0', 'America/New_York', 'America/Menominee', 'Antarctica/Macquarie', 'America/Antigua', 'Pacific/Bougainville', 'America/Creston', 'Asia/Almaty', 'America/Toronto', 'Asia/Macau', 'Africa/Gaborone', 'America/Indiana/Marengo', 'Europe/Nicosia', 'America/Indiana/Petersburg', 'America/Ojinaga', 'Asia/Barnaul', 'Universal', 'Antarctica/Vostok', 'Asia/Yerevan', 'Canada/Saskatchewan', 'America/El_Salvador', 'Europe/Podgorica', 'America/Blanc-Sablon', 'Atlantic/Canary', 'GB-Eire', 'Pacific/Chatham', 'Europe/Luxembourg', 'America/Rosario', 'Europe/Istanbul', 'Europe/Isle_of_Man', 'Etc/GMT-13', 'Europe/Chisinau', 'Europe/Paris', 'Asia/Yakutsk', 'Asia/Ho_Chi_Minh', 'Asia/Ust-Nera', 'Etc/GMT-1', 'Africa/Juba', 'Europe/Rome', 'Africa/Banjul', 'America/Ensenada', 'America/Vancouver', 'Etc/GMT+3', 'Pacific/Rarotonga', 'Asia/Kuala_Lumpur', 'Pacific/Port_Moresby', 'Pacific/Guadalcanal', 'Asia/Riyadh', 'America/Santo_Domingo', 'Africa/Tunis', 'Asia/Anadyr', 'Asia/Chungking', 'Asia/Nicosia', 'America/Hermosillo', 'America/Dawson_Creek', 'Asia/Jerusalem', 'Atlantic/Faeroe', 'America/Argentina/Salta', 'Asia/Tbilisi', 'Asia/Dacca', 'Asia/Irkutsk', 'US/East-Indiana', 'Europe/Berlin', 'Etc/GMT+11', 'Antarctica/Palmer', 'Africa/Casablanca', 'Indian/Cocos', 'America/Indiana/Vincennes', 'America/Porto_Acre', 'America/St_Vincent', 'Europe/Bratislava', 'Pacific/Samoa', 'Asia/Rangoon', 'America/Panama', 'America/Argentina/ComodRivadavia', 'Etc/Universal', 'America/Godthab', 'Europe/Skopje', 'Asia/Vladivostok', 'America/Montreal', 'America/Monterrey', 'Pacific/Noumea', 'UCT', 'Pacific/Gambier', 'Africa/Brazzaville', 'Africa/Mbabane', 'Asia/Omsk', 'America/Matamoros', 'Etc/GMT+7', 'Antarctica/South_Pole', 'Mexico/General', 'Europe/Uzhgorod', 'Africa/Windhoek', 'America/Anguilla', 'America/Resolute', 'MST', 'Pacific/Auckland', 'Pacific/Guam', 'Europe/Moscow', 'Indian/Christmas', 'America/St_Barthelemy', 'Europe/Warsaw', 'Atlantic/Jan_Mayen', 'America/Los_Angeles', 'Jamaica', 'Pacific/Tongatapu', 'Africa/Harare', 'Asia/Tashkent', 'America/Iqaluit', 'Asia/Amman', 'W-SU', 'Asia/Baghdad', 'America/Tortola', 'Europe/Andorra', 'America/Nome', 'NZ', 'America/Cuiaba', 'Pacific/Pohnpei', 'Asia/Hong_Kong', 'America/Boa_Vista', 'Africa/Asmara', 'US/Hawaii', 'MST7MDT', 'America/Bahia_Banderas', 'Brazil/DeNoronha', 'Etc/GMT-0', 'US/Alaska', 'America/Maceio', 'America/Chihuahua', 'Asia/Tel_Aviv', 'America/Argentina/Buenos_Aires', 'Europe/Tiraspol', 'Australia/Melbourne', 'US/Mountain', 'America/Inuvik', 'Egypt', 'America/Indiana/Knox', 'Pacific/Galapagos', 'Asia/Krasnoyarsk', 'Africa/Ndjamena', 'Africa/Bangui', 'Factory', 'Asia/Kathmandu', 'America/Indiana/Winamac', 'Antarctica/McMurdo', 'US/Indiana-Starke', 'Asia/Bangkok', 'America/North_Dakota/Center', 'Africa/Conakry', 'Europe/Tallinn', 'Atlantic/Stanley', 'Asia/Aden', 'America/Recife', 'Europe/Samara', 'Africa/Khartoum', 'Asia/Yangon', 'America/Port-au-Prince', 'America/Argentina/La_Rioja', 'Zulu', 'Etc/GMT-5', 'Etc/GMT+12', 'Europe/Sarajevo', 'Europe/Athens', 'Pacific/Truk', 'Etc/GMT+8', 'Cuba', 'America/Marigot', 'Asia/Saigon', 'Asia/Taipei', 'Etc/GMT+9', 'Africa/Bamako', 'Singapore', 'Africa/Dar_es_Salaam', 'Asia/Makassar', 'Europe/Copenhagen', 'Eire', 'Europe/London', 'America/Nipigon', 'Chile/Continental', 'Asia/Atyrau', 'Asia/Tehran', 'Australia/Tasmania', 'Canada/Newfoundland', 'Pacific/Efate', 'America/Fort_Nelson', 'Africa/Cairo', 'Europe/Lisbon', 'America/Port_of_Spain', 'America/St_Lucia', 'Asia/Hovd', 'ROC', 'Asia/Jakarta', 'Turkey', 'Africa/Tripoli', 'Pacific/Norfolk', 'Pacific/Ponape', 'Australia/Eucla', 'Antarctica/Troll', 'Africa/Douala', 'America/Fortaleza', 'Canada/Atlantic', 'America/Asuncion', 'US/Pacific', 'America/Glace_Bay', 'America/Atka', 'Australia/Victoria', 'Etc/GMT0', 'Africa/Johannesburg', 'Africa/Nouakchott', 'America/Argentina/San_Juan', 'Iceland', 'Africa/Asmera', 'Brazil/East', 'Indian/Mayotte', 'Asia/Qostanay', 'Asia/Damascus', 'America/Aruba', 'Hongkong', 'Australia/ACT', 'Australia/Canberra', 'Indian/Antananarivo', 'Pacific/Honolulu', 'America/Metlakatla', 'Africa/Abidjan', 'Asia/Dhaka', 'Asia/Magadan', 'America/Caracas', 'Europe/Amsterdam', 'America/Guyana', 'America/Cambridge_Bay', 'America/Thunder_Bay', 'Pacific/Tarawa', 'Europe/Zurich', 'Asia/Kamchatka', 'America/Indiana/Indianapolis', 'Asia/Vientiane', 'CST6CDT', 'Asia/Aqtau', 'Europe/Belgrade', 'America/Kralendijk', 'Asia/Ashgabat', 'Mexico/BajaSur', 'Asia/Samarkand', 'Africa/Lusaka', 'Africa/Algiers', 'America/Yellowknife', 'Australia/Darwin', 'America/Manaus', 'Pacific/Easter', 'Indian/Mauritius', 'Canada/Mountain', 'Africa/Lome', 'Europe/Brussels', 'America/Argentina/Mendoza', 'Indian/Chagos', 'America/Rainy_River', 'Atlantic/South_Georgia', 'Asia/Bahrain', 'Australia/Adelaide', 'America/La_Paz', 'Europe/Ulyanovsk', 'America/Montserrat', 'Europe/Jersey', 'America/Anchorage', 'America/Merida', 'Etc/GMT-14', 'Asia/Kashgar', 'Asia/Tomsk', 'America/Cordoba', 'GMT-0', 'America/Bogota', 'Asia/Manila', 'Asia/Dubai', 'America/Tijuana', 'America/Edmonton', 'Europe/Vienna', 'America/St_Kitts', 'Europe/Minsk', 'America/Indianapolis', 'Atlantic/Madeira', 'America/Argentina/Rio_Gallegos', 'Europe/Saratov', 'America/Grenada', 'Canada/Eastern', 'Pacific/Majuro', 'Asia/Brunei', 'Africa/Blantyre', 'Africa/Porto-Novo', 'Australia/Hobart', 'America/Guayaquil', 'Africa/Nairobi', 'America/Cayenne', 'Europe/Belfast', 'Pacific/Midway', 'GMT0', 'Indian/Mahe', 'Pacific/Pago_Pago', 'Pacific/Tahiti', 'Asia/Phnom_Penh', 'Pacific/Johnston', 'Asia/Jayapura', 'Africa/Timbuktu', 'America/Bahia', 'Africa/Djibouti', 'Canada/Yukon', 'America/Jamaica', 'Europe/Volgograd', 'Asia/Famagusta', 'Pacific/Wake', 'NZ-CHAT', 'Europe/Sofia', 'Africa/El_Aaiun', 'Europe/Vilnius', 'America/Swift_Current', 'Greenwich', 'Europe/Monaco', 'Asia/Khandyga', 'Etc/GMT-12', 'America/Louisville', 'Atlantic/St_Helena', 'Pacific/Apia', 'Australia/North', 'America/Havana', 'Asia/Macao', 'America/Virgin', 'America/Noronha', 'Asia/Dili', 'Atlantic/Reykjavik', 'Asia/Yekaterinburg', 'America/Belem', 'Australia/Currie', 'Africa/Kinshasa', 'Australia/NSW', 'Pacific/Fakaofo', 'America/Knox_IN', 'Etc/GMT-6', 'Asia/Oral', 'America/Managua', 'America/Argentina/Catamarca', 'America/Santarem', 'Asia/Chita', 'America/Kentucky/Louisville', 'HST', 'Australia/West', 'America/Belize', 'America/Argentina/Tucuman', 'America/Nuuk', 'Asia/Novokuznetsk', 'Europe/Kirov', 'America/Phoenix', 'Etc/GMT+0', 'GB', 'Asia/Urumqi', 'Arctic/Longyearbyen', 'Australia/Lord_Howe', 'Etc/Greenwich', 'Australia/South', 'Japan', 'Atlantic/Cape_Verde', 'America/Dawson', 'Africa/Freetown', 'America/Danmarkshavn', 'America/St_Johns', 'Asia/Srednekolymsk', 'America/Indiana/Vevay', 'America/Argentina/Jujuy', 'Africa/Accra', 'America/Indiana/Tell_City', 'WET', 'Asia/Ujung_Pandang', 'Indian/Reunion', 'Asia/Thimbu', 'Chile/EasterIsland', 'Pacific/Niue', 'America/Lower_Princes', 'Asia/Colombo', 'Pacific/Marquesas', 'America/Mendoza', 'Israel', 'Asia/Katmandu', 'Canada/Central', 'Europe/San_Marino', 'UTC', 'Africa/Malabo', 'Etc/UTC', 'Asia/Muscat', 'America/Argentina/Cordoba', 'Europe/Riga', 'America/Araguaina', 'America/Argentina/San_Luis', 'America/Grand_Turk', 'America/Kentucky/Monticello', 'Asia/Beirut', 'Etc/GMT-8', 'America/Regina', 'Europe/Busingen', 'Asia/Hebron'}

>>> datetime.now(tz=ZoneInfo("Asia/Kolkata"))
datetime.datetime(2020, 10, 11, 11, 21, 46, 31856, tzinfo=zoneinfo.ZoneInfo(key='Asia/Kolkata'))

>>> datetime.now(tz=ZoneInfo("Hongkong"))
datetime.datetime(2020, 10, 11, 13, 52, 33, 186045, tzinfo=zoneinfo.ZoneInfo(key='Hongkong'))

The release of a new Python version is a big milestone for the community. You may not be able to start using the cool new features immediately, but in a few years Python 3.9 will be as widespread as Python 3.6 is today.

Discussion (0)

pic
Editor guide