3 Pro Hacks For Beginners in Python

CodXCrypt
3 min readApr 2, 2021

For a beginner, coding is a little tough and sometimes boring. It is a dream of any person to do something mind-blowing, especially in python code.

Img source: https://adolfoeliazat.com/wp-content/uploads/2020/08/Scientific-Python-Scipy-696x385.jpg. Please note that CodXCrypt doesn’t own the image.

So, here I have few hacks up my sleeve that can take your simple code to the next level. Some hacks need a library that you should import to the code. Don’t worry. These libraries come along with the initial setup and there is nothing to install. Why wait? Let’s dive in….

Hack 1 → Random

Random is one of the amazing libraries in python. It makes the same code run differently every time the program is executed. First,

import random

Here comes the hack,

Snippet 1

random.random()

Every time we use a function of a library, the name of the library must be mentioned. This function generates a number from 0 to 1. The generated number is different every time.

Snippet 2

random.randint(x,y)

If you replace the x and y with values, the randint() generates a unique value in the specified range. But what if your expected values follow an interval? Like, if you want values like 2,4,6…?

Try randrange() in that case.

random.randrange(first_num,last_num,interval)

Snippet 3

random.choice(a)

If you want a random value from what you have stored that is in a list or tuple or related types. Then this choice works best.

You can also use shuffle() to rearrange a set of data randomly.

random.shuffle(a)

Hack 2 →

Phew! That was long. Don’t worry. This is short. Let’s start with:

import time

Time is one of the important libraries used in multi-threading. It simply means to do many tasks at the same time. Here, each thread refers to a work or a task. The computer pauses 1 thread (work) and runs another thread (work) based on many criteria. This way, not a single millisecond is wasted.

We will use sleep() for this hack. This allows the computer to stop its current thread and sleep for a given time and then resume its task. Here we will do some simple programs.

Create a variable that holds some string. Let's assume it will be,

a = “I am learning Pro Hacks from the CodXCrypt published article”

Here comes the hack.

Run a for loop with the string and let the computer sleep for a constant time in each for loop.

for i in a:

time.sleep(.1)

print(i, end = “”)

end = “” is used to print the letters side by side. Try this code and see the printing itself is so cool in the terminal.

Hack 3 → Emoji

Who doesn’t love emojis? Wouldn’t it be so cool to add some emoji to your code output? Then this hack is for you. You can print an emoji in two ways.

print(“\U0001f600”)

The above statement prints a grinning face. You can play with the last three values for each value results in a unique emoji.

print(“\N{grinning face}”)

This statement also prints a grinning face. If you know the name of the emoji, then this works best.

Author:

Grace Hephzibah M.

Follow CodXCrypt for more opportunities:

--

--

CodXCrypt

CodXCrypt Community is an Open source platform for coding enthusiasts.