Working With New Generation of Python

Working With New Generation of Python

Python is a great tool for writing short yet power scripts in order to perform complex tasks like image processing and NLP. But you can further enhance your code and reduce some lines of script by incorporating the following features that latest Python versions offer.

WALRUS OPERATOR

Assignment expressions can be used intelligently to avoid extra lines of code. Syntax that enables developer to combine assignment expression within a looping condition were much awaited. Python 3.8 fortunately came up with such kind of assignment operator named as Walrus operator (?:=?).

No alt text provided for this image
walrus operator in python



DICTIONARY UNION

Combining two collections is a commonly used operation. Out of all, merging two dictionaries / map using a simple operator can be very handy. With Python 3.9, you can do that with merge operator symbolised as ‘|?

No alt text provided for this image
Merging multiple dictionaries


Further you can enhance the merge operator by using it with assignment symbolised as ‘?|=?

No alt text provided for this image
Merging and assigning dictionaries using single operator



REVERSING DICTIONARY

From Python 3.8 onwards, developers can use?reversed()?function with dictionary now. This can be useful while ordering large dataset without any need of looping through the complete collection.

No alt text provided for this image
Reversing the dictionary in python 3.8list comprehension



COLLECTION COMPREHENSION

Generally, construction of data structures like list, dictionary or tuples with a regular pattern demands few lines of code consisting of the loop structure and its body statements.

As Python is known for its powerful and compact syntax, version 3.8 came up with a concept of comprehensions for collections which allows the programmers to generate collections in one line just like a PRO!

No alt text provided for this image
list comprehension


Comprehensions can have conditional statements in order to provide more control on generating collections.

No alt text provided for this image
list comprehension with if-else statement


Unlike list and dictionary comprehensions, tuple comprehensions return generator which needs to be iterated further for consumption

No alt text provided for this image
Example of other comprehensions



TYPE HINTING

Type annotations are very common in various languages which aims to provide more meaning to the code. It can be classified as

Active Type Annotations

Such annotations decorate the code and also perform some actions (such as validations) which affects the flow of control by validating data types

Passive Type Annotations

Such annotations decorate the code to enhance readability and descriptiveness but does not affect the flow of control in any sense

Python’s type hinting / annotations are passive in nature. The type annotations can provide information regarding datatypes of argument and return type of function as shown

No alt text provided for this image
Type hinting in python 3.8


Being a dynamically typed language, it is often hard for a developer to read large code snippets and identify the data type and structure of expected values. Adding type hints to your code can reduce this issue and greatly enhance readability while backtracking code snippets

MISCELLANEOUS FUNCTIONS

Developers spend most of their time in processing the data structures. Modern day languages like Kotlin, Swift offers loads of features to process the data structures in a concise manner without compromising efficiency.

Following are some of the most used processing operations :

Sorting

Arranging the collection in a specific order depending on a comparator

Filtering

Filtering the actual collection in order to provide new collection of items which satisfy certain conditions

Mapping

Converting existing collection into new collection with different nature of items

No alt text provided for this image
Examples of sorting, filtering and mapping


All of the mentioned operations use?lambda ?syntax which allows the develop to write functions in a compressed syntax


f-STRING FORMATTING

String formatting and interpolation are handy features when a software demands high amount of formatted data to be pulled from or pushed to the user. Python offers?f-strings?in order to fulfil such requirements.

No alt text provided for this image
Example of f-strings


Developers can use?=?in order to evaluate and print the self descriptive strings. You can follow the code snippet given below in order to clarify this description

No alt text provided for this image
f-strings with short syntax



VIRTUAL ENVIRONMENT

PIP works like a charm in adding dependencies to the projects. But straight away installing project dependencies globally offers few disadvantages on system level such as unnecessary memory consumption.

To resolve this issue, python offers?virtual environment?which offers a separate space for a project to all of its dependencies and also keeps those dependencies private to the project.

Installing virtualenv

No alt text provided for this image
installing virtualenv library


Creating virtual environment

No alt text provided for this image
Setting up virtual environment


Activating / Deactivating virtual environment

No alt text provided for this image
Activating and deactivating virtual environment on demand



REFERENCES


CONCLUSION

Latest updates in Python has enhanced the syntax of the language and has offered more powerful features to the developers. Python has maintained its USP of offering maximum functionality in minimal code.

“ Whenever the language developers increase the compactness of language beyond a threshold, it in turns increase the complexity of the code. Although the nature of releases this language is providing is familiar to the world of software developers and also resembles modern languages like Swift. “

You can also?read

For more content like this you can follow me on my?Medium profile ?or follow the links below

要查看或添加评论,请登录

社区洞察

其他会员也浏览了