一、作用域

只要变量在内存中就能被调用!但是(函数的栈有点区别)

对于变量的作用域,执行声明并在内存中存在,如果变量在内存中存在就可以被调用。

1 if 1==1:
2     name = 'tianshuai'
3 print  name

所以下面的说法是不对的:

外层变量,可以被内层变量使用
内层变量,无法被外层变量使用

二、三元运算

1 result = 值1 if 条件 else 值2

例子:

1 name = raw_input("please input your name: ")
2 if name = "tianshuai":
3     print "you are so shuai!!!"
4 else:
5     print "you are ok"

上面的例子可以用三元运算一句解决:

1 name = raw_input("please input your name: ")
2 shuai = "shuaige" if name == "tianshuai" else "is ok"
3 print shuai

注:循环可以包含循环,列表可以包含列表,元组当然也可以包含元组,字典可以包含字典!思想不要太局限!放开想!

三、python是一门什么语言

编程语言主要从以下几个角度进行分类:

编译型和解释型

静态语言和动态语言

强类型定义语言和弱类型语言

 

编译型和解释型:

编译型,其实他和汇编语言是一样的:也是有一个负责翻译的程序来对我们的源代码进行转换,生成相对应的可执行代码。

这个说的更专业一点,就是编辑(Complie),而负责编译的程序自然就称谓编译器(Compiler)。如果我们写的程序代码都包含在一个源

文 件中,那么通常编译之后就会生成一个可执行文件,我们就直接运行了,而对于一个比较复杂的项目,为了方便管理,我们通常把代码分散在各个原文件中,作为不 通的模块来组织。这是编译各个文件时就会生成目标文件(Objec file)而不是之前所说的可执行文件。一般一个源文件的编译都会对应一个目标文件。这些目标文件里的内容基本上已经是可执行代码了,但是由于只是整个项 目的一部分,所以我们还不能直接运行。待所有的源文件编译都大功告成,我们就可以最后把这些半成品的目标文件“打包”成一个可执行文件了,这个工作由另一 个程序负责完成,由于过程好像是把包含可执行代码的目标文件连接装配起来,所以这个操作又称为连接(Link),而负责连接的程序就叫。。。。。连接程序 (Linker)。连接程序除了连接文件之外,可能还有各种资源,图标文件啊、声音等。连接完成后,一般就可以得到我们降妖的可执行文件了。

 

上面我们大概介绍了编译型语言的特点,现在在看看解释性。从字面上来看“编译”和“解释”都有“翻译”的意思,他们的区别则在于翻译的时机不一样。

打个比方:如果你打算预读一本外文书,而你不知道这么外语,那么你可以找一名翻译,给他足够的时间让他从头到尾把整本书翻译好,

然后把书的母语版交给你阅读。这个过程就编译,或者你也立刻让这名翻译辅助你阅读,让他一句一句的给你翻译,如果你想往回看某个章节他也的重新给你翻译。

 

两 种方式:前者就相当于我们刚才说的编译型:一次把所有的代码转换成机器语言,然后写成可执行文件。

而后者就相当于我们要的节诶实行:在程序运行的前一刻, 还只有源程序而没有可执行程序;而程每执行到资源程序的某一条执行,则会有一个称之为解释程序的外壳程序,将源代码转换成二进制代码以供执行,总而言之就 是不断的解释、执行、解释、执行。。。所以解释型语言是离不开解释程序的。

 

由于程序总是以源代码的形式出现,因此只要有相应的解释器,一直几乎不成问题。编译型程序虽然源代码也可以执行,但前提必须针对不通的系统分别进行编译,对于复杂的工程来说,的确是一件不小的时间小号,而且何忧可能一些细节的地方还有修改源代码。

但是解释性程序省却了编译的步骤,修改调试也非常方便,编辑完毕之后即可运行,不必想编译型语言修改了小小的改动要等很长的Compiling...Linking...

不过凡是有利有弊,由于解释性程序试讲编译的过程放在执行过程中,这就决定了解释性程序注定要比编译型慢上一大截,就想几百倍的速度差距也不足为奇

 

但 既然编译型与解释性各有优缺点又相互对立,所以一批新星的语言都有把两者折中起来的趋势,例如Java语言虽然比较接近解释性语言的特性,但在执行之前预 先进行一次预编译,生成的代码是介于机器码和Java源代码之间的中介码,运行的时候而又JVM(Java的虚拟机平台,可视为解释器)解释执行。他即保 留了源代码的高抽象、可抑制的特点,又已完成了对源代码的大部分预编译工作,所以执行起来比“纯解释性”程序要快的多。宗旨,随着设计技术与硬件的不断发 展,编译型与解释性两种方式的界限正在不断的变模糊

 

 

