Python学习-函数的简单使用

本文详细介绍了三种编程方法论:面向对象、面向过程及函数式编程,并通过具体示例展示了如何在Python中实现这些概念。文章还探讨了函数定义、过程调用、参数传递以及返回值处理等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 三种编程的方法论
# 面向对象  :类  class
# 面向过程  :过程  def
# 函数式编程  :函数  def  最早的,但是现在又流行了

import time



#函数式编程


def func1():  #():内可以定义形参
    '''Testing codes'''   #这个是文件的描述,非常重要一定要写
    #之后加入程序的逻辑代码
    print('testing code')  #这里简单一点
    return 0  #定义返回值,这里可以return很多东西

#定义过程


def func2():
    '''For the processing'''
    print('testing processing code')
    #这个是没有返回值的


x = func1()


y = func2()


print(x)   #这里就有返回值了

print(y)  #这个是没有返回值的,就是None,就是

#可以看出这个实际就是说python自动将过程的返回定义为None。
if y == None:
    print('The return code is None!')


def open_file(date_now1, time_now1):
    '''Used to open files'''
    with open('testing.txt', 'a') as file:
        file.write('Today is {date1}, and now is {time1}\n'.format(date1=date_now1, time1= time_now1))
        #这里注意,字符串用’%n'% xx 这个形式,只能使用int参数,不能用字符串
        #字符串要用{}和format
    return 0


def ipt_info():
    "Import time and date information!"

    date_now = "%Y-%m-%d %X"
    time_now = time.strftime(date_now)   #这个功能是用date_now 的参数形式返回电脑里面的时间
    print(date_now, time_now)
    open_file(date_now, time_now)
    return date_now, time_now, '可以有很多的输出,还可以是列表和字典,虽然实际是一个'


print(ipt_info())


def calu1(a, b ,c):
    """Calculate numbers and make a jugement."""
    d = a + b + c
    if d <20:
        return d
    else:
        return "It's too large"


print(calu1(5, 6, 79)) #这个结果就证明了return可以在if的判断里面使用


def calu1(a, b ,c= 6):   #这样就是有一个默认的值,这样,你如果不输入一个参数,这个参数就是这样的
    """Calculate numbers and make a jugement."""
    d = a + b + c
    if d <20:
        return d
    else:
        return "It's too large"


print(calu1(5, 6)) #这个结果就证明了有默认的就可以少一个输入


def calu1(a, b ,*c):   #多输入的参数会变成列表
    """Calculate numbers and make a jugement."""
    d = a + b + c[0]  #只使用列表第一个
    if d <20:
        return d
    else:
        return "It's too large"


print(calu1(5, 6, 1, 9)) #这个结果就证明了有默认的就可以少一个输入


def calu1(a, b ,**c):   #输入的参数会变成字典
    """Calculate numbers and make a jugement."""
    d = a + b   #只使用列表第一个
    if d <20:
        return d
    else:
        return c


print(calu1(b=5, a=21, dic1= "aa"))  #字典输入的结果形式一定要是这样的
#另外,直接使用a,b直接赋值就可以不用顺序,但是如果出现了一个定义参数,剩下的尽量全部都要直接赋值,不然由于位置参数和顺序参数混杂容易出现问题
#关键参数不能在位置参数之前

 

转载于:https://www.cnblogs.com/Ian-learning/p/7852844.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值