发布于2019-08-06 11:38 阅读(713) 评论(0) 点赞(3) 收藏(0)
###Ⅰ.Some basic Python Language Syntax
>>> 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
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,
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))
作者:好难受
链接:https://www.pythonheidong.com/blog/article/8456/cd0031bc0cc282975619/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!