Python函数01

Posted xingweikun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python函数01相关的知识,希望对你有一定的参考价值。

函数的定义

def happy():
    print("Happy birthday to you!")
def happyB(name):
    happy()
    happy()
    print("Happy birthday,dear {}!".format(name))
    happy()
happyB("Mike")
print()
happyB("Lily")
Happy birthday to you!
Happy birthday to you!
Happy birthday,dear Mike!
Happy birthday to you!

Happy birthday to you!
Happy birthday to you!
Happy birthday,dear Lily!
Happy birthday to you!

lambda函数

f=lambda x,y:x+y
print(type(f))
print(f(10,12))
<class 'function'>
22

函数的参数传递

可选参数和可变数量参数

def dup(str,times=2):
    print(str*times)
dup("knock~")
knock~knock~
def dup(str,times=2):
    print(str*times)
dup("knock~",4)
knock~knock~knock~knock~
def vfunc(a,*b):
    print(type(b))
    for n in b:
        a+=n
    return a
vfunc(1,2,3,4,5)
<class 'tuple'>
15

参数的位置和名称传递

func(x1,y1,z1,x2,y2,z2):
    return
result=func(1,2,3,4,5,6)

相当于

result=func(x2=4,y2=5,z2=6,x1=1,y1=2,z1=3)

函数的返回值

def func(a,b):
    return a*b
s=func("knock~",2)
print(s)
knock~knock~
def func(a,b):
    return b,a
s=func("knock~",2)
print(s,type(s))
(2, 'knock~') <class 'tuple'>

datetime 库

获得当前日期和时间对象

from datetime import datetime
datetime.now()
datetime.datetime(2021, 5, 15, 17, 34, 13, 282483)

获得当前日期和时间对应的UTC

datetime.utcnow()
datetime.datetime(2021, 5, 15, 9, 40, 48, 840603)

采用ISO 8601标准显示时间

today=datetime.now()
today.isoformat()
'2021-05-15T17:44:55.106858'

返回今天是星期几

today.isoweekday()
6

strftime()方法格式化

today.strftime("%Y-%m-%d %H:%M:%S")
'2021-05-15 17:44:55'
now=datetime.now()
print("今天是{0:%Y}年{0:%m}月{0:%d}日{0:%H}时{0:%M}分".format(now))
今天是202105151800

实例 七段数码管绘制

import turtle
import datetime
def drawGap():
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.right(90)
def drawDigit(d):
    drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,6,8] else drawLine(False)
    turtle.left(90)
    drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)
def drawDate(date):
    turtle.pencolor("red")
    for i in date:
        if i == '-':
            turtle.write('年',font=("Arial",18,"normal"))
            turtle.pencolor("green")
            turtle.fd(40)
        elif i == '=':
            turtle.write('月',font=("Arial",18,"normal"))
            turtle.pencolor("blue")
            turtle.fd(40)
        elif i == '+':
            turtle.write('日',font=("Arial",18,"normal"))
        else:
            drawDigit(eval(i))
def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
    turtle.hideturtle()
main()

在这里插入图片描述

以上是关于Python函数01的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记:python3,代码片段(2017)

13 个非常有用的 Python 代码片段

你如何在 python 中处理 graphql 查询和片段?

python使用上下文对代码片段进行计时,非装饰器

python 元组有用的函数,方法和片段。

Python代码阅读(第26篇):将列表映射成字典