python_day6

一:迭代&递归

1.迭代求阶乘

def factorial(n):
     result=n
     for i in range(1,n):
         result*=i
     return result
number=int(input('请输入一个正整数:'))
result1=factorial(number)
print(number,'的阶乘为:',result1)
View Code

调试过程中报错:IndentationError: unindent does not match any outer indentation level

是因为缩进有错误,python对缩进要求很严格。

2.递归求阶乘

def factorial(n):
    if n==1:
        return 1
    else:
        return n*factorial(n-1)
number=int(input('请输入一个正整数:'))
result=factorial(number)
print('%d 的阶乘为: %d' %(number,result))
View Code

注意这里的输出格式:

print('%d 的阶乘为: %d'   %(number,result)) 

3.迭代求斐波拉契数列

def factorial(n):
    t1=1
    t2=1
    for i in range(3,n+1):
        result=t1+t2
        t1=t2
        t2=result
    return result
number=int(input('请输入月份:'))
result=factorial(number)
print('第%d 个月兔子的总对数为: %d' %(number,result))
View Code

4.递归求斐波拉契数列

def fab(n):
    if n<1:
        print('输入有误!')
        return -1
    if n==1 or n==2:
        return 1
    else:
        return fab(n-1)+fab(n-2)
result=fab(20)
if result!=-1:
    print('20个月后共有%d对小兔子'%result)
View Code

5.递归_汉诺塔

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')
View Code

二:字典

1.引言

>>> brand=['李宁','耐克','鱼C工作室']
>>> slogan=['一切皆有可能','just do it ','让编程改变世界']
>>> print('鱼C工作室的口号为:',slogan[brand.index('鱼C工作室')])
鱼C工作室的口号为: 让编程改变世界
View Code

2.创建和访问字典

元组用小括号,列表用中括号,字典用大括号。字典里包括key和value。

#创建字典的第一种方式
>>> dict1={'李宁':'一切皆有可能','耐克':'just do it ','鱼C工作室':'让编程改变世界'} >>> print('鱼C工作室',dict1['鱼C工作室']) 鱼C工作室 让编程改变世界
>>> dict3=dict((('F',70),('i',105)))
>>> dict3
{'F': 70, 'i': 105}

用那么多大括号是因为dic只有一个映射关系的参数。

#创建字典的第二种方法,这里前面不用加引号,会自动加引号
>>> dict1=dict(小甲鱼='让编程改变世界',耐克='just do it')
>>> dict1
{'小甲鱼': '让编程改变世界', '耐克': 'just do it'}

字典的使用:

>>> dict1['耐克']='lallala'
>>> dict1
{'小甲鱼': '让编程改变世界', '耐克': 'lallala'}
>>> dict1['爱迪生']='天才就是...'
>>> dict1
{'小甲鱼': '让编程改变世界', '耐克': 'lallala', '爱迪生': '天才就是...'}

 3.字典的一些方法

fromkeys()
字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值

>>> 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')}
View Code

keys()

for eachkey in dict1.keys():
    print(eachkey)
View Code

values()

for eachvalue in dict1.values():
    print(eachvalue)

items()

for eachitem in dict1.items():
    print(eachitem)
View Code

如果访问字典中不存在的项,越界会报错,这时可以用get()方法

>>> print(dict1.get(31,'没有'))
赞
>>> print(dict1.get(32,'没有'))
没有
View Code

查看key是否在字典中,返回true和false。

>>> 31 in dict1
True
>>> 32 in dict1
False
View Code

clear()方法

>>> dict1.clear()
>>> dict1
{}

copy()方法,浅拷贝

>>> 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)
49639520
>>> id(b)
49130208
>>> id(c)
49639520
>>> 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'}
View Code

pop()  ,popitem(),setdefault()

>>> a.pop(2)
'two'
>>> a
{1: 'one', 3: 'three', 4: 'four'}
>>> a.popitem()
(4, 'four')
>>> a
{1: 'one', 3: 'three'}
>>> a.setdefault('wwq')
>>> a
{1: 'one', 3: 'three', 'wwq': None}
>>> a.setdefault(5,'five')
'five'
>>> a
{1: 'one', 3: 'three', 'wwq': None, 5: 'five'}
View Code

update(),用一个字典去更新另一个字典

>>> b={'wwq':'仙女'}
>>> a.update(b)
>>> a
{1: 'one', 3: 'three', 'wwq': '仙女', 5: 'five'}
View Code

 

转载于:https://www.cnblogs.com/wwq1204/p/10694817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值