001-python介紹
1、python優勢
簡單、執行沒有C語言效率高
2、編程語言分類
編譯型:一次編譯到處執行,比如exe ,依賴操作系統。
解釋型:逐行解釋執行,效率不如編譯型,比如python,可跨平台執行。
3、解釋器
cpython: 就是cmd模式的python
ipython
pycharm:用的較多
4.python第一程序
#1.打印輸出 print("hello world")
002-變量與常量
1、變量命名規則
- 數字不能直接當變量,不能數字開頭
- 不能用python關鍵字
- 只能用數字、字母、下划線組成,比如:hobby_of_alex
1.1 a與 "a"不同
a = 20 c = 30 print(a) #輸出結果20 print("a") #輸出結果a
1.2 "周傑倫" 與 周傑倫
print("周傑倫") #輸出周傑倫 print(周傑倫) # defined
1.3 變量的命名規則
jay = 10 #可以為變量 jay10 = 20 #可以為變量 # if = 3 #關鍵字不能為變量名 # # ____ = 12 # 線可以為變量名 # #abc = 12 #是注釋不可以為變量名
1.4 python的變量名長度不做限制
zhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaoku = "葯匣子" print( zhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaokuzhongGongZhongYangGuowuyuanliaoningshengdongxingxianlongquanshanzhuangyaoshanbuzhuguanjinglilibaoku)
1.5 python的代碼定義的變量的不能縮進
a = 10 #這種縮進會有問題
1.6 python的運輸
a = 20 print(a) a = a + 10 print(a)
2、變量賦值
- 變量賦值中,先計算等號右邊的,最后將計算的結果賦值給變量
HOBBY_OF_ALEX = "馬殺雞" print(HOBBY_OF_ALEX) HOBBY_OF_ALEX = "大寶劍" print(HOBBY_OF_ALEX)
3、
常量
常量的值不可變的,通常用大寫字母表示,比如 HOOBBY_OF_ALEX
003-注釋
1、單行注釋(井號)
# 這是注釋 # 這是循環 # ......
2、多行注釋(雙引號)
004-整數-int
1、整數 int()
- 整數就是阿拉伯數字 0123456 -1-2-3
>>> n = 123456 >>> type(n) <class 'int'> >>> n = 0-1-2-3 >>> type(n) <class 'int'>
005-浮點數-float
-
1、浮點數 float()
- 浮點數就是小數,3.14 -.3.14
>>> n = 3.14 >>> type(n) <class 'float'> >>> n = -3.14 >>> type(n) <class 'float'>
006-字符串-str
-
1、字符串 str()
- 一組字符的集合,‘abc123你好’ "abc123你好"
>>> s = 'abc,123,你好' >>> type(s) <class 'str'> >>> s = "abc,123,你好" >>> type(s) <class 'str'>
007-布爾型-bool
- 1、布爾型 bool()
- Trun 真
- False 假
# 0為假 >>> bool(0) False # 1為真 >>> bool(1) True
008-運算符
1、整數運算符
+ - * /(非整除) //(整除) %(取余) **(平方)
>>> 1 + 2 3 >>> 2 - 1 1 >>> 2 * 3 6 >>> 10 / 3 3.3333333333333335 >>> 10 // 3 3 >>> 10 % 3 1 >>> 10 % 2 0 >>> 2 ** 2 4 >>> 2 ** 3 8
# print(a // 4) # 整除 10 / 4 = 2.......2
# print(a % 5) # 如果余數等於0. 表示整除. a是5的倍數
#print(2**9) #2的9次冪的積
2.python正負數除的向上和向下去整;下面例子說明當是正整數的除法的時候會向上取整;負整數的除法向下取整

