Impress your colleagues with clean and concise code.

Learn 10 awesome beginner-friendly Python coding “tricks” with in-depth explanations.

The following list is in no particular order.

I hope you enjoy it!

1. Swap Two Variables without a Third

In Python, it is possible to swap two variables without a third one:

a = 1b = 2a, b = b, a

Explanation

The above “trick”…

You can see a staggering 200+ miles from a commercial airplane

The distance to the horizon depends on how high you are observing it.

If you are 5'6'’, standing on the shoreline, the horizon appears 2.75 miles away from you.

In this article, you learn how to calculate the distance to the horizon with a simple formula.

How Far Can You See From a Plane

How to find the inverse function

To find the inverse function, follow these four steps:

  1. Write the equation as a linear equation (swap f(x) with y).
  2. Swap variables x and y.
  3. Solve for y.
  4. Apply the inverse notation (replace y with f^(-1)(x)).

Example

Find the inverse function of

How many quarts are in a gallon

There are 4 quarts in 1 gallon.

This is easy to remember. Consider the word “quart”. It refers to a quarter.

When speaking about volumes, a quart is a quarter of a gallon. In other words, it is one-fourth of a gallon—so there are four of them in a gallon.

What Is the Resolution for 4k

The resolution for 4k is

3840 x 2160

This means a 4k screen has a whopping 8.3 million pixels in total. This is 36 times more than a good old 480p SD screen!

Why Is It Called 4k?

The 4k refers to 4000. But the resolution of 4k is “only” 3840 x 2160.

The reason…

Learn everything about structural pattern matching in Python

Switch Case in Python

Python switch-case statements are here! And they are actually called match-case statements.

This means you can soon replace this type of if-else mess:

With a more compact match-case statement:

In some situations, the latter approach is better. It can make the code more readable and less repetitive.

Notice…

Artturi Jalli

I’m a tech expert, entrepreneur, and software engineer from Finland. I write about programming, tech, and science. Visit my blog at www.codingem.com.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store