设为首页 加入收藏

TOP

Python基础第三天(一)
2017-09-30 14:30:38 】 浏览:9051
Tags:Python 基础 第三

三元运算

三元运算又叫三目运算,是对简单的条件语句的缩写,例如if判断

# 标准if判断语法

if 1 == 1:
    name = "yes"
else:
    name = "no"

# 如果 1==1 成立,name = "yes", 否则 name = "no"

# 三元运算简写语法

name = "yes" if 1 == 1 else "no"

# 如果条件成立,将yes赋值给name变量,否则将no赋值给name变量
View Code

集合

set集合,是一个无序且不重复的元素集合

# 创建集合

s1 = {11,22}                    
s2 = set()                         
s3 = set([11,22,33,4])

# 操作集合

s = set()
s.add(123) # 把要传入的元素做为一个整个添加到集合中
s.update(123) # 把要传入的元素拆分,做为个体传入到集合中      
s.clear() # 删除集合中所有元素

s1 = {11,22,33}
s2 = {22,33,44}
s3 = s1.difference(s2)  # s1中存在,s2中不存在
s3 = s2.difference(s1)  # s2中存在,s2中不存在
s3 = s1.symmetric_difference(s2) #s1 s2 中不同时存在的
s1.difference_update(s2) # 更新s1中存在,s2中不存在
s1.symmetric_difference_update(s2)  # 更新s1 s2 中不同时存在的
s1 = {11,22,33}
s1.discard(1111) # 如果1111是集合s1中的元素删除,不存在不报错
s1.remove(11111) #  如果1111是集合s1中的元素删除,不存在报错
ret = s1.pop() # 删除集合中任意一个对象,返回它
s3 = s1.union(s2) # 返回一个新集合,集合元素是s1 s2的并集
s3 = s1.intersection(s2)# 返回一个新集合,集合元素是s1 s2的交集
s1.intersection_update(s2) # s1中成员是共同属于s1和s2

# li = [11,22,33] # list __init__
# li()            # list __call__
# li[0]           # list __getitem__
# li[0] = 123     # list __setitem__
# def li[1]       # list __delitem__
old_dict = {
    "#1": 8,
    "#2": 4,
    "#4": 2,
}

new_dict = {
    "#1": 4,
    "#2": 4,
    "#3": 2,
}
# old_kyes = old_dict.keys()
# old_set = set(old_kyes)
new_set = set(new_dict.keys())
old_set = set(old_dict.keys())

remove_set = old_set.difference(new_set)
add_set = new_set.difference(old_set)
update_set = old_set.intersection(new_set)

import re
re.match()   
View Code

函数

描述

在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处,函数式编程最重要的是增强代码的重用性和可读性

def sendmail():
    try:
        import smtplib
        from email.mime.text import MIMEText
        from email.utils import formataddr
        msg = MIMEText('邮件内容', 'plain', 'utf-8')
        msg['From'] = formataddr(["武沛齐",'wptawy@126.com'])
        msg['To'] = formataddr(["走人",'424662508@qq.com'])
        msg['Subject'] = "主题"

        server = smtplib.SMTP("smtp.126.com", 25)
        server.login("wptawy@126.com", "WW.3945.5")
        server.sendmail('wptawy@126.com', ['3628905@qq.com',], msg.as_string())
        server.quit()
    except:
        # 发送失败
        return "失败"
    else:
        # 发送成功
        return "cc"
ret = sendmail()


print(ret)
if ret == "cc":
    print('发送成功')
else:
    print("发送失败")
发邮件函数

定义和使用

def 函数名(参数):
       
    ...
    函数体
    ...
    返回值
格式

函数的定义主要有如下要点:

  • def:表示函数的关键字
  • 函数名:函数的名称,日后根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...
  • 参数:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据。

返回值

# 在函数中,一旦执行return,函数执行过程立即终止

def f1():
     print(123)
     return "111"
     print(456)

 r = f1()
 print(r)

 def f2():
     print(123)

 r = f2()
 print(r)
返回值

参数

函数中有不通的参数

  • 普通参数(严格按照顺序,将实际参数赋值给形式参数)
def sendmail(xxoo, content):
    # xxoo = alex
    try:
        import smtplib
        from email.mime.text import MIMEText
        from email.utils import formataddr
        msg = MIMEText(content, 'plain', 'utf-8')
        msg['From'] = formataddr(["武沛齐",'wptawy@126.com'])
        msg['To'] = formataddr(["走人",'424662508@qq.com'])
        m
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python【7】-数据分析准备 下一篇Python -- set

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目