zoukankan      html  css  js  c++  java
  • 管窥python语法

    刚接触python,mark下所见所得:

    1.Python调用底层API,可在任何platform上运行,包括Windows、Mac、Unix;

    2.用#符号对代码或语句进行注释,#后的代码不被编译;

    3.print的时候使用逗号[,]告诉解释器不作新行输出;

    4.python程序可以运行在windows shell里(cmd line);

    5.代码可以在notepad下编辑,纯文本结构文件;

    6.变量声明:v=2; v="can be changed to a string"; #the value of v can be changed at any time;

          name="paul cheng";同样用加号进行字符串连接;

    7.while循环:[代码逻辑结构完全用缩进控制,新鲜清洁]

        n=1

        while n<10:

          print n

          n=n+1

    8.function:

      1)definition:

        def hello():

          print "hello"

          return 1234

      2)invoke:

        "print hello()" will output result like: [after execution of print "hello", function returns 1234 for printing]

          hello

          1234

    9.Tuples, Lists, Dictionaries.

        1)Tuples: month = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun')

        2)Lists: cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester'] 

            add: cats.append("appendOne")

            delete: cats.remove("Tom") or del cats[n];

            get one: cats[1] will get "Snappy";

            get many: cats[0:2] will get "'Tom','Snappy','Kitty'";

        3)Dictinonaries: phonebook = {'paul cheung':123456, 'chirs paul':654321}

            add: phonebook['key']=value

            delete: del phonebook['key']

            keys: phonebook.keys() will return all keys as a list;

            values: phonebook.values() will return all values as a list;

    10.loop-for:

        for item in list:

          print item

    11.class:

        class person:  #definition

          def _init_(selef, name, age):  #.ctor

            self.name=name

            self.age=age

        class student(person):  #inheritence

          {statement...}

    12.module: #include variables/function/class;

    13.import module:

        import module_name

        from module_name import function_name/class_name

    14.use imported module:

        module_name.function_name/class;

        function_name if use from...import...

    15.file I/O:

        file = open('file_name', 'r/w')

        file.seek(int, int) #moving cursor

    16.error handling:

        try:

          {statement...}

        except NameError:

          {...}  #handle a type of error

        except SyntaxError:

          {...}  #handle another type of error

        or except (NameError, SyntaxError)

          {...}  #handle multilple types of error together

  • 相关阅读:
    (转)多线程同步event
    初始化列表中成员列出的顺序和它们在类中声明的顺序相同
    确定基类有虚析构函数
    (转)list::splice()函数详解
    MANIFEST.MF文件的格式
    NIO入门了解Buffer
    Failed to load class "org.slf4j.impl.StaticLoggerB
    线程挂起自己,让出CPU
    database如何管理超过4GB的文件
    线程同步(C# 编程指南)
  • 原文地址:https://www.cnblogs.com/paul-cheung/p/3200420.html
Copyright © 2011-2022 走看看