How to use Functions In Python

  • Sibling of Floor function: Suppose we want to find the floor value of a real number, we generally import floor function from math and then apply on the number. But we can simply apply the floor division with 1, which will give us a fruitful result. 

    Original Code: 

from math import floor
n = 3/2
print(floor(n))

Golfed Code: 

n = 3/2
print(n//1)
  • Sibling of Ceil function: Suppose we want to find the ceil value of a real number, we generally import ceil function from math and then apply on the number. But we can do this operation in a more beautiful manner by first multiplying with -1 and then apply floor division with 1 and again multiply with -1. 

    Original Code: 

from math import ceil
n = 3/2
print(ceil(n))

Golfed Code: 

n = 3/2
print(-(-n//1))
  • Lambda functions: Lambda definition does not include a “return” statement, it always contains an expression which is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions. 

    Original Code: 

def c(a):
  if a < 3: return a-5
  else: return a+5

Golfed Code: 

c=lambda a:a<3and a-5or a+5
  • To get the entire alphabet string: We can use the map function of python to get the string of whole alphabet set. 

    Original code: 

string = 'abcdefghijklmnopqrstuvwxyz'
or 
string = [chr(i+97)for i in range(26)]

Golfed Code: 

string = map(chr,range(97,123))

Code Golfing in Python

Code Golf in Python refers to attempting to solve a problem using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters “wins”. 

Python is a fantastic language for code golfing due to backward compatibility, quirks, it being a high-level language, and all the coercion. So, here we will look at some Code golfing techniques in Python language. 

Similar Reads

Using loops

Collapse two numerical loops: Suppose you are iterating over a matrix of m rows and n columns. Instead of two nested for loops, one for the row and one of the columns, it’s usually shorter to use a single loop to iterate over the m*n matrix....

Using operators

Addition or Subtraction by 1: For integer n you can write  -~n is equivalent to n+1 ~-n is equivalent to n-1...

Using Functions

Sibling of Floor function: Suppose we want to find the floor value of a real number, we generally import floor function from math and then apply on the number. But we can simply apply the floor division with 1, which will give us a fruitful result.  Original Code:...

Using indexing

Indexing Technique for conditions: When we have a certain condition which will give the answer in the form of small integers then we can use that in integer index in list or tuple to get our final answer.  Original Code:...

Using assignment

Assigning the same values to multiple variables: We can assign the same value to multiple variables either in a single line or multiple lines. Original Code:...

Using conversion

Converting iterables into the list: Imagine you have any ordered iterable like a tuple or string but you want to convert it into a list, so you can do this with * operator.  Original Code:...

During joining of different iterables

Joining multiple Lists: We can join the multiple lists using + operator, but for code golfing we can do the same using * operator.  Original Code:...