###Ⅰ.Some basic Python Language Syntax
1.List
>>> a=[1,2,3,4]
>>> print a
[1, 2, 3, 4]
>>> a[1]="Don have a lot of xx”
>>> a[2]="Don is very ugly"
>>> a[3]="i wanna kill my mom"
>>> a.append("new element")
>>> print a
[1, 'Don have a lot of xx’, 'Don is very ugly', 'i wanna kill my mom', 'new element']
>>>
####2. for Loop
Introduction of Python built-in function is called range( ).It comes with Python And it’s really a way to generate lists that have integrate that increment.
For example:
(1)
>>> range(6)
[0, 1, 2, 3, 4, 5]
>>>
(2)
>>> range(3,31,31)
[3]
>>>
#####It generates a list from 0 through 5(it starts with 0,but not including 6 actually.
#####ie. if you want to take the sum of 0 through 9.Each time you go thru sum the loop,add to sum,define the sum to be the previous sum + what i is in this part of the loop.
>>> sum=0
>>> for i in range(10):
... sum=sum+i
... print (sum)
...
0
1
3
6
10
15
21
28
36
45
####3.While Loop
######This while loop calculates the sum of 0 thru 9 (including nine) and place it in the variable sum.
>>> sum=0
>>> i=0
>>> while i<10:
... sum=sum+i
... i=i+1
... print(sum)
...
0
1
3
6
10
15
21
28
36
45
####4.Iterative Function
#####The Definition Of Factorial: It is gonna do it iteratively .It start from 1 up to whatever number this is , and keep multiplying the product by each successively larger number.
For example:
(1)
number=eval(input("Enter a non-negative integer to take the factorial number of :"))
#If you are in the version3 of python: number=eval(input("Enter a non-negative integer to take the factorial number of :"))
product =1
for i in range(number):
product=product*(i+1)
print(product)
>>>362880
(2)By the time you had already defined this function right here.you can now call the factorial straight from the interpreter.If you had another program you could call it multiple ways.just like: factorial(arbitrary number)
# return the factorial of the argument "number"
def factorial (number):
product=number
for i in range(number):
product=product*(i+1)
return product
user_input=eval(input("Enter a non-negative integer to take the factorial number of :"))
factorial_of_user_input = factorial(user_input)
print (factorial_of_user_input)if
result:
input:3
>>>6
####5.Recursion function
#####Some way recursion is really deep and complicated and in some levels, it’s way simpler than anything.
# rerturn the factorial of the argument "number"
def factorial (number):
if number<=1: #base case
return 1
else:
return number*factorial(number-1)
user_input=eval(input("Enter a non-negative integer to take the factorial number of :"))
factorial_of_user_input = factorial(user_input)
print (factorial_of_user_input)
####6. Arrange the List
def insertion_sort(list):
for index in range (len(list)): # this starts index
value=list[index]
i=index-1
while i>=0:
if value < list[i]:
list[i+1]=list[i] # shift number in slot right i to slot i+1
list[i]=value # shift value left into slot i
i=i-1 # perform the loop over again
else:
break;# we are done and there is nothing to shift to let-side
# a=[ ] -> insertion_sort(a) -> print a
###Ⅱ. Fibonacci Number
####Fibonacci who is one of the most famous mathematicians of all time, and he was a mathematicians in medieval Italy ,and he is most famous for the Fibonacci number.
####The definition of Fibonacci number: The first two are defined as 0 and 1,And every number after that is the sum of the previous two,
F
n
=
F
n
−
1
+
F
n
−
1
F_{n}=F_{n-1}+F_{n-1}
Fn=Fn−1+Fn−1
or the Mathematical Formation:KaTeX parse error: Expected '}', got '\matrix' at position 65: …right]}{\left({\̲m̲a̲t̲r̲i̲x̲{{n-k-1}\cr{k}\…
#####Like this: 0,1,1,2,3,5,8,13,21,34
#####As you add more and more terms through the Fibonacci sequence and you take the last two terms that you generated. For instance, 21:34 And that is essentially Golden Radio etc. As a matter of fact,the Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in Fibonacci tilling.
#####This one uses squares of sizes 1,1,2,3,5,8,13,21, and 34.
#####There are three ways to define Fibonacci number as a function in Python,as you can see,
(1)
def Fibonacci(n):
terms=[0,1] # that was by the definition of Fibonacci number
i=2 #Constructing the second term
while i<=n: #All the way up to and including the nth term
terms.append(terms[i-1]+terms[i-2])
i=i+1 # if it never increment "i" then this loop will keep going on forever and ever
return terms[n]
(2)
def Fibonacci(n):
if n==0:
return 0
elif n==1:# else if
return 1
else : # otherwise
return (Fibonacci(n-1)+Fibonacci(n-2))
(3)
def Fibonacci(n): #Simplify version
if n<2:
return n
else:
return (Fibonacci(n-1)+Fibonacci(n-2))