10 tricks and facts about python programming you should know

July 30th, 2018 . 5 minutes read
Blog featured image

Python is one of the most popular programming languages nowadays on account of its code readability and simplicity.
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Here in this blog, I am sharing 10 tricks and facts about python programming.

Python offers a stepping stone into the world of programming. It is a high-level built-in data structure, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components or services. Python supports modules and packages, thereby encouraging program modularity and code reuse.

what is python

So, let’s start with some interesting tricks and facts about python programming.

10 Facts About Python Programming to Know-

1. We can return multiple values in python:

It’s a very different feature in python that returns multiple values at a time. Even though Java doesn’t support multi-value returns. But by using some conditions we can return an array in Java programming. Here is an example of python function that returns multiple values at a time.

2. We can easily swap two number in python without using 3rd variable:

3. Storing Elements Of A List Into New Variables:

Firstly, we talk about the List. Python offers a range of compound datatypes often referred to as sequences. The list is one of the most frequently used and very versatile datatypes used in Python. They are just like the arrays declared in other languages.

But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., they can be altered once declared.

We can use a list to initialize a no. of variables. While unpacking the list, the count of variables shouldn’t exceed the no. of elements in the list.

4. Combining Multiple Strings:

We can easily concatenate all the tokens available in the list, see the below example.

5. Check The Memory Usage Of An Object:

In Python 2.7, a 32-bit integer consumes 24-bytes whereas it utilizes 28-bytes in Python 3.5. To verify the memory usage, we can call the method. See the below example

6. Allow Negative indexing:

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item, and so on. It is a very useful concept of the List and Tuple in python. For example:

7. Else clause with for loop:

for loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement. They are really useful once you understand where to use them. I, myself, came to know about them a lot later.

The common construct is to run a loop and search for an item. If the item is found, we break out of the loop using the break statement. There are two scenarios in which the loop may end. The first one is when the item is found and a break is encountered. The second scenario is that the loop ends without encountering a break statement.

Now we may want to know which one of these is the reason for a loop’s completion. One method is to set a flag and then check it once the loop ends. Another is to use the else clause.

This is the basic structure of a for/else loop: for item in a container:

For Ex:

8. Getting the last Output Stored in Interpreter:

In the interpreter _ stores the value of the last output.

9. Applying if – else in a fashion, similar to the ternary operator in C:

10. We can easily print the string N times.

I’ll tell you more tricks on my next blog that coming soon, if you have any query then please comment on the comment box. I’ll definitely try to solve your queries.

Also read-

Habilelabs is the premier software development company providing high-quality web and mobile solutions successful. Contact us to discuss your project.

Author: shainkey.jain