Posts

Difference between sort and sorted functions in Python

Image
Why Sort Lists? We often need to sort the elements of a list for various reasons like searching for elements, merging elements, optimizing list performance and readability etc. In Python we can use 'sort' and 'sorted' functions to sort a list. While both functions can be used to sort a list there are some differences in the usage of these functions that should be kept in mind before using one over the other. Sorted Sorted function (built-in) takes a list as argument (along with other optional arguments) and returns a new list with elements of original list sorted in ascending or descending order.        Output: To sort the list in descending order, add reverse = True with the list argument.      Output: Sort Sort function/ method will sort the original list without the need of creating a new list. It is applied on the list object rather than taking the list as an argument.      Output: Keep in mind that you should not assign the sort fun...

Modulo Multiplication Patterns using Python

Image
This article is inspired from a YouTube video by Mathologer that explains the mathematics behind vortex mathematics and Tesla's 3-6-9 pattern. Please refer the video to understand the logic behind these patterns.  The Python code to generate (at the end) Modulo Multiplication patterns take two inputs, Modulus and Multiplier. Here are some examples with different values of input. Modulus - 500, Multiplier - 2 Multiplier 2 generate the famous cardioid curve. A cardioid is generated by tracing a point on a circle rolling on another circle of the same radius. To read more about cardioid refer to this article .  Modulus - 7417, Multiplier - 240 Some more patterns (Modulus x Multiplier):                          701x260                                               ...

Fibonacci Numbers using Python

Fibonacci sequence is a series of numbers where each number is the sum of two preceding numbers. The sequence is given by: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and so on We can generate above Fibonacci sequence programmatically with or without recursion.  Fibonacci series without recursion: Below function prints x Fibonacci numbers starting from 0.  def fib (x): a = 0 b = 1 if x < 1 : print ( 'invalid input' ) else : if x == 1 : print (a) else : print (a , end = ' ' ) print (b , end = ' ' ) for i in range ( 2 , x): print (a+b , end = ' ' ) c = a + b a = b b = c fib( 12 ) Output: 0 1 1 2 3 5 8 13 21 34 55 89  Fibonacci series with recursion: def fib (x): if x <= 1 : return x else : return fib(x - 1 ) + fib(x - 2 ) terms = int ( input ( "How many Fibonacci numbers to generate?" )...