Posts

Python - numpy - ndarray

Multiple ways to create ndarrays   - matrices import numpy as np create1 = np.array([1,2,3,4])    ## creates a 1 dimensional array with 4 elements - takes input as 1 list create1 = np.array( [ [1,2,3,4],[5,6,7,8] ] )  ## creates 2 dimensinal array -- takes input as 2 lists. notice the big square bracktes outside create1 = np.array( [ [1,2,3,4],[5,6,7,8]..... [n1,n2,n3,n4] ] )  ## creates n dimensinal array -- takes input as 2 lists. create1 = np.arrange # create a copy of ndarray copy1 = np.array(create1) # this will create a new copy of array instead of assigning refrence to it

Python - How to Create Copy of a List

x = [1,2,3] y = x  --- y will reference the same list in memory as x but if we want to make a new copy of list x and assign the reference to y use below y = list(x)    --- this will make a copy of list x in memory and assign the reference to y

Python - String concatenation

String concatenation is done by using + operator '1' + '2' '12'

Python - Sets

Are unordered like dictionaries. only Unique values are allowed  Supports set operations like Union and Intersect. Sets Sets  are data structures, similar to lists or dictionaries. They are created using curly braces, or the  set   function . They share some functionality with lists, such as the use of  in  to check whether they contain a particular item. num_set = {1, 2, 3, 4, 5} word_set = set(["spam", "eggs", "sausage"]) print(3 in num_set) print("spam" not in word_set) Result: >>> True False >>> To create an empty set, you must use  set() , as  {}  creates an empty  dictionary . Use discard to remove an element from a set - if element is not found it will do nothing. num_set.discard(1) Use remove to remove an element from a set - if the element is not found it will throw an error num_set.remove(1) Set Union and Intersection method to find Unique items in 2 groups or common items in tow gr...

Python - Recursion

Recursion Recursion  is a very important concept in functional programming.  The fundamental part of recursion is self-reference - functions calling themselves. It is used to solve problems that can be broken up into easier sub-problems of the same type. A classic example of a  function  that is implemented recursively is the  factorial  function , which finds the product of all positive integers below a specified number.  For example, 5! (5 factorial) is 5 * 4 * 3 * 2 * 1 (120). To implement this recursively, notice that 5! = 5 * 4!, 4! = 4 * 3!, 3! = 3 * 2!, and so on. Generally, n! = n * (n-1)!.  Furthermore, 1! = 1. This is known as the  base case , as it can be calculated without performing any more factorials.  Below is a recursive implementation of the factorial  function . def factorial (x): if x == 1: return 1 else: return x * factorial (x-1) print(factorial(5)) Result: >>> 120 >...

Python Comments- Multi-Line comments

Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code. Creating a Comment Comments start with a  # , and Python will ignore them: Example #This is a comment  print ( "Hello, World!" ) Multi-Line Comments Python does not really have a syntax for multi-line comments. Or, not quite as intended, you can use a multiline string. Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it: Example "" " This is a comment written in  more than just one line " "" print ( "Hello, World!" )