摘要:表達(dá)式表達(dá)式是運(yùn)算符和操作數(shù)所構(gòu)成的序列運(yùn)算符優(yōu)先級(jí)同級(jí)的運(yùn)算符的優(yōu)先級(jí)還是有區(qū)別的比如邏輯運(yùn)算符里的的優(yōu)先級(jí)大于兩個(gè)括號(hào)同級(jí),左結(jié)合出現(xiàn)賦值符號(hào)時(shí),右結(jié)合優(yōu)先級(jí)在文本文件中編寫代碼腳本是后綴名為的文件,通過命令行執(zhí)行推薦的,大型工程適合用
表達(dá)式
表達(dá)式(Expression)是運(yùn)算符(operator)和操作數(shù)(operand)所構(gòu)成的序列
>>> 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運(yùn)算符優(yōu)先級(jí)
同級(jí)的運(yùn)算符的優(yōu)先級(jí)還是有區(qū)別的 比如邏輯運(yùn)算符里的and的優(yōu)先級(jí)大于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) //兩個(gè)括號(hào)同級(jí),左結(jié)合 4 >>> a = 1 >>> b = 2 >>> c = a + b //出現(xiàn)賦值符號(hào)時(shí),右結(jié)合 >>> 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)) //優(yōu)先級(jí):not > and > or False在文本文件中編寫Python代碼
python腳本是后綴名為.py的文件,通過命令行“python filename.py”執(zhí)行
推薦的IDE:PyCharm、vsCode,大型工程適合用PyCharm,學(xué)習(xí)適合用vsCode,vsCode中推薦的插件:python、Terminal、Vim、vscode-icons注釋
單行注釋用#
多行注釋用```
主要有條件控制(if else)、循環(huán)控制(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后面不僅可以是布爾值,還可以是表達(dá)式 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")循環(huán)(while for)
# encoding: utf-8 # 循環(huán) # 循環(huán)語句 # 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 # 主要是用來遍歷/循環(huán) 序列或者集合、字典 # 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的時(shí)候終止,打印出1 # # break # # continue 遇到x==2的時(shí)候跳過,打印出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": # 內(nèi)部循環(huán)跳出后,外部循環(huán)還在執(zhí)行 break print(y) else: print("fruit is gone")
# encoding: utf-8 # for (i=0; i<10; i++){} # 以上的for循環(huán)用python實(shí)現(xiàn) # for x in range(0, 10): # # range(0,10) 表示從0開始的10個(gè)數(shù)字,并不包含10 # print(x) # for x in range(0, 10, 2): # # range(0,10,2) 2表示步長(zhǎng) # print(x, end=" | ") # # 打印結(jié)果:0 | 2 | 4 | 6 | 8 | for x in range(10, 0, -2): print(x, end=" | ") # 打印結(jié)果: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]
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/44904.html
摘要:基礎(chǔ)之控制結(jié)構(gòu)學(xué)習(xí)目標(biāo)代碼塊與縮進(jìn)條件語句語句語句的嵌套斷言循環(huán)循環(huán)循環(huán)中斷循環(huán)控制語句綜合嵌套列表解析式基礎(chǔ)相關(guān)鏈接學(xué)習(xí)目標(biāo)是簡(jiǎn)潔易學(xué)面向?qū)ο蟮木幊陶Z言。 Py...
閱讀 2742·2021-09-02 15:11
閱讀 914·2019-08-26 18:18
閱讀 1872·2019-08-26 11:57
閱讀 3325·2019-08-23 16:59
閱讀 2003·2019-08-23 16:51
閱讀 2312·2019-08-23 16:11
閱讀 3131·2019-08-23 14:58
閱讀 1113·2019-08-23 11:34