函数编程总结python python函数编程训练题( 二 )


# datetime.datetime.today() 获取当前本地日期的date对象
# datetime.utcnow() 返回当前UTC时间的datetime对象
# time.strptime()把时间字符串解析为元组
# time.time()返回当前时间的时间戳
# time.sleep()暂停指定秒数
# time.strftime() 返回指定格式的日期字符串
# time.mktime() 接收时间元组并返回时间戳
# os.getcwd() 获取当前工作目录
# os.listdir() 获取指定路径下的目录和文件列表
# os.makedirs() 递归创建目录
# os.rename() 重命名目录或文件
# os.path.exists() 判断路径是否存在
# upper() 全部转换为大写字母
# lower()全部转换为小写字母
# sys.stdout.write() 标准输出打印
# sys.stdout.flush()刷新输出
# shutil.copy() 复制单个文件到另一文件或目录
# write() 写入文件内容
# winsound.Beep() 打开电脑扬声器
# zfill() 在字符串前面填充0
三、循环语句
# break终止当前循环
# continue 终止本循环进入下一次循环
# with open() as file 以with语句打开文件(数据保存)
四、转义字符
\行尾续行符
\' 单引号
\'' 双引号
\a 响铃
\e 转义
\n 换行
\t 横向制表符
\f 换页
\xyy 十六进制yy代表的字符
\\反斜杠符号
\b 退格
\000 空
\v 纵向制表符
\r 回车
\0yy 八进制yy代表的字符
\other 其他的字符以普通格式输出
Python的函数参数总结import math
a = abs
print(a(-1))
n1 = 255
print(str(hex(n1)))
def my_abs(x):
# 增加了参数的检查
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x = 0:
return x
else:
return -x
print(my_abs(-3))
def nop():
pass
if n1 = 255:
pass
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
x, y = move(100, 100, 60, math.pi / 6)
print(x, y)
tup = move(100, 100, 60, math.pi / 6)
print(tup)
print(isinstance(tup, tuple))
def quadratic(a, b, c):
k = b * b - 4 * a * c
# print(k)
# print(math.sqrt(k))
if k0:
print('This is no result!')
return None
elif k == 0:
x1 = -(b / 2 * a)
x2 = x1
return x1, x2
else:
x1 = (-b + math.sqrt(k)) / (2 * a)
x2 = (-b - math.sqrt(k)) / (2 * a)
return x1, x2
print(quadratic(2, 3, 1))
def power(x, n=2):
s = 1
while n0:
n = n - 1
s = s * x
return s
print(power(2))
print(power(2, 3))
def enroll(name, gender, age=8, city='BeiJing'):
print('name:', name)
print('gender:', gender)
print('age:', age)
print('city:', city)
enroll('elder', 'F')
enroll('android', 'B', 9)
enroll('pythone', '6', city='AnShan')
def add_end(L=[]):
L.append('end')
return L
print(add_end())
print(add_end())
print(add_end())
def add_end_none(L=None):
if L is None:
L = []
L.append('END')
return L
print(add_end_none())
print(add_end_none())
print(add_end_none())
def calc(*nums):
sum = 0
for n in nums:
sum = sum + n * n
return sum
print(calc(1, 2, 3))
print(calc())
l = [1, 2, 3, 4]
print(calc(*l))
def foo(x, y):
print('x is %s' % x)
print('y is %s' % y)
foo(1, 2)
foo(y=1, x=2)
def person(name, age, **kv):
print('name:', name, 'age:', age, 'other:', kv)
person('Elder', '8')
person('Android', '9', city='BeiJing', Edu='人民大学')
extra = {'city': 'Beijing', 'job': 'Engineer'}

推荐阅读