a = 10 print(a // 3) # 向上取整 print(a // -3) # 向下取整
3.python換行 與 不換行

hua = ''' 我喜歡你. 你敢喜歡我么? 滾''' print(hua) haha = "你好啊. \n我叫賽利亞" print(haha)
009-交互式-input
1、交互式 input()
- 阻塞:程序不再往下走,等待用戶輸入
# name = input("請輸入你的名字:") # print(name * 2) # 實際收入 # 讓用戶輸入他的工資. 扣除百分之2的稅 # 輸出該用戶的實際工資數 # input收到的數據都是字符串類型 salary = input('工資:') # 想辦法吧salary轉化成能進行數學運算的數據類型 # int, float, 字符串轉化成int: int(). float(), 一般情況下, 基本數據類型之間轉化 數據類型() salary = int(salary) print("我的工資是:",salary * 0.98) print()
>>> name = input('請輸入你的名字:') 請輸入你的名字:alex >>> print(name) alex
010-條件分支-if

# money = int(input("請輸入你的工資:")) # # if money > 500: # print("我要沖騰訊QQ會員") # print("買一桶泡面") # # # print("我要回家了") # 用戶輸入a,b 兩個數字 # 比較a和b的大小. # 輸出比較大的那一個數字 # a = int(input("請輸入一個a:")) # b = int(input("請輸入一個b:")) # if a > b: # print(a) # else: # print(b) # # 如果工資超過12000, 買個手機 # # 如果工資超過8000, 小於12000, 大寶劍 # # 如果工資超過6000, 小於8000, 小寶劍 # # 如果工資小於6000, 洗個澡 # money = int(input('請輸入你的工資:')) # if money > 12000: # print("買個手機") # elif money > 8000: # print("大寶劍") # elif money > 6000: # print("小寶劍") # else: # print("洗個澡") # 假設用戶的用戶名是alex, 密碼是123 # 請模擬用戶登錄的過程\ # username = "alex" # password = "123" # username_input = input("請輸入你的用戶名:") # password_input = input("請輸入你的密碼:") # # if username == username_input and password == password_input: # print("登錄成功") # else: # print("用戶名或密碼錯誤")
1、條件分支 if if語句匹配規則:從上往下匹配,匹配成功即停止 下面是if的四種使用方法 if 條件: 代碼塊 print('xxxx') if 條件: 代碼塊 else: 代碼塊 if 條件: 代碼塊 elif 條件: 代碼塊 else: 代碼塊 if 條件: 代碼塊 elif 條件: 代碼塊 elif 條件: 代碼塊 else: 代碼塊 salary = int(input('請輸入你的工資:')) if salary > 8000: print('買iphone xs') elif salary > 6000: print('大寶劍') elif salary > 3000: print('買自行車') else: print('吃大餐') print('吃泡面') #無論上面是否匹配成功‘吃泡面’一定會打印
011-循環語句-while
# while True: # print("我愛你")

# count = 0 # while count < 10: # print("我愛你", count) # count = count + 1 # 1 2 3 4 5 6 7 8 9 10 # 11

# 求1-100的所有數的和 # # 1+2+3+4+5+6+7+8+...100=? # count = 360 # temp = 0 # while count <= 500: # temp = temp + count # count = count + 1 # print(temp) # 累加(1-100) """ temp:0, count : 1 temp:0+1, count:2 temp:0+1+2, count: 3 temp:0+1+2+3, count:4 temp:? , count: 1000 # 求1-2+3-4+5 ... 99的所有數的和 # 1. 從1數數到99 # 2. 判斷每個數是奇數還是偶數 # 3. 奇數干什么, 偶數干什么 """ i = 1 temp = 0 while i <= 100: # print(i) # i % 2 == 0 偶數 # i % 2 == 1 奇數 if i % 2 == 0: # 偶數 temp = temp - i else: # 奇數 temp = temp + i i = i + 1 print(temp)
1、while循環 死循環:只要條件成立,會一直循環下去 while True: print('hello world!') 條件成立停止循環 count = 1 #計數器 while count < 10: print('hello world!') count = count + 1 累加循環:把每次相加的和存入變量,再用變量和下個值相加 # 計算1+2+3+4+...100的值 count = 1 # 計數器用於記錄循環次數1~100 temp = 0 while count <= 100: # print(count) # 查看是否循環1-100 temp = temp + count # 每次計算過程 temp=0+1+2+3, 每次計算結果 temp = 6 count = count + 1 # 每次循環加1 print(temp) -------------------------------- 5050 # 計算1-2+3-4+5-...100的值 count = 1 # 計數器用於記錄循環次數1~100 temp = 0 while count <= 100: if count % 2 == 0: # print(count) temp = temp - count # 遇到偶數就減 else: # print(count) temp = temp + count # 否則就是奇數,遇到奇數就加 count = count + 1 # 每次循環次數加1 print(temp) # 循環和條件分支全部走完,繼續往下走輸出這一條 ------------------------------------- 50
012-嵌套
- 1、嵌套最好不要超過三層
if 條件: 代碼 if 條件: 代碼 if 條件: 代碼 else: 代碼 else: 代碼 else: 代碼
013-continue 和 break

while True: s = input("請輸入你要噴的內容(輸入Q, 程序退出):") if s == "Q": continue print("你對打野說:", s)
-
1、continue
- 結束本次循環,繼續下一次循環
count = 0 while count < 5: count = count + 1 if count == 3: continue print('hello world',count) ----------------------------------------- hello world 1 hello world 2 hello world 4 hello world 5
break結束當前循環語句

count = 0 while count < 10: count = count + 1 if count == 5: break print('hello world',count) -------------------------------------- hello world 1 hello world 2 hello world 3 hello world 4
python學習總結
