码迷,mamicode.com
首页 > 编程语言 > 详细

python 基础 三元运算等

时间:2016-05-07 19:34:08      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

基本数据类型:

set:是一个不重复且无序的集合

 

class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
     
    Build an unordered collection of unique elements.
    """
    def add(self, *args, **kwargs): # real signature unknown
        """
        Add an element to a set,添加元素
         
        This has no effect if the element is already present.
        """
        pass
 
    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from this set. 清楚内容"""
        pass
 
    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a set. 浅拷贝  """
        pass
 
    def difference(self, *args, **kwargs): # real signature unknown
        """
        Return the difference of two or more sets as a new set. A中存在,B中不存在
         
        (i.e. all elements that are in this set but not the others.)
        """
        pass
 
    def difference_update(self, *args, **kwargs): # real signature unknown
        """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素"""
        pass
 
    def discard(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set if it is a member.
         
        If the element is not a member, do nothing. 移除指定元素,不存在不保错
        """
        pass
 
    def intersection(self, *args, **kwargs): # real signature unknown
        """
        Return the intersection of two sets as a new set. 交集
         
        (i.e. all elements that are in both sets.)
        """
        pass
 
    def intersection_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """
        pass
 
    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
        pass
 
    def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set.  是否是子序列"""
        pass
 
    def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. 是否是父序列"""
        pass
 
    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return an arbitrary set element.
        Raises KeyError if the set is empty. 移除元素
        """
        pass
 
    def remove(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set; it must be a member.
         
        If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
        """
        pass
 
    def symmetric_difference(self, *args, **kwargs): # real signature unknown
        """
        Return the symmetric difference of two sets as a new set.  对称交集
         
        (i.e. all elements that are in exactly one of the sets.)
        """
        pass
 
    def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the symmetric difference of itself and another. 对称交集,并更新到a中 """
        pass
 
    def union(self, *args, **kwargs): # real signature unknown
        """
        Return the union of sets as a new set.  并集
         
        (i.e. all elements that are in either set.)
        """
        pass
 
    def update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the union of itself and others. 更新 """
        pass

 

  

 

#书写格式

re= 值1 if 条件 else 值2

#如果条件成立 那么‘’值1‘’赋值给re变量,否则 将‘值2’赋值给re变量

  深浅拷贝

一、数字和字符串

对于‘数字’和‘字符串’而言,赋值、浅拷贝和深拷贝无意义,因为他永远指向同一个内存地址。

import copy
#数字和字符串
n1 = 123
#n1 = ‘i am sunlieqi age 21‘
print(id(n1))
########赋值########
n2=n1
print(id(n2))
####浅拷贝####
n2 = copy.copy(n1)
print(id(n2))



####深拷贝###
n3 = copy.deepcopy(n1)
print(id(n3))

  技术分享

二、其他基本数据类型

对于字典、元组、列表而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。

1、赋值

赋值 只是创建一个变量,该变量指向原来内存地址,如:

n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n2 = n1

 

  技术分享

2、浅拷贝

 

浅拷贝,在内存中只额外创建第一层数据

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n3 = copy.copy(n1)

  技术分享

深拷贝

深拷贝 ,在内存中将所有的数据重新重建一分(除了最后一层,即:python内部对字符串和数字的优化)

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n4 = copy.deepcopy(n1)

  技术分享

函数

一、背景


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

while True:
    if cpu利用率 > 90%:
        #发送邮件提醒
        连接邮箱服务器
        发送邮件
        关闭连接
    
    if 硬盘使用空间 > 90%:
        #发送邮件提醒
        连接邮箱服务器
        发送邮件
        关闭连接
    
    if 内存占用 > 80%:
        #发送邮件提醒
        连接邮箱服务器
        发送邮件
        关闭连接

看上述代码,if条件语句下的内容可以被提取出来循环使用,如下

def 发送邮件(内容)
    #发送邮件提醒
    连接邮箱服务器
    发送邮件
    关闭连接
    
while True:
    
    if cpu利用率 > 90%:
        发送邮件(‘CPU报警‘)
    
    if 硬盘使用空间 > 90%:
        发送邮件(‘硬盘报警‘)
    
    if 内存占用 > 80%:

  上面两种方式,第二次比第一次的重用性和可读性要好,其实这就是函数式变成和面向过程编程的区别:

  • 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
  • 面向对象:对函数进行分类和封装,让开发“更快更好更强...”

函数式编程最重要的是增强代码的重用性和可读性,提高了代码的重复利用率

二、定义和使用

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

  函数的定义要点:

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

1、返回值

函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。

以上要点中,比较重要有参数和返回值:

def 发送短信():
       
    发送短信的代码...
   
    if 发送成功:
        return True
    else:
        return False
   
   
while True:
       
    # 每次执行发送短信函数,都会将返回值自动赋值给result
    # 之后,可以根据result来写日志,或重发等操作
   
    result = 发送短信()
    if result == False:
        记录日志,短信发送失败...

  2、参数

 

为什么要有参数?

 

def CPU报警邮件()
    #发送邮件提醒
    连接邮箱服务器
    发送邮件
    关闭连接

def 硬盘报警邮件()
    #发送邮件提醒
    连接邮箱服务器
    发送邮件
    关闭连接

def 内存报警邮件()
    #发送邮件提醒
    连接邮箱服务器
    发送邮件
    关闭连接
 
while True:
 
    if cpu利用率 > 90%:
        CPU报警邮件()
 
    if 硬盘使用空间 > 90%:
        硬盘报警邮件()
 
    if 内存占用 > 80%:
        内存报警邮件()

 

  有参数实现

def 发送邮件(邮件内容)

    #发送邮件提醒
    连接邮箱服务器
    发送邮件
    关闭连接

 
while True:
 
    if cpu利用率 > 90%:
        发送邮件("CPU报警了。")
 
    if 硬盘使用空间 > 90%:
        发送邮件("硬盘报警了。")
 
    if 内存占用 > 80%:
        发送邮件("内存报警了。")

  函数的有三种不同的参数:

  • 普通参数
# ######### 定义函数 ######### 

# name 叫做函数func的形式参数,简称:形参
def func(name):
    print name

# ######### 执行函数 ######### 
#  ‘wupeiqi‘ 叫做函数func的实际参数,简称:实参
func(‘wupeiqi‘)
  •   默认参数
def func(name, age = 18):
    
    print "%s:%s" %(name,age)

# 指定参数
func(‘wupeiqi‘, 19)
# 使用默认参数
func(‘alex‘)

注:默认参数需要放在参数列表最后
  •   动态参数
def func(*args):

    print args


# 执行方式一
func(11,33,4,4454,5)

# 执行方式二
li = [11,2,2,3,3,4,54]
func(*li)
----------------
ef func(**kwargs):

    print args


# 执行方式一
func(name=‘wupeiqi‘,age=18)

# 执行方式二
li = {‘name‘:‘wupeiqi‘, age:18, ‘gender‘:‘male‘}
func(**li)
---------------------------------------------
def func(*args, **kwargs):

    print args
    print kwargs

  发邮件实例:

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", "邮箱密码")
server.sendmail(‘wptawy@126.com‘, [‘424662508@qq.com‘,], msg.as_string())
server.quit()

  内置函数

技术分享

 

open函数 该函数用于文件处理

 

python 基础 三元运算等

标签:

原文地址:http://www.cnblogs.com/sunlieqiblog/p/5468854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!