第二章 Python基礎語法

2.1 環境的安裝

  • 解釋器:py2 / py3 (環境變量)html

  • 開發工具:pycharmpython

2.2 編碼

  1. 編碼基礎git

    • ascii ,英文、符號,8位爲一個東西,2**8網絡

    • unicode ,萬國碼,能夠表示全部,32位爲一個東西,2**32ide

    • utf-8,unicode的壓縮,用盡可能少的位數表示一個東西,中文用3個字節=24位工具

    • gbk學習

    • gb2312開發工具

  1. python編碼相關ui

    • 對於Python默認解釋器編碼:編碼

      • py2: ascii

      • py3: utf-8

    • 若是想要修改默認編碼,則可使用:

      # -*- coding:utf-8 -*- 
    • 注意:對於操做文件時,要按照:以什麼編寫寫入,就要用什麼編碼去打開。

  1. bytes 和 str 關係

    • '你不是人' ------ 字節 ; 計算機存儲/網絡傳輸 ----- 二進制

    • 8位bit(比特) = 1個字節

    • str + encode(編碼) = bytes

    • bytes + decode(編碼) = str

 

2.3 變量

  1.問:爲何要有變量?

    爲某個值建立一個「外號」,之後在使用時候經過此外號就能夠直接調用。

  2 .變量命名規則

    • 只能是數字、字母、下劃線。

    • 不能數字開頭。

    • 不能是python關鍵字

    • 見名之意,建議下劃線連接

2.4 輸入

  • input輸入獲得的永遠是字符串

  • py2 和 py3的區別

    • py2 :name = raw_input("你好")

    • py3 : name = input("你好")

2.5 輸出

  • python2 : print"你好"

  • python3: peint("你好")

2.6 註釋

  • 單行註釋 #

  • 多行註釋 """ """

2.7 if 語句

  • if

    if 條件:
    代碼塊
    else
    代碼塊
  • elif

    if 條件:
    代碼塊
    elif 條件:
    代碼塊
    else:
    代碼塊
  • 練習題

    # 第一題:讓用戶輸入一個數字,猜:若是數字 > 50,則輸出:大了; 若是數字 <= 50 ,則輸出:小了。
    num = input('請輸入一個數字')
    number = int(num)
    if number > 50:
        print('大了')
    else:
        print('小了')
        
        
    # 第二題:用戶名密碼登錄
    username = input('請輸入用戶名:')
    password = input('請輸入密碼:')
    ​
    if username == 'alex' and password == "oldboy" : 
        print('歡迎登錄')
    else:
        print('用戶名或密碼錯誤')
    練習題

     

     

2.7 while 語句

  • while

    while 條件:
    代碼塊
    """
    while True:
      print("你好")
    """
  • else

    while 條件:
    代碼塊
    else: # 當條件不在知足while後觸發,或條件 = False
    代碼塊
    count = 1
    while Ture:
        print(count)
        if  count == 10:
            break
        count = count + 1
    else:
        print("代碼塊")
    print"結束"
    View Code

     

  • break (終止當前循環)

    # 經過break實現 1 ~ 10
    count = 1
    while True:
         print(count)
         if count == 10:
            break
         count = count + 1
    print('結束')
    View Code

     

  • continue(本次循環若是遇到continue,則再也不繼續往下走,而是回到while條件位置)

    conunt = 1
    while count <=10:
        print(count)
        continue
        count = count + 1

     

2.8 字符串格式化

  1. %s 直接作佔位符

    template = "我是%s,年齡%s, 職業%s。" %("alex",73,'講雞湯',)
    print(template)
  1. %d數字佔位符

    name = input('')
    s = "12345%d"%(name)
    print(s)
  1. %% 表示%

    num = input('>>')
    s = '目前是的學習進度:%s%%'%num
    print(s)

 

2.9 運算符

  1. 算數運算

    #練習題:1 ~ 100之間全部數相加
    total = 0
    count = 1
    while count <= 100:
        total = total + count
        count = count + 1
    print(total)
    練習題

     

  2. 比較運算
  3. 賦值運算
  4. 邏輯運算

2.10 三元(目)運算

  1. 格式

    a = 前面 if 條件 else 後面
    # 等同於
    if 條件:
    v= 後面
    else:
    v= 前面
  2. 示例

    # 讓用戶輸入,若是是整數,則轉換成整數,不然賦值爲None
    data = input("請輸入")
    value = int(data) if data.isdecimal() else None
  3. a = 1,b = 2 交換a,b變量

    • a,b = b,a

    • c = a, b = a ,a = c

2.11 書寫規範

  1. PEP8規範

  2. 特殊字符

    • \n 換行

    • \t tab鍵(製表符)

    • \r 回到當前行起始位置---應用進度條

      v1 = r"D:\code\s21day14\n1.mp4" (推薦)
      print(v1)


      v2 = "D:\\code\\s21day14\\n1.mp4"
      print(v2)
  3. pycharm自動生成頭部代碼

2.12 項目結構目錄

  1. 項目調用書寫規範

  2. 單可執行文件

  3. 多可執行文件

    • src ---- 業務相關 好比 stdent.py core.py

    • lib ---- 公用的類庫

    • db ---- 數據/庫

    • config ---- 配置

    • bin ---- 可執行文件(根目錄)

    • log ---- 日誌文件

2.13 git

  1. 提交數據

    • git status 查看當前目錄狀態

    • git add . 收集當前目錄下全部問題

    • git commit -m'註釋' 寫入提交的數據

    • git push origin master

 

 原文連接:https://www.danpinsuiyue.com/228520744.html

相關文章
相關標籤/搜索