摘要:表達式表達式是運算符和操作數所構成的序列運算符優先級同級的運算符的優先級還是有區別的比如邏輯運算符里的的優先級大于兩個括號同級,左結合出現賦值符號時,右結合優先級在文本文件中編寫代碼腳本是后綴名為的文件,通過命令行執行推薦的,大型工程適合用
表達式
表達式(Expression)是運算符(operator)和操作數(operand)所構成的序列
>>> 1 + 1 2 >>> a = [1,2,3] >>> 1 + 1 + 1 + 1 4 >>> 1 + 2 * 3 7 >>> 1 * 2 + 3 5 >>> a = 1 + 2 * 3 >>> a = 1 >>> b = 2 >>> c = a and b or c >>> c = int("1") + 2運算符優先級
同級的運算符的優先級還是有區別的 比如邏輯運算符里的and的優先級大于or
>>> a = 1 >>> b = 2 >>> c = 3 >>> a + b * c 7 >>> 1 or 2 1 >>> 1 and 3 3 >>> a or b and c 1 >>> a or (b and c) 1 >>> a or 3 1 >>> (a or b) and c 3 >>> (a or b) and (c + 1) //兩個括號同級,左結合 4 >>> a = 1 >>> b = 2 >>> c = a + b //出現賦值符號時,右結合 >>> print(c) 3 >>> c = a or b >>> print(c) 1 >>> a = 1 >>> b = 2 >>> c = 2 >>> not a or b + 2 == c False >>> ((not a) or ((b + 2) == c)) //優先級:not > and > or False在文本文件中編寫Python代碼
python腳本是后綴名為.py的文件,通過命令行“python filename.py”執行
推薦的IDE:PyCharm、vsCode,大型工程適合用PyCharm,學習適合用vsCode,vsCode中推薦的插件:python、Terminal、Vim、vscode-icons注釋
單行注釋用#
多行注釋用```
主要有條件控制(if else)、循環控制(for while)、分支條件控制(if else)
# encoding: utf-8 mood = False if mood : print("go to left") # print("back away") # print("back away") else : print("go to right") a = 1 b = 2 c = 2 # if后面不僅可以是布爾值,還可以是表達式 if a or b + 1 == c : print("go to left") # print("back away") # print("back away") else : print("go to right")
# encoding: utf-8 """ 一段小程序 """ # constant 常量 建議全大寫 ACCOUNT = "hughie" PASSWORD = "123456" # python變量建議都用小寫,用下劃線分隔單詞,不用駝峰命名 print("please input account") user_account = input() print("please input password") user_password = input() if ACCOUNT == user_account and PASSWORD == user_password: print("success") else: print("fail")
# encoding: utf-8 # snippet 片段 if condition: pass else: pass a = True if a: # pass 空語句/占位語句 pass else: print("") if True: pass if False: pass # 嵌套分支 if condition: if condition: pass else: pass else: if condition: pass else: pass # 代碼塊 if condition: code1 code11 code22 code333 code444 code5555 code6666 code2 code3 else: code1 code2 code3
# encoding: utf-8 """ a = x a = 1 print("apple") a = 2 print("orange") a = 3 print("banana") print("shopping") """ a = input() print("a is" + a) if a == 1: print("apple") else: if a == 2: print("orange") else: if a == 3: print("banana") else: print("shopping") # 改寫為elif a = input() print(type(a)) print("a is " + a) a = int(a) if a == 1: print("apple") elif a == 2: print("orange") elif a == 3: print("banana") else: print("shopping")循環(while for)
# encoding: utf-8 # 循環 # 循環語句 # while for # CONDITION = True # while CONDITION: # print("I am while") counter = 1 # 遞歸常用while while counter <= 10: counter += 1 print(counter) else: print("EOF")
# encoding: utf-8 # 主要是用來遍歷/循環 序列或者集合、字典 # a = ["apple", "orange", "banana", "grape"] # for x in a: # print(x) # a = [["apple", "orange", "banana", "grape"], (1, 2, 3)] # for x in a: # for y in x: # # print(y, end="") # print(y) # else: # print("fruit is gone") # a = [1, 2, 3] # for x in a: # if x == 2: # # break 遇到x==2的時候終止,打印出1 # # break # # continue 遇到x==2的時候跳過,打印出1,3 # continue # print(x) # else: # print("EOF") a = [["apple", "orange", "banana", "grape"], (1, 2, 3)] for x in a: # if "banana" in x: # break for y in x: if y == "orange": # 內部循環跳出后,外部循環還在執行 break print(y) else: print("fruit is gone")
# encoding: utf-8 # for (i=0; i<10; i++){} # 以上的for循環用python實現 # for x in range(0, 10): # # range(0,10) 表示從0開始的10個數字,并不包含10 # print(x) # for x in range(0, 10, 2): # # range(0,10,2) 2表示步長 # print(x, end=" | ") # # 打印結果:0 | 2 | 4 | 6 | 8 | for x in range(10, 0, -2): print(x, end=" | ") # 打印結果:10 | 8 | 6 | 4 | 2 |
# encoding: utf-8 a = [1, 2, 3, 4, 5, 6, 7, 8] # for i in range(0, len(a), 2): # print(a[i], end=" | ") # 1 | 3 | 5 | 7 | b = a[0:len(a):2] print(b) # [1, 3, 5, 7]
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/44904.html
摘要:基礎之控制結構學習目標代碼塊與縮進條件語句語句語句的嵌套斷言循環循環循環中斷循環控制語句綜合嵌套列表解析式基礎相關鏈接學習目標是簡潔易學面向對象的編程語言。 Py...
閱讀 2733·2021-09-02 15:11
閱讀 905·2019-08-26 18:18
閱讀 1866·2019-08-26 11:57
閱讀 3317·2019-08-23 16:59
閱讀 1993·2019-08-23 16:51
閱讀 2305·2019-08-23 16:11
閱讀 3120·2019-08-23 14:58
閱讀 1106·2019-08-23 11:34