Another 5 Cool Python Tricks
Tags: python
Published: 2020-06-21 07:35:15

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lt12qc5d11epvtlrqaoy.png)

Trick #1 - Reversing Strings:

Ever heard of "String Slicing" before?
If not, let me explain it real quick.
In Python a string is immutable, slicing creates a new substring from the source string and the original string remains unchanged.
Using the symbol '[::-1]' any string can be reserved after the variable name:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rk1y1bvs9wzvfmltkws0.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o3kq8dlfmlzil5jc24gu.JPG)

Trick #2 - Swapping Two Variables:

Swapping in Python occurs  when two variables refers to mutually exchanging the values of the variables.
And there several way to achieve this in Python.

The first and most common method is using a temporary variable:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/upa958wpm17enp0bk4zy.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/39sesiolabhay8gxv7uh.JPG)

Another way to achieve swapping, is without a temporary variable:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b4sg06nmb1iw4sc4pg21.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e0ioxtpij4e0q53b1o8t.JPG)

And finally, an alternative method for swapping variables is using the elegance of Python with and Optimal Solution:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z3sjz8tyjne5cgbh1tqj.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vqoziewo2un146w9h2pf.JPG)

Trick #3 - More Than Only Conditional Operator:

To achieve this in a single expression, you have to make use of the logical operators, for example if you you have to print the value of a variable that is greater than 100 and lower than 200, then the code should be something like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jfg7rs382zy4wdramq67.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b1wl6q2f2308o89bksfh.JPG)

Combining the conditional operator into a single expression, the code looks something like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oxknttgm4vvbo56n6s0r.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oeftodcvnc1m5ub70lfi.JPG)

Trick #4 - Find The Occurrence of All Elements in a List:

Let's say you have the following list:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4fljhy6k9scobt7mc01h.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vh4fyhe2mzzgvly9n4o2.JPG)

To find the occurrence of each letter you need to use the 'Counter' method:

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ted7yjh07gpb9qbqltui.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0fx5zx2cco5mpb8wgu6k.JPG)

Trick #5 - Converting Mutable to Immutable:

This is achievable with the 'frozenset()' function.
Frozen set is an immutable version of a Python set object. 
While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zpmem9v1b80p2aq9wu7f.png)
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jpxtvrrcbu4chv5tsun3.JPG)

In this example, since the 'frozenset()' was applied to the list 'list', the item assignment is restricted.

So that's it... hope you'll find it useful (!)