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

Python语言程序设计基础(5)—— 函数和代码复用的更多相关文章

  1. Python基础篇(三)_函数及代码复用

    Python基础篇_函数及代码复用 函数的定义.使用: 函数的定义:通过保留字def实现. 定义形式:def <函数名>(<参数列表>): <函数体> return ...

  2. python语言程序设计基础 习题 天天向上的力量(math.pow)

    实例1: 一年365天,,以第一天的能力值为基数1.0,当好好学习时能力值相比前一天提高1%,没有学习时能力值相比前天要降低1%,每天努力和每天放任,一年下来的能力值相差多少? 解析: 如果每天都好好 ...

  3. python语言程序设计基础(嵩天)第二章课后习题

    p56: *2.1 实例1的修改.改造实例代码1.1,采用eval(input(<提示内容>))替换现有输入部分,并使输出的温度值为整数. 源代码: TempStr=input(" ...

  4. Python语言程序设计基础(4)—— 程序的控制结构

    PM2.5 pm = eval(input()) if pm>=75: print("空气存在污染") else : print("空气没有污染") pr ...

  5. python语言程序设计基础(嵩天)第四章课后习题部分答案

    p121: *题4.1:猜数字游戏.在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了!”:小于预设的数,显示“遗憾,太小了!”,如此循环,直至猜中该数 ...

  6. python语言程序设计基础(第二版)第五章答案随笔

    1.实现isOdd()函数,参数为整数,如果整数是奇数,返回True,否则返回False def isOdd(num):    if num % 2 == 0:        return True  ...

  7. Python语言程序设计基础(7)—— 文件和数据格式化

    返回字符串 file = input() #返回字符串 fo = open(file,"r").read(6) print(fo) 返回列表形式 file = input() fo ...

  8. Python语言程序设计基础(6)—— 组合数据类型

    tuple 元组(创建后不能修改) tuple = "cat","dog","tiger","human" print( ...

  9. Python语言程序设计基础(3)—— 基本数据类型

    天天向上 dayup,dayfactor = 1.0,0.01 for i in range(365): if i % 7 in [6,0]: dayup = dayup*(1-dayfactor) ...

随机推荐

  1. 安装并配置hive

    教程:http://www.bubuko.com/infodetail-2523113.html 安装好mysql,然后进入mysql: 创建hive用户: 给hive用户赋权: 创建hive数据库, ...

  2. shiro app

    写在前面 我们知道,shiro框架在Java Web应用中使用时,本质上是通过filter方式集成的. 也就是说,它是遵循过滤器链规则的:filter的执行顺序与在web.xml中定义的顺序一致,如下 ...

  3. GraphQL 暂停

    别人的文章 http://blog.csdn.net/imwebteam/article/details/53888708 Java 文档都打不开,已经在GitHub上提了 in README.md ...

  4. Java基础26-对象初始化过程

    /* 1.因为new Test1()用到了Test1类,所以会把它从硬盘上加载进入内存 2.如果有static静态代码块就会随着类的加载而执行,还有静态成员和普通方法也会随着类的加载而被加载 3.在堆 ...

  5. (转)Saltstack系列

    Saltstack系列1:安装配置 Saltstack系列2:Saltstack远程执行命令 Saltstack系列3:Saltstack常用模块及API Saltstack系列4:Saltstack ...

  6. egret打包android + android微信登录--小结

    公司用egret做了款游戏,需要打android包,做安卓端的微信登录,于是乎开始了第一安卓上的打包,正的是一脸懵 首先遇到的问题有如下: 1. egret打安卓包时经常运行不起来, 主要是gradl ...

  7. C++程序设计基础(6)内存分配

    1.知识点 三步走:申请,释放,指针置空. 1.1malloc.free函数 在C语言中内存malloc函数申请动态空间,以下展示其基本用法: int *p = NULL; p = ();//申请 f ...

  8. java输出九九乘法口诀表

    使用双重for循环输出九九乘法口诀表 public static void main(String[] args){ formula();} /** * for 循环实现9*9乘法口诀表 * &quo ...

  9. [LeetCode]21. Merge Two Sorted Lists合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  10. c++ sizeof对象大小整理

    1. sizeof 是运算符,而不是函数. 2. 当sizeof 的对象是表达式时,求的大小是表达式返回值的类型大小,但并不计算表达式的值,比如: ; ; cout << sizeof(c ...