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.
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,
Here comes the hack,
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.
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.
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.
Hack 2 →
Phew! That was long. Don’t worry. This is short. Let’s start with:
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:
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.
The above statement prints a grinning face. You can play with the last three values for each value results in a unique emoji.
This statement also prints a grinning face. If you know the name of the emoji, then this works best.
Grace Hephzibah M.
Grace Hephzibah M - Campus Ambassador - CodXCrypt | LinkedIn
Hey! I am Grace Hephzibah M. I am currently pursuing B.Tech in Computer Science department which includes AIML…
Follow CodXCrypt for more opportunities: