JavaSE Filecoin idea离线安装 物联网项目 search directory ios4 Vanilla JS linux内存管理 css获取最后一个元素 mysql错误代码1064 hadoop组件 python调用方法 python支持中文 java中scanner java正则表达式详解 如何查看java版本 java获取文件大小 javastringbuilder 幽城幻剑录五内 python入门经典 苹果滚动截屏 3389扫描器 ps出血 华为动态照片 视频添加水印 quickchm 免费ftp空间 dnf胜利的证明有什么用 冰冠堡垒单刷路线 mac修改器 谷歌浏览器升级 js绑定事件的方法 python去掉空格 python游戏编程 php队列 二代妖精下载 audition消除人声 视频转码工具 word怎么
当前位置: 首页 > 学习教程  > python

经常遗忘的python基础

2021/2/8 12:55:32 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

1、异常 # -*- coding: utf-8 -*- # Time : 2021/2/7 10:11 # Author : XCJ # FileName: 异常te.py # Software: PyCharm # Blog :https://blog.csdn.net/w137093940try:print(1/0) except Exception as e:print("检测到异常")# -*- coding: utf-…

1、异常

# -*- coding: utf-8 -*-
# @Time    : 2021/2/7 10:11
# @Author  : XCJ
# @FileName: 异常te.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/w137093940

try:
    print(1/0)
except Exception as e:
    print("检测到异常")
# -*- coding: utf-8 -*-
# @Time    : 2021/2/7 10:11
# @Author  : XCJ
# @FileName: 异常tef.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/w137093940

try:
    print(1/0)
    #print(1/1)
except Exception as e:
    print("检测到异常")
finally:
    print("不管有没有异常都执行")
# -*- coding: utf-8 -*-
# @Time    : 2021/2/7 10:11
# @Author  : XCJ
# @FileName: 异常tee.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/w137093940

try:
    print(1/1)
    #print(1/0)
except Exception as e:
    print("检测到异常")
else:
    print("没有检测到异常")

# -*- coding: utf-8 -*-
# @Time    : 2021/2/7 10:11
# @Author  : XCJ
# @FileName: 异常teef.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/w137093940

try:
    #print(1/1)
    print(1/0)
except Exception as e:
    print("检测到异常")
else:
    print("没有检测到异常")
finally:
    print("有没有异常都执行")

2、函数参数传参

# -*- coding: utf-8 -*-
# @Time    : 2021/2/7 10:17
# @Author  : XCJ
# @FileName: 函数的参数形式.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/w137093940

# 无参数
def func1():
    print("func1:","hello world!")

# 固定参数
def func2(msg):
    print("func2:",msg)

# 默认参数
def func3(msg='hello world!'):
    print("func3:",msg)

func1()
func2("hello world!")
func3()

# 未知个数参数
def func4(*args):
    msg = ""
    for arg in args:
        msg += arg
    print("func4:",msg)


func4('hello','world','!')
# 拆包形式
func4(*['hello','world','!'])
func4(*('hello','world','!'))
mylist = ['hello','world','!']
func4(*mylist)

# 键值对形式参数
def func5(**kwargs):
    for key in kwargs:
        print(key, kwargs[key])
    print()

func5(jhon=15,mike=18,lili=17)
# 拆包形式
func5(**dict(jhon=15,mike=18,lili=17))
mydict=dict(jhon=15,mike=18,lili=17)
func5(**mydict)

3、三个功能函数

# -*- coding: utf-8 -*-
# @Time    : 2021/2/7 10:30
# @Author  : XCJ
# @FileName: 三个功能函数.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/w137093940

# 遍历函数map
# 例1:给每个分数都加10
scores = [50,54,52,54,56,59,51,57,57]
new_scores = list(map(lambda x:x+10, scores))
def func(x):
    return x+10
new_scores1 = list(map(func, scores))
print(new_scores)
print(new_scores1)

# 筛选函数filter
# 例2:筛选分数大于55的
scores = [50,54,52,54,56,59,51,57,57]
new_scores2 = list(filter(lambda x:x>55, scores))
print(new_scores2)

# 累计函数reduce
# 例3:计算分数的总和
from functools import reduce
scores = [50,54,52,54,56,59,51,57,57]
scores_sum = reduce(lambda a,b:a+b,scores)
print(sum(scores))
print(scores_sum)



本文链接: http://www.dtmao.cc/news_show_2000365.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?