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()
Python语言程序设计基础(5)—— 函数和代码复用的更多相关文章
- Python基础篇(三)_函数及代码复用
Python基础篇_函数及代码复用 函数的定义.使用: 函数的定义:通过保留字def实现. 定义形式:def <函数名>(<参数列表>): <函数体> return ...
- python语言程序设计基础 习题 天天向上的力量(math.pow)
实例1: 一年365天,,以第一天的能力值为基数1.0,当好好学习时能力值相比前一天提高1%,没有学习时能力值相比前天要降低1%,每天努力和每天放任,一年下来的能力值相差多少? 解析: 如果每天都好好 ...
- python语言程序设计基础(嵩天)第二章课后习题
p56: *2.1 实例1的修改.改造实例代码1.1,采用eval(input(<提示内容>))替换现有输入部分,并使输出的温度值为整数. 源代码: TempStr=input(" ...
- Python语言程序设计基础(4)—— 程序的控制结构
PM2.5 pm = eval(input()) if pm>=75: print("空气存在污染") else : print("空气没有污染") pr ...
- python语言程序设计基础(嵩天)第四章课后习题部分答案
p121: *题4.1:猜数字游戏.在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了!”:小于预设的数,显示“遗憾,太小了!”,如此循环,直至猜中该数 ...
- python语言程序设计基础(第二版)第五章答案随笔
1.实现isOdd()函数,参数为整数,如果整数是奇数,返回True,否则返回False def isOdd(num): if num % 2 == 0: return True ...
- Python语言程序设计基础(7)—— 文件和数据格式化
返回字符串 file = input() #返回字符串 fo = open(file,"r").read(6) print(fo) 返回列表形式 file = input() fo ...
- Python语言程序设计基础(6)—— 组合数据类型
tuple 元组(创建后不能修改) tuple = "cat","dog","tiger","human" print( ...
- Python语言程序设计基础(3)—— 基本数据类型
天天向上 dayup,dayfactor = 1.0,0.01 for i in range(365): if i % 7 in [6,0]: dayup = dayup*(1-dayfactor) ...
随机推荐
- 【LDAP】ldap目录服务的命名模型
ldap的命名模型 命名模型规定了在目录中如何组织和表示条目 1. 目录信息树(DIT) 目录信息树有点类似于DNS的结构.每一个条目都有自己的父条目(因为主条目的父条目是top,所以这句话是成立 ...
- innosetup卸载软件后,删除定时任务schedule task
代码如下: //innosetup自带的方法,当卸载软件时,根据卸载的状态改变时而触发 procedure CurUninstallStepChanged(CurUninstallStep: TUni ...
- pat1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- (六-1)Firefox插件安装
1.安装火狐插件 ①旧版本firefox 火狐浏览器右上角-->附件-->获取更多附件-->搜索-->Firebug 安装 Firebug 扩展:https://addons. ...
- 修改Linux时区的2种办法
由于Azure 上所有的服务时间都采用了 UTC 时间.UTC 时间比中国时间晚 8 个小时,该如何按照自己的需要来进行修改呢,下面提供2种办法以供参考: 1.修改 /etc/localtime 文件 ...
- js使用占位符替换字符串
js使用占位符替换字符串是一个ES6中的模版字符串语法. 在``中使用 ${} var a = 5; var b = 10; console.log(`Fifteen is ${a + b} and ...
- 3..net可以做什么
.net可以做什么呢? (1)桌面应用程序 Winform(.net开发的桌面应用程序叫winform应用程序) (2)internet应用程序 ASP.net(.net开发的internet应用程 ...
- 了解WaitForSingleObject中WAIT_ABANDONED 返回值
1.互斥量内核对象 互斥量内核对象用来确保一个线程独占对一个资源的访问.互斥量对象包含一个使用计数.线程ID以及递归计数.互斥量与关键段的行为完全相同.但是互斥量是内核对象,而关键段是用户模式下的同步 ...
- random模块/string模块
一.random模块 random模块可以很容易生成随机数和随机字符串. random.randint(1, 100) # 1-100之间取一个随机数 random.randrange(1, 100) ...
- python调用其他文件的类和函数
在同一个文件夹下 调用函数 source.py文件: def func(): pass new.py文件: import source # 或者 from source import func 调用类 ...