New and Exciting Features of Python Programming 2.10-4: A Must have for Better and Faster Computer
Python for Everyone:
With its unprecedented power and usefulness, Python has become of
the hottest and fastest growing programming language since it was launched by
Guido Van Rossum and is become one of the buzzwords in Digital Transformation
together with other domains in the Pantheon of Technology – Data Science and
Big Data.
Data Science is a
multi-disciplinary skill in Statistics, Computer Science and Business
Management. Data Science was regarded as the solution to provide scalable
insights with the availability of Big Data. The advancement in Technology makes
these domains and areas visible to almost sectors and industries. Big
enterprises invested huge amount of money to install Data Science team in their
organization to take care the operations of the business.
Skills in
Statistics, Data Mining or Data Wrangling was successfully used and applied to
these companies and areas because of the Computer Programming Language such as
Python. Business Models was carefully curated by experts in Business
Management. Thus, Data Science was regarded as the sexiest job in the 20th
Century because it encapsulated a wide spectrum of knowledge, skills, and
expertise.
Based on Stack
Overflow Developer’s Survey in 2019, Python was regarded as the second and the
most preferred language with 73% of the developers are choosing it over other
programming languages and is expected to dominate the Marketplace for
Programming Languages.
Alright.
Let’s
start with our discussion.
WHAT ARE THE NEW AND EXCITING FEATURES OF PYTHON 3.10-4?
Switch Statements is one of the Big Updates to be found in
Python 3.10 – 4 from the old or previous Context Managers, although it was yet
in the Production environment, but its going to be the best one. We can still
use the current interpreter.
This upgrade to Python 3.10-4 is currently in Alpha, but its
worth to explore and learn about these upcoming new features. These features
will surely give you a lot of convenience and ease of using Python. In this
Article, I will be discussing some of the new features and I will also be
discussing some of the minor fixes and changes.
In
this Video, I will be discussing the following.
1. Error
Messages
2. Structural
Pattern Matching (Switch Statement)
3. The
Union Operator
4. Better
Context Managers
Error Messages
One of the most common reason of
Error in Python is when a certain statement is not in compliance with the
prescribed usage. This what we called a “syntax” error. Usually, when Python encounter
this kind of error, it immediately reports it with a reason. There are some who
do not understand this reason that is why it can be frustrating.
With this new feature, there will
be big improvements with regards to the syntax, indentation, attribute, and
name errors. It is now straightforward error messages or direct to the point.
Cases in Error Message
1 Case 1: Dictionary Not Close
SyntaxError:
unexpected EOF while parsing
To others, this seems very confusing,
and they don’t understand what this syntax error is is or where to locate this
error. The Error here is the “{”. The Syntax Error occur for not closing the Dictionary.
2. Invalid
String Literal
SyntaxError: EOL while scanning string literal
The cause of this Error is the
unterminated string literal. You noticed that there is no closing apostrophe at
the end of the email.
3. Missing
“:” Before Blocks
SyntaxError: invalid syntax
In this Error, it is not clear
what part in the syntax that makes the script error. So we cannot decipher what
is the Error in this syntax only to find that we forgot to write or end the
line 1 with “:”.
4. Inappropriate
Indentation
PROPER INDENTATION
Before dig into why indentation
error happens in Python, let us figure out the basic idea of Python.
Python Programming Language was
first introduced in 1991 and from then, programmers around the world have
adapted to it and created numerous applications for web and mobile. In Python, proper
organization is vital of codes is very important for them to easily locate the
problem is there is an error between each line of code. Code should be arranged
with correct whitespaces and therefore if there is any instance that a certain
line of code is not in line with the other, the overall code will not run, and
the interpreter will simply return an error function.
In Python, all the lines of code
are arranged according to blocks, so it becomes easier for you to spot an
error. For example, if you have used the if statement in any line, the next
line must have an indentation.
Structural Pattern Matching (Switch Statement)
Its
very time and about time to improve the Structural Pattern Matching.
The pattern matching will be going to take the value
following match
and it allows us to write out other
potential cases, each defined by case
. Where we find a match between the match
-case
we will execute the respective code.
Switch
statements are mostly seen in various programming languages which provide a
neater avenue of implementing a conditional logic. They come in easy and very
useful when there a lot of conditions to evaluate.
For
illustration, we will be using structural pattern matching to write a simple
function — get_mood(day: str) -> str
that will return a string which value depends on the input
parameter. The returned value gets more exciting as we are going through the
weekend. A little and simple function, but will do for demonstration purposes:
In
a nutshell, structural pattern matching will provide a neater way to organize
your code and avoid a bunch of if-else statements. To recap:
·
Use the case keyword to evaluate for a condition (case ‘Monday’ is identical to if day == ‘Monday’)
·
Separate multiple conditions with the pipe
operator — | — e.g., if two input values should
result in the same return value
·
Use the underline operator — _ — to specify the default case.
Union Operator
The union()
method
returns a set that contains all items from the original set, and all items from
the specified set(s).
You can specify as many sets you want, separated by commas.
It does not have to be a set, it can be any iterable object.
If an item is present in more than one set, the result will
contain only one appearance of this item.
Hints or clue are big thing in Python. As you probably know,
Python will not require you to specify the data types for variable upon
declaration, but it provides a different option how to do so.
It really frustrating, though when declaring a function which
accepts a number that can be an integer of a float. In this case, how can you
specify the data type? In Python 3.10, we can use the “Union” operator.
When declaring a function that returns a square root of a number
with type hints look like this one below.
There must have a better solution — and there is — starting
from Python 3.10. You can ditch the Union
operator altogether and separate potential
data types with the pipe operator — |
.
BETTER CONTEXT MANAGERS
The concept behind Python 3.10 about the Context Manager
is now new. What is new is that it allows the enclosing parenthesis for
continuation across multiple text lines in the context managers.[1]
Let’s take this example. I have two TXT files:
·
first_file.txt —
contains “This is the first file.”
·
second_file.txt —
contains “And this is the second file.”
We’ll now use context manager to open and process the
contents from both.
The output is shown below:
As you can see, you can now manage multiple files in a much
more organized manner. Not a very significant feature, but definitely a very
good improvement.
Comments
Post a Comment