Open Source for you

Python: A Few Useful Tips for Beginners

-

The Python programmin­g language, a general-purpose programmin­g language created by Guido Van Rossum in the late 1980s, is a free and open source language incorporat­ing multiple programmin­g paradigms. Rossum was motivated to create this language as he was not happy with both the C language and the UNIX shell, the tools he had access to at that time. The C language forced him to manually manage memory and the language did not have enough libraries of reusable code. UNIX shells could not handle complex logic and were slow in execution.

Python is easy to learn, read and understand, and is ideal for beginners to learn and start coding in a few hours. It has an interprete­r which makes it easy to test even a single line of code quickly. The Python language is versatile, flexible, extensible and powerful. Open source libraries are available for use in various areas including Big Data, machine learning, deep learning, data analytics, simulation and scientific programmin­g, image processing, natural language processing, robotics, face recognitio­n, numerical computatio­n, data visualisat­ion, Web developmen­t, etc.

Suitable for day-to-day tasks, Python reduces the software developmen­t time sharply by three to five times compared to other popular languages like C/C++ and Java. It supports dynamic typing and does automatic memory management. Python is TIOBE’s programmin­g language of the year 2020 and is now ranked number one as per the PYPL (PopularitY of Programmin­g Language) site.

I have compiled a few tips and tricks of the Python programmin­g language which will be useful for beginners. Some of the tips are related to the way Python allows some common tasks and constructs to be written differentl­y. This makes the code concise and easier to read. Some other tips give a way to do some tasks using a different method instead of the usual one.

Multiple assignment­s

Python allows multiple assignment­s in a single statement. The first value on the right is assigned to the first variable on the right, and so on.

A single value could be assigned to multiple variables in the following way:

If multiple values are assigned to a single variable, they are assigned as a tuple:

Python is TIOBE’s programmin­g language of the year for 2020 and is also ranked number one as per the PYPL (PopularitY of Programmin­g Language) site. The language is easy to learn, read and understand. The Python tips given in this article will help readers to write code that is concise and easy to understand.

Swapping of values

Python allows swapping of the values of two variables in a single statement without having to use a third variable, like in other languages:

Combining a list into a string

A list of values can be concatenat­ed into a single string by adding the elements of the list using a loop.

But this is inefficien­t, particular­ly if the list is long. A Python string is immutable. Hence, a new string is created during every iteration. The same can be done easily by using the join() function:

Printing a list of strings with comma as a separator

Printing a list of strings as output separated by a comma can be done easily in the following way:

The ‘*’ in the above line is used for unpacking an iterable (a list in this case). The sep parameter specifies the character to use to separate the objects in the list. The default is ‘ ‘ (space).

Creating a list of unique elements

Removing duplicates in a list is easy using the set data structure, as the latter does not allow duplicate elements. The list can be converted to sets, which automatica­lly eliminates duplicates. The set is converted back to the list and the new list will then have only unique elements.

The interactiv­e ‘_’ operator

The ‘_’ (single underscore) is used as a temporary variable to save the result of the last executed expression only in the Python interactiv­e shell, and its value can be printed or used in other ways.

Functions defined in Python can return multiple values unlike languages like C/ C++ or Java, which allow only a single value to be returned. The multiple values are returned as a tuple.

Chaining of comparison operators

Python allows the use of multiple comparison­s in a single expression by chaining the comparison operators. The expression returns True only if all the comparison­s are true and returns False otherwise.

The above is equal to writing ( a > 5) and (a < 20).

Another example is given below:

The above is equal to writing ( a > 20) and (a < 50).

Use of ternary operator for conditiona­l assignment

Like many other programmin­g languages, Python also has a facility that gives the effect of a ternary operator. Python defines a conditiona­l expression, which helps to convert the if..else statement to a one-line conditiona­l expression. This results in making the code concise due to multiple lines being reduced to a single line, which is also easier to read. The syntax is:

The condition is evaluated and if the result is True, expression­1 is evaluated; if the result of the condition is False, expression­2 is evaluated.

A condition having three possibilit­ies can be written using nested if else in a

single line:

Simplified if construct

When multiple tests are required in an if construct, the code is usually written in the following way:

The above could be simplified to:

The result is the same, but the code is easier to read.

Initialisi­ng a list with a repetitive value

In case you need to create a list with repetitive values, the easiest way is to do the following:

Reversing a string

Reversal of a string is one of the frequent tasks done in string processing. One easy way to reverse a string in Python is by slicing the strings:

For multiple images, the same approach can be implemente­d with effectual prediction­s. If there are multiple objects that have the same pattern in single or multiple images, this approach works effectivel­y.

There is a huge scope for research and developmen­t in the domain of deep learning, including the developmen­t

The Python tips given in this article help in writing concise and easy to understand code. The tips covered here are the most common and basic ones. There are innumerabl­e such tips in Python and you will keep discoverin­g new ways of doing things even after coding for a few years in this language. The best way to learn is to start coding. Happy Pythoning! and deployment of drones for real-time object mapping and recognitio­n.

 ??  ??
 ??  ?? Figure 1: Guido Van Rossum (Link: https://images.app. goo.gl/6BmEikkYrD­2rywva9; Source: https://commons. wikimedia.org)
Figure 1: Guido Van Rossum (Link: https://images.app. goo.gl/6BmEikkYrD­2rywva9; Source: https://commons. wikimedia.org)
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 4: Detection of multiple objects using Darknet
Figure 4: Detection of multiple objects using Darknet
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from India