<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python 基礎語法梳理

      最近涉及到python的東西比較多,抽一點時間把基礎語法規整下。

      1、面向對象

      #coding=utf-8
      
      def _class_test_01():
      
          s = squire(3,4)
      
          print("width %d lenth %d area is %d" 
              %(s.width, s.length, s.calc_area()))
      
      
      class squire:
      
          width = 0
          length = 0
      
          #構造函數
          def __init__(self, w, l):
      
              self.width = w
              self.length = l
      
          def calc_area(self):
              return self.width * self.length
      View Code

      2、列表、字典、元組

      #coding=utf-8
      
      def _container_test_01():
      
          # 列表
          l_temp = ['fredric','fed', 'other','base']
      
          # 獲取長度及切片
          print("len: " 
              + str(len(l_temp)) # 長度 3
              + "  value 1:3  " 
              + ''.join(l_temp[1:3])) # fredother
      
          # 遍歷列表
          for item in range(0, len(l_temp)):
              print(l_temp[item])
      
      
          # 出隊列
          print(l_temp.pop(1))
      
          # 注意insert會替換原來的值
          l_temp.insert(1, 'hi')
          print(l_temp) # fredric hi other
      
          # 排序
          l_temp.sort() # 根據字母排序
          print(l_temp)
      
      
      
      def _container_test_02():
      
          #元組
          t_temp_01 = (1,2,3,4,5)
          t_temp_02 = (6,7)
      
          # 元組的數據不允許修改,但可以拼接
          t_temp_03 = t_temp_01 + t_temp_02
      
          print("len: %d value[1]: %d max: %d min: %d" 
              %(len(t_temp_03), t_temp_03[1], 
                  max(t_temp_03), min(t_temp_03)))
      
      
      def _container_test_03():
      
          # 字典
          d_temp = {"username":"fredric", "password":"fredricpwd", "note":"none"}
      
          print(d_temp["username"])
          print(d_temp["password"])
          print(d_temp["note"])
      
          del  d_temp["note"]
      
          # 遍歷字典
          for key in d_temp.keys():
              print(key + "  " + d_temp[key])
      
          d_temp.clear()
      
          print("key len after clear: %d" %(len(d_temp.keys())))
      View Code

      3、文件操作

      #coding=utf-8
      import fileinput
      
      def _file_test_01():
      
          config = {}
      
          #db.cfg 中數據如下:
          #    host = 192.168.0.1
          #    db = test
          #    username = root
          #    password = root
          for line in fileinput.input('./config/db.cfg'):
              
              key = line.split("=")[0].strip()
              value = line.split("=")[1].strip()
      
              config.update({key:value})
      
      
          # 打印保存在字典里的key/value
          for key in config.keys():
              print(key + " " + config[key])
      View Code

      4、語句流程

      #coding=utf-8
      
      # 該全局變量可以被使用
      status = False
      
      def _flow_test_01():
      
          v_str_01     = "abcdefg"
          count         = 0
          global status
      
          while (count < len(v_str_01)):
              
              if v_str_01[count] == 'd':
      
                  #此時修改的是全局變量status,如果沒有上面局部變量的定義,則為一局部變量
                  status = True
      
                  break
      
              elif v_str_01[count] == 'f':
      
                  status = True
      
                  break
      
              else:
                  status = False;
      
      
              count += 1
      
          if True == status:
      
              print("get value:  " + v_str_01[count])
      View Code

      5、http POST JSON數據

      #coding=utf-8
      
      import http.client, urllib.parse
      import json
      
      # POST請求測試,請求和返回都是JSON數據
      def _http_test_01():
      
          str = json.dumps({'username':'fredric'})
      
          headers = {"Content-type": "application/json",
              "Accept": "text/plain"}
      
          conn = http.client.HTTPConnection("127.0.0.1" ,3000)
      
          conn.request('POST', '/dopost', str, headers)
          response = conn.getresponse()    
          data = response.read().decode('utf-8')
          
          # 打印返回的JSON數據
          print(json.loads(data)["res"])
          print(json.loads(data)["data"])
      
          conn.close()
      View Code

      6、mysql數據庫操作

      #coding=utf-8
      
      #采用pip install PyMysql安裝
      import pymysql
      import sys
      
      def _mysql_test_01():
      
          db = pymysql.connect("localhost","root", "root", "demo")
          
          cursor = db.cursor()
      
          insert_sql = "INSERT INTO myclass(id, \
             name, sex, degree) \
             VALUES ('%d', '%s', '%d', '%d')" % \
             (3, 'fred', 20, 2000)
      
          try:
                 cursor.execute(insert_sql)
                 db.commit()
          except:
              # 此處捕獲異常,諸如主鍵重復時打印:pymysql.err.integrityerror
              print("Error: insert failed:", sys.exc_info()[0])
              db.rollback()
      
      
          select_sql = "SELECT * FROM myclass"
      
          try:
      
              cursor.execute(select_sql)
      
              results = cursor.fetchall()
              for row in results:
                 print ("id=%d,name=%s,sex=%d,degree=%d" %(row[0], row[1], row[2], row[3]))
          except:
      
              print ("Error: select failed", sys.exc_info()[0])
      
      
          db.close()
      View Code

      7、字符串

      #coding=utf-8
      
      def _string_test_01():
      
          v_temp = "test string value"
      
          # 首字母大寫
          print(v_temp.capitalize()[0])
      
          # 部分字符串
          print(v_temp[0:3]);
      
          # 循環遍歷字符串
          for item in range(0, len(v_temp)):
              print(v_temp[item])
      
      
      def _string_test_02():
      
          v_str_01 = "start"
          v_str_02 = "end"
      
          v_str_list = [v_str_01, " ", v_str_02]
      
          # 字符串拼接
          res = "".join(v_str_list)
          print(res)
      
          # 字符串替換
          print(res.replace('start', 'hello start'))
      
      
      def _string_test_03():
      
          v_str_03 = "16"
      
          v_int_01 = 0;
      
          # 字符串轉整數,后面的8 代表8進制的整數
          v_int_01 = int(v_str_03, 8)
      
          print(v_int_01 == 14) 
      View Code

      8、線程

      #coding=utf-8
      import _thread
      import threading
      import time
      
      lock = threading.Lock()
      
      def _thread_test_01():
      
          try:
              _thread.start_new_thread( _do_thread, ("thread_01",1,))
              _thread.start_new_thread( _do_thread, ("thread_02",2,))
              _thread.start_new_thread( _do_thread, ("thread_03",3,))
      
          except:
                 print ("Error: 無法啟動線程")
      
          while 1:
              pass
      
      
      def _do_thread(name, delay):
      
          print("start thread %s " %(name))
      
          #獲取鎖
          lock.acquire()
      
          time.sleep(delay)
      
          print("%s: %s" % (name, time.ctime(time.time())))
      
          #釋放鎖
          lock.release()
      View Code

      9、模塊化(測試主函數)

      # -*- coding:utf-8 -*-
      import test_module.t_string as t_s_module
      import test_module.t_flow as t_f_module
      import test_module.t_container as t_c_module
      import test_module.t_file as t_f_module
      import test_module.t_thread as t_t_module
      import test_module.t_http as t_h_module
      import test_module.t_class as t_c_module
      import test_module.t_mysql as t_m_module
      
      #t_s_module._string_test_01()
      #t_s_module._string_test_02()
      #t_s_module._string_test_03()
      #t_f_module._flow_test_01()
      #print(t_f_module.status) #全局變量
      #t_c_module._container_test_01()
      #t_c_module._container_test_02()
      #t_c_module._container_test_03()
      #t_f_module._file_test_01()
      #t_t_module._thread_test_01()
      #t_h_module._http_test_01()
      #t_c_module._class_test_01()
      t_m_module._mysql_test_01()
      View Code

      源碼文件附帶如下:

      https://files.cnblogs.com/files/Fredric-2013/python.rar

      posted @ 2017-06-13 17:26  Fredric_2013  閱讀(221)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 精品国产高清中文字幕| 成人午夜免费无码视频在线观看| 亚洲精品综合网中文字幕| 毛片无遮挡高清免费| 蜜桃一区二区三区免费看| 最近中文字幕完整版| 色偷偷亚洲女人天堂观看| 暖暖免费观看电视在线高清| 吉川爱美一区二区三区视频| 精品久久久久久国产| 日韩老熟女av搜索结果| 新版天堂资源中文8在线| 亚洲精品一区二区三区大| 亚洲va久久久噜噜噜久久狠狠 | 成人区人妻精品一区二蜜臀| 欧美亚洲综合成人A∨在线| 临猗县| 欧美亚洲h在线一区二区| 人妻激情偷一区二区三区| 国产又色又爽又黄的视频在线| 欧美videosdesexo吹潮| 国产亚洲av手机在线观看| 国产中文字幕日韩精品| 亚洲国产aⅴ成人精品无吗 | 国产熟女一区二区三区蜜臀| 在线国产你懂的| 99riav国产精品视频| av新版天堂在线观看| 推油少妇久久99久久99久久| 蜜臀久久精品亚洲一区| 精品一区二区久久久久久久网站| 人妻无码ΑV中文字幕久久琪琪布| 污网站在线观看视频| 九九热免费精品视频在线| 阿荣旗| 日韩成av在线免费观看| 中文字幕精品人妻丝袜| 亚洲欧美日韩综合一区在线| 日韩精品中文字一区二区| 九九re线精品视频在线观看视频| 日本中文一区二区三区亚洲|