静态语言和动态语言:

通常我们所说的动态语言、静态语言是指动态类型语言和静态类型语言。

1、 动态类型语言:动态类型语言是指在运行期间才去做数据类型检查的语言,也就是说,在动态类型的语言编程时,永远也不用给任何变量指定数据类型,该语言会在 第一次赋值给变量是,在内部将数据类型记录下来。Python和Ruby就是典型类型的动态类型语言,其他的各种脚本语言如VBScript也多少属于动 态类型语言。

2、静态类型语言:静态类型语言与动态类型语言刚好相反,他的数据类型是在编译期间检查的,也就是说在写程序的时候要声明所有变量的数据类型,C/C++是静态类型的典型代表,其他的静态类型语言还有C#、JAVA等

对于动态语言与静态语言的区分,套用一句比较流行的话是:Static typing when possible,dynamic typing when needed

 

强类型定义语言和弱类型语言

1、强类型定义语言:强制数据类型定义的语言。也就是说,一旦一个变量被指定了某个数据类型,如果不经过强制转换,那么他就永远是这个数据类型了。举个例子:如果你定义了一个整形变量a,那么程序根本不可能讲a当作字符串类型处理。强类型定义语言是类型安全的语言。

2、弱类型定义语言:数据类型可以被忽略的语言。他与强类型定义语言相反,一个变量可以赋予不同数据类型。

 

强类型定义语言在速度上可能略逊色与弱类型定义语言,但是他是强类型定义语言带来的严谨性能够有效便面许多错误,另外,“这么语言是不是动态语言”与“这么语言是否类型安全”之间是完全没有联系的。

例如:Python是动态语言,也是强类型定义语言(类型安全的语言);VBScript是动态语言是弱类型定义语言(类型不安全的语言);

JAVA是静态语言,是强类型定义语言(类型安全的语言)

Python基础

Python之路【第二篇】:Python基础

一、整数

如: 18、73、84

每一个整数都具备如下功能:

