第二章 Python入門

第二章 Python入門

1 環境的安裝

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

    • 文件 a.pylinux

      #!/usr/bin/env python
      # -*- coding:utf-8 -*-
      
      print('你好')
    • 運行:解釋器 文件路徑程序員

    • 在linux上有一種特殊的執行方法:面試

      • 給文件賦予一個可執行權限
      • ./a.py 【原理:linux自動去找文件的第一行/usr/bin/env python 這個是解釋器路徑,後面加a.py】
  • 開發工具:pycharmide

    • 下載:工具

      • Profession 專業版 ,功能多,激活碼獲取地址http://idea.lanyus.com/,激活時選擇中間的(激活碼獲取地址已不能使用,後續再發破解方法)
      • Community 社區版,免費的,功能相對較少
    • 操做:開發工具

      • 建立文件字體

      • 重命名編碼

      • 鼠標滾輪實現字體縮放idea

      • Debug模式

        先加斷點,而後以debug模式運行,而後點擊一次運行運行一次。最後是模式切換

      • pycharm自動生成文件頭部


2 第一個腳本(第一個文件)

print('這是個人第一個腳本')

3 編碼

3.1 編碼基礎

  • ascii

    • 固定8位,只能用來表示英文及少部分符號
    • 能表示 2**8 個字符
  • unicode

    • 固定32位,包含全部語言

    • 能表示 2**32 個字符

    • ha --> ASCII 佔用16位存儲空間

      ha --> Unicode 佔用64位存儲空間

  • utf-8

    • 最少8位最多32位,中文固定佔用3字節。是unicode的壓縮形式
  • gbk

    • 中文佔用2字節
  • gb2312

    • 中文用2字節。便於之後擴展建議使用utf-8,由於gbk不支持外文
  • 單位

    8bit = 1byte
    1024byte = 1kb
    1024kb = 1MB
    1024MB = 1GB
    1024GB = 1TB
    1024TB = 1PB
    1024PB = 1EB
    1024EB = 1ZB
    1024ZB = 1YB
    1024YB = 1NB
    1024NB = 1DB
    經常使用到TB就夠了

3.2 python編碼相關

對於Python默認解釋器編碼:

  • py2: ascii
  • py3: utf-8

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

# -*- coding:utf-8 -*-

注意:

  • 對於操做文件時,要按照:以什麼編寫寫入,就要用什麼編碼去打開。
  • cmd中可能沒法運行python2代碼的中文,由於命令提示符可能不是utf-8格式,須要使用 chcp 65001進行替換爲utf-8,win10運行會進行報錯,暫無官方說法

4 輸出

print('想要輸出的內容')

特殊:

  • py2:print "你好"
  • py3:print('你好')

注意:輸出時既可使用單引號,也可使用雙引號,但必須是成對使用。錯誤寫法:print("你好')

5 數據類型

'alex' / "alex",通常稱爲字符串
666 , 通常稱爲數字/整型
True / False , 通常稱爲布爾類型
  1. 字符串:

    • 單引號
    • 雙引號
    • 三引號 支持換行
  2. 整形

  3. 布爾型

  4. 輸出:

    特殊

  • py2:print "你好"
  • py3:print("你好")

6 變量

content = '釣魚要釣刀魚'
content = 666
print(content)

變量的要求

  1. 變量名只能包含:字母/數字/下劃線

  2. 數字不能開頭

  3. 不能是python的關鍵字。

    ['and','as','assert','break','class','continue','def','del','elif','except','else','exec','finally','for','from','global','if','import','in','is','lambda','not','or','pass','print','raise','return','try','while','with','yield']

  4. 建議:

    • 見名知意:name = 'alex' age = 18
    • 用下劃線連接:alex_add = "吳佩琦"
    • 補充:AlexDad = "吳佩琦"(駝峯式命名,不建議)

問:爲何要有變量?

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

7 綜上練習題

# 第一題
age = 18
new_age = age +1
print(new_age)

# 第二題
name = "alex"
new_name = name + "sb"
print(new_name)

# 第三題
age = "666"
new_age = age + "666"
pirnt(new_age)

# 第四題
age = "666"
new_age = age + 666
print(new_age)

# 第五題
age = 6
new_age = age * 2 
print(new_age)

# 第六題
name = "alex"
new_name = name * 2
print(new_name)

# 第七題
age = 18
value = age > 19
print(value)

# 第八題
_ = 9
_9 = 9
9name = 'alex'
True = 9
print = 666

8 輸入

user_name = input('請輸入你的姓名')

注意:

  • input 輸入獲得的內容永遠是字符串
  • py版本區別:
    • py2:name = raw_input('請輸入姓名')
    • py3:name = input('請輸入姓名')

實例:

user_naem = input('請輸入你的姓名')
password = input('請輸入你的密碼')
# 用戶名和密碼拼接
content = '你的用戶名是:' + user_name +';你的密碼是:' + password
# 輸出用戶名和密碼
print(content)

9 註釋

# 單行註釋
'''
多行註釋
'''

10 條件判斷

  1. 初級條件語句

    # 請實現一個功能:讓用戶輸入性別,若是是 男,則輸出:再見;若是是 女:則輸出 來啊來啊;不然:滾
    gender = input('請輸入性別:')
    '''
    若是是男生:打印再見
    若是是女:打印來呀來呀
    不然:滾
    '''
    if gender == '男':
    	print('再見')
    elif gender == '女':
    	print('來來')
    else:
    	print('滾')
  2. elif 條件

    # 請實現一個功能:讓用戶輸入性別,若是是 男,則輸出:再見;若是是 女:則輸出 來啊來啊;不然:滾
    gender = input('請輸入性別:')
    '''
    若是是男生:打印再見
    不然:打印來呀來呀
    '''
    if gender == '男':
    	print('再見')
    elif gender == '女':
        print('來來來')
    else:
    	print('滾')
    print('end')
  3. 練習題

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

11 循環

while 循環

break關鍵字

continue關鍵字

# # 1.循環打印"人生苦短,我用PYTHON"
# while True:
#     print('人生苦短,我用python')
#
# # 2.while 後加入條件
# while 1>0 and 2>1:
#     print('死循環')
#
# # 3. 數字相加
# count = 1
# count = count + 1
# print(count)
#
# # 4. 請經過循環,讓count每次循環 +1
'''
count = 0
while True:
    print(count)
    count += 1
'''
# 5. 經過循環打印1,2,3...10
'''
content = 1
while content <= 10:
    print(content)
    content += 1
    '''

# 6 請打印1,2,3,4,5,6,8,9,10
'''
第一種寫法:
num = 0
while num<=6:
    num += 1
    print(num)
num = 8
while num <=10:
    print(num)
    num += 1
'''

'''
錯誤寫法
# while num <= 10 and num != 7:
#     num += 1
#     print(num)
'''

'''
第二種寫法:
count = 1
while count <= 10:
    if count != 7:
        print(count)
    count += 1
'''

'''
第三種寫法:
count = 1
while count <= 10:
    if count = 7:
        pass
    else:
        print(count)
    count += 1
'''
# 7. 關鍵字:break
'''
while True:
    print(666)
    break    # 終止當前循環
print('結束')
'''

# 練習:經過break實現 1~10
'''
count = 1
while True:
    print(count)
    if count == 10:
        break
    count += 1
'''
# break 終止當前循環
'''
while True:
    print('你好')
    while True:
        print('666')
        break   # 終止當前循環
'''

# 8. 關鍵字:continue
# 示例:1,2,3,4,5,6,8,9,10
'''
count = 1
while count < 10:
    print(count)
    continue # 本次循環若是遇到continue,則再也不繼續往下走,而是回到while條件位置
'''
'''
count = 1
while count <= 10:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1
'''

# 9 while...else...
count = 1
while True:
    print(count)
    if count == 10:
        break
    count += 1
else:  # 不在知足while條件時觸發,或 條件 == False; break 跳出無效
    print('else代碼塊')
print('結束')

12.字符串格式化

注意:

記得在格式化後面加 , 號表示結束

  • %s
  • %d
  • %%
# 字符串格式化
'''
name = input('姓名')  # ctrl + d  複製快捷鍵
do = input('在幹什麼')
template = '%s 在教室,%s'%(name,do,)
print(template)
'''

# 直接作佔位符
# template = '我是%s,年齡%s,職業%s'%('alex',73,'程序員',)
# print(template)
# template = '我是%s,年齡%d,職業%s'%('alex',73,'程序員',)
# print(template)

# 若是想在格式化字符串中輸出 % 號,必須使用兩個 % 
# name = 'alex'
# template = '%s 如今的手機電量是100%%'%(name)
# print(template)

# 練習
name = input('請輸入姓名')
age = input('請輸入年齡')
job = input('請輸入職業')
msg = '''
---------- info os Ales Li ----------
Name     :%s
Age      :%s
Job      :%s
---------------- end ----------------
'''
date = msg %(name,age,job,)
print(date)

13.運算符

  1. 算數運算符

    • 餘數運算符 %

    • 冪次方運算符 **

    • 地板除(丟棄小數取整)運算符 //

# ----------   算術運算符  ----------


# -----  餘數 % -----
# %在運算符中用來取餘數
# value = 11 % 3
# print(value)

# 練習題:打印 1 ~ 100 之間的奇數
# num = 1
# while num <= 100:
#     if (num % 2) != 0:
#         print(num)
#     num += 1

# ----- 冪次方 ** -----
# val = 2 ** 8
# print(val)

# ----- 地板除 // -----
# val = 9 // 2
# print(val)

# 練習:計算 1 - 100 相加
# count = 1
# date = 0
# while count <=100:
#     date += count
#     count += 1
# print(date)
  1. 比較運算符
    • <=,>=,==,+,-,*,/
  2. 賦值運算符
    • += count +=1(count = count + 1 )
    • -=
    • %=
    • *=
  3. 邏輯運算符
    • and 兩個多爲真
    • or 一個爲真
    • not 取反
# ---------- 邏輯運算符 ----------

# 二般狀況下
## 小知識
# —— int
# —— str
# —— bool

# 數字轉布爾值,只有0爲False,其他都爲True
# v1 = 1
# v2 = bool(v1)
# print(v2)

# 字符串轉布爾值,只有空字符串爲False
# v1 = "1"
# v2 = bool(v1)
# print(v2)

# 布爾值轉換其餘
# v1 = False
# v2 = str(v1)
# v3 = int(v1)
# print(v2)
# print(v3)
# **************************** 面試題 **************************
'''
    對於  or  來說
    value = 1 or 9
    第一個值若是是轉換成布爾值爲真,則value = 第一值
    第一個值若是是轉換成布爾值爲假,則value = 第二值
    若是有多個 or 條件,則從左到右依次進行上述流程。
    示例:
        v1 = 0 or 1
        v2 = 8 or 10
        v3 = o or 9 or 8

'''

# 對於and,若是遇到
'''
    若是第一個值轉換成布爾值是True,則value = 第二個值
    若是第一個值轉換成布爾值是False,則value = 第一個值
    若是有多個and條件,則從左到右依次進行上述流程。
    示例:
        v1 = 1 and 9
        v2 = 1 and 0
        v3 = 0 and 7
        v4 = 0 and ""
        v5 = 1 and 0 and 9
        print(v5)
'''

# 綜合,優先級爲  () > not > and > or
v1 = 1 and 9 or 0 and 6
  • 總結

    # 運算級順訊關係:
    # 冪運算              **   (5**2就是5的2次方)
    # 正負號            -x    +x
    # 算術操做符    *   /   //    +    -  %
    # 比較操做符   <  <=  >   >=   ==   !=
    # 邏輯運算符 not(高)   and(中)   or(低)
相關文章
相關標籤/搜索