局部变量

   全局变量

def test():
# 声明使用全局变量x
global x
x = 100
y = 300 # 局部变量:作用域和生存周期仅在从定义开始到函数结束 x = 200 # 全局变量:作用域从定义开始到进程结束
test()
print(x)

    Δ3

'''
1990.1.1 星期1
2018.11.1
''' def isleap(y):
'''
function:判断是否为闰年
y:判断的年份
return:True / False
'''
return True if (y % 4 == 0 and y % 100 != 0 or y % 400 == 0) else False def dayofmonth(m, y):
'''
function:m月有多少天
'''
if m in (1,3,5,7,8,10,12):
days = 31
elif m in (4,6,9,11):
days = 30
else:
if isleap(y):
days = 29
else:
days = 28
return days def main():
year, month = eval(input('请输入年份和月份(year,month):')) sumdays = 0
# 1990~year
for y in range(1990, year):
sumdays += (365+isleap(y)) # year.1.1 ~ year.month.1
for m in range(1, month):
sumdays += dayofmonth(m, year) sumdays += 1 weekday = sumdays % 7 # 计算month月有多少天
monthdays = dayofmonth(month, year) # 打印日历
m = '一二三四五六七八九十'
if month == 11:
monthstr = '十一'
elif month == 12:
monthstr = '十二'
else:
monthstr = m[month-1]
print('\33[34m{:>7}月 {}'.format(monthstr, year))
print('\33[0m', end='') # 关闭属性
print('\33[47m日 一 二 三 四 五 六')
print('\33[0m', end='') # 关闭属性
print(' '*weekday, end='')
for d in range(1, monthdays+1):
print('{:>2}'.format(d), end=' ' if (weekday+d)%7 else '\n')
print() # 调用函数
main()

  练习题:

import random

'''
练习1:定义一个函数common_divisori(),求得两个整型数的最大公约数
'''
def common_divisori(m, n):
if m < n:
m, n = n, m
# 辗转相除
while True:
mod = m % n
if mod == 0:
return n
m = n
n = mod '''
练习2:定义一个函数maxoflist(),不使用max内置函数,实现求得整型列表的最大值
'''
def maxoflist(l):
m = l[0]
for i in range(1, len(l)):
if l[i] > m:
m = l[i]
return m '''
练习3:定义一个函数sortoflist(),不使用sorted()和sort()的基础上为一个整型列表排序(从小到大)
'''
# 冒泡排序
def sortoflist(l):
n = len(l)
for i in range(n-1):
for j in range(n-i-1):
if l[j] > l[j+1]:
l[j], l[j+1] = l[j+1], l[j] return l def main():
'''
m,n = eval(input("请输入两个整型数:"))
res = common_divisori(m, n)
print("{}和{}的最大公约数是:{}".format(m, n, res))
'''
l = [random.randint(1, 100) for i in range(10)]
'''
print(l)
res = maxoflist(l)
print("最大的元素是%d" % res)
'''
l = sortoflist(l)
print(l) main()

  参数类型:
    位置参数
    默认参数
    可变参数
    关键字参数

# 位置参数
def max2num(x, y):
print(x, y)
return x if x > y else y # 默认参数
def power(x, y=2):
s = 1
while y:
s *= x
y -= 1
return s # 默认参数的默认值最好不是可变类型
'''
def add_end(l=[]):
l.append('python')
return l
''' def add_end(l=None):
if l == None:
l = []
l.append('python')
return l # 可变参数
def sumall(*numbers):
# print(type(numbers))
s = 0 for i in numbers:
s += i return s # 位置参数在可变参数后
def test(*args, n=100):
print(n, args) # 关键字参数
def stuinfo(name, age, city='北京', **kw):
print(name, age, city, kw) # 命名关键字参数
def stuinfo2(name, age, city='北京', *, height, gender):
print(name, age, city, height, gender) # 多种参数类型混合使用 位置参数在第一位,关键字参数一定在最后
def test2(locate, *args, name, age, default=100, **kw):
print(locate, args, default, name, age, kw) res = max2num(10, 20)
print(res) res = max2num(y=100, x=200)
print(res) print(power(10, 4))
print(power(10)) ls = [1,2,3]
add_end(ls)
print(ls) print(add_end())
print(add_end())
print(add_end()) res = sumall(1,2,3,4,5)
print(res) test(1,2,4,5) stuinfo('张', 20, height=178, gender='M', school='河北农大')
stuinfo2('张', 20, height=178, gender='M')
stuinfo2('张', 20, gender='M', height=175) test2(1, 'hello', 'world', name='uplooking', age=14, python='guido', height=180)

  递归调用:
    在函数内调用函数本身
    1. 找到终止条件
    2. 找到递归条件

def sumn(n):
'''
n的前n项和
'''
if n == 0:
return 0
return n + sumn(n-1)
print(sumn(10))

  练习:

'''
Fibnacci数列的第n项
'''
def fibnacci(n):
if n <= 0:
return False
if n == 1:
return 0
elif n == 2:
return 1
return fibnacci(n-1) + fibnacci(n-2)

for i in range(1, 21):
print(fibnacci(i), end = ' ')
print()

Python_day5的更多相关文章

  1. python_day5学习笔记

    一.正则表达式 字符: \d 匹配任何十进制数:相当于类[0-9] \D 匹配任何非数字字符:相当于类[^0-9] \s  匹配任何空白字符:相当于类[  \t\n\r\f\v] \S  匹配任何非空 ...

随机推荐

  1. 4、Zookeeper简单介绍

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...

  2. 涂抹mysql笔记-数据备份和恢复

    <>物理备份和逻辑备份<>联机备份和脱机备份<>本地备份和远程备份<>完整备份和增量备份<>完整恢复和增量恢复<>复制表相关文件 ...

  3. ubuntu18.04 安装mysql server

    mysql 5.7支持的最高版本是Ubuntu17 ,即使安装成功后,也会出现各种妖蛾子,本人就被这种问题困扰了好一会.在Ubuntu 18.04下安装mysql,建议安装8.0以上版本! 1. 配置 ...

  4. C语言 练习题

    subString #include <iostream> int subString(char* sSeek, char* sKey) { char* p = sSeek; while( ...

  5. Android控件使用FragmentTabHost,切换Fragment;

    大部分APP的主界面都很类似,要么底部导航的,要么就是侧滑菜单,还有底部导航+侧滑菜单的:底部导航实现大概有几种方式: TabHost+Fragment RadioGroup+Fragment Fra ...

  6. exchang2010OWA主界面添加修改密码选项

    原文链接:http://www.mamicode.com/info-detail-1444660.html exchange邮箱用户可以登录OWA修改密码,当AD用户密码过期或者重置密码勾选了“用户下 ...

  7. leetcode337

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  8. Controller层aop

    利用@Around通知修改Controller的返回值 自定义一个注解@OperationBtn 在切入点Controller上加上自定义注解 接下来就是重点了,AspectJ写切面类,对该Contr ...

  9. kalman filter卡尔曼滤波器- 数学推导和原理理解-----网上讲的比较好的kalman filter和整理、将预测值和观测值融和

    = 参考/转自: 1 ---https://blog.csdn.net/u010720661/article/details/63253509 2----http://www.bzarg.com/p/ ...

  10. ssh 报错Host key verification failed 或Ubuntu connect to serve 失败

    ssh 报错Host key verification failed  或Ubuntu connect to serve 失败  通常是因为没有装ssh sudo apt-get install  o ...