zoukankan      html  css  js  c++  java
  • Python初学1

    windows版python下载:

      https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ

      安装时勾选pip和Add python.exe to Path。

    windows版pycharm下载:

      https://pan.baidu.com/s/1DV81hxSsodtukUHNW-Bzgg

    一些基础知识:

      1、if __name__ == '__main__'的意思是:

        当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行。

      2、在python执行py文件

      1. import sys #引入sys库体  
      2. sys.path.append("C://myPython") #往系统路径中加入自己存放py文件的地址 
        然后就可以开始通过import的方法导入相关的方法和内容了
      3. from test import * #从test.py文件中加载所有的内容。 

    文件读写:

      with open(r'f: est 1.txt','r') as f:
        print f.read()      读

      f.close()    关闭

      with相当于try...catch;  

      按行读:

      with open(r'f: est 1.txt','r') as f:
        for l in f.readlines():
          print l.strip()

      f.close()

      写文件:  

      with open(r'f: est 1.txt','w') as f:
        f.write('abcde')

      f.close()

    序列化:

      import cPickle as pickle  引入包

      with open(r'f: est 1.txt', 'wb') as f:
        dic = 10
        pickle.dump(dic, f)

      f.close()

      with open(r'f: est 1.txt', 'rb') as f:
        aa = pickle.load(f)
        print(aa)

      f.close()

    多线程:

      

    import random
    import time,threading
    def thread_run(urls):
      for url in urls:
        print "%s ---->>>> %s"%(threading.current_thread().name,url)
        time.sleep(random.random())
      print "thread %s end..."%threading.current_thread().name
    
    t1 = threading.Thread(target=thread_run,name="Thread_1",args=(["url_1","url_2","url_3"],)) t2 = threading.Thread(target=thread_run,name="Thread_1",args=(["url_4","url_5","url_6"],)) t1.start() t2.start() t1.join() t2.join() print "end....."

      

     //join是主线程等待子线程

      

    import random
    import threading
    import time
    class myThread(threading.Thread):
        def __init__(self,name,urls):
            threading.Thread.__init__(self,name=name)
            self.urls = urls
    
        def run(self):
            for url in self.urls:
                print "%s ---->>> %s" %(threading.current_thread().name,url)
            print "%s ended..."%threading.current_thread().name
    
    t1 = myThread(name="Thread_1",urls=["url_1","url_2","url_3"])
    t2 = myThread(name="Thread_2",urls=["url_4","url_5","url_6"])
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print "%s end...." %threading.current_thread().name
    

      线程同步:

    import random
    import threading
    import time
    mylock = threading.RLock()
    class myThread(threading.Thread):
        def __init__(self,name,urls):
            threading.Thread.__init__(self,name=name)
            self.urls = urls
    
        def run(self):
            for url in self.urls:
                mylock.acquire()
                print "%s ---->>> %s --" %(threading.current_thread().name,url)
                mylock.release()
    
    t1 = myThread(name="Thread_1",urls=["url_1","url_2","url_3"])
    t2 = myThread(name="Thread_2",urls=["url_4","url_5","url_6"])
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print "%s end...." %threading.current_thread().name
    

      

  • 相关阅读:
    npm 安装Vue环境时报错
    WinSCP与SecureCRT
    LeetCode100---Same Tree
    LeetCode404---Sum of Left Leaves
    LeetCode283---Move Zeroes
    LeetCode344---Reverse String
    Java多线程一
    Java知识点总结
    Java泛型
    深入浅出设计模式学习笔记四:单例模式
  • 原文地址:https://www.cnblogs.com/jlyg/p/9141471.html
Copyright © 2011-2022 走看看