Python语言程序设计基础(5)—— 函数和代码复用

lambda

sum = lambda x,y : x + y
print(sum(3,3),type(sum))

 

默认参数

def prints(str,times = 2) :
    print(str*times)
prints("Tree",3)

 

变长参数

def sums(*a):
    res = 0
    for i in a:
        res += i
    return res

print(sums(1,2,3,4,5))

 

return

def swap(a,b):
    return b,a
print(swap("Tree",3))

 

作用范围

n = 1
def func(a,b):
    n = b
    return a*b
print(func("Dream",2),n)
ls = []

def funs(a,b):
    ls.append(b)
funs(2,3)
print(ls)
lss = []
def funss(a):
    lss = []
    lss.append(a)
funss(2)
print(lss)

 

datatime.datatime

from datetime import datetime
today = datetime.now()
print(today)
print(today.year,today.month,today.day,today.hour,today.minute,today.second)

someday = datetime(2018,10,18)
print(someday)

print(today.strftime("%Y %m %d %H:%M:%S"))
print("今天是{0:%Y}年{0:%m}月{0:%d}日".format(today))

 

 七段数码管

import turtle
import datetime

def drawLine(flag):
    turtle.pendown() if flag else turtle.penup()
    turtle.forward(40)
    turtle.right(90)

def drawDight(x) :
    if x in [2,3,4,5,6,8,9]:
        drawLine(True)
    else :
        drawLine(False)

    if x in [0,1,3,4,5,6,7,8,9]:
        drawLine(True)
    else :
        drawLine(False)

    if x in [0,2,3,5,6,7,8,9]:
        drawLine(True)
    else :
        drawLine(False)

    if x in [0,2,6,8]:
        drawLine(True)
    else :
        drawLine(False)

    turtle.right(-90)

    if x in [0,4,5,6,8,9]:
        drawLine(True)
    else :
        drawLine(False)

    if x in [0,2,3,5,6,7,8,9]:
        drawLine(True)
    else :
        drawLine(False)

    if x in [0,1,2,3,4,7,8,9]:
        drawLine(True)
    else :
        drawLine(False)

    turtle.right(180)
    turtle.penup()
    turtle.forward(20)

def drawDate(date):
    print(date)
    for i in date:
        #print(int(i))
        drawDight(int(i))

def main():
    turtle.setup(0.5,0.5)
    turtle.penup()
    turtle.forward(-300)
    turtle.pensize(3)
    drawDate(datetime.datetime.now().strftime("%Y%m%d"))
    turtle.hideturtle()

main()

 

Pycharm调试技巧

F8 下一步,不进入函数

F7 下一步,进入函数,包括库函数

 

反转字符串

str = input()
lens = len(str)
ls = list(str)
for i in range(lens//2):
    #print(str[i])
    ls[i] ,ls[lens-i-1] = ls[lens-i-1], ls[i]
print("".join(ls))
def reverse(s):
    if len(s) == 1:
        return s
    return reverse(s[1:]) + s[0]

print(reverse("TreeDream"))

 

科赫曲线

import turtle
import os

def koch(size,n):
    if n == 0:
        turtle.forward(size)
    else:
        for angle in [0,60,-120,60]:
            turtle.left(angle)
            koch(size/3,n-1)

def main():
    x = int(input())
    turtle.setup(0.5,0.5)
    turtle.speed(0)
    turtle.penup()
    turtle.goto(-300,-50)
    turtle.pendown()
    turtle.pensize(2)
    koch(600,x)
    turtle.hideturtle()

main()

 

转载于:https://www.cnblogs.com/TreeDream/p/9815373.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值