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()