Python shortcuts you need to know
Published: 2023-06-10 17:53:07
A few beginner focused Python shortcut ideas
Assalamu Alaikum,
Many of those who do competitive programming have chosen Python for competitive programming. Many of them are fairly skilled and many are still new.
Today my blog is for those new coders. Let’s get started.
The main reasons people choose problem solving with Python are:
- Python is easy to read: you can easily understand what is meant by reading the code
- Less code in Python: The same solution can be easily achieved in Python with very few lines of code in any other language. Sometimes we need 100 lines of code to code with C++, where it can be completed in 10 lines with Python.
- Takes less time: Since less code is written, it is natural to take less time to write python code.
However, Python also has some disadvantages. The main disadvantage of Python in competitive programming is the execution time limit. Python’s small code also takes a lot of time. But if you use C++ or JAVA or any other language in the same code, it will take less time. Anyway, I will discuss later in another blog about how to reduce the execution time of Python.
Now, Lets see some shortcuts of python code:
Shortcut 01:
We see test cases at the input of many problem statements. If the test case requires input, most new coders probably take the input like this and run the loop:
t = int(input())
while(t):
#your code
t = t - 1
or they might take input like this:
t = int(input())
for i in range(t):
#your code
Both the ways are fine, but in my opinion, we can modify the 2nd way as more shortcut.
example:
for _in range(int(input())):
# your code
In this case, if you want to mention the test case number somewhere, you can express it with (_) if you want 🙌 (I used _ here, because it is normally a non-used character. You can use any other variable, but be careful about declaring variable name inside the test case loop. Don’t use same name twice)
# if _ = 0
print("Case ", _+1)
# if _ = 1
print("Case ", _+1)
Output:
Case 1
Case 2
Shortcut 02:
While taking multiple input, most of the time we use this process:
a = input()
a = a.split(" ")
print(a)
Input:
“1 2 3 4 5”
Output:
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
Here all array items come as strings. But we need integers. Then wherever you need to create an integer, create an integer and then use the array. well its 😰 very difficult work! But actually no, Python can easily take multiple integer inputs at once.
Lets have a look at the code below:
a = [int(x) for x in input().split()]
print(a)
Input :
1 2 3 4 5
Output :
[1, 2, 3, 4, 5]
Now each element here is input as an integer. There is another way:
a = list(map(int, input().split()))
print(a)
Input :
“1 2 3 4 5”
Output :
[1, 2, 3, 4, 5]
Shortcut 03:
Sometimes, to print the list items without commas in the output of many problems, we often show the output like this:
a = [1,2 3, 4, 5]
for i in a:
print(i, end=" ")
print(end="\n")
Output:
1 2 3 4 5
But this is not good practice. The easiest and most appropriate way is:
a = [1,2 3, 4, 5]
print(*a)
Shortcut 04
Sometimes we want to find out digit count or sum of digits from a number. So, for this, we can code like this:
a = 1234567890
digit_count = len([int(x) for x in str(a)])
sum_of_digit = sum([int(x) for x in str(a)])
print("Total digit: ", digit_count)
print("Sum of digits: ", sum_of_digit)
Output :
“Total digit : 10”
“Sum of digits : 45”
That’s it for today’s, I’ll be back later with some new Python shortcuts. Until then, Allah Hafez 🙌
python
Published: 2023-06-10 17:53:07