Tips to code golf in Python [Part-2]
Like in golf, the low score wins, similarly “Code Golf” in Python refers to having the shortest amount of code in terms of “characters” or “bytes”.
Missed the Part 1? Read it here: https://justforfun4278.medium.com/tips-to-code-golf-in-python-part-1-67fc3acadbe0
Tip #11
Use &
instead of in
to check if an element is in a set
a = {1, 2, 3, 4}
if 4 in a:
print("IN")# can be reduced to
if{4}&a:print("IN")
Tip #12
When you have to check for two integer or two boolean values using and
or or
you can use *
and |
respectively
a = 1
b = 1
if a and b:
# can be reduced to
if a*b:a = 1
b = 0
if a or b:
# can be reduced to
if a|b:
Tip #13
To reverse a string use [::-1]
. This is a well-known trick
s = "hello"
for i in range(len(s)-1, -1, -1):
print(s[i]# can be reduced to
for i in s[::-1]:print(i)
Tip #14
Use *
to unpack arguments in an easy and short way
a = "justforfun"
b = list(a)
# can be reduced to
*b,=ab = set(a)
# can be reduced to
b={*a}b = tuple(a)
# can be reduced to
b=(*a,)for i in a:print(i, end=" ")
# can be reduced to
print(*a)
Tip #15
Convert every check into a single condition in an if
, elif
, while
loops
n = 10
if n > 1 and n < 11:
# can be reduced to
if 1<n<11:
Tip #16
Check for the last i-th element of an iterable using ~
a = [1, 2, 3, 4......]
# challenge is to print the i-th last element
# Normal way
print(a[::-1][i]) # where i is some index
# can be reduced to
print(a[~i])
# Note `~` is used instead of `-` because -0 evaluates to 0 so u wont be able to get -ith terms if i=0 whereas ~0 evaluates to -1
Tip #17
Assign variables to the functions/ methods that are used more frequently
a = int(input())
b = int(input())
c = int(input())
# can be reduced to
i=input;j=int;a=j(i());b=j(i());c=j(i())
Tip #18
Use multiplication operator instead of range
for i in range(10):
# can be reduced to
for i in" "*10:
# this works as " "*10 is " " which is of length 10 so it will iterate 10 times
Tip #19
Use the wildcard import *
to reduce some chars
import math as m
print(m.sqrt(144))# can be reduced to
from math import*
print(sqrt(144)) # skip all `m.` too!# Note neither wildcard nor code golfing tricks are good to use in professional works
Tip #20
Use ~
to replace -x-1
n = 1
-x-1 # -2
~x # -2# Similarly it can be used for an if statement as a boolean too
n = False
for i in something:
n = not n# can be reduced to
n = 0 # False is a subclass of `0` which evaluates to False
for i in something:
n = ~n # changes 0 to -1 which evaluates to True, changes -1 to 0 which evaluates to Falsea = 1
b = 2
a*(b-1)
# can be reduced to
a*~-ba*(b+1)
# can be reduced to
a*-~b
That’s it for this part. Hope you learned something new today that you didn’t know exists in Python. This may help you to boost your code golfing skills. I will be posting many parts on Code Golfing in Python, so stay alert!
If you missed Part 1/ want to read it again: https://justforfun4278.medium.com/tips-to-code-golf-in-python-part-1-67fc3acadbe0
My Github: https://github.com/techguy940