• Core Python Programming笔记,Note


    >>> logfile=open('/tmp/mylog.txt','a')

    >>> print >> logfile, 'fatail error: invalid arguments'

    >>> logfile.close()

    >>>

    user=raw_input("please input your name:")

    Numbers:

    int

    long

    bool (True,False)

    float

    complex (6.23+1.5j)

    tuple

    1.元组,数组(是个线性结构,可以用整数索引)

    Lists [] and Tuples (): 

    hold any arbitrary number of arbitrary Python objects, the items are ordered

    and accessed via index offsets, similar to arrays, except that lists and tuples can store 

    different types of objects.

    Tuples can be thought of for now as "read-only" lists.

    Subsets can be taken with the slice operator ([] and [:]) in the same manner as strings.

    Dictionaries

    Dictionaries (or "dicts for short) are Python's mapping type and work like associative arrays

    or hashes found in perl.

    >>> aDict = {'host':'earth'}

    >>> aDict

    {'host': 'earth'}

    >>> aDict['port']=8080

    >>> aDict

    {'host': 'earth', 'port': 8080}

    >>> aDict.keys()

    ['host', 'port']

    >>> for key in aDict:

    ...     print key, aDict[key]

    ...

    host earth

    port 8080

    if expression1:

    if_suite

    elif expression2:

    elif_suite

    else:

    else_suite

    >>> while cnt<3:

    ...     print 'loop %d'%(cnt)

    ...     cnt+=1

    ...

    loop 0

    loop 1

    loop 2

    >>> for item in range(10):

    ...     print item

    >>> for item in ['e-mail','net-surfing','homework']:

    ...     print item

    ...

    e-mail

    net-surfing

    homework

    List Comprehensions:

    >>> squared = [x**2 for x in range(4)]

    >>> squared

    [0, 1, 4, 9]

    >>> for i in [x**2 for x in range(6)]:

    ...     print i

    ...

    0

    1

    4

    9

    16

    25

    Useful Built-In Functions for New Python Programmers

    dir([obj]),help([obj]),int(obj),len(obj),open(fn,mode),range([start,]stop[,step]),

    rraw_input(str),str(obj),type(obj)

    Typical Python file structure

    #!/usr/bin/env python		(1)Startup line (Unix)
    "this is a test module"		(2)Module documentation
    
    import sys
    import os			(3)Module imports
    
    debug = True			(4)(Global) Variable declarations
    
    class FooClass (object):
    	"Foo class"
    
    	pass			(5)Class declarations (if any)
    
    def test():
    	"test function"
    	foo = FooClass()
    	if debug:
    		print 'run test()'	(6)Funcation declarations(if any)
    
    if __name__ == '__main__':
    	test();			
    

      

    P 115 / 1155

  • 相关阅读:
    POJ2888 Magic Bracelet [矩阵快速幂+Burnside+欧拉函数]
    数列的 GCD [计数问题]
    com组件的注册
    WCF 传输和接受大数据
    数据库中已存在名为 'View_Business' 的对象。
    windows 两个用户,默认其中一个用户登录
    用C#读取,写入ini文件
    小心得,关于串口
    未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
    无法激活服务,因为它不支持 ASP.NET 兼容性
  • 原文地址:https://www.cnblogs.com/wucg/p/2145778.html
Copyright © 2020-2023  润新知