This is the 17th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Introduction:

Over the past few decades, Python has created a name for itself in the world of programming or scripting languages. The main reason Python is highly favored is because of its extreme user-friendliness. Python is also used to deal with complex programs or coding challenges. Emerging fields such as machine learning (ML), artificial intelligence (AI) and data science are also addressing the high demand for learning the language. Python is a powerful programming language compared to traditional languages such as Java, C#, and others, and has quickly become a favorite among developers, data scientists, and AI/ML enthusiasts.

Python as a programming language has many use cases that appeal to learners and experts in the IT industry. At a basic level, Python can be used as a programming language to practice data structures and algorithms or to develop simple projects or games. Python’s versatility as a language makes it easy for its users to extend their projects and create websites, software, or predictive models. Automation is taking over much of the IT industry, and Python is in the lead as the language of choice for automated data analysis or data science tasks. In addition, Python has a large number of libraries and a strong community of programmers who continue to add value to Python as a language.

Learn About Python and its use cases:

One of the many reasons beginners are drawn to Python is its user-friendliness. Python abandons the dreaded semicolon and uses simple indentation structures for its syntax. Python also found a use case as an extension to an application that requires a programmable interface. Some of Python’s other benefits include its most coveted feature, its libraries. The Python library is a huge resource that can be used for many critical code writing tasks, such as:

  • Regular expression based code
  • String processing
  • Internet protocols, such as HTTP, FTP, SMTP, XML-RPC, POP, and IMAP
  • A unified code
  • The difference between the file system and the computed file
  • CGI programming
  • Mathematical modeling
  • Database query
  • The data analysis
  • Data visualization
  • Automation code

All of these functions can be performed on many Unix, Linux, macOS, and Windows systems.

Analyze the differences between Python 3.9 V/s and Python 3.10

Python has received numerous upgrades over the years, and many features have been added in new versions. Here, let’s focus on the two latest additions to Python. Exploring the functionality of updates can help you work with it and, of course, find smarter ways to work with the update library. All the code attached below is for educational purposes only, and is taken from the original Python documentation released with new releases, such as Python 3.9 and Python 3.10

Python 3.9:

IANA time zone database

A new module called ZoneInfo has been created in Python 3.9. With this module, you can access the IANA or Internet Number Assigned Authority 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))
Copy the code

Output:

2020- 07 -2 12:00:00- 05:00
2020- 07 -2 12:00:00 EST
2020- 07 -2 17:00:00+00:00
Copy the code

Functions to merge and update dictionaries

Python 3.9 added another cool feature that is getting a lot of attention. Python 3.9 now allows you to use operations to conform and/or update dictionaries. The new operator (|) and ie (| =) have been added to the Python 3.9 built-in dict class. You can access these operators to merge or update the dictionary using code similar to the tag below.

Code:

>>> A = {" v ":1.'art' : 2,Py ":3}
>>> B = {' v ':'d’, 'Topic: ''python3.9}'Copy the code

Merge code:

>>> A | b {' art ':2.'py:' 3, 'v' : 'd', 'Topic: ''python3.9'} > > > b | a {" v ": 1," art ": 2, 'Py ":3.'topic' : 'python3.9}'Copy the code

Update code:

>>> a |= b
>>> a
{'art': 2.'py': 3.'v':'d'}
Copy the code

Delete the prefix and suffix

String handling is easier to solve with new features added in Python 3.9. The code marked below removes prefixes and suffixes from the sample string. The new method used in the following sample code is:

  • Removeprefix () – This method is appropriately named for its functionality, which is to remove prefixes that exist in a given sample string.
  • Removesuffix () – This method removes existing suffixes from the sample string passed to it.

These new methods were created to replace the old strip() method due to programmers’ negative comments about the nature of its defects. Marked below is a sample code that will help you understand the implementation of these two new approaches.

Code:

print("The sea is playing outside.".removeprefix("On sea"))
Copy the code

Output:

'Playing outside'Copy the code

Use type prompts for built-in generic types in Python 3.9

Python 3.9 enabled support for common syntax for all standard sets, which are currently available in the input module. A generic type is usually defined as a container, such as a list. It is a type that can be easily parameterized. Typically, a generic type has arguments of one or more types, while a parameterized generic is a specific instance of a generic data type that has container elements; for example, a List or dictionary built-in collection type is a variety of supported types, rather than specifically supported types using Typing.Dict or Typing

Code:

def print_value(input: str) : # specifies that the value passed will be a string
Copy the code

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

Python 3.10:

The brand new Python 3.10 introduces a new feature called structured pattern matching. This matching process runs with the same matching case logic, but it also compares with a comparison object to track a given pattern.

Code for Python 3.9:

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")
Copy the code

Code for Python 3.10:

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")
Copy the code

Improved syntax error messages

A large number of programmers have trouble mismatching or debugging code. Python 3.10 added a very user-friendly feature called association advice, which comes with syntax error message tags. This helps you quickly find fixes that have errors or faulty code in them.

Code:

named_car = 77
print(new_car)
Copy the code

Output:

NameError: name 'new_car' is not defined. Did you mean: named_car?
Copy the code

Better type hints

Upgrading from Python 3.9, we can assign multiple input types of parameters without the union keyword and only use the OR symbol. It is easier to define multiple input types for the same variable

Code for Python 3.9:

def add(a: Union[int.float], b: Union[int.float]) :
Copy the code

Code for Python 3.10:

def add(a: int | float, b: int | float) :
Copy the code

Improved context Managers Context managers help 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') 作为 fout:
    fout.write('hello')
Copy the code

Latest grammar:

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

That’s all for this article

We’ve covered most of the updates in Python 3.10, and you’ll be using the most. If you can add some stars to my GitHub repository, it will be better 😊.

I have been writing a tech blog for a long time and this is one of my tech articles/tutorials. Hope you like it! Here is a summary of all my original and works source code: Nuggets – sea, and this is my recently built blog: Haiyong. Site, there is no content, put some HTML games, widgets, interested can try, source code can be their own F12 copy, or directly find me to.

If you do learn something new from this post, like it, bookmark it and share it with your friends. πŸ€— Finally, don’t forget ❀ or πŸ“‘ for support.