class int(object):
    """
    int(x=0) -> int or long
    int(x, base=10) -> int or long
    
    Convert a number or string to an integer, or return 0 if no arguments
    are given.  If x is floating point, the conversion truncates towards zero.
    If x is outside the integer range, the function returns a long instead.
    
    If x is not a number or if base is given, then x must be a string or
    Unicode object representing an integer literal in the given base.  The
    literal can be preceded by '+' or '-' and be surrounded by whitespace.
    The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
    interpret the base from the string as an integer literal.
    >>> int('0b100', base=0)
    """
    def bit_length(self): 
        """ 返回表示该数字的时占用的最少位数 """
        """
        int.bit_length() -> int
        
        Number of bits necessary to represent self in binary.
        >>> bin(37)
        '0b100101'
        >>> (37).bit_length()
        """
        return 0

    def conjugate(self, *args, **kwargs): # real signature unknown
        """ 返回该复数的共轭复数 """
        """ Returns self, the complex conjugate of any int. """
        pass

    def __abs__(self):
        """ 返回绝对值 """
        """ x.__abs__() <==> abs(x) """
        pass

    def __add__(self, y):
        """ x.__add__(y) <==> x+y """
        pass

    def __and__(self, y):
        """ x.__and__(y) <==> x&y """
        pass

    def __cmp__(self, y): 
        """ 比较两个数大小 """
        """ x.__cmp__(y) <==> cmp(x,y) """
        pass

    def __coerce__(self, y):
        """ 强制生成一个元组 """ 
        """ x.__coerce__(y) <==> coerce(x, y) """
        pass

    def __divmod__(self, y): 
        """ 相除,得到商和余数组成的元组 """ 
        """ x.__divmod__(y) <==> divmod(x, y) """
        pass

    def __div__(self, y): 
        """ x.__div__(y) <==> x/y """
        pass

    def __float__(self): 
        """ 转换为浮点类型 """ 
        """ x.__float__() <==> float(x) """
        pass

    def __floordiv__(self, y): 
        """ x.__floordiv__(y) <==> x//y """
        pass

    def __format__(self, *args, **kwargs): # real signature unknown
        pass

    def __getattribute__(self, name): 
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        """ 内部调用 __new__方法或创建对象时传入参数使用 """ 
        pass

    def __hash__(self): 
        """如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。"""
        """ x.__hash__() <==> hash(x) """
        pass

    def __hex__(self): 
        """ 返回当前数的 十六进制 表示 """ 
        """ x.__hex__() <==> hex(x) """
        pass

    def __index__(self): 
        """ 用于切片,数字无意义 """
        """ x[y:z] <==> x[y.__index__():z.__index__()] """
        pass

    def __init__(self, x, base=10): # known special case of int.__init__
        """ 构造方法,执行 x = 123 或 x = int(10) 时,自动调用,暂时忽略 """ 
        """
        int(x=0) -> int or long
        int(x, base=10) -> int or long
        
        Convert a number or string to an integer, or return 0 if no arguments
        are given.  If x is floating point, the conversion truncates towards zero.
        If x is outside the integer range, the function returns a long instead.
        
        If x is not a number or if base is given, then x must be a string or
        Unicode object representing an integer literal in the given base.  The
        literal can be preceded by '+' or '-' and be surrounded by whitespace.
        The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
        interpret the base from the string as an integer literal.
        >>> int('0b100', base=0)
        # (copied from class doc)
        """
        pass

    def __int__(self): 
        """ 转换为整数 """ 
        """ x.__int__() <==> int(x) """
        pass

    def __invert__(self): 
        """ x.__invert__() <==> ~x """
        pass

    def __long__(self): 
        """ 转换为长整数 """ 
        """ x.__long__() <==> long(x) """
        pass

    def __lshift__(self, y): 
        """ x.__lshift__(y) <==> x<<y """
        pass

    def __mod__(self, y): 
        """ x.__mod__(y) <==> x%y """
        pass

    def __mul__(self, y): 
        """ x.__mul__(y) <==> x*y """
        pass

    def __neg__(self): 
        """ x.__neg__() <==> -x """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): 
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __nonzero__(self): 
        """ x.__nonzero__() <==> x != 0 """
        pass

    def __oct__(self): 
        """ 返回改值的 八进制 表示 """ 
        """ x.__oct__() <==> oct(x) """
        pass

    def __or__(self, y): 
        """ x.__or__(y) <==> x|y """
        pass

    def __pos__(self): 
        """ x.__pos__() <==> +x """
        pass

    def __pow__(self, y, z=None): 
        """ 幂,次方 """ 
        """ x.__pow__(y[, z]) <==> pow(x, y[, z]) """
        pass

    def __radd__(self, y): 
        """ x.__radd__(y) <==> y+x """
        pass

    def __rand__(self, y): 
        """ x.__rand__(y) <==> y&x """
        pass

    def __rdivmod__(self, y): 
        """ x.__rdivmod__(y) <==> divmod(y, x) """
        pass

    def __rdiv__(self, y): 
        """ x.__rdiv__(y) <==> y/x """
        pass

    def __repr__(self): 
        """转化为解释器可读取的形式 """
        """ x.__repr__() <==> repr(x) """
        pass

    def __str__(self): 
        """转换为人阅读的形式,如果没有适于人阅读的解释形式的话,则返回解释器课阅读的形式"""
        """ x.__str__() <==> str(x) """
        pass

    def __rfloordiv__(self, y): 
        """ x.__rfloordiv__(y) <==> y//x """
        pass

    def __rlshift__(self, y): 
        """ x.__rlshift__(y) <==> y<<x """
        pass

    def __rmod__(self, y): 
        """ x.__rmod__(y) <==> y%x """
        pass

    def __rmul__(self, y): 
        """ x.__rmul__(y) <==> y*x """
        pass

    def __ror__(self, y): 
        """ x.__ror__(y) <==> y|x """
        pass

    def __rpow__(self, x, z=None): 
        """ y.__rpow__(x[, z]) <==> pow(x, y[, z]) """
        pass

    def __rrshift__(self, y): 
        """ x.__rrshift__(y) <==> y>>x """
        pass

    def __rshift__(self, y): 
        """ x.__rshift__(y) <==> x>>y """
        pass

    def __rsub__(self, y): 
        """ x.__rsub__(y) <==> y-x """
        pass

    def __rtruediv__(self, y): 
        """ x.__rtruediv__(y) <==> y/x """
        pass

    def __rxor__(self, y): 
        """ x.__rxor__(y) <==> y^x """
        pass

    def __sub__(self, y): 
        """ x.__sub__(y) <==> x-y """
        pass

    def __truediv__(self, y): 
        """ x.__truediv__(y) <==> x/y """
        pass

    def __trunc__(self, *args, **kwargs): 
        """ 返回数值被截取为整形的值,在整形中无意义 """
        pass

    def __xor__(self, y): 
        """ x.__xor__(y) <==> x^y """
        pass

    denominator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """ 分母 = 1 """
    """the denominator of a rational number in lowest terms"""

    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """ 虚数,无意义 """
    """the imaginary part of a complex number"""

    numerator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """ 分子 = 数字大小 """
    """the numerator of a rational number in lowest terms"""

    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """ 实属,无意义 """
    """the real part of a complex number"""


    

int
int

相关文章: