Code Golfing Tricks in Python
In this tutorial, we will learn basic code golfing tricks in Python.
Code Golfing means writing a statement or expression using a minimum number of characters. In simple words, we can say Code Golfing means to code a given program by using the minimum number of characters. As a result, code optimization can be obtained.
Implementation is the key, so let’s directly jump to the implementation part.
Lists
- Printing elements of a list: If we want to print elements of a list, we need to use a for loop but this can also be done using * operator. This same approach can be used to print elements of a set or a tuple.
# Print elements of a list using loop l=[22,45,30,9,4] for i in l: print(i) # Print elements of a list using * l=[22,45,30,9,4] print(*l)
- Adding elements to a list: Elements in a list can be added by using the append method. But this can also be accomplished by using the += operator.
l=[1,2,3,4,5] ele=6 # Using append method l.append(ele) # Using += operator l+=[ele]
Functions
- Floor function in Python: Floor function is used to fetch the closest integer less than or equal to a given number. Floor function is present in the “math” module of Python library. The same can also be done by using floor division operator (//).
#Using floor function from math import floor x=5/3 print(floor(x)) #Using // operator x=5/3 print(x//1) # We need to use floor division operator with 1
- Ceil function in Python: Ceil function is used to fetch the closest integer greater than or equal to a given number. Ceil function is also present in the “math” module of Python library. The same can also be done by using floor division operator (//).
# Using ceil function from math import ceil x=5/3 print(ceil(x)) # Using // operator x=5/3 print(-(-x//1)) # We need to use floor division operator with 1
Loops
- Print diagonal elements of a Square Matrix: Diagonal elements can directly be printed by using two for loops. But, we can implement the same by using a single for loop.
l=[[1,2,3],[4,5,6],[7,8,9]] # Using 2 for loops for i in range(3): for j in range(3): if i==j: print(l[i][j]) #Using a single for loop for i in range(3): print(l[i][i])
Indexing
- Print a string in Reverse: In Python, indexing starts from 0 to n-1 in the forward direction and from -1 to -n in the backward direction. Hence, this backward indexing can be used to print elements of a string. The same approach is valid for lists and tuples.
s='CodeSpeedy' # Without code golfing for i in range(len(s)-1,-1,-1): print(s[i],end='') # With code golfing for i in s[::-1]: print(i,end='')
Assignment
- Assigning same values to multiple variables: We can assign the same values to multiple variables with the help of code golfing.
# Without code golfing x,y,z=0,0,0 # With code golfing x=y=z=0
- Assigning a single character to multiple variables: We can assign a single character to multiple variables of the same type with the help of code golfing.
# Without code golfing x,y,z='a','b','c' # With code golfing x,y,z='abc'
Recommended Posts:
- Mathematical Functions in Python
- List and Dictionary Manipulation in Python
Leave a Reply