程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

What you need to know about the differences between Python 3.10 and python 3.9

編輯:Python

Introduce :

In the last few decades ,Python Create a name for yourself in the field of programming or scripting languages .python The main reason for its high popularity is its extreme user friendliness .Python It is also used to deal with complex program or coding challenges . machine learning (ML)、 Artificial intelligence (AI) Emerging fields such as and data science also meet the high demand for learning this language . And Java、C# Compared with traditional languages such as other languages ,Python It's a powerful programming language , Quickly become a developer 、 Data scientists and AI/ML The lover's favorite .

Python As a programming language , There are many use cases that attract IT Learners and experts in the industry . On a basic level ,Python It can be used as a programming language to practice data structures and algorithms or to develop simple projects or games .Python Versatility as a language allows its users to easily expand their projects and create websites 、 Software or prediction model . Automation is taking over IT Most areas of the industry , and Python As the preferred language for automated data analysis or data science tasks . besides ,Python It has a large number of libraries and a strong programmer community , They are constantly Python Add more value as a language .

understand Python And its use cases :

Beginners are Python One of the many reasons for its attraction is its user friendliness .Python Abandoned the daunting semicolon , And use a simple indentation structure as its syntax .Python A use case was also found as an extension of an application that requires a programmable interface .Python Some of its other benefits include its most coveted features , Its library .Python Libraries are a huge resource , It can be used for many key code writing , for example :

  • Regular expression based code
  • string manipulation
  • Internet Protocol , Such as HTTP、FTP、SMTP、XML-RPC、POP、IMAP
  • Unified code
  • The difference between the file system and the calculation file
  • CGI Programming
  • mathematical modeling
  • Database query
  • Data analysis
  • Data visualization
  • Automation code

All these functions can be used in many Unix、Linux、macOS and Windows System-on-system execution .

analysis Python 3.9 V/s Python 3.10 The difference of

these years ,Python A lot of upgrades have been made , And many functions have been added in the new version . ad locum , Let's focus on Python Two latest versions added . Exploring the updated features can help you use it smoothly , Of course , You can also find smarter ways to work with update Libraries . All codes attached below are for educational purposes only , And from the new version ( for example Python 3.9 and Python 3.10) Original published together Python file

Python 3.9:

IANA Time Zone Database

stay Python 3.9 Created a file named zoneinfo New module for . Through this module , You can visit IANA Or Internet number assignment agency time zone database . By default , This module uses the system's local time zone data .

Code :

print(datetime(2021, 7, 2, 12, 0).astimezone())
print(datetime(2021, 7, 2, 12, 0).astimezone().strftime("%Y-%m-%d %H:%M:%S %Z"))
print(datetime(2021, 7, 2, 12, 0).astimezone(timezone.utc))
​

Output :

2020-07-2 12:00:00-05:00
2020-07-2 12:00:00 EST
2020-07-2 17:00:00+00:00
​

Functions to merge and update dictionaries

Python 3.9 Added another cool feature , It's got a lot of attention .Python 3.9 You can now merge or update dictionaries using operators . New operator ie ( | ) and ( |= ) Has been added to Python 3.9 built-in dict Class . You can access these operators to merge or update dictionaries using code similar to the tags below .

Code :

>>> a = {‘v’: 1, 'art’: 2, 'py’: 3}
>>> b = {’v’: 'd’, 'topic’: 'python3.9’}
​

Merge code :

>>> a | b
{’art’: 2, 'py’: 3, ’v’:’d’, 'topic’: 'python3.9’}
>>> b | a
{’v’: 1,’art’: 2, 'py’: 3, 'topic’:’python3.9’ }
​

Update code :

>>> a |= b
>>> a
{'art': 2, 'py': 3,'v':'d'}
​

Remove prefixes and suffixes

Use Python 3.9 The new feature added in can solve the string processing problem more easily . The code marked below is used to remove prefixes and suffixes from the sample string . The new method used in the following example code is :

  • removeprefix() - This method is properly named according to its function , It removes the prefix that exists in a given sample string .
  • removesuffix() – This method removes the existing suffix... From the sample string passed to it .

Due to the programmer's negative evaluation of the nature of its defects , These new methods are created to replace the old strip() Method . Marked below is a sample code , It can help you understand the implementation of these two new methods .

Code :

print(" The sea is playing outside ".removeprefix(" Hai Yong "))
​

Output :

‘ Play outside ’
​

stay Python 3.9 Use type hints for built-in generic types in

Python 3.9 This version enables support for the common syntax of all standard collections , These functions are currently available in the input module . A generic type is usually defined as a container , For example, a list . It is a type that can be easily parameterized . Usually , Generic types have one or more types of parameters , Parameterized generics are specific instances of generic data types with container elements , for example , List or dictionary built-in collection types are various types supported , Not specifically supported types Typing.Dict or typing.List

Code :

def print_value(input: str): # Specifies that the value passed will be of type string
​

By using the following method , We will be able to find out whether the following input is a string

Python 3.10:

Match using structural patterns all-new Python 3.10 A new function called structural pattern matching is introduced in . This matching process runs with the same Matching Case Logic , But it also compares with the comparison object to track a given pattern .

Python 3.9 Code for :

http_code = "419"
if http_code == "200":
print("OK")
elif http_code == "404":
print("Not Found Here")
elif http_code == "419":
print("Value Found")
else:
print("Code not found")
​

Python 3.10 Code for :

http_code = "419"
match http_code:
case "200":
print("Hi")
case "404":
print("Not Found")
case "419":
print("You Found Me")
case _: #Default Case
print("Code not found")
​

Improved syntax error messages

A large number of programmers face difficulties in mismatching or debugging code .Python 3.10 Added a very user-friendly feature , It's called relevance advice , It is marked with a syntax error message . This helps you quickly find fixes for errors or wrong code .

Code :

named_car = 77
print(new_car)
​

Output :

NameError: name 'new_car' is not defined. Did you mean: named_car?
​

Better type tips

from Python 3.9 upgrade , We don't have to use it union Keywords and only use OR Symbols to assign multiple input types of parameters . Defining multiple input types for the same variable is a simpler way

Python 3.9 Code for :

def add(a: Union[int, float], b: Union[int, float]):
​

Python 3.10 Code for :

def add(a: int | float, b: int | float):
​

Improved context manager The context manager helps with resources such as files . You can now use multiple contexts in a single block . This will greatly enhance your code , Because you no longer need multiple blocks or statements .

Previous grammar :

with open('output.log', 'rw') As fout:
fout.write('hello')
​

The latest grammar :

with (open('output.log', 'w') as fout, open('input.csv') as fin):
fout.write(fin.read()

That's all for this sharing , Thank you for reading .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved