美文网首页
python实战笔记

python实战笔记

作者: ShutLove | 来源:发表于2018-05-02 23:38 被阅读0次
    1. cmd模块提供了一个编写交互式shell的功能
      方法do_action,action即是一个交互的命令,do_action下面加的一行字符串或者"""多行字符串可以当做action命令的说明,cmdloop方法可以进入交互式shell。
    import cmd
    
    class HelloShell(cmd.Cmd):
        intro = 'Welcome to the hello shell. Type help or ? to list commands.\n'
        prompt = '(hello) '
    
        def do_hello(self, arg):
            'hello command will print hello'
            print("hello")
    
        def do_exit(self, arg):
            'exit the hello shell'
            return True
    
    if __name__ == '__main__':
        HelloShell().cmdloop()
    
    1. python调用系统命令
    from subprocess import call
    call(["ls", "-l"])
    
    1. python中没有switch case,可用如下方式代替
    def f(x):
        return {
            'a': 1,
            'b': 2
        }.get(x, 9) # 9 is default if x not found
    
    1. python中对字符串trim方法
    s = "  a string example  "
    s = s.strip()
    
    1. python中对字符串list进行join
    my_list = ["Hello", "world"]
    print "-".join(my_list)
    
    1. if name == "main":用法
      如果当前文件是命令行中被执行的文件,如python test.py,且test.py中包含上面代码,则test.py中name在解释执行的时候就是main,因此就可以执行if后面的代码,否则name的值是module's name。
      https://stackoverflow.com/questions/419163/what-does-if-name-main-do
    2. 列表append和extend区别
      a = [1,2];b = [3,4]
      a.append(b)后a变为[1,2,[3,4]]
      a.extend(b)后a变为[1,2,3,4]

    相关文章

      网友评论

          本文标题:python实战笔记

          本文链接:https://www.haomeiwen.com/subject/vfmhrftx.html