Day6:Python学习笔记

师从:小甲鱼

递归 斐波纳契数列(Fibonacci Sequence)

斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会从 1963 年起出版了以《斐波纳契数列季刊》为名的一份数学杂志,用于专门刊载这方面的研究成果。

用迭代方式实现

def fab(n):                     #定义fab函
    n1=1
    n2=1
    n3=1

    if n<1:
        print("输入错误!")
        return -1

    while (n-2)>0:
        n3=n2+n1
        n1=n2
        n2=n3
        n-=1

    return n3

n=int(input("输入月数:"))          #输入时要强制转换为int型
result=fab(n)
if result !=-1:
    print("总共有%d对小兔子诞生!" %result)

结果:

输入月数:20
总共有6765对小兔子诞生!

用递归实现

def fab(n):                     #定义fab函
    
    if n<1:
        print("输入错误!")
        return -1

    if n==1 or n==2:
        return 1
    else:
       return fab(n-2)+fab(n-1)
  
n=int(input("输入月数:"))          #输入时要强制转换为int型
result=fab(n)
if result !=-1:
    print("总共有%d对小兔子诞生!" %result)

汉诺塔(其实还没搞懂)

def hanoi(n,x,y,z):
    if n==1:
        print(x,"-->",z)
    else:
        hanoi(n-1,x,z,y)   # 将前n-1个盘子从x移动到y上
        print(x,"-->",z)  #将最底下的盘子从x移动到z上
        hanoi(n-1,y,x,z)  #将y上的n-1个盘子移动到z上

n=int(input("输入汉诺塔层数:"))
hanoi(n,'X','Y','Z')

结果:

```python
输入汉诺塔层数:3
X --> Z
X --> Y
Z --> Y
X --> Z
Y --> X
Y --> Z
X --> Z

字典

dict.from(s,[v])
>>> dict1={}
>>> dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1,2,3),'Number')
{1: 'Number', 2: 'Number', 3: 'Number'}
>>> dict1.fromkeys((1,2,3),('one','two','three'))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> dict1=dict1.fromkeys(range(32),'赞')
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞', 10: '赞', 11: '赞', 12: '赞', 13: '赞', 14: '赞', 15: '赞', 16: '赞', 17: '赞', 18: '赞', 19: '赞', 20: '赞', 21: '赞', 22: '赞', 23: '赞', 24: '赞', 25: '赞', 26: '赞', 27: '赞', 28: '赞', 29: '赞', 30: '赞', 31: '赞'}
>>> 
>>> for eachkey in dict1.keys():
	print(eachkey)

	
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>>> for eachValue in dict1.values():
	print(eachvalue)

	
Traceback (most recent call last):
  File "<pyshell#18>", line 2, in <module>
    print(eachvalue)
NameError: name 'eachvalue' is not defined
>>> for eachValue in dict1.values():
	print(eachValue)

	
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
>>> for eachItem in dict1.items():
	print(eachItem)

	
(0, '赞')
(1, '赞')
(2, '赞')
(3, '赞')
(4, '赞')
(5, '赞')
(6, '赞')
(7, '赞')
(8, '赞')
(9, '赞')
(10, '赞')
(11, '赞')
(12, '赞')
(13, '赞')
(14, '赞')
(15, '赞')
(16, '赞')
(17, '赞')
(18, '赞')
(19, '赞')
(20, '赞')
(21, '赞')
(22, '赞')
(23, '赞')
(24, '赞')
(25, '赞')
(26, '赞')
(27, '赞')
(28, '赞')
(29, '赞')
(30, '赞')
(31, '赞')
>>> 
>>> dict1.clear()  #清空字典
>>> dict1
{}
>>> 

copy()

```python
>> a={1:'one',2:'two',3:'three'}
>>> b=a.copy()
>>> c=a
>>> c
{1: 'one', 2: 'two', 3: 'three'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> id(a)
49841400
>>> id (b)
52626120
>>> id(c)
49841400
>>> c[4]='four'
>>> c
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> 

pop() 弹出给定键的值

popitem()弹出随机键的值

>>> a.pop(2)
'two'
>>> a
{1: 'one', 3: 'three', 4: 'four'}
>>> a.popitem()
(4, 'four')
>>> a.popitem()
(3, 'three')
>>> a
{1: 'one'}
>>> 
>>> b={'小白':'狗'}
>>> a.update(b)
>>> a
{1: 'one', '小白': '狗'}
{1: 'one', '小白': '狗'}
>>> a[2]
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    a[2]
KeyError: 2
>>> a
{1: 'one', '小白': '狗'}
>>> a(2)
Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    a(2)
TypeError: 'dict' object is not callable
>>> a[1]
'one'
>>> a[0]  #要调用键值
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    a[0]
KeyError: 0
>>> a['小白']  #可以这样调用
'狗'
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值