Learn Python from Khan Academy

print (3+7) print (type(3+7)) # print ("Hello") print (type("Hello")) #
 * 1) Khan Academy
 * 2) https://www.khanacademy.org/science/computer-science > Python Programming

a = [7,13,15] a[0] = 5 b = a print (b)
 * 1) List

c = b[:] #[5, 13, 15] print (c) d = b[0:2] #[5, 13] [m:n] m=start from, n=up to but not included print (d)

d.append(10) #[5, 13, 10] print (d)

range(3) #[0, 1, 2] range(1,6) #[1, 2, 3, 4, 5]  up to but not including the upper range(0,8,2) #[0, 2, 4, 6]
 * 1) Range

sum = 0 for i in range(10): sum = sum + i   print (sum)
 * 1) For loop
 * 1) For loop

sum = 0 i = 0 while i < 10: sum = sum + i   print (sum) i = i + 1
 * 1) While loop
 * 1) While loop

a = "This is a string" len(a) b = "This is another string" a+b #Concatenate
 * 1) String
 * 1) String

b.split(' ') #['This', 'is', 'another', 'string'] b.find('no') #9 b.find('is') #2

b.replace('is','as') #'Thas as another string' Did not replace b

match_string = "3+4*2" print (match_string) eval(match_string) #11 eval(match_string +'1') #87

expression_string = "a + b" eval(expression_string)


 * 1) Sample of Factorial Program

number = 2 product = 1 for i in range(number): product = product * (i+1)
 * 1) Window pop for an input
 * 2) number = input("Enter a non-negative integer to take the factorial of:")
 * 3) number = eval(input("Enter a non-negative integer to take the factorial of:")) #python 3
 * 4) python 3 = the result of input is string as is
 * 5) python 2 = the result will be evaluated automatically

print (product)

def factorial1(number): product = 1 for i in range(number): product = product * (i+1) return product
 * 1) Define a function
 * 2) Interactive
 * 1) Interactive

user_input = 5 factorial_of_user_input = factorial1(user_input) print (factorial_of_user_input) print (factorial1(4))
 * 1) Use of function

def factorial2 (number): if number <= 1: #base case return 1 else: return number * factorial2(nummber - 1)
 * 1) Now we are doing the "Recursive" Factorial function
 * 2) Is it faster?


 * 1) Comparing Iterative vs Recursive Functions?


 * 1) Fibonacci numbers
 * 0,1,1,2,3,5,8,13,21,34,55
 * 1) Add previous two numbers to be new number
 * 2) Golden Ratio = two numbers next to each other
 * 3)               having the later divided by the former
 * 4)  21/13 ~ Golden ratio
 * 5)  55/34 ~ even closer to Golden ratio


 * 1)  When a > b,  (a+b)/a = a/b = GOlden ratio


 * 1) Assignment
 * 2)   fibonacci(1) = 1
 * 3)   fibonacci(3) = 2
 * 4)   fibonacci(0) = 0

def fibonacci_interative(n): print ("Fibonacci: " + str(n)) terms = [0,1] i = 2 while i <=n: terms.append(terms[i-1]+terms[i-2]) i = i+1 return terms[n]
 * 1) Iterative Fibonacci Function

def fibonacci_recursive1(n): if n == 0: return 0 elif n == 1: return 1 else: return (fibonacci_recursive1(n-1)+fibonnaci_recursive1(n-2))
 * 1) Recursive Fibonacci Function

def fibonacci_recursive2(n): if n<2: return n   else: return (fibonacci_recursive2(n-1)+fibonacci_recursive2(n-2))
 * 1) ?? NameError: global name 'fibonacci_recursive' is not defined

n = 5 print ("Fibonacci of " + str(n) + " is " + str(fibonacci_recursive2(n)))

a = [7,1,3,5,2,8] a.sort
 * 1) Sort


 * 1) Insertion Sort
 * 2) Starting with the 2nd position and compare with what's before it
 * 3) 2nd position: [7,"3",1,2,4,6] > ["3",7,1,2,4,6]
 * 4) 3rd position: [3,7,"1",2,4,6] > [3,"1",7,2,4,6] > ["1",3,7,2,4,6]
 * 5) 4th position: [1,3,7,"2",4,6] > [1,3,"2",7,4,6] > [1,"2",3,7,4,6]
 * 6) 5th position: [1,2,3,7,"4",6] > [1,2,3,"4",7,6]
 * 7) 6th position: [1,2,3,4,7,"6"] > [1,2,3,4,"6",7]
 * 8) STOP

def sort_insertion(list): for index in range(1,len(list)): value = list[index] i = index - 1 while i >= 0: if value < list[i]:    # since the right number is less than the left list[i+1] = list[i] # swap position - right number gets left number list[i] = value    # left nymber gets right number i = i - 1 else: break

a = [7,1,3,5,9,2,3] print (a) sort_insertion(a) print (a)

b = [56,11,32,511,19,21,23] print (b) sort_insertion(b) print (b)

def sort_insertion2(list): for index in range(1,len(list)): value = list[index] i = index - 1 # i is the previous value while i >= 0 and value < list[i]: # since the right number is less than the left list[i+1] = list[i] # swap position - right number gets left number list[i] = value    # left nymber gets right number i = i - 1
 * 1) Do a bit improvement

sort_insertion2(b) print (b)