Python笔记——函数的用法

1、判断函数是否可用:callable(f)

>>> callable(f)
False


2、改变函数参数(注意是不可变的数据结构:如 字符串、数字、元组),并不会改变外部变量值,这时函数参数只在函数内有效

def y(i):
    i=1      #调用该函数时,创建一个新的命名空间,重新创建新的一个局部变量

    return i  # return 可以没有

>>> i=10
>>> y(i)
1
>>> i
10
>>>

3、改变函数参数(注意是可变的数据结构,如:列表),可以改变外部变量的值,例如:

def y(i):
    i[0]='this is test'      #与不可变量相比,由于是可变量,指向与外部变量相同的存储空间.  此时在函数的命名空间中并没有创建变量i

>>> j=[0,1,2,3,4]        #如果不想改变可变参数的值可以,创建一个副本: i0=i[:]
>>> y(j)
>>> j
['this is test', 1, 2, 3, 4]


4、函数局部变量与全局变量的用法:调用函数时,将创建新的一个命名空间,特点:一、函数内创建的新的变量只在这个这个命名空间有效;二、可以直接调用全局变量,但如果函数内有创建相同的变量,将屏蔽全局变量,此时要想调用的话用globals()['j']这个函数

def y(i):
    i[0]='this is test'
    j="this is the local varity"
    print j
    print globals()['j']

>>> i=[0]
>>> j="global varity"
>>> y(i)
this is the local varity
global varity


5、递归:先写出递归的数学递推表达式,然后写成python语法程序:

def search(seq,num,lower=0,upper=None):
    if upper==None:
        upper=len(seq)-1
    if seq[lower]==num:
        print 'the num locate %d index in seq' % lower
    elif seq[upper]==num:
        print 'the num locate %d index in seq' % upper
    elif seq[lower]==seq[upper]:
        print 'the num is not in seq'
    elif seq[(lower+upper)/2]<num:
        return search(seq,num,(lower+upper)/2+1,upper)
    elif seq[(lower+upper)/2]>num:
        return search(seq,num,lower,(lower+upper)/2-1)
    elif seq[(lower+upper)/2]==num:
        print 'the num locate %d index in seq' % ((lower+upper)/2)

>>> x
[0, 1, 3, 5, 7, 9, 10, 10, 10, 11, 15]
>>> search(x,9)
the num locate 5 index in seq
>>> search(x,10)
the num locate 6 index in seq
>>> search(x,11)
the num locate 9 index in seq
>>> search(x,15)
the num locate 10 index in seq


6、变量命名空间实例

以下是一个示例,演示了如何引用不同作用域和命名空间,以及 global 和 nonlocal 如何影响变量绑定: 
def scope_test():     def do_local(): 
        spam = "local spam"     def do_nonlocal():         nonlocal spam 
        spam = "nonlocal spam"     def do_global():         global spam 
        spam = "global spam"  
    spam = "test spam"     do_local() 
    print("After local assignment:", spam)     do_nonlocal() 
    print("After nonlocal assignment:", spam)     do_global() 
    print("After global assignment:", spam)  
scope_test() 
print("In global scope:", spam) 
以上示例代码的输出为: 
After local assignment: test spam 
After nonlocal assignment: nonlocal spam 

After global assignment: nonlocal spam 

In global scope: global spam 
注意: local 赋值语句是无法改变 scope_test 的 spam 绑定。 nonlocal 赋值语句改变了 scope_test 的spam 绑定,并且 global 赋值语句从模块级改变了 spam 绑定。 你也可以看到在 global 赋值语句之前对 spam 是没有预先绑定的。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python,sort_index()函数是pandas库DataFrame数据对象的一个方法,用于根据行标签和列名称对数据进行排序。函数的语法如下:sort_index(axis=0, level=None, ascending=True, inplace=False, kind="quicksort", na_position="last", sort_remaining=True, ignore_index=False)。\[1\] 另外,pandas的DataFrame数据对象还有另一种排序方式,即根据指定某一列的值进行排序,这可以通过sort_values()函数实现。\[2\] 需要注意的是,sort_index()函数是根据行标签和列名称进行排序,而sort_values()函数是根据某一列的值进行排序。 除了pandas库的排序函数,还可以使用其他排序算法,比如冒泡排序和选择排序。冒泡排序是一种简单的排序算法,它通过比较相邻元素的大小并交换位置来实现排序。\[3\]选择排序是另一种常见的排序算法,它通过每次选择最小的元素并将其放置在已排序部分的末尾来实现排序。 希望以上信息对您有所帮助! #### 引用[.reference_title] - *1* *2* [python函数sort_index 和 sort_values排序](https://blog.csdn.net/lss1990lss/article/details/119559207)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [蓝桥杯python组————笔记整理](https://blog.csdn.net/qq_46639904/article/details/124023255)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值