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

python基础

时间:2016-05-09 08:27:40      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

一、python三元运算

二、基本数据类型之集合

三、赋值和深浅拷贝

四、自定义函数的创建、调用和函数参数

五、练习

一、python三元运算

  result = 值1 if 条件 else 值2  :如果条件成立则result等于值1 ,否则等于值2

二、set 集合

  1. set 是一个无序且不重复的元素集合,支持并集、交集、差集、对称差集等数学运算,set不记录元素位置,因此不支持索引,分片等操作。
  2. 声名方法:    

    SET_NAME = set()

    SET_NAME = {1,2,3,4.....}  (不能用此方法创建空集合,系统会认为是创建一个空字典。)方法:

  3.方法:

技术分享
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.
        返回两个或多个集合中的不同值并将这些不同值生成一个新的集合。
        (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

    def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iand__(self, *args, **kwargs): # real signature unknown
        """ Return self&=value. """
        pass

    def __init__(self, seq=()): # known special case of set.__init__
        """
        set() -> new empty set object
        set(iterable) -> new set object
        
        Build an unordered collection of unique elements.
        # (copied from class doc)
        """
        pass

    def __ior__(self, *args, **kwargs): # real signature unknown
        """ Return self|=value. """
        pass

    def __isub__(self, *args, **kwargs): # real signature unknown
        """ Return self-=value. """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __ixor__(self, *args, **kwargs): # real signature unknown
        """ Return self^=value. """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs): # real signature unknown
        """ Return self|value. """
        pass

    def __rand__(self, *args, **kwargs): # real signature unknown
        """ Return value&self. """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __ror__(self, *args, **kwargs): # real signature unknown
        """ Return value|self. """
        pass

    def __rsub__(self, *args, **kwargs): # real signature unknown
        """ Return value-self. """
        pass

    def __rxor__(self, *args, **kwargs): # real signature unknown
        """ Return value^self. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __sub__(self, *args, **kwargs): # real signature unknown
        """ Return self-value. """
        pass

    def __xor__(self, *args, **kwargs): # real signature unknown
        """ Return self^value. """
        pass

    __hash__ = None
View Code

三、赋值和深浅拷贝

赋值:a = 3 

   b = a 

  a,b指向同一片内存地址,可以使用id()来查看,赋值操作不会开辟新的内存空间,它只是复制了新对象的引用。在这只是增加了b这个变量名的内存。

如果想修改一个对象并且让原始对象不受影响,就需要进行复制(拷贝)操作。

数字的字符串还能被拷贝

如果元组内只包含数字或字符串则不能深拷贝

1.浅拷贝(shallow copy):

  创建方式:

    import copy

    copy.copy()

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

import copy

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

n3 = copy.copy(n1)

技术分享

 

2.深拷贝:

创建方式:

 import copy

   copy.deepcopy()

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

import copy

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

n4 = copy.deepcopy(n1)

技术分享

 

 

 

 四、自定义函数的创建、调用和函数参数

 函数是将一些代码组织到一起可以实现特定功能的代码块,将重复率较高的代码封装到函数中,需要时调用函数就可以实现功能。

1.自定义函数的创建

def 函数名(形参):
    函数体
    ....
    返回值
  • def:创建函数的关键字
  • 函数名:函数的名称,以后可根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑运算
  • 形参:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据,如果不指定默认返回None

 

python基础

标签:

原文地址:http://www.cnblogs.com/yangxiaolan/p/5472507.html

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