diff --git a/404.html b/404.html index 932b0a0..1c0f64b 100755 --- a/404.html +++ b/404.html @@ -462,6 +462,8 @@ + + @@ -718,6 +720,27 @@ + + + + + + +
往下之前,請你回想 Repetition Structures - Nested Loop 中的例子:
+輸入一個正整數 \(n\),輸出 \([1, n]\) 間的最大質數。
+這個程式碼的功能是正確的,但是程式碼的可讀性不高,因為程式碼的長度太長,並且程式碼的邏輯不夠清晰。
+我們可以把判斷質數的部分抽出來,並且將他包裝成一個函式:
+這樣的好處是,我們可以將相同的程式碼重複使用,並且可以讓程式碼更加簡潔。
+那如果我想要進一步程式碼封裝成一個接受正整數 \(n\) 的函式,並且回傳 \([1, n]\) 間的最大質數呢?
+不曉得你有沒有感受到,當程式碼越來越長,我們就越需要函式來幫助我們將程式碼分割成更小的部分,這樣我們就可以更容易的閱讀程式碼。
+這個引子告訴我們,函式是一個可以將程式碼包裝成一個獨立的單位,並且可以重複使用的工具。
+函式的定義是以 def
開頭,後面接著函式的名稱,以及括號內的參數(Parameter),縮排內的程式碼就是函式的內容。
你可以選擇是否要在函式中加上 return
來回傳值,如果沒有,函式則會回傳 None
。
舉個例子,定義一個函式 greet
,他接受一個參數 name
,並印出 Hello, {name}
:
greet("World")
會先印出 Hello, World
,接著回傳 None
。
字串 "World"
被稱為引數(Argument),而 name
則被稱為參數(Parameter)。但也不用太拘泥。
再舉個例子,定義一個函式 freq(x)
接受一個數字字串 x
,回傳 0-9
的出現次數,以序對的方式回傳:
Output | |
---|---|
我們可以在參數後面加上 :
來指定參數的型別,並且在函式後面加上 ->
來指定回傳值的型別。
有時候我們會希望函式的參數有預設值,這樣在呼叫函式時就不需要填入引數。
+例如,函式 max_of(x, k=1)
接受兩個參數 x
串列與 k
數字,回傳 x
中最大的 k
個數字,以串列的方式回傳,而 k
的預設值為 1
:
再舉一個例子,函式 weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5)
接受三個參數 math
, english
, programming
以及三個預設值 math_weight=0.25
, english_weight=0.25
, programming_weight=0.5
,回傳加權分數:
Output | |
---|---|
有預設值的參數必須放在沒有預設值的參數後面。但如果你在呼叫函式的時候,指定了參數的名稱,則可以不用遵守這個規則。
+其實還有一些神奇的用法,但就不在這裡討論了。
+還記得 map
嗎? 他可以將一個函式應用到一個串列上。
先回憶一下,我們可以這樣使用 map
:
I Really Want to Stay At Your House” by Rosa Walton
+這種接受函式為參數的函式被稱為高階函式(Higher-Order Function)。
+但是,如果函式只會被使用一次,我們可以使用 lambda
來定義一個匿名函式:
Output | |
---|---|
再舉個例子,用過 min
嗎? 他可以找出一個串列中的最小值,我們也可以自訂規則,例如有一個座標串列 points
,我們可以這樣找出最靠近原點的點:
Output | |
---|---|
再舉個例子,兩個串列 a
與 b
,我們可以這樣計算兩個串列的內積:
Output | |
---|---|
有時候我們會希望函式回傳另一個函式,這樣的函式被稱為工廠函式(Factory Function)。
+舉個例子,定義一個函式 make_power_fun(n)
,他接受一個數字 n
,回傳一個函式,這個函式接受一個數字 x
,回傳 x
的 n
次方:
這樣的好處是,我們可以在不同的地方使用不同的 n
,而不需要重複定義函式。
函式可以被定義在另一個函式的內部,這樣的函式被稱為巢狀函式(Nested Function)。
+例如,函式 collect_anagrams(words, target)
接受一個字串串列 words
與一個字串 target
,回傳 words
中與互為 target
是相同字母異序詞的串列:
於是我在函式的內部先定義了一個輔助函式(Helper Function) is_anagram(x, y)
,他接受兩個字串 x
與 y
,回傳 x
與 y
是否為相同字母異序詞:
Output | |
---|---|
這個在刷 LeeCode 時會常常用到。
+來到本章的重頭戲,遞迴(Recursion)。
+遞迴是一種函式呼叫自己的技巧,這樣的函式被稱為遞迴函式(Recursive Function)。
+先來個經典的例子,計算 \(n!\),迴圈的寫法是這樣的:
+記得高中數學課本上的定義嗎?
+這樣的定義就可以直接翻譯成程式碼:
+當 n == 0
時,我們稱為基本情況(Base Case),這是遞迴的終止條件,當 n > 0
時,我們稱為遞迴情況(Recursive Case),這是遞迴的執行條件。
每一個遞迴函式都應該有一個基本情況,這樣的遞迴才會終止,否則就會陷入無窮遞迴,咦?有沒有很熟悉?還記得無窮迴圈嗎?
+再舉個例子,計算費氏數列的第 \(n\) 項,迴圈的寫法是這樣的:
+同樣的,還記得高中數學課本上的定義嗎?
+這樣的定義就可以直接翻譯成程式碼:
+但是,遞迴的效率通常比迴圈低,因為遞迴會造成大量的函式呼叫,而且容易造成大量的重複計算。
+再來一個經典的例子,計算整數 a, b
的最大公因數,你可能有聽過輾轉相除法,又稱為歐幾里得演算法(Euclidean algorithm),他的定義是這樣的:
直接寫成程式碼:
+這個要寫成迭代(Iterative)版本還比較難。
+關於遞迴,就先講到這裡,未來進入演算法的章節時,會再深入討論,而且會有更多的例子。
+Itsa - [C_MM48-易] F91
+你仔細觀察一下,其實可以寫成這樣:
+ +Itsa - [C_RU13-易] 大一點的Fibonacci
+Itsa - [C_MM103-易] 費式數列
+Itsa - [C_MM143-易] 求Emirp
+Itsa - [C_MM144-易] 求組合數C(n,r)
+Itsa - 題目10. 輾轉相除法
+Itsa - [C_RU10-中] 爬樓梯
+Itsa - [C_RU14-易] 好高的水晶塔
+\u9ece\u660e\u8cc7\u8a0a\u793e(LMcps)\u5275\u7acb\u65bc2018\u5e74\uff0c\u6307\u5c0e\u8005\u70ba\u674e\u60e0\u6587\u8001\u5e2b\u3002
\u672c\u66f8\u7684\u6b63\u78ba\u6253\u958b\u65b9\u5f0f\u662f\u6697\u8272\u6a21\u5f0f\u3002
print(\"Hello LMcps Book!\")\nprint(\"Welcome to LMcps!\")\n
"},{"location":"#why_does_it_start","title":"Why does it start?","text":"\u57282024/1/25\uff0c\u662f\u75ab\u60c5\u5f8c\uff0c\u7b2c\u4e00\u6b21\u6b77\u5c46\u793e\u54e1\u8fd4\u56de\u6bcd\u6821\u8207\u5b78\u5f1f\u59b9\u5011\u5206\u4eab\u7d93\u9a57\u7684\u65e5\u5b50\uff0c\u5728\u8207\u8001\u5e2b\u53ca\u5b78\u5f1f\u59b9\u5011\u4ea4\u6d41\u5f8c\uff0c\u767c\u73fe\u4e86\u76ee\u524d\u8cc7\u8a0a\u6559\u80b2\u7684\u7a98\u56f0\uff0c\u8af8\u5982\u7db2\u8def\u4e0a\u7684\u8cc7\u6e90\u96d6\u591a\uff0c\u4f46\u4e0d\u6613\u5165\u9580\uff0c\u65bc\u662f\u6c7a\u5b9a\u5beb\u4e00\u672c\u66f8\uff0c\u4ee5\u5927\u91cf\u7684\u5716\u7247\u53ca\u7c21\u55ae\u7684\u6587\u5b57\uff0c\u8b93\u5b78\u5f1f\u59b9\u5011\u80fd\u5920\u5feb\u901f\u5165\u9580\uff0c\u4e26\u4e14\u80fd\u5920\u5728\u77ed\u6642\u9593\u5167\uff0c\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\u8207\u81ea\u4fe1\u3002
"},{"location":"#content","title":"Content","text":"\u9019\u672c\u66f8\u8457\u91cd\u65bc\u61c9\u7528\u9762\uff0c\u4e26\u4e14\u5be6\u4f5c\u70ba\u4e3b\uff0c\u4e5f\u4e26\u975e\u5e36\u4f60\u6253\u7af6\u7a0b\uff0c\u800c\u662f\u5e36\u4f60\u4e86\u89e3\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u3002\u5be6\u4f5c\u8a9e\u8a00\u4ee5Python\u70ba\u4e3b\uff0c\u6703\u6709\u5c11\u6578C++\u7684\u5be6\u4f5c\u3002
"},{"location":"#wait_to_be_done","title":"Wait to be done","text":"MIT License
Copyright (c) 2023 Cheung4843
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"},{"location":"blog/","title":"Blog","text":""},{"location":"blog/2024/02/06/hello-blog-/","title":"Hello Blog !","text":"Hi, this is a test post.
For the better experience of the development, I think recording the process of the development is a good idea.
If you are interested in the project, you can follow the blog to get the latest information about the project.
Also, plaease contact me if you wanna join the project. I am looking forward to working with you.
"},{"location":"fundamental/","title":"Fundamental","text":"\u9019\u88e1\u5c07\u6703\u6559\u4f60\u57fa\u790e\u7684Python\u8207C++\u7684\u57fa\u790e\u8a9e\u6cd5\u3002
"},{"location":"fundamental/cpp/","title":"C++","text":"\u6211\u662fc++\u7cfb\u5217\u7684\u7b46\u8005bloodnighttw\uff0c\u6211\u9810\u8a08\u5728\u6b64\u6587\u7ae0\u5206\u4e0b\u5217\u5e7e\u500b\u7ae0\u7bc0\uff0c\u4e26\u76e1\u91cf\u4ee5\u521d\u5b78\u8005\u7684\u89d2\u5ea6\u5beb\u51fa\u5167\u5bb9\u3002
\u5982\u679c\u5728\u64b0\u5beb\u4e0a\u6709\u4efb\u4f55\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u5bc4\u96fb\u5b50\u90f5\u4ef6\u5230 bbeenn1227@gmail.com
\u6216\u8005 emails@bntw.dev
\u4f86\u505a\u8a62\u554f(\u4e0d\u904e\u8acb\u8868\u660e\u4f60\u7684\u8eab\u4efd\uff0c\u4e0d\u7136\u6211\u53ef\u80fd\u6703\u5ffd\u7565)\uff0c\u6216\u8005\u53ef\u4ee5\u5728\u6211\u66f4\u5e38\u51fa\u73fe\u7684discord\u8a62\u554f\u6211\u3002
\u6b64\u9023\u7d50\u70ba\u9ece\u660e\u8cc7\u8a0a\u793e\u7684discord\u7fa4\u7d44\u9023\u7d50\uff0c\u52a0\u5165\u4e4b\u5f8c\u53ef\u4ee5tag @bloodnighttw
\uff0c\u53ea\u8981\u6211\u5728\u7dda\u4e0a\u4e14\u6709\u6642\u9593\u6211\u90fd\u6703\u76e1\u91cf\u5e6b\u4f60\u56de\u7b54\u3002
\u53e6\u5916\u6709\u4efb\u4f55\u932f\u5b57\u6216\u53ef\u4ee5\u6539\u5584\u7684\u5730\u65b9\u8acb\u767cissues\uff0c\u6211\u5011\u6703\u518d\u8a0e\u8ad6\u904e\u5f8c\u9069\u6642\u5730\u505a\u51fa\u4fee\u6b63\u3002
"},{"location":"fundamental/cpp/create_env/","title":"0. \u74b0\u5883\u67b6\u8a2d","text":"\u6b63\u6240\u8b02\u5de5\u6b32\u5584\u5176\u4e8b\uff0c\u5fc5\u5148\u5229\u5176\u5668\uff0c\u6b64\u7ae0\u6211\u5011\u5206\u5225\u6703\u5728\u5206\u5225\u8b1b\u8ff0\u5982\u4f55\u5728windows\u3001macos\u4e0a\u5b89\u88dd\u76f8\u95dc\u5957\u4ef6\u3002
\u6211\u5011\u9810\u8a08\u8981\u4f7f\u7528GNU C++ Compiler\u9032\u884c\u7de8\u8b6f\uff0c\u4ee5\u53ca\u4f7f\u7528Visual studio code + clangd\u8207c++11
\u7684\u6a19\u6e96\u64b0\u5beb\u7a0b\u5f0f\uff0c\u4e0b\u9762\u6703\u8a73\u8ff0\u5b89\u88dd\u6d41\u7a0b\u3002
\u7576\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u50cf\u662fdev c++
\u3001codeblock
\u9019\u985e\u7684\u8edf\u9ad4\u9032\u884c\u64b0\u5beb\uff0c\u53ea\u4e0d\u904e\u6709\u4e00\u9ede\u9700\u8981\u6ce8\u610f\uff0c\u820a\u7248\u7684dev c++
\u9810\u8a2d\u6c92\u6709\u555f\u7528c++11\u6a19\u6e96\u7684\u652f\u63f4\uff0c\u4f60\u5fc5\u9808\u8981\u5728\u8a2d\u5b9a\u88e1\u9762\u5c0b\u627e\u958b\u555f\u7684\u65b9\u5f0f\u3002
GNU C++ Compiler (G++)\u672c\u8eab\u662f\u6c92\u6709\u652f\u63f4Windows\u7684\uff0c\u4f46\u6211\u5011\u53ef\u4ee5\u900f\u904e\u4f7f\u7528wsl2\u6216\u8005\u5225\u4eba\u7528\u597d\u7684\u79fb\u690d\u7248\u672c\u9032\u884c\u4f7f\u7528\uff0c\u9019\u908a\u6211\u5011\u6703\u5b89\u88dd\u5225\u4eba\u5beb\u597d\u7684\u79fb\u690d\u7248\u3002
"},{"location":"fundamental/cpp/create_env/#1mingw","title":"1.\u4e0b\u8f09\u4e26\u5b89\u88ddmingw","text":"mingw32-gcc-g++
installation
> apply change
\u6309\u4e0bapply
\u5b89\u88dd
C:\\MinGW
\u88e1\u9762\uff0c\u627e\u5230\u88e1\u9762\u7684bin
\u8cc7\u6599\u593e\uff0c\u9ede\u9032\u53bb\u4e26\u8907\u88fd\u8def\u5f91\u3002 \u9ede\u5165 \u9032\u968e
>\u74b0\u5883\u8b8a\u6578
\uff0c\u4e26\u5728Path\u4e2d\u8cbc\u4e0a\u525b\u624d\u8907\u88fd\u7684\u8b8a\u6578\uff0c\u7136\u5f8c\u4fdd\u5b58\u9000\u51fa\u3002
\u958b\u555f\u63d0\u793a\u547d\u4ee4\u5b57\u5143\u5f8c\uff0c\u6253\u5165g++ -v
\uff0c\u5982\u679c\u6709\u7248\u672c\u8a0a\u606f\u4ee3\u8868\u5b89\u88dd\u6210\u529f\u3002
macos\u672c\u8eab\u5c31\u6709\u63d0\u4f9bclangd\u4f5c\u70ba\u7de8\u8b6f\u5668\u4f7f\u7528\uff0c\u5728\u5b89\u88ddxcode\u5f8c\u61c9\u8a72\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982\u679c\u9084\u662f\u9700\u8981GNU C++ Compiler\uff0c\u8acb\u53c3\u8003\u4e0b\u9762\u6b65\u9a5f\u3002
"},{"location":"fundamental/cpp/create_env/#1homwbrew","title":"1.\u5b89\u88ddhomwbrew\u5957\u4ef6\u7ba1\u7406\u5668","text":"\u6839\u64da\u5b98\u7db2\u7684\u6307\u793a\u5b89\u88ddhomebrew\u3002
"},{"location":"fundamental/cpp/create_env/#2g","title":"2.\u4f7f\u7528\u7ba1\u7406\u5668\u5b89\u88ddg++\u5957\u4ef6","text":"brew search gcc
\u641c\u5c0b\u76f8\u95dc\u5957\u4ef6brew install gcc
\u5b89\u88ddCompilergcc-13 -v
\u6216g++-13
\u78ba\u8a8d\u662f\u5426\u6210\u529f\u5b89\u88ddWarning
\u8acb\u6ce8\u610f\uff0c\u5728macos\u4e0a\u9810\u8a2d\u4f7f\u7528\u7684g++
\u6307\u4ee4\u70baclang
\u800c\u4e0d\u662fGNU C++ Compiler
\uff0c\u8acb\u6539\u4f7f\u7528g++-XX
\u4f86\u64cd\u4f5cg++\u7de8\u8b6f\u5668\u3002
\u524d\u5f80\u9019\u500b\u7db2\u7ad9\u4e0b\u8f09\u5c6c\u65bc\u4f60\u7cfb\u7d71\u7684visual studio code\u5b89\u88dd\u6a94\u3002
"},{"location":"fundamental/cpp/create_env/#2_extension","title":"2. \u5b89\u88dd extension","text":"C/C++
clangd
\u9019\u5169\u500bextension\uff0c\u4e26\u91cd\u555fvscode Disable Intellisence
\u5982\u679c\u9047\u5230\u5b57\u9ad4\u975e\u7b49\u5bec\uff0c\u8acb\u5c0b\u627e\u4e00\u500b\u7b49\u5bec\u5b57\u9ad4\u4e26\u4f7f\u7528\uff0c\u7b49\u5bec\u5b57\u9ad4\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002
"},{"location":"fundamental/cpp/hello_world/","title":"1. Hello World","text":""},{"location":"fundamental/cpp/hello_world/#hello_world","title":"Hello World (\u8f38\u51fa)","text":""},{"location":"fundamental/cpp/hello_world/#_1","title":"\u521d\u5b78\u8005\u7684\u7b2c\u4e00\u6b65","text":"\u9996\u5148\uff0c\u8acb\u5728vscode (Visual studio code)\u4e2d\u65b0\u589e\u4e00\u500b\u540d\u70bahello_world.cpp\uff0c\u4e26\u6253\u5165\u4e0b\u5217\u7a0b\u5f0f\u78bc\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\" << endl;\n return 0;\n}\n
\u78ba\u8a8d\u597d\u5b58\u6a94\u904e\u5f8c\uff0c\u6309\u4e0b\u53f3\u4e0a\u89d2\u7684\u57f7\u884c\u6309\u9375\uff0c\u4f60\u5c31\u6703\u6210\u529f\u770b\u5230\u4f60\u5beb\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\u3002 \u63a5\u8457\uff0c\u6211\u5011\u5c07\u66f4\u6df1\u5165\u8a0e\u8ad6\u9019\u6bb5\u7a0b\u5f0f\uff0c\u5230\u5e95\u767c\u751f\u4e86\u4ec0\u9ebc\u4e8b\u60c5\u3002
"},{"location":"fundamental/cpp/hello_world/#hello_world_1","title":"Hello World\u7a0b\u5f0f\u89e3\u91cb","text":"\u5728\u9019\u6bb5\u7a0b\u5f0f\u4e2d\uff0c\u5df2\u7d93\u6709\u8a31\u591a\u7a0d\u5fae\u8907\u96dc\u7684\u6982\u5ff5\uff0c\u5148\u5225\u6015\uff0c\u6211\u5011\u5c31\u53ea\u8981\u4e86\u89e3\u5230:
int main(){......}
\u88e1\u9762\u958b\u59cb\u57f7\u884c\u7684return 0;
\u4ee3\u8868\u7a0b\u5f0f\u6210\u529f\u7d50\u675f\u3002\u8b93\u6211\u5011\u7126\u9ede\u653e\u5728\u7b2c\u516d\u884c:
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\" << endl;\n return 0;\n}\n
\u9019\u908a\u6709\u4e86\u6211\u5011\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\uff0c\u4f60\u53ef\u4ee5\u628acout
\u7576\u6210\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\uff0c\u628a<<
\u7576\u6210\u50b3\u905e\u7684\u7bad\u982d\uff0c\u800c\u9019\u6bb5\u7a0b\u5f0f\u5c31\u662f\u8981\u628a\u9019\u6bb5\u5b57\u50b3\u7d66\u96fb\u8166\u5370\u51fa\u3002
\u9019\u908a\u7684endl
\u4ee3\u8868\u8457\u63db\u884c\u7684\u610f\u601d\uff0c\u900f\u904e<<
\u50b3\u7d66\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\u3002
\u9019\u908a\u63d0\u4f9b\u53e6\u5916\u4e00\u500b\u63db\u884c\u7684\u5beb\u6cd5\uff0c\u5177\u9ad4\u5dee\u7570\u5728\u719f\u6089\u8a9e\u6cd5\u5f8c\uff0c\u6703\u5728\u53e6\u5916\u4e00\u7bc7\u6587\u7ae0\u505a\u8aaa\u660e\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\\n\";\n return 0;\n}\n
\\n
\u70ba\u63db\u884c\u5b57\u5143\uff0c\u5728\u5b57\u5143\u4e2d\u6709\u8a31\u591a\u5b57\u5143\u6703\u6709\u7279\u5b9a\u529f\u7528\uff0c\u6703\u4ee5\\
\u958b\u982d\uff0c\u9023\u7d50\u4e00\u500b\u5b57\u6bcd\u6216\u6578\u5b57\uff0c\u4ee3\u8868\u8457\u8df3\u812b\u5b57\u5143\uff0c\u4f5c\u70ba\u7279\u6b8a\u7528\u9014\u4f7f\u7528\u3002 \u7d30\u5fc3\u5982\u4f60\uff0c\u9019\u662f\u53c8\u8981\u63d0\u554f\u4e86\uff0c\u5206\u865f;
\u53c8\u4ee3\u8868\u4ec0\u9ebc\u610f\u601d\uff1f
\u5206\u865f;
\u662f\u544a\u8a34\u96fb\u8166\u8aaa\uff0c\u9019\u4e9b\u6307\u4ee4\u8ddf\u4e0b\u4e00\u500b\u5b57\u5143\u6216\u4e0b\u4e00\u884c\u662f\u5206\u958b\u7684\uff0c\u4e0d\u8981\u628a\u4ed6\u9023\u5728\u4e00\u8d77\uff0c\u4e5f\u56e0\u70ba\u6709\u9019\u6771\u897f\uff0c\u6211\u5011\u7a0b\u5f0f\u53ef\u4ee5\u9019\u6a23\u5beb\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\\n\"; cout << endl;\n return 0;\n}\n
Danger
\u5341\u5206\u4e0d\u5efa\u8b70\u4f60\u9019\u6a23\u5beb\uff0c\u5c0d\u65bc\u7a0b\u5f0f\u719f\u7df4\u7684\u4eba\u4f86\u8aaa\uff0c\u9019\u6a23\u7684\u6392\u7248\uff0c\u7a0b\u5f0f\u78bc\u8b80\u8d77\u4f86\u6703\u975e\u5e38\u75db\u82e6\uff0c\u5c0d\u65bc\u521d\u5b78\u8005\uff0c\u5247\u5bb9\u6613\u8aa4\u89e3\u7a0b\u5f0f\u78bc\u3002
"},{"location":"fundamental/cpp/hello_world/#_2","title":"\u52a0\u5165\u8b8a\u6578 (\u8f38\u5165)","text":""},{"location":"fundamental/cpp/hello_world/#_3","title":"\u8b80\u53d6\u5b57\u4e32","text":"#include <iostream>\n\nusing namespace std;\n\nint main(){\n string str;\n cin >> str;\n cout << str << endl;\n return 0;\n}\n
\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u4e32\u6587\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u6587\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002
"},{"location":"fundamental/cpp/hello_world/#_4","title":"\u8b80\u53d6\u6578\u5b57","text":"\u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23:
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp;\n cin >> temp;\n cout << temp << endl;\n return 0;\n}\n
\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u500b\u6578\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u4e00\u4e32\u6578\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002
Info
\u5982\u679c\u4eca\u5929\u6253\u5165\u7684\u6578\u5b57\u975e\u5e38\u975e\u5e38\u7684\u5927\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u5370\u51fa\u4f86\u7684\u6578\u5b57\u8ddf\u6253\u5165\u7684\u6578\u5b57\u4e0d\u4e00\u6a23\uff0c\u5176\u539f\u56e0\u662f\u6ea2\u4f4d\uff0c\u9019\u9ede\u6211\u5011\u6703\u5728\u4e0b\u4e00\u7ae0\u8ac7\u5230\u5176\u539f\u56e0\u8207\u89e3\u6cd5\u3002
"},{"location":"fundamental/cpp/hello_world/#_5","title":"\u8f38\u5165\u7a0b\u5f0f\u89e3\u91cb","text":"\u6b63\u5982cout
\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u628acin
\u7576\u6210\u4f60\u7684\u9375\u76e4\uff0c\u900f\u904e>>
\u544a\u8a34\u96fb\u8166\u5f9e\u9375\u76e4\u8f38\u5165\u81f3\u53f3\u908a\u7684\u8b8a\u6578\u3002
\u5e38\u898b\u7684\u8aa4\u5340
\u5c0d\u65bc\u521d\u5b78\u8005\u4f86\u8aaa\uff0c\u53ef\u80fd\u6703\u4e0d\u5c0f\u5fc3\u5beb\u51fa\u9019\u6a23\u7684\u7a0b\u5f0f\uff1a
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp1,temp2;\n cin << temp1 << temp2;\n cout << temp1 << \"!=\" << temp2 << endl;\n return 0;\n}\n
\u6b64\u6642\u4f60\u6703\u767c\u73fe\u7121\u6cd5\u7de8\u8b6f\uff0c\u5176\u539f\u56e0\u662f\u4f60\u7684cin
\u7684<<
\u61c9\u8a72\u8981\u662f>>
\u624d\u5c0d \u3002
\u5e38\u898b\u7684\u932f\u8aa4\u4e5f\u5305\u62ec\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a
cout
\u7684<<
\u65b9\u5411\u932f\u8aa4 #include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp1,temp2;\n cin >> temp1 >> temp2;\n cout >> temp1 >> temp2;\n return 0;\n}\n
cout
\u7684\u4f4d\u7f6e\u932f\u8aa4
#include <iostream>\n\nusing namespace std;\n\nint main(){\n \"error\" >> cout;\n return 0;\n}\n
cout
\u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002 cin
\u7684\u4f4d\u7f6e\u932f\u8aa4
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int a;\n a << cin;\n return 0;\n}\n
\u539f\u56e0\u540c\u4e0a\uff0ccin
\u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002 \u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u4e00\u9023\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp1;\n string temp2;\n cin >> temp1 >> temp2;\n cout << temp1 << \"!=\" << temp2 << endl;\n return 0;\n}\n
input1234 4321\n
output1234!=4321\n
\u4f60\u6703\u767c\u73fe\u4e00\u4ef6\u4e8b\u60c5\uff0ccin
\u662f\u900f\u904e\u7a7a\u683c\u6216\u8005\u63db\u884c\u4f86\u5206\u958b\u7684\uff0c\u4eca\u5929\u5982\u679c\u60f3\u8981\u8b80\u53d6\u4e00\u884c\uff0c\u8acb\u53c3\u8003\u4e0b\u65b9\u7a0b\u5f0f\u3002
\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff0c\u5176\u4e2dtemp2\u8acb\u6539\u6210\u8b8a\u6578\u7684\u540d\u7a31\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n string temp2;\n getline(cin,temp2);\n cout << temp2 << endl;\n return 0;\n}\n
"},{"location":"fundamental/cpp/hello_world/#practice","title":"Practice","text":"\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:
Info
\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002
ZeroJudge - a001. \u54c8\u56c9
Reference code#include <iostream>\n\nint main(){\n string word;\n cin >> word;\n cout << \"hello, \" << word << endl;\n\n return 0;\n}\n
"},{"location":"fundamental/python/","title":"Python","text":"\u55e8\uff0c\u6211\u662f @cheung4843\uff0c\u662fPython\u7cfb\u5217\u7684\u4f5c\u8005\u3002\u5728\u4e00\u958b\u59cb\u57fa\u790e\u8a9e\u6cd5\u7684\u90e8\u5206\u6703\u6559\u5f97\u7279\u5225\u8a73\u7d30\uff0c\u6587\u7ae0\u4e2d\u7684\u5c08\u6709\u540d\u8a5e\u5c07\u6703\u4ee5\u9ec3\u8272\u8207\u7da0\u8272\u6a19\u793a\uff0c\u8acb\u4f60\u7a0d\u5fae\u6ce8\u610f\u4e00\u4e0b\uff0c\u82e5\u4e0d\u61c2\u4e5f\u6c92\u95dc\u4fc2\uff0c\u6211\u6703\u5728\u5f8c\u9762\u7ae0\u7bc0\u518d\u6b21\u63d0\u5230\u3002
\u5728\u6bcf\u4e00\u500b\u5c0f\u7bc0\u7684\u5f8c\u9762\u90fd\u6703\u6709\u7b46\u8a18\u5340\u4ee5\u53ca\u554f\u984c\u5340\uff0c\u5c24\u5176\u662f\u554f\u984c\u5340\uff0c\u8acb\u4f60\u597d\u597d\u601d\u8003\u3002\u800c\u5728\u6bcf\u4e00\u7bc7\u6587\u7ae0\u6700\u5f8c\u90fd\u6703\u653e\u4e0a\u4e00\u4e9b\u7df4\u7fd2\u984c\uff0c\u8acb\u5617\u8a66\u5b8c\u6210\u4ed6\u5011!
"},{"location":"fundamental/python/dictionaries/","title":"Dictionaries","text":""},{"location":"fundamental/python/dictionaries/#introduction","title":"Introduction","text":"\u4f86\u4ecb\u7d39\u672c\u4e3b\u984c\u4e2d\u6700\u5f8c\u4e00\u500b\u8cc7\u6599\u7d50\u69cb\uff1a\u5b57\u5178(Dictionaries)\uff0c\u662f\u7531\u9375(Key)\u548c\u503c(Value)\u6240\u7d44\u6210\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u7d93\u7531\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u9375\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u800c\u503c\u5247\u53ef\u4ee5\u662f\u4efb\u4f55\u578b\u614b\u7684\u8cc7\u6599\u3002
Question
\u7559\u7d66\u4f60\u601d\u8003\uff0c\u4f60\u4e00\u5b9a\u77e5\u9053\u7b54\u6848\uff0c\u5c31\u7b97\u4f60\u6c92\u8fa6\u6cd5\u99ac\u4e0a\u77e5\u9053\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5beb\u7a0b\u5f0f\u4f86\u6e2c\u8a66\u3002
"},{"location":"fundamental/python/dictionaries/#create_a_dictionary","title":"Create a Dictionary","text":"\u4f60\u53ef\u4ee5\u7528{}
\u3001dict()
\u6216\u8005 Comprehension \u4f86\u5efa\u7acb\u4e00\u500b\u5b57\u5178\u3002
scores = {\"Compiler\": 100, \"AWS\": 95, \"Data Science\": 92}\nprint(scores)\n\n# when keys are strings, using keyword arguments\ndirections = dict(North=\"\u2191\", South=\"\u2193\", East=\"\u2192\", West=\"\u2190\")\nprint(directions)\n\ncapital = dict([(\"Taiwan\", \"Taipei\"), (\"Japan\", \"Tokyo\"), (\"Korea\", \"Seoul\")])\nprint(capital)\n\n# comprehension\ncube = {x: x ** 3 for x in range(-2, 3)}\nprint(cube)\n
Output{'Compiler': 100, 'AWS': 95, 'Data Science': 92}\n{'North': '\u2191', 'South': '\u2193', 'East': '\u2192', 'West': '\u2190'}\n{'Taiwan': 'Taipei', 'Japan': 'Tokyo', 'Korea': 'Seoul'}\n{-2: -8, -1: -1, 0: 0, 1: 1, 2: 8}\n
\u90a3\u70ba\u4ec0\u9ebc\u8aaa\u5b57\u5178\u662f\u6709\u5e8f\u7684\u5462?\u4f60\u591a\u57f7\u884c\u5e7e\u6b21\uff0c\u8f38\u51fa\u7684\u9806\u5e8f\u8207\u4f60\u5efa\u7acb\u7684\u9806\u5e8f\u662f\u4e00\u6a23\u7684\u55ce?
"},{"location":"fundamental/python/dictionaries/#operations","title":"Operations","text":""},{"location":"fundamental/python/dictionaries/#accessing_and_modifying_elements","title":"Accessing and Modifying elements","text":"\u4f60\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u4e5f\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u4fee\u6539\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u6703\u5f97\u5230 KeyError
\u3002
recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"King Gnu\"] = \"SPECIALZ\"\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"HEALTH\"] = \"Blue Monday\"\nprint(recent_listening[\"HEALTH\"])\n\nprint(recent_listening[\"ALI\"])\n
Output\u98db\u884c\u8247\nSPECIALZ\nBlue Monday\nKeyError: 'ALI'\n
King Gnu - \u98db\u884c\u8247
Blue Monday
\u90a3\u9ebc\u8a72\u5982\u4f55\u6aa2\u67e5\u9375\u662f\u5426\u5b58\u5728\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 in
\u4f86\u6aa2\u67e5\uff0c\u4f86\u907f\u514d\u932f\u8aa4\u3002
recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\n\nprint(\"King Gnu\" in recent_listening)\nprint(\"ALI\" in recent_listening)\n
OutputTrue\nFalse\n
Question
\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u5b57\u5178\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u9375\u5b58\u5728?
"},{"location":"fundamental/python/dictionaries/#removing_elements","title":"Removing elements","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 del
\u4f86\u522a\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002
\u4f8b\u5982\u6211\u9019\u9663\u5b50\u90fd\u4e0d\u5beb Java \u4e86\uff0c\u6240\u4ee5\u628a Java \u5f9e\u6211\u7684\u5fc3\u4e2d\u522a\u9664\u3002
my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\nprint(my_love)\n\ndel my_love[\"Java\"]\nprint(my_love)\n
Output{'python': 100, 'Java': 70, 'Js': 60}\n{'python': 100, 'Js': 60}\n
"},{"location":"fundamental/python/dictionaries/#iterating","title":"Iterating","text":"\u76f4\u63a5\u4f86\u770b\u4f8b\u5b50\u5427\u3002
my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love:\n print(key, my_love[key])\n
Outputpython 100\nJava 70\nJs 60\n
\u5982\u679c\u4f60\u53ea\u60f3\u8981\u53d6\u5f97\u9375\u6216\u8005\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 keys()
\u6216\u8005 values()
\u3002
my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love.keys():\n print(key)\n\nprint(my_love.keys())\n\nfor value in my_love.values():\n print(value)\n\nprint(list(my_love.values()))\n
Outputpython\nJava\nJs\ndict_keys(['python', 'Java', 'Js'])\n100\n70\n60\n[100, 70, 60]\n
\u90a3\u5982\u679c\u4f60\u60f3\u8981\u540c\u6642\u53d6\u5f97\u9375\u548c\u503c\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 items()
\u3002
my_cat = {\"name\": \"Fat Orange\", \"age\": 12, \"is_cute\": True}\n\nfor key, value in my_cat.items():\n print(f\"{key}: {value}\")\n
Outputname: Fat Orange\nage: 12\nis_cute: True\n
\u6211\u611b\u6211\u7684\u6a58\u8c93\u3002
"},{"location":"fundamental/python/dictionaries/#methods","title":"Methods","text":""},{"location":"fundamental/python/dictionaries/#get","title":"get","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 get()
\u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u5f88\u65b9\u4fbf\u5594\u3002
my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info['name'])\nprint(my_info.get('hobbies')[1])\n\n# print(my_info['is_handsome']) # This will cause an error\nprint(my_info.get('is_handsome', 'Of course I am!'))\n
OutputSean\nworking out\nOf course I am!\n
\u5e0c\u671b\u4f60\u4e0d\u8981\u89ba\u5f97\u6211\u81ea\u6200\u3002
"},{"location":"fundamental/python/dictionaries/#pop","title":"pop","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 pop()
\u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u540c\u6642\u522a\u9664\u8a72\u9375\u503c\u5c0d\u3002
my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info.pop('age'))\nprint(my_info)\n
Output20\n{'name': 'Sean', 'hobbies': ['coding', 'working out']}\n
"},{"location":"fundamental/python/dictionaries/#clear","title":"clear","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 clear()
\u4f86\u6e05\u7a7a\u5b57\u5178\u3002
my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nmy_info.clear()\nprint(my_info)\n
Output{}\n
umm... \u518d\u904e\u4e0d\u4e45\u6211\u5c31\u8981 21 \u6b72\u4e86\u3002
\u6211\u8a8d\u70ba\u9019\u4e9b\u65b9\u6cd5\u5c31\u5920\u7528\u4e86\uff0c\u5176\u4ed6\u7684\u4f60\u53ef\u4ee5\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u56c9~
"},{"location":"fundamental/python/dictionaries/#practice","title":"Practice","text":"Itsa - [C_ST19-\u6613] \u6642\u9593\u8f49\u63db
Reference coden = int(input())\n\ntime_diff = {\"TW\": 0, \"JA\": 60, \"USE\": -720, \"USC\": -780, \"USW\": -840, \"UK\": -480}\n\nfor _ in range(n):\n time, cur, to = input().split()\n minutes = int(time[:2]) * 60 + int(time[3:])\n minutes += time_diff[to] - time_diff[cur]\n\n if minutes < 0:\n minutes += 1440\n minutes %= 1440\n\n h, m = divmod(minutes, 60)\n print(f\"{h:02d}{m:02d} {to}\")\n
\u4e00\u5f8b\u5148\u8f49\u63db\u6210\u5206\u9418\uff0c\u518d\u9032\u884c\u6642\u5340\u8f49\u63db\uff0c\u6700\u5f8c\u518d\u8f49\u63db\u6210\u5c0f\u6642\u548c\u5206\u9418\u3002 divmod
\u56de\u50b3\u4e00\u500b\u5e8f\u5c0d\uff0c\u5546\u8207\u9918\u6578\u3002
Itsa - [C_AR111-\u6613] \u5c0d\u8a71\u6a5f\u5668\u4eba
Itsa - [C_AR152-\u6613] \u6b63\u6574\u6578\u7d71\u8a08
Tipsorted()
and key
Itsa - [C_AR42-\u6613] \u904e\u534a\u5143\u7d20
Itsa - [C_AR188-\u6613] \u9663\u5217\u5143\u7d20
Tip\u9664\u4e86\u7528\u5b57\u5178\u4f86\u89e3\u984c\u4e4b\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5b78 Boyer\u2013Moore majority vote algorithm
"},{"location":"fundamental/python/functions/","title":"Functions","text":""},{"location":"fundamental/python/functions/#introduction","title":"Introduction","text":""},{"location":"fundamental/python/lists/","title":"Lists","text":""},{"location":"fundamental/python/lists/#introduction","title":"Introduction","text":"\u4e0d\u66c9\u5f97\u4f60\u770b\u5230\u9019\u88e1\u6709\u6c92\u6709\u767c\u73fe\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u90fd\u662f\u7528\u4e00\u500b\u4e00\u500b\u7684\u8b8a\u6578\u4f86\u5132\u5b58\u8cc7\u6599\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u8981\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u9019\u6a23\u7684\u65b9\u5f0f\u5c31\u6703\u8b8a\u5f97\u5f88\u9ebb\u7169\u3002
\u6240\u4ee5\u9019\u88e1\u8ddf\u4f60\u4ecb\u7d39 List(\u4e32\u5217)\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u800c\u4e14\u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u4f8b\u5982: \u6578\u5b57\u3001\u5b57\u4e32\u3001\u5e03\u6797\u503c\u7b49\u7b49\u3002
"},{"location":"fundamental/python/lists/#create_a_list","title":"Create a list","text":"\u8981\u5efa\u7acb\u4e00\u500b List\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []
\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u6211\u5011\u8981\u5132\u5b58\u7684\u8cc7\u6599\uff0c\u8cc7\u6599\u4e4b\u9593\u7528\u9017\u865f ,
\u9694\u958b\u3002
\u4f86\u8209\u500b\u4f8b\u5b50\uff0c\u5efa\u7acb\u4e09\u500b List\uff0c\u4e00\u500b\u90fd\u5132\u5b58\u6574\u6578\uff0c\u4e00\u500b\u662f\u90fd\u5132\u5b58\u5b57\u4e32\uff1b\u6700\u5f8c\u4e00\u500b\u5247\u653e\u4e86\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\u3002
numbers = [1, 2, 3, 4, 5]\nprint(numbers)\n\nwhat_i_learned = [\"C\", \"C++\", \"Python\", \"Java\", \"C#\", \"JavaScript\", \"TypeScript\"]\nprint(what_i_learned)\n\nstudent_info = [\"Sean\", 20, \"Computer Science\", 4.0, True, \"aLIEz\"]\nprint(student_info)\n\nprint(type(numbers), type(what_i_learned), type(student_info))\n
ouput[1, 2, 3, 4, 5]\n['C', 'C++', 'Python', 'Java', 'C#', 'JavaScript', 'TypeScript']\n['Sean', 20, 'Computer Science', 4.0, True, 'aLIEz']\n<class 'list'> <class 'list'> <class 'list'>\n
Sawano Hiroyuki - aLIEz Aldnoah.Zero Full Lyrics
\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u6703\u807d\u904e Array(\u9663\u5217)\uff0cList \u5c31\u662f Python \u4e2d\u7684 Array\uff0c\u4f46\u53c8\u66f4\u5f37\u5927\uff0c\u56e0\u70ba List \u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u800c\u901a\u5e38 Array \u53ea\u80fd\u5132\u5b58\u4e00\u7a2e\u8cc7\u6599\u578b\u614b\u3002
\u4f46 Python \u4e5f\u6709\u63d0\u4f9b\u771f\u6b63\u7684\u300c\u9663\u5217\u300d\uff0c\u4f46\u6211\u5148\u4e0d\u63d0\uff0c\u8acb\u4f60\u7a0d\u5fae\u6709\u500b\u5370\u8c61\u5c31\u597d\u3002
\u984c\u5916\u8a71\uff0c\u7576\u521d\u5f9e C, C++, Java \u8f49\u5230 Python \u7684\u6642\u5019\uff0c\u771f\u5fc3\u89ba\u5f97\u600e\u9ebc\u53ef\u4ee5\u9019\u9ebc\u96a8\u4fbf\uff0c\u9023\u578b\u614b\u90fd\u4e0d\u7528\u5ba3\u544a\uff0c\u73fe\u5728\u89ba\u5f97\u771f\u9999\u3002
\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u6709\u63d0\u5230 range
\u9019\u500b\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6574\u6578\u7684\u5e8f\u5217\uff0c\u4f8b\u5982: range(5)
\u6703\u7522\u751f\u4e00\u500b\u5f9e \\(0\\) \u5230 \\(4\\) \u7684\u6574\u6578\u5e8f\u5217\u3002
list(range(5))
\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 range
\u7269\u4ef6\u8f49\u63db\u6210 List\u3002
x = range(5)\nprint(type(x))\nnumbers = list(x)\nprint(numbers)\n
ouput<class 'range'>\n[0, 1, 2, 3, 4]\n
\u90a3\u80fd\u5426\u628a\u5b57\u4e32\u8f49\u63db\u6210 List \u5462? \u7576\u7136\u53ef\u4ee5!
x = \"Signals\"\nprint(type(x))\nwords = list(x)\nprint(words)\n
ouput<class 'str'>\n['S', 'i', 'g', 'n', 'a', 'l', 's']\n
Lazer Boomerang - Signals (Official Audio)
\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u4f55\u521d\u59cb\u5316\u4e00\u500b\u5143\u7d20\u90fd\u662f \\(0\\) \u7684\u4e32\u5217\u3002
n = 5\nzeros = [0] * n\nprint(zeros)\n
ouput[0, 0, 0, 0, 0]\n
\u5982\u4f55\u5c07\u5169\u500b\u4e32\u5217\u5408\u4f75\u6210\u4e00\u500b\u4e32\u5217\u5462?
a = [1, 2, 3]\nb = [4, 5, 6]\nc = a + b\nprint(c)\n
ouput[1, 2, 3, 4, 5, 6]\n
\u9084\u6709\u4e00\u500b\u65b9\u6cd5\uff0c\u5c31\u662f\u4f7f\u7528 extend
\u9019\u500b\u65b9\u6cd5\uff0c\u9019\u5728\u5f8c\u9762\u6703\u4ecb\u7d39\u5230\u3002
\u6211\u76f8\u4fe1\u4ee5\u4e0a\u9019\u4e9b\u4f8b\u5b50\u80fd\u8b93\u4f60\u6293\u5230\u4e00\u4e9b\u6982\u5ff5\uff0c\u63a5\u4e0b\u4f86\u5c07\u4ecb\u7d39\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u8207\u65b9\u6cd5\uff0c\u8b93\u4f60\u66f4\u719f\u6089 List\u3002
"},{"location":"fundamental/python/lists/#operations","title":"Operations","text":""},{"location":"fundamental/python/lists/#accessing_elements","title":"Accessing elements","text":"\u8981\u5b58\u53d6 List \u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []
\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7d22\u5f15\u503c\u5f9e \\(0\\) \u958b\u59cb\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528\u8ca0\u6578\uff0c\u8ca0\u6578\u7684\u7d22\u5f15\u503c\u662f\u5f9e\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u958b\u59cb\u7b97\u8d77\u3002
numbers = [1, 2, 3, 4, 5]\n\n# First element\nprint(numbers[0])\n# Last element\nprint(numbers[-1])\nprint(numbers[4])\n
ouput1\n5\n5\n
\u8b93\u6211\u5011\u914d\u5408\u8ff4\u5708\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\n\nfor i in range(len(numbers)):\n print(numbers[i], end=' ')\n
ouput1 2 3 4 5\n
len
\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u4e32\u5217\u7684\u9577\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e32\u5217\u4e2d\u5143\u7d20\u7684\u500b\u6578\u3002
\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97 Repetiton Structures - Foreach \u7684\u5167\u5bb9\uff0c\u6211\u5011\u7528\u9019\u500b\u6982\u5ff5\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\n\nfor number in numbers:\n print(number, end=' ')\n
ouput1 2 3 4 5\n
"},{"location":"fundamental/python/lists/#slicing","title":"Slicing","text":"\u5207\u7247(Slicing)\u53ef\u4ee5\u7528\u4f86\u53d6\u5f97\u4e32\u5217\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982:
numbers = [1, 2, 3, 4, 5, 6]\n\nprint(numbers[0:4])\nprint(numbers[:4])\nprint(numbers[:-2])\nprint(numbers[1:3])\nprint(numbers[:6:2])\nprint(numbers[::-1])\nprint(numbers[:])\n
ouput[1, 2, 3, 4]\n[1, 2, 3, 4]\n[1, 2, 3, 4]\n[2, 3]\n[1, 3, 5]\n[6, 5, 4, 3, 2, 1]\n[1, 2, 3, 4, 5, 6]\n
@EditTime : 2024-01-31 21:56
\u4f60\u5f88\u8070\u660e\uff0c\u61c9\u8a72\u767c\u73fe\u4e86\u7528\u6cd5\u8207 range
\u4e00\u6a23\uff0c\u5206\u5225\u662f\u8d77\u59cb\u7d22\u5f15\u503c\u3001\u7d50\u675f\u7d22\u5f15\u503c\u3001\u9593\u9694\uff0c\u5982\u679c\u4e0d\u5beb\u7684\u8a71\uff0c\u9810\u8a2d\u503c\u5206\u5225\u662f \\(0\\)\u3001\u4e32\u5217\u9577\u5ea6\u3001\\(1\\)\u3002
\u8981\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []
\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7136\u5f8c\u518d\u6307\u5b9a\u65b0\u7684\u503c\u3002
music_info = [\"Time_To_Pretend\", \"Lazer Boomerang\", \" 2019/4/26\", 1, False]\nmusic_info[0] = \"Time To Pretend\"\nmusic_info[3] = 2\nmusic_info[-1] = True\nprint(music_info)\n
ouput['Time To Pretend', 'Lazer Boomerang', ' 2019/4/26', 2, True]\n
Lazer Boomerang - Time To Pretend (Official Audio)
\u6211\u5011\u53ef\u4ee5\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u503c\uff0c\u9019\u7a31\u70ba \u53ef\u8b8a\u52d5\u7684(mutable)\uff0c\u800c\u5b57\u4e32\u5247\u4e0d\u884c\uff0c\u9019\u7a31\u70ba \u4e0d\u53ef\u8b8a\u52d5\u7684(immutable)\u3002
sad_cat = \"\ud83d\ude3f\"\nsad_cat[0] = \"\ud83d\ude38\"\n
ouputTypeError: 'str' object does not support item assignment\n
"},{"location":"fundamental/python/lists/#checking_elements","title":"Checking elements","text":"\u8981\u6aa2\u67e5\u4e32\u5217\u4e2d\u662f\u5426\u6709\u67d0\u500b\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 in
\u9019\u500b\u904b\u7b97\u5b50\u3002
numbers = [1, 2, 3, 4, 5]\nprint(1 in numbers)\nprint(6 not in numbers)\n
ouputTrue\nTrue\n
Question
\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u4e32\u5217\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u6700\u58de\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?
\u7b54\u6848\u662f \\(n\\) \u6b21\u5594\uff0c\u56e0\u70ba\u4e00\u500b\u4e00\u500b\u627e\uff0c\u76f4\u5230\u627e\u5230\u6216\u662f\u627e\u5b8c\u70ba\u6b62\uff0c\u50cf\u662f\u4f8b\u5b50\u4e2d\u7684 6
\u3002
\u5728\u6f14\u7b97\u6cd5(Algorithm)\u7684\u7ae0\u7bc0\u4e2d\u6703\u63d0\u5230\u6642\u9593\u8907\u96dc\u5ea6(Time Complexity)\u9019\u500b\u89c0\u5ff5\uff0c\u9019\u88e1\u5c31\u5148\u7d66\u4f60\u4e00\u500b\u5370\u8c61\u5c31\u597d\u4e86~
"},{"location":"fundamental/python/lists/#methods","title":"Methods","text":"\u4f86\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u4e32\u5217\u65b9\u6cd5\u3002
"},{"location":"fundamental/python/lists/#append","title":"append","text":"append
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u4e00\u500b\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.append(6)\nprint(numbers)\nnumbers.append([7, 8, 9])\nprint(numbers)\n
ouput[1, 2, 3, 4, 5, 6]\n[1, 2, 3, 4, 5, 6, [7, 8, 9]]\n
\u5e0c\u671b\u7b2c\u4e8c\u500b append
\u6c92\u6709\u5687\u5230\u4f60\uff0c\u5b83\u53ef\u4ee5\u65b0\u589e\u4e00\u500b\u4e32\u5217\uff0c\u4f46\u662f\u9019\u500b\u4e32\u5217\u6703\u8b8a\u6210\u4e32\u5217\u4e2d\u7684\u4e00\u500b\u5143\u7d20\u3002\u5982\u679c\u4f60\u662f\u60f3\u8981\u628a 7
\u30018
\u30019
\u9019\u4e09\u500b\u5143\u7d20\u52a0\u5230\u4e32\u5217\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 extend
\u9019\u500b\u65b9\u6cd5\u3002
extend
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u53e6\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.extend([6, 7, 8])\nprint(numbers)\n
ouput[1, 2, 3, 4, 5, 6, 7, 8]\n
"},{"location":"fundamental/python/lists/#pop","title":"pop","text":"pop
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u88ab\u79fb\u9664\u7684\u5143\u7d20\u3002
fruits = [\"apple\", \"banana\", \"cherry\", \"durian\", \"elderberry\"]\nprint(fruits.pop())\nprint(fruits)\nprint(fruits.pop(1))\nprint(fruits)\n
ouputelderberry\n['apple', 'banana', 'cherry', 'durian']\nbanana\n['apple', 'cherry', 'durian']\n
\u901a\u5e38\uff0c\u6211\u5011\u6703\u4f7f\u7528 pop
\u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u56e0\u70ba\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u7d22\u5f15\u503c\u8d85\u51fa\u7bc4\u570d\u7684\u932f\u8aa4\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7d22\u5f15\u503c\uff0c\u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u4e0d\u5efa\u8b70\u3002
clear
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.clear()\nprint(numbers)\n
ouput[]\n
"},{"location":"fundamental/python/lists/#reverse","title":"reverse","text":"reverse
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u53cd\u8f49\u4e32\u5217\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.reverse()\nprint(numbers)\n
ouput[5, 4, 3, 2, 1]\n
"},{"location":"fundamental/python/lists/#sort","title":"sort","text":"sort
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u6392\u5e8f\u4e32\u5217\uff0c\u9810\u8a2d\u662f\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u5982\u679c\u8981\u7531\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u53ef\u4ee5\u6307\u5b9a reverse=True
\u3002
numbers = [5, 4, 3, 2, 1]\nnumbers.sort()\nprint(numbers)\nnumbers.sort(reverse=True)\nprint(numbers)\n
ouput[1, 2, 3, 4, 5]\n[5, 4, 3, 2, 1]\n
\u751a\u81f3\u4f60\u53ef\u4ee5\u8a2d\u5b9a key
\uff0c\u4f86\u6c7a\u5b9a\u6392\u5e8f\u7684\u4f9d\u64da\uff0c\u4f8b\u5982: key=len
\uff0c\u5c31\u662f\u6839\u64da\u5143\u7d20 x
\u7684 len(x)
\u4f86\u6392\u5e8f\u3002
fruits = [\"apple\", \"banana\", \"watermelon\", \"pineapple\"]\nfruits.sort(key=len)\nprint(fruits)\n
ouput['apple', 'banana', 'pineapple', 'watermelon']\n
"},{"location":"fundamental/python/lists/#copy","title":"copy","text":"copy
\u9019\u500b\u65b9\u6cd5\u56de\u50b3\u4e00\u500b\u6dfa\u8907\u88fd(Shallow Copy)\u7684\u4e32\u5217\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1] = 10\nprint(numbers_copy)\nprint(numbers)\n
ouput[1, 2, 3, 4, 5]\n[1, 2, 3, 4, 5]\nAfter changing numbers[1]\n[1, 2, 3, 4, 5]\n[1, 10, 3, 4, 5]\n
\u9019\u500b\u4f8b\u5b50\u9084\u4e0d\u80fd\u770b\u51fa\u6dfa\u8907\u88fd\uff0c\u4fee\u6539\u4e86 numbers
\u7684\u7b2c\u4e8c\u500b\u5143\u7d20\uff0cnumbers_copy
\u4e0d\u6703\u8ddf\u8457\u6539\u8b8a\u3002
numbers = [1, [2, 3], 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1][0] = 6\nprint(numbers_copy)\nprint(numbers)\n
ouput[1, [2, 3], 4, 5]\n[1, [2, 3], 4, 5]\nAfter changing numbers[1]\n[1, [6, 3], 4, 5]\n[1, [6, 3], 4, 5]\n
\u90a3\u4ec0\u9ebc\u53c8\u662f\u6df1\u8907\u88fd(Deep Copy)\u5462? \u9019\u500b\u5c31\u8981\u7b49\u5230\u6211\u5011\u5b78\u5230\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08(Object-Oriented Programming)\u7684\u6642\u5019\uff0c\u518d\u4f86\u8ddf\u4f60\u4ecb\u7d39\uff0c\u6211\u4e0d\u61c9\u8a72\u6316\u5751\u7684\u3002
\u9084\u6709\u66f4\u591a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u53c3\u8003 Python Documentation - List\uff0c\u4f46\u6211\u4e0a\u9762\u4ecb\u7d39\u7684\u65b9\u6cd5\uff0c\u4f60\u81f3\u5c11\u8981\u6703\uff0c\u4f60\u4e5f\u8981\u990a\u6210\u770b\u5b98\u65b9\u6587\u4ef6\u7684\u7fd2\u6163\u3002
"},{"location":"fundamental/python/lists/#list_comprehension","title":"List Comprehension","text":"\u4e32\u5217\u7d9c\u5408\u904b\u7b97(List comprehension)\u8b93\u4f60\u7528\u4e00\u884c\u7a0b\u5f0f\u78bc\uff0c\u5c31\u53ef\u4ee5\u5efa\u7acb\u51fa\u64c1\u6709\u67d0\u9805\u6027\u8cea\u5143\u7d20\u7684\u4e32\u5217\u3002
\u8209\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a
\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b
\uff0c\u5176\u4e2d b[i] = a[i] ** 2
\uff0c\u4e5f\u5c31\u662f b
\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0d\u61c9 a
\u5143\u7d20\u7684\u5e73\u65b9\u3002
\u6211\u5011\u5148\u4f7f\u7528\u8ff4\u5708\u4f86\u5be6\u4f5c:
a = [1, 2, 3, 4, 5]\nb = []\n\nfor num in a:\n b.append(num ** 2)\n\nprint(b)\n
ouput[1, 4, 9, 16, 25]\n
\u518d\u4f86\u4f7f\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97:
a = [1, 2, 3, 4, 5]\nb = [num ** 2 for num in a]\n\nprint(b)\n
ouput[1, 4, 9, 16, 25]\n
\u975e\u5e38\u5730\u512a\u96c5\uff0c\u6211\u5011\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a
\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b
\uff0c\u5176\u4e2d b
\u7684\u5143\u7d20\u90fd\u662f a
\u5143\u7d20\u4e2d\u7684\u5076\u6578\u3002
a = [1, 2, 3, 4, 5]\nb = [num for num in a if num % 2 == 0]\n\nprint(b)\n
ouput[2, 4]\n
\u9084\u8a18\u5f97 Operators - Bonus: map for input \u63d0\u5230\u7684 map
\u55ce?
\u5148\u4f86\u8907\u7fd2\u4e00\u4e0b\uff0cmap
\u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u5957\u7528\u5230\u4e00\u500b\u5e8f\u5217\u7684\u6bcf\u4e00\u500b\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u4e00\u500b map
\u7269\u4ef6\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 list
\u5c07 map
\u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\u3002
a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = map(int, a)\nprint(type(b))\n\nc = list(b)\nprint(c)\n
ouput<class 'map'>\n[1, 2, 3, 4, 5]\n
@EditTime : 2024-02-01 23:21
\u90a3\u80fd\u4e0d\u80fd\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97\u4f86\u5be6\u4f5c\u5462?
a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = [int(num) for num in a]\nprint(b)\n
ouput[1, 2, 3, 4, 5]\n
\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002
\u9084\u6709\u5f88\u591a\u61c9\u7528\uff0c\u4f8b\u5982: \u4e32\u5217\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0f\u5beb\uff0c\u6211\u5011\u60f3\u8981\u628a\u5b83\u5011\u8f49\u63db\u6210\u5927\u5beb\u3002
lowercase = [\"a\", \"b\", \"c\", \"d\", \"e\"]\nuppercase = [letter.upper() for letter in lowercase]\nprint(uppercase)\n
ouput['A', 'B', 'C', 'D', 'E']\n
\u6ce8\u610f\u56c9\uff0c\u9019\u908a\u7528\u5230\u7684\u662f\u65b9\u6cd5\u5594\u3002
"},{"location":"fundamental/python/lists/#nested_lists","title":"Nested Lists","text":"\u5de2\u72c0\u4e32\u5217(Nested Lists)\uff0c\u5c31\u662f\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u4e5f\u662f\u4e32\u5217\u3002
"},{"location":"fundamental/python/lists/#2d","title":"2D","text":"\u5148\u4f86\u8209\u4e00\u500b\u4f60\u53ef\u80fd\u807d\u904e\u7684\u540d\u8a5e\uff0c\u4e8c\u7dad\u9663\u5217(2D Array)\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5de2\u72c0\u4e32\u5217\u4f86\u5be6\u4f5c\u4e8c\u7dad\u9663\u5217\uff0c\u4f8b\u5982\u5169\u500b \\(3 \\times 3\\) \u7684\u4e8c\u7dad\u9663\u5217\uff0c\u4f86\u9032\u884c\u77e9\u9663\u52a0\u6cd5\u3002
matrix_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nmatrix_2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]\n\nmatrix_3 = [[0 for i in range(3)] for j in range(3)]\n\nfor i in range(3):\n for j in range(3):\n matrix_3[i][j] = matrix_1[i][j] + matrix_2[i][j]\n\nprint(matrix_3)\n
ouput[[10, 10, 10], [10, 10, 10], [10, 10, 10]]\n
\u6211\u5e0c\u671b matrix_3
\u7684\u521d\u59cb\u5316\u6c92\u6709\u5687\u5230\u4f60\uff0c\u8acb\u4f60\u628a [0 for i in range(3)]
\u7576\u6210\u4e00\u500b\u6574\u9ad4 E
\uff0c\u518d\u628a [E for j in range(3)]
\u7576\u6210\u4e00\u500b\u6574\u9ad4\uff0c\u9019\u6a23\u5c31\u4e0d\u6703\u89ba\u5f97\u5f88\u8907\u96dc\u4e86\uff0ci, j
\u90fd\u53ea\u662f\u8a08\u6578\u7528\u7684\u800c\u5df2\uff0c\u4e0d\u8981\u88ab\u5b83\u5011\u5687\u5230\u3002
\u92f8\u9f52\u72c0\u4e32\u5217(Jagged Lists)\uff0c\u5c31\u662f\u5141\u8a31\u5de2\u72c0\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7684\u9577\u5ea6\u4e0d\u4e00\u6a23\u3002
\u984c\u5916\u8a71\uff0c\u4e4b\u524d\u5728\u5b78 C \u7684\u6642\u5019\uff0c\u53ea\u80fd\u7528\u6307\u6a19\u4f86\u5be6\u4f5c\uff0c\u4f46\u7528\u8d77\u4f86\u771f\u7684\u5f88\u9ebb\u7169\u3002
jagged = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]\n\nfor row in jagged:\n for elem in row:\n print(elem, end=' ')\n print()\n
ouput1 2 3 \n4 5 \n6 7 8 9 \n
\u5e0c\u671b\u9019\u500b\u8ff4\u5708\u7684\u5beb\u6cd5\u4e0d\u6703\u5687\u5230\u4f60\uff0crow
\u662f jagged
\u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u4e32\u5217\uff0celem
\u662f row
\u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u6574\u6578\u3002
Python \u7684\u8a9e\u6cd5\u771f\u7684\u5f88\u9748\u6d3b\uff0c\u4f46\u4e5f\u8981\u5f88\u5c0f\u5fc3\uff0c\u4ed6\u63d0\u4f9b\u4e86\u5f88\u591a\u5de5\u5177\uff0c\u4f46\u5982\u679c\u4f60\u4e0d\u6e05\u695a\u4ed6\u662f\u600e\u9ebc\u904b\u4f5c\u7684\uff0c\u5c31\u6703\u5bb9\u6613\u6703\u5beb\u51fa\u6548\u7387\u4f4e\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u4e5f\u662f\u96d9\u9762\u5203\uff0c\u6240\u4ee5\u8981\u591a\u591a\u7df4\u7fd2\uff0c\u591a\u591a\u601d\u8003\uff0c\u4f60\u53ef\u4ee5\u7684\u3002
"},{"location":"fundamental/python/lists/#practice","title":"Practice","text":"Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578
Reference codetable = []\nfor i in range(6, 2 ** 13, 2):\n s = 0\n for j in range(1, i):\n if i % j == 0:\n s += j\n if s == i:\n table.append(i)\n\nwhile True:\n N = int(input())\n print(' '.join([str(x) for x in table if x <= N]))\n
\u5efa\u7acb\u4e00\u500b\u4e32\u5217 table
\uff0c\u88e1\u9762\u653e\u4e86 \\(6\\) \u5230 \\(2^{13}\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\uff0c\u63a5\u8457\u8f38\u5165\u4e00\u500b\u6574\u6578 \\(N\\)\uff0c\u8f38\u51fa \\(1\\) \u5230 \\(N\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\u3002 \u90a3\u9ebc \\(2^{13}\\) \u662f\u600e\u9ebc\u4f86\u7684?\u311c\uff0c\u6211\u5237\u5f8c\u53f0\u6e2c\u8cc7\u63a8\u6572\u4f86\u7684\u3002
join
\u65b9\u6cd5\u53ef\u4ee5\u5c07\u5b57\u4e32\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7528\u6307\u5b9a\u7684\u5b57\u4e32\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u908a\u7528\u7a7a\u767d\u5b57\u4e32 ' '
\u4f86\u9023\u63a5\u3002
\u9019\u7a2e\u6280\u5de7\u7a31\u70ba\u300c\u5efa\u8868\u300d\uff0c\u4e8b\u5be6\u4e0a\u5b8c\u7f8e\u6578\u5f88\u758f\u6563\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u81ea\u5df1\u5efa\u5b8c\u8868\u5f8c\u76f4\u63a5\u5beb\u6b7b\uff0c\u4f46\u9019\u6a23\u7684\u8a71\uff0c\u7a0b\u5f0f\u78bc\u5c31\u4e0d\u5177\u6709\u901a\u7528\u6027\u3002
table = [6, 28, 496, 8128]\n\nN = int(input())\nprint(' '.join(str(x) for x in table if x <= N))\n
\u5982\u679c\u5c31\u55ae\u7d14\u89e3\u984c\u7684\u8a71\uff0c\u9019\u6a23\u4e5f\u662f\u53ef\u4ee5\u7684\u3002
\u6b63\u898f\u7684\u984c\u76ee\u90fd\u6703\u898f\u7bc4\u6e2c\u8cc7\u7bc4\u570d\uff0c\u5982\u679c\u4f60\u4ee5\u5f8c\u4e0a\u5927\u5b78\u5f8c\u9047\u5230\u4e0d\u592a\u9748\u5149\u7684\u52a9\u6559\u6c92\u6709\u6a19\u6e2c\u8cc7\u7bc4\u570d\uff0c\u8a18\u5f97\u96fb\u6b7b\u4ed6\u5011\u3002
Itsa - [C_MM44-\u6613] The Numbers
Reference codeN, M = input().split()\n\ncnt = 0\nfor i in range(0, 6):\n if M[i] == N[0] and M[i + 1] == N[1]:\n cnt += 1\n\nprint(cnt)\n
\u5b57\u4e32\u4e5f\u53ef\u4ee5\u50cf\u662f\u4e32\u5217\u4e00\u6a23\uff0c\u4f7f\u7528\u7d22\u5f15\u503c\u4f86\u5b58\u53d6\u5143\u7d20\u3002
Itsa - [C_AR01-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 I
Reference codearr = input().split()\nprint(' '.join(arr[::-1]))\n
arr.reverse()
\u4e5f\u884c\uff0c\u4f46\u662f\u9019\u6a23\u6703\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5c31\u4f7f\u7528 arr[::-1]
\u3002
arr = input().split()\narr.reverse()\nprint(' '.join(arr))\n
Itsa - [C_AR03-\u6613] \u8a08\u7b97\u9663\u5217\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7acb\u65b9\u548c
Reference codearr = map(int, input().split())\nans = 0\n\nfor num in arr:\n ans += num ** 3\n\nprint(ans)\n
\u6211\u4e26\u6c92\u6709\u628a map
\u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\uff0c\u56e0\u70ba\u6211\u53ea\u9700\u8981\u4e00\u6b21\u8fed\u4ee3\uff0c\u6240\u4ee5\u76f4\u63a5\u7528 map
\u7269\u4ef6\u5c31\u597d\u3002
\u4f86\u770b\u4e00\u884c\u89e3:
print(sum(int(x) ** 3 for x in input().split()))\n
\u53c3\u8003\u5c31\u597d\u3002
Itsa - [C_AR09-\u6613] \u5169\u6578\u5dee\u503c
Reference codearr1 = list(map(int, input().split(',')))\narr1.sort()\narr2 = arr1.copy()\narr2.sort(reverse=True)\n\nmax_num,min_num = 0, 0\nfor i in range(len(arr1) - 1, -1, -1):\n # convert list to number\n max_num = max_num * 10 + arr1[i]\n min_num = min_num * 10 + arr2[i]\n\nprint(max_num - min_num)\n
\u4e0d\u61c2\u6392\u5e8f\u6c92\u95dc\u4fc2\uff0c\u6211\u5011\u5148\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u4ee5\u5f8c\u6211\u6703\u6559\u4f60\u539f\u7406\u3002
\u800c\u9019\u984c\u7684\u91cd\u9ede\u662f\uff0c\u5982\u4f55\u5c07\u4e32\u5217\u8f49\u63db\u6210\u6578\u5b57\u3002\u5f9e\u500b\u4f4d\u6578\u958b\u59cb\uff0c\u6bcf\u6b21\u4e58 \\(10\\)\uff0c\u518d\u52a0\u4e0a\u4e0b\u4e00\u500b\u6578\u5b57\u3002
Itsa - [C_AR022-\u6613] \u5b57\u6bcd\u51fa\u73fe\u7684\u983b\u7387
Reference codestring = input()\n\nfreq = [0] * 26\n\nfor c in string:\n if c.isalpha():\n freq[ord(c.lower()) - ord('a')] += 1\n\nprint(' '.join(str(x) for x in freq))\n
\u9019\u984c\u9700\u8981\u4e00\u500b\u5148\u5099\u77e5\u8b58\uff0cASCII \u78bc\uff0cord
\u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u5b57\u5143\u7684 ASCII \u78bc\u3002
\u518d\u5229\u7528 isalpha
\u65b9\u6cd5\uff0c\u4f86\u5224\u65b7\u5b57\u5143\u662f\u5426\u70ba\u5b57\u6bcd\uff0c\u4ee5\u53ca lower
\u65b9\u6cd5\uff0c\u4f86\u5c07\u5b57\u6bcd\u8f49\u63db\u6210\u5c0f\u5beb\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u5b57\u6bcd\u6620\u5c04\u5230 0
\u5230 25
\u4e4b\u9593\u3002
\u95dc\u65bc\u5b57\u4e32\u7684\u4e3b\u984c\uff0c\u5f8c\u9762\u6703\u518d\u8ddf\u4f60\u4ecb\u7d39\u3002
Itsa - [C_AR025-\u6613] \u8a08\u7b97ASCII\u5b57\u5143
Reference codestring = input()\n\nfreq = [0] * 128\n\n\nfor i in range(len(string)):\n freq[ord(string[i])] += 1\n\nfor i in range(len(freq) - 1, -1, -1):\n if freq[i] > 0:\n print(f'{i} {freq[i]}')\n
\u9019\u984c\u8ddf\u4e0a\u4e00\u984c\u5f88\u50cf\u3002
Itsa - [C_AR029-\u96e3] \u6587\u5b57\u7de8\u78bc
Reference codeplain_text = input()\n\nN = len(plain_text)\nM = 1\nwhile M * M < N:\n M += 1\n\narr = [[' ' for _ in range(M)] for _ in range(M)]\n\nfor i in range(N):\n arr[i // M][i % M] = plain_text[i]\n\ncipher_text = []\nfor i in range(M):\n for j in range(M):\n cipher_text.append(arr[j][i])\n\nprint(''.join(cipher_text))\n
\u77e9\u9663\u8f49\u7f6e\uff0c\u6240\u4ee5 arr[j][i]
\u8b8a\u6210 arr[i][j]
\u3002
Itsa - [C_MM42-\u4e2d] \u6c42(-1)^(n+1)x[1/(2n-1)]\u7684\u548c
Itsa - [C_AR02-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 II
Itsa - [C_AR10-\u4e2d] \u65b0\u901a\u8a71\u8cbb\u7387
Itsa - [C_AR021-\u6613] \u6210\u7e3e\u7d71\u8a08
Itsa - [C_AR023-\u6613] \u5b57\u6839\u8207\u5b50\u5b57\u4e32
Itsa - [C_AR031-\u4e2d] \u4e00\u7dad\u77e9\u9663\u8868\u793a\u4e8c\u7dad\u5e73\u9762\u7a7a\u9593
Itsa - [C_AR33-\u6613] \u8f49\u7f6e\u77e9\u9663
Itsa - [C_AR34-\u6613] \u8eab\u5206\u8b49\u9a57\u8b49\u5668
Itsa - [C_AR35-\u6613] \u751f\u8096\u554f\u984c
Itsa - [C_AR41-\u6613] \u4e00\u6574\u6578\u5e8f\u5217\u6240\u542b\u4e4b\u6574\u6578\u500b\u6578\u53ca\u5e73\u5747\u503c
Itsa - [C_AR46-\u6613] \u9663\u5217\u5e73\u65b9\u548c\u554f\u984c
Itsa - [C_AR48-\u6613] \u6578\u5b57\u52a0\u5bc6
@EditTime : 2024-02-03 21:30
"},{"location":"fundamental/python/operators/","title":"Operators","text":""},{"location":"fundamental/python/operators/#introduction","title":"Introduction","text":"\u5728\u9019\u500b\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u904b\u7b97\u5b50\uff0c\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u5c0d\u8b8a\u6578\u505a\u904b\u7b97\uff0c\u4f8b\u5982: \u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u7b49\u7b49\u3002
"},{"location":"fundamental/python/operators/#arithmetic_operators","title":"Arithmetic Operators","text":"\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u6211\u5011\u719f\u6089\u7684\u56db\u5247\u904b\u7b97\u5c31\u662f\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u5206\u5225\u662f +
\u3001-
\u3001*
\u3001/
\uff0c\u5206\u5225\u4ee3\u8868\u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3002
x, y = 10, 3\nprint(x + y)\nprint(x - y)\nprint(x * y)\nprint(x / y)\nz = x // y\nprint(z)\nprint(type(z))\nprint(x % y)\nprint(x ** y)\n
ouput13\n7\n30\n3.3333333333333335\n3\n<class 'int'>\n1\n1000\n
\u6211\u76f8\u4fe1\u770b\u5b8c\u8f38\u51fa\u5f8c\uff0c\u4f60\u80fd\u5927\u81f4\u660e\u77ad\u5404\u500b\u904b\u7b97\u5b50\u7684\u4f5c\u7528\uff0c\u5176\u4e2d\u60f3\u8acb\u4f60\u7559\u610f *
\u4e58\u6cd5\u904b\u7b97\u5b50\uff0c\u8acb\u4f60\u4e0d\u8981\u6253\u6210 x
\uff0c\u4ee5\u53ca /
\u8207 //
\u7684\u5dee\u5225\uff0c\u5f8c\u8005\u6703\u5c07\u7d50\u679c\u5411\u4e0b\u53d6\u6574\u3002\u9084\u6709\u6bd4\u8f03\u7279\u5225\u7684 %
\uff0c\u7559\u610f\u5230 10 = 3 * 3 + 1
\uff0c\u5c31\u662f\u53d6\u9918\u6578\uff1b\u800c\u770b\u8d77\u4f86\u6700\u5947\u7279\u7684 **
\uff0c\u4ed6\u7684\u4f5c\u7528\u662f\u6c42\u51aa\u3002
\u6211\u5011\u4f86\u8907\u7fd2\u4e00\u4e0b\u570b\u5c0f\u6578\u5b78:
# Upper case for constant\nPI = 3.14\nr = 2\narea = PI * (r ** 2)\nperimeter = 2 * PI * r\nprint(f'Area: {area}, Perimeter: {perimeter}')\n\n# Sum of 1 to 100\ntotal = (1 + 100) * 100 // 2\nprint(f'The sum of 1 to 100 is {total}')\n
ouputArea: 12.56, Perimeter: 12.56\nThe sum of 1 to 100 is 5050\n
\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c\u904b\u7b97\u5b50\u4e4b\u9593\u6709\u512a\u5148\u9806\u5e8f\uff0c\u7576\u4f60\u4e0d\u78ba\u5b9a\u7d50\u679c\u7684\u6642\u5019\uff0c\u8acb\u5584\u7528\u62ec\u865f\u3002
x = 'A' + 'n' + 'i' + 'm' + 'a' + 'l' + 's'\nprint(x)\n
ouputAnimals\n
Martin Garrix - Animals (Official Video)
\u518d\u8acb\u4f60\u7559\u610f\u4e00\u4ef6\u4e8b\uff0c\u4e0d\u540c\u985e\u5225\u5c0d\u65bc\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4e0d\u540c\u7684\uff0c\u4f8b\u5982\u5b57\u4e32\u7684 +
\u6703\u5c07\u5169\u500b\u5b57\u4e32\u76f8\u9023\uff0c\u800c\u6574\u6578\u7684 +
\u5247\u6703\u5c07\u5169\u500b\u6574\u6578\u76f8\u52a0\u3002
Question
2 ** 3 ** 2
\u7684\u7d50\u679c\u662f\u591a\u5c11?2 ** (3 ** 2)
\u7684\u7d50\u679c\u662f\u591a\u5c11?(2 ** 3) ** 2
\u7684\u7d50\u679c\u662f\u591a\u5c11?\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7d50\u679c\u6703\u662f True
\u6216 False
\uff0c\u9019\u500b\u7d50\u679c\u6211\u5011\u7a31\u70ba\u5e03\u6797\u503c\u3002
\u6211\u5011\u4f86\u4ee5\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u6027\u8cea\u4f86\u505a\u4e00\u4e9b\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7df4\u7fd2\u3002
a, b, c = 3, 4, 5\n\nprint(a < b)\nprint(a > b)\nprint(a <= b)\nprint(a >= b)\nprint(a ** 2 + b ** 2 == c ** 2)\nprint(a ** 2 + b ** 2 != c ** 2)\nprint((a + b) > c)\n
ouputFalse\nTrue\nFalse\nTrue\nFalse\nTrue\n
\u9019\u908a\u8981\u63d0\u9192\u4f60\uff0c ==
\u662f\u6bd4\u8f03\u904b\u7b97\u5b50\uff0c\u800c =
\u662f\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u7684\u610f\u601d\u662f\u4e0d\u4e00\u6a23\u7684\u3002
\u6700\u5f8c\u4f86\u770b\u4e00\u500b\u4f8b\u5b50:
age = 14\nprint(12 <= age <= 18)\n\nx, y, z = 1, 2, 3\nprint(x < y < z)\nprint(x < y and y < z)\n
ouputTrue\nTrue\nTrue\n
\u5728Python\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6bd4\u8f03\u904b\u7b97\u5b50\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u6a23\u7684\u5beb\u6cd5\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002
Question
print(1 < 2 < 3 < 4 < 5)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(1 < 2 < 3 < 4 > 5)
\u6703\u5370\u51fa\u4ec0\u9ebc?\u6211\u5011\u7e7c\u7e8c\u5f80\u4e0b\u770b\uff0c\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u908f\u8f2f\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u591a\u500b\u5e03\u6797\u503c\u7d50\u5408\u6210\u4e00\u500b\u5e03\u6797\u503c\u3002
has_license = True\nis_drunk = False\nage = 18\n\nprint(f\"Can I drive a car? {has_license and not is_drunk and age >= 18}\")\nprint(not has_license)\nprint(not is_drunk)\nprint(age >= 18 or is_drunk or not has_license)\n
ouputCan I drive a car? True\nFalse\nTrue\nTrue\n
\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c and
\u53ea\u6709\u5728\u6240\u6709\u5e03\u6797\u503c\u90fd\u662f True
\u7684\u6642\u5019\uff0c\u7d50\u679c\u624d\u6703\u662f True
\uff0c\u800c or
\u53ea\u8981\u6709\u4e00\u500b\u5e03\u6797\u503c\u662f True
\uff0c\u7d50\u679c\u5c31\u6703\u662f True
\u3002
\u56e0\u6b64\u6709\u6240\u8b02\u7684\u77ed\u8def\u6c42\u503c(Short-circuit Evaluation)\uff0c\u7576 and
\u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f False
\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a08\u7b97\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f False
\uff1b\u800c or
\u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f True
\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a55\u4f30\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f True
\u3002
\u800c not
\u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u53ea\u6703\u5c07\u5e03\u6797\u503c\u53cd\u8f49\u3002
Question
print(not True and False)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(not True or False)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(not True and not False)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(not True or not False)
\u6703\u5370\u51fa\u4ec0\u9ebc?\u5728\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u4f4d\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u662f\u5c0d\u4e8c\u9032\u4f4d\u7684\u904b\u7b97\uff0c\u6211\u5011\u53ef\u4ee5\u7528 bin()
\u4f86\u89c0\u5bdf\u4e8c\u9032\u4f4d\u7684\u7d50\u679c\u3002
a, b = 2, 3\nprint(f\"a={bin(a)}\")\nprint(f\"b={bin(b)}\")\n\nprint(a & b, bin(a & b))\nprint(a | b, bin(a | b))\nprint(a ^ b, bin(a ^ b))\nprint(~a, bin(~a))\n\nprint(a << 1, bin(a << 1))\nprint(a >> 1, bin(a >> 1))\n
ouputa=0b10\nb=0b11\n2 0b10\n3 0b11\n1 0b1\n-3 -0b11\n4 0b100\n1 0b1\n
&
\u662f\u4f4d\u5143\u7684 and
\uff0c |
\u662f\u4f4d\u5143\u7684 or
\uff0c ^
\u662f\u4f4d\u5143\u9593\u7684\u9032\u884c\u4e92\u65a5\u6216\u904b\u7b97\uff0c ~
\u662f\u4f4d\u5143\u7684 not
\uff0c <<
\u662f\u4f4d\u5143\u7684\u5de6\u79fb\uff0c >>
\u662f\u4f4d\u5143\u7684\u53f3\u79fb\u3002
\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u60f3\u60f3\u6211\u5011\u8a72\u5982\u4f55\u5224\u65b7\u4e00\u500b\u6578\u5b57\u662f\u5947\u6578\u9084\u662f\u5076\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 %
\u4f86\u5224\u65b7\uff0c\u4f46\u662f\u6211\u5011\u4e5f\u53ef\u4ee5\u7528\u4f4d\u5143\u904b\u7b97\u4f86\u5224\u65b7\u3002
a, b = 5678, 4843\nprint(f\"Is a even? {a % 2 == 0}\")\nprint(f\"Is b odd? {b & 1 == 1}\")\n
ouputIs a even? True\nIs b odd? True\n
Question
2
\u7684\u51aa?*
\u4e58\u6cd5\u904b\u7b97\u5b50?\u6211\u5011\u5df2\u7d93\u5b78\u6703\u4e86\u4e00\u4e9b\u904b\u7b97\u5b50\uff0c\u73fe\u5728\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u4e00\u4e9b\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u904b\u7b97\u7d50\u679c\u6307\u5b9a\u7d66\u8b8a\u6578\u3002
\u5148\u4f86\u770b\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 +=
\u4f86\u5c07\u8b8a\u6578\u52a0\u4e0a\u67d0\u500b\u503c\uff0c\u9019\u500b\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002
x = 1\nx = x + 2\ny = 1\ny += 2\nprint(x, y)\n
ouput3 3\n
\u518d\u4f86\u770b\u770b\u5176\u4ed6\u7684\u6307\u6d3e\u904b\u7b97\u5b50\u3002
x = 1\nx += 2\nprint(x)\nx -= 1\nprint(x)\nx *= 3\nprint(x)\nx //= 2\nprint(x)\nx **= 2\nprint(x)\nx <<= 1\nprint(x)\n
ouput3\n2\n6\n3\n9\n18\n
\u5c0d\u4e86\uff0c\u8acb\u4f60\u5225\u5fd8\u8a18 =
\u4e5f\u662f\u4e00\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u3002
x = y = z = 1\nprint(x, y, z)\n
ouput1 1 1\n
\u5e0c\u671b\u9019\u500b\u4f8b\u5b50\u53ef\u4ee5\u8b93\u4f60\u66f4\u719f\u6089\u6307\u6d3e\u904b\u7b97\u5b50\u3002
Question
and=
\u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50?&=
\u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4ec0\u9ebc?\u70ba\u4e86\u80fd\u8b93\u4f60\u7df4\u7fd2\u4e00\u4e9b\u984c\u76ee\uff0c\u6211\u5148\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u5370\u51fa\u6d6e\u9ede\u6578\u5230\u6307\u5b9a\u4f4d\u6578\u3002
\u5728 Say Hello to Python - Input \u4e2d\uff0c\u6709\u7a0d\u5fae\u63d0\u904e\uff0c\u5982\u679c\u4f60\u5b8c\u5168\u6c92\u5370\u8c61\uff0c\u8acb\u4f60\u56de\u53bb\u8907\u7fd2\u4e00\u4e0b\u3002
\u7d66\u4f60\u6d6e\u9ede\u6578 x
\uff0c\u8acb\u4f60\u5370\u51fa x
\u7684\u5e73\u65b9\u6839\u8207\u5e73\u65b9\uff0c\u4e26\u4e14\u53ea\u5370\u51fa\u5c0f\u6578\u9ede\u5f8c\u5169\u4f4d\u3002
x = 3.1415926\nprint(f\"The square root of {x} is {x ** 0.5:.2f}\")\nprint(f\"The square of {x} is {x ** 2:.2f}\")\n
ouputThe square root of 3.1415926 is 1.77\nThe square of 3.1415926 is 9.87\n
@EditTime : 2024-01-27 11:52
"},{"location":"fundamental/python/operators/#bonus_map_for_input","title":"Bonus: map for input","text":"\u518d\u6b21\u8907\u7fd2 Variable and Input - Mutiple Input \u4e2d\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 split()
\u4f86\u5c07\u8f38\u5165\u7684\u5b57\u4e32\u5207\u5272\u6210\u591a\u500b\u5b57\u4e32\u3002
\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u5c07\u9019\u4e9b\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u600e\u9ebc\u505a\u5462?
a, b, c = input().split()\nprint(int(a) + int(b) + int(c))\n
input1 2 3\n
ouput6\n
\u96d6\u7136\u9019\u6a23\u5beb\u4e5f\u53ef\u4ee5\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u8f38\u5165\u5f88\u591a\u500b\u6578\u5b57\uff0c\u9019\u6a23\u5beb\u5c31\u6703\u5f88\u9ebb\u7169\uff0c\u9019\u6642\u5019\u6211\u5011\u53ef\u4ee5\u7528 map()
\u4f86\u5e6b\u52a9\u6211\u5011\u3002
a, b, c = map(int, input().split())\nprint(a + b + c)\n
input4 5 6\n
ouput15\n
map(function, iterable)
\u6703\u5c07 iterable
\u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u4e1f\u9032 function
\u4e2d\uff0c\u5728\u9019\u88e1\u7684 iterable
\u662f input().split()
\uff0c\u800c function
\u662f int
\uff0c\u56e0\u6b64 map(int, input().split())
\u6703\u5c07 input().split()
\u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u8f49\u63db\u6210\u6574\u6578\u3002
\u4f60\u53ef\u4ee5\u5617\u8a66\u5c07\u4f7f\u7528\u5225\u7684\u51fd\u5f0f\uff0c\u4f8b\u5982 float
\u6216 str
\uff0c\u4f46\u8acb\u4f60\u8a18\u5f97\u4e0d\u8981\u52a0\u4e0a\u62ec\u865f\uff0c\u56e0\u70ba\u6211\u5011\u53ea\u662f\u8981\u5c07\u51fd\u5f0f\u7684\u540d\u7a31\u50b3\u9032\u53bb\uff0c\u800c\u4e0d\u662f\u8981\u57f7\u884c\u51fd\u5f0f\u3002
\u6211\u5011\u4f7f\u7528 Unpacking \u7684\u65b9\u5f0f\u4f86\u5c07 map()
\u7684\u7d50\u679c\u6307\u6d3e\u7d66\u8b8a\u6578\u3002\u6211\u76f8\u4fe1\u4f60\u9084\u8a18\u5f97\u4ec0\u9ebc\u662f Unpacking \u5427?
\u5982\u679c\u4f60\u4e0d\u592a\u80fd\u7406\u89e3\uff0c\u4e5f\u6c92\u95dc\u4fc2\uff0c\u5148\u5b78\u6703\u600e\u9ebc\u7528\u5c31\u597d\u3002
"},{"location":"fundamental/python/operators/#practice","title":"Practice","text":"\u6709\u4e86\u672c\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u5176\u5be6\u5df2\u7d93\u53ef\u4ee5\u505a\u5f88\u591a\u984c\u76ee\u4e86\uff0c\u6211\u5011\u4f86\u505a\u4e00\u4e9b\u7df4\u7fd2\u984c\u5427!
Itsa - [C_MM01-\u6613] \u8a08\u7b97\u68af\u578b\u9762\u7a4d
Reference codea, b, h = map(int, input().split())\narea = (a + b) * h / 2\nprint(f\"Trapezoid area:{area}\")\n
Itsa - [C_MM02-\u6613] \u8a08\u7b97\u4e09\u89d2\u5f62\u9762\u7a4d
Reference codea, b = map(int, input().split())\narea = a * b / 2\nprint(area)\n
Itsa - [C_MM04-\u6613] \u8a08\u7b97\u7e3d\u548c\u3001\u4e58\u7a4d\u3001\u5dee\u3001\u5546\u548c\u9918\u6578
Reference codea, b = map(int, input().split())\n\nprint(f\"{a}+{b}={a + b}\")\nprint(f\"{a}*{b}={a * b}\")\nprint(f\"{a}-{b}={a - b}\")\nprint(f\"{a}/{b}={a // b}...{a % b}\")\n
Itsa - [C_MM06-\u6613] \u82f1\u54e9\u8f49\u516c\u91cc
Reference codemile = int(input())\nkm = mile * 1.6\nprint(f\"{km:.1f}\")\n
"},{"location":"fundamental/python/operators/#assignment","title":"Assignment","text":"Itsa - [C_MM07-\u6613] \u8a08\u7b97\u5e73\u65b9\u503c\u8207\u7acb\u65b9\u503c
Itsa - [C_MM08-\u6613] \u8a08\u7b97\u5169\u6578\u548c\u7684\u5e73\u65b9\u503c
Itsa - [C_MM10-\u6613] \u651d\u6c0f\u6eab\u5ea6\u8f49\u83ef\u5f0f\u6eab\u5ea6
Itsa - [C_MM11-\u6613] \u8cfc\u7968\u8a08\u7b97
Itsa - [C_MM12-\u6613] \u76f8\u9047\u6642\u9593\u8a08\u7b97
Itsa - [C_MM14-\u6613] \u8a08\u7b97\u6642\u9593\u7684\u7d44\u5408
@EditTime : 2024-01-28 22:03
"},{"location":"fundamental/python/repetition_structures/","title":"Repetition Structures","text":""},{"location":"fundamental/python/repetition_structures/#introduction","title":"Introduction","text":"\u96fb\u8166\u6700\u6703\u505a\u7684\u4e8b\uff0c\u5c31\u662f\u91cd\u8907\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u9019\u88e1\u8981\u8ddf\u4f60\u4ecb\u7d39\u8ff4\u5708(Loop)\uff0c\u8ff4\u5708\u53ef\u4ee5\u8b93\u6211\u5011\u91cd\u8907\u57f7\u884c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u800c\u4e0d\u7528\u4e00\u884c\u4e00\u884c\u7684\u5beb\u51fa\u4f86\u3002
\u5982\u679c\u4f60\u8981\u5370 \\(100\\) \u6b21 Hello World!
\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?
# ...\nprint(\"Hello World!\")\nprint(\"Hello World!\")\nprint(\"Hello World!\")\n
\u4f60\u6703\u771f\u7684\u5beb \\(100\\) \u884c\u55ce?\u7576\u7136\u4e0d\u6703\uff0c\u4e0d\u7136\u4f60\u6703\u6c23\u6b7b\uff0c\u7136\u5f8c\u628a\u96fb\u8166\u7838\u4e86\u3002
"},{"location":"fundamental/python/repetition_structures/#for_loop","title":"For Loop","text":"\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b for
\u8ff4\u5708\u7684\u7528\u6cd5\u3002\u4e26\u8209\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5370\u51fa \\(1\\) \u5230 \\(5\\):
for i in range(1, 6, 1):\n print(i, end=' ')\n
ouput1 2 3 4 5\n
Time Loop - Purrple Cat
range
\uff0c\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6578\u5217\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u503c\u3001\u7d50\u675f\u503c\u3001\u9593\u9694\u503c\u3002\u9019\u500b\u51fd\u5f0f\u7684\u7528\u6cd5\u5982\u4e0b:
\u5982\u679c\u53ea\u6709\u4e00\u500b\u53c3\u6578\uff0c\u90a3\u5c31\u662f\u7d50\u675f\u503c\uff0c\u8d77\u59cb\u503c\u9810\u8a2d\u70ba \\(0\\)\uff0c\u9593\u9694\u503c\u9810\u8a2d\u70ba \\(1\\)\u3002
range(start=0, stop, step=1)
\u4e26\u4e14\u6ce8\u610f\uff0cstop
\u662f\u4e0d\u5305\u542b\u5728\u6578\u5217\u4e2d\u7684\u3002
\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u60f3\u628a\u5b57\u4e32 \"Speed_of_Light\"
\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u90fd\u5370\u51fa\u4f86\u3002
for c in \"Speed_of_Light\":\n print(c, end=' ')\n
ouput
S p e e d _ o f _ L i g h t \n
DJ OKAWARI feat. \u4e8c\u5bae\u611b \u300cSpeed of Light\u300d \u9019\u88e1\u7684\u8ff4\u5708\u66f4\u63a5\u8fd1\u6982\u5ff5\u4e0a\u7684 Foreach\uff0c\u6bcf\u6b21\u8ff4\u5708\u90fd\u6703\u53d6\u51fa\u5b57\u4e32\u4e2d\u7684\u4e00\u500b\u5b57\u5143\uff0c\u4e26\u4e14\u5c07\u4ed6\u653e\u5230 c
\u4e2d\uff0c\u9019\u500b\u6982\u5ff5\u8acb\u4f60\u8a18\u8d77\u4f86\u3002
\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(\\sum_{i=1}^n{i}=1+2+\\cdots+n\\)
n = int(input())\ntotal = 0\n\n# 1 + 2 + ... + n\nfor i in range(n + 1):\n total += i\n\nprint(total)\n
input100\n
ouput5050\n
\u63a5\u4e0b\u4f86\uff0c\u8ddf\u4f60\u4ecb\u7d39 break
\u8207 continue
\uff0c\u9019\u5169\u500b\u95dc\u9375\u5b57\u53ef\u4ee5\u7528\u4f86\u63a7\u5236\u8ff4\u5708\u7684\u57f7\u884c\u3002
break
\u53ef\u4ee5\u7528\u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u800c continue
\u5247\u662f\u5f37\u5236\u8df3\u5230\u4e0b\u4e00\u6b21\u8ff4\u5708\u3002
for i in range(1, 11):\n if i == 5:\n break\n print(i, end=' ')\n
ouput1 2 3 4\n
\u53ef\u4ee5\u767c\u73fe\uff0ci == 5
\u7684\u6642\u5019\uff0c\u8ff4\u5708\u5c31\u88ab\u5f37\u5236\u4e2d\u65b7\u4e86\u3002
for i in range(1, 11):\n if i == 5:\n continue\n print(i, end=' ')\n
ouput1 2 3 4 6 7 8 9 10\n
\u53ef\u4ee5\u767c\u73fe\uff0c5
\u88ab\u8df3\u904e\u4e86\u3002
\u63a5\u8457\u4ecb\u7d39\u5de2\u72c0\u8ff4\u5708(Nested Loop)\uff0c\u4e5f\u5c31\u662f\u8ff4\u5708\u88e1\u9762\u9084\u6709\u8ff4\u5708\u3002\u505a\u500b\u6bd4\u55bb\u7684\u8a71\uff0c\u5982\u679c\u662f\u5169\u5c64\u7684\u8ff4\u5708\uff0c\u5c31\u50cf\u662f\u6642\u91dd\u8207\u5206\u91dd\uff0c\u5167\u5c64\u8ff4\u5708\u6bcf\u8dd1\u4e00\u5708\uff0c\u5916\u5c64\u8ff4\u5708\u624d\u8dd1\u4e00\u683c\u3002
\u6211\u5011\u4f86\u5370\u4e00\u500b\u76f4\u89d2\u4e09\u89d2\u5f62\u5427\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u5370\u51fa \\(n\\) \u5c64\u7684\u76f4\u89d2\u4e09\u89d2\u5f62\uff0c\u5c07\u6700\u9802\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(0\\)\uff0c\u6700\u5e95\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(n-1\\)\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u5c64\u6709 \\(i+1\\) \u500b\u661f\u661f *
\u3002
n = int(input())\n\nfor i in range(n):\n for j in range(i + 1):\n print('*', end='')\n print()\n
\u6211\u7a31\u5167\u5c64\u7684\u8ff4\u5708\u53eb\u505a j
\u8ff4\u5708\uff0c\u5916\u5c64\u7684\u8ff4\u5708\u53eb\u505a i
\u8ff4\u5708\u3002 j
\u8ff4\u5708\u63a7\u5236\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\uff0c\u800c i
\u8ff4\u5708\u5247\u63a7\u5236\u7e3d\u5171\u6709\u5e7e\u5c64\u3002\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\u90fd\u662f i + 1
\uff0c\u6700\u5f8c\u6703\u63db\u884c\u3002
5\n
output*\n**\n***\n****\n*****\n
\u518d\u4f86\u770b\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u4e5d\u4e5d\u4e58\u6cd5\u8868\u3002
for i in range(1, 10):\n for j in range(1, 10):\n print(f\"{i} * {j} = {i * j}\", end='\\t')\n print()\n
output1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 \n2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 \n3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 \n4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 \n5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 \n6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 \n7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 \n8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 \n9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 \n
i
\u63a7\u5236\u5217(Row)\uff0cj
\u63a7\u5236\u884c(Column)\uff0c\u6bcf\u4e00\u5217\u7684\u6578\u5b57\u90fd\u662f i
\uff0c\u6bcf\u4e00\u884c\u7684\u6578\u5b57\u90fd\u662f j
\uff0c\u6240\u4ee5 i * j
\u5c31\u662f\u76f8\u61c9\u7684\u4e58\u7a4d\u3002
\u71b1\u8eab\u5b8c\u4e86\uff0c\u4f86\u770b\u4e00\u500b\u7a0d\u5fae\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57\uff0c\u5224\u65b7\u4ed6\u662f\u4e0d\u662f\u8cea\u6578\u3002
\u9019\u500b\u4f8b\u5b50\u6703\u662f\u7b49\u7b49\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\u7684\u57fa\u790e\uff0c\u6240\u4ee5\u8acb\u4ed4\u7d30\u770b\u3002
n = int(input())\nis_prime = True\n\nfor i in range(2, n):\n if n % i == 0:\n is_prime = False\n break\n\nif is_prime:\n print(\"Yes\")\nelse:\n print(\"No\")\n
input17\n4843\n
ouputYes\nNo\n
n
\u5982\u679c\u53ef\u4ee5\u6574\u9664 1
\u8207\u81ea\u5df1\u4ee5\u5916\u7684\u6578\u5b57\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 break
\u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8a08\u7b97\u3002
\u6211\u5011\u628a\u554f\u984c\u7528\u5f97\u66f4\u8907\u96dc\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u8f38\u51fa\u5728 \\([1, n]\\) \u4e4b\u9593\u6700\u5927\u7684\u8cea\u6578\u3002
\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u8b93 range
\u5012\u8457\u6578\u56de\u4f86\u3002
n = int(input())\n\nfor i in range(n, 0, -1):\n is_prime = True\n for j in range(2, i):\n if i % j == 0:\n is_prime = False\n break\n\n if is_prime:\n print(f\"{i} is the largest prime in [1, {n}]\")\n break\n
input100\n2\n
output97 is the largest prime in [1, 97]\n2 is the largest prime in [1, 2]\n
i
\u5217\u8209 \\([n, 1]\\) \u7684\u6578\u5b57\uff0c\u800c j
\u5217\u8209 \\([2, i-1]\\) \u7684\u6578\u5b57\uff0c\u5982\u679c i
\u53ef\u4ee5\u6574\u9664 j
\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u99ac\u4e0a\u8df3\u51fa\u5167\u5c64 j
\u8ff4\u5708\uff0c\u7e7c\u7e8c\u5217\u8209\u4e0b\u4e00\u500b\u6578\u5b57\u3002\u5982\u679c i
\u662f\u8cea\u6578\uff0c\u90a3\u5c31\u5370\u51fa\u4f86\uff0c\u4e26\u4e14\u8df3\u51fa\u5916\u5c64 i
\u8ff4\u5708\u3002
\u8acb\u4f60\u4ed4\u7d30\u7aef\u8a73\u9019\u5169\u500b\u4f8b\u5b50\u4e2d\u5f37\u8abf\u7684\u884c\u6578\u3002
@EditTime : 2024-01-30 16:33
"},{"location":"fundamental/python/repetition_structures/#while_loop","title":"While loop","text":"\u63a5\u8457\u4f86\u4ecb\u7d39 while
\u8ff4\u5708\uff0c\u4ed6\u7684\u4f7f\u7528\u5834\u666f\u662f\u7576\u4f60\u4e0d\u77e5\u9053\u8ff4\u5708\u8981\u57f7\u884c\u5e7e\u6b21\u7684\u6642\u5019\uff0c\u5c31\u53ef\u4ee5\u7528 while
\u8ff4\u5708\uff0c\u4f46\u662f\u5225\u5beb\u51fa\u7121\u7aae\u8ff4\u5708(Infinite Loop)\u5594\u3002
\u4f46\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u5148\u4f86\u770b\u5982\u4f55\u7528 while
\u8ff4\u5708\u4f86\u5370\u51fa \\(1\\) \u5230 \\(5\\)\u3002
i = 1\nwhile i < 6:\n print(i, end=\" \")\n i += 1\n
ouput1 2 3 4 5\n
\u56de\u9867\u4e00\u4e0b for loop\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe\u908f\u8f2f\u5176\u5be6\u4e00\u6a23\u3002
\u7576\u689d\u4ef6\u6210\u7acb\u7684\u6642\u5019\uff0c\u5c31\u6703\u57f7\u884c\u8ff4\u5708\uff0c\u76f4\u5230\u689d\u4ef6\u4e0d\u6210\u7acb\u3002
\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\(n!\\)
n = int(input())\ni = 1\nfact = 1\n\nwhile i <= n:\n fact *= i\n i += 1\n\nprint(fact)\n
input5\n1\n
ouput120\n1\n
\u4f46\u76ee\u524d\u9019\u5169\u500b\u4f8b\u5b50\u7121\u6cd5\u770b\u51fa while
\u7684\u9b45\u529b\uff0c\u56e0\u70ba\u4f60\u90fd\u77e5\u9053\u8ff4\u5708\u4ec0\u9ebc\u6642\u5019\u6703\u7d50\u675f\uff0c\u6240\u4ee5\u8ddf\u4f60\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u554f\u984c:
\u8003\u62c9\u8332\u731c\u60f3 wikipedia
\u662f\u6307\u5c0d\u65bc\u6bcf\u4e00\u500b\u6b63\u6574\u6578\uff0c\u5982\u679c\u5b83\u662f\u5947\u6578\uff0c\u5247\u5c0d\u5b83\u4e583\u518d\u52a01\uff0c\u5982\u679c\u5b83\u662f\u5076\u6578\uff0c\u5247\u5c0d\u5b83\u9664\u4ee52\uff0c\u5982\u6b64\u5faa\u74b0\uff0c\u6700\u7d42\u90fd\u80fd\u5920\u5f97\u52301\u3002
\u90a3\u6211\u5011\u8981\u5beb\u7684\u7a0b\u5f0f\u662f\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa\u4ed6\u6703\u5728\u5e7e\u6b65\u5f8c\u8b8a\u6210 \\(1\\)\u3002
\\[ f(n)=\\begin{cases}\\frac{n}{2}, & \\text{if } n \\text{ is even} \\\\ 3n+1, & \\text{if } n \\text{ is odd}\\end{cases} \\]\u4f9d\u7167\u5b9a\u7fa9\uff0c\u6211\u5011\u53ef\u4ee5\u5beb\u51fa\u4ee5\u4e0b\u7a0b\u5f0f\u78bc:
n = int(input())\nstep = 0\n\nwhile n != 1:\n print(n, end=\" -> \")\n if n % 2 == 0:\n n = n // 2\n else:\n n = 3 * n + 1\n step += 1\n\nprint(1)\nprint(f\"step = {step}\")\n
input22\n1\n
ouput22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1\nstep = 15\n1\nstep = 0\n
\u518d\u4f86\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\gt 1\\)\uff0c\u8f38\u51fa \\(n\\) \u7684\u8cea\u56e0\u6578\u5206\u89e3\u3002
\u8acb\u4f60\u5148\u81ea\u5df1\u60f3\u4e00\u4e0b\uff0c\u518d\u770b\u770b\u53c3\u8003\u7a0b\u5f0f\u78bc\u3002
Examplen = int(input())\nfactor = 2\n\nwhile n != 1:\n while n % factor == 0:\n print(factor, end=' ')\n n //= factor\n factor += 1\n
input20\n4843\n
ouput2 2 5\n29 167\n
\u5167\u5c64\u7684 while
\u8ff4\u5708\u6703\u4e00\u76f4\u57f7\u884c\uff0c\u76f4\u5230 n
\u4e0d\u662f factor
\u7684\u500d\u6578\u70ba\u6b62\u3002\u5916\u5c64\u7684 while
\u8ff4\u5708\u5247\u662f\u5217\u8209\u6240\u6709\u7684\u8cea\u56e0\u6578\uff0c\u76f4\u5230 n
\u8b8a\u6210 \\(1\\) \u70ba\u6b62\u3002
\u984c\u5916\u8a71\uff0c\u7576\u521d\u9047\u5230\u9019\u500b\u984c\u76ee\u7684\u6642\u5019\uff0c\u60f3\u4e86\u4e00\u4e0b\u7d50\u679c\u4e00\u6b21\u5c31AC\uff0c\u662f\u5f88\u6709\u6210\u5c31\u611f\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5\u4e5f\u8acb\u4f60\u597d\u597d\u52aa\u529b\uff0c\u9010\u6b65\u57f9\u990a\u81ea\u4fe1\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7684\u3002
@EditTime : 2024-01-30 17:55
"},{"location":"fundamental/python/repetition_structures/#bonus_for_else_and_while_else","title":"Bonus: for ... else and while ... else","text":"for
\u8207 while
\u8ff4\u5708\u90fd\u53ef\u4ee5\u642d\u914d else
\u4f86\u4f7f\u7528\uff0c\u7576\u8ff4\u5708\u6b63\u5e38\u7d50\u675f\u7684\u6642\u5019(\u6c92\u6709 break
)\uff0c\u5c31\u6703\u57f7\u884c else
\u3002
\n
"},{"location":"fundamental/python/repetition_structures/#practice","title":"Practice","text":"Itsa - [C_MM03-\u6613] \u5169\u6578\u7e3d\u548c
Reference codewhile True:\n a, b = map(int, input().split())\n print(a + b)\n
\u9019\u984c\u6703\u51fa\u73fe\u5728\u9019\u88e1\uff0c\u55ae\u7d14\u662f\u56e0\u70ba\u8981\u91cd\u8907\u8f38\u5165\u3002 Itsa - [C_MM26-\u6613] \u8f38\u51fa 1x1\u30012x2\u3001...\u3001NxN\u4e4b\u7d50\u679c
Reference codeN = int(input())\n\nfor i in range(1, N + 1):\n print(f\"{i}*{i}={i * i}\")\n
Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578
Reference codeN = int(input())\n\nfor i in range(6, N + 1, 2):\n s = 0\n for j in range(1, i):\n if i % j == 0:\n s += j\n if s == i:\n if i == 6:\n print(i, end='')\n else:\n print(' ' + str(i), end='')\nprint()\n
\u9019\u662f\u66b4\u529b\u89e3\uff0c\u6703TLE\uff0c\u4e0b\u4e00\u7ae0\u6703\u4ecb\u7d39\u5982\u4f55\u6700\u4f73\u5316\uff0c \u4f46\u5982\u679c\u4f7f\u7528 C++ \u7684\u8a71\uff0c\u76f8\u540c\u908f\u8f2f\u7684\u7a0b\u5f0f\u78bc\u662f\u53ef\u4ee5AC\u7684\u3002 Itsa - [C_MM34-\u6613] \u56e0\u6578\u554f\u984c
Reference codeN = int(input())\n\nprint(1, end=\"\")\nfor i in range(2, N + 1):\n if N % i == 0:\n print(f\" {i}\", end=\"\")\nprint()\n
@EditTime : 2024-01-30 18:40
"},{"location":"fundamental/python/repetition_structures/#assignment","title":"Assignment","text":"Itsa - [C_MM21-\u6613] \u7b97\u968e\u4e58
Itsa - [C_MM25-\u6613] \u8a08\u7b97\u6b63\u6574\u6578\u88ab3\u6574\u9664\u4e4b\u6578\u503c\u4e4b\u7e3d\u548c
Itsa - [C_MM27-\u6613] \u8a08\u7b97\u5169\u6574\u6578\u9593\u6240\u6709\u6574\u6578\u7684\u7e3d\u548c
Itsa - [C_MM28-\u6613] \u8a08\u7b971\u5230N\u4e4b\u9593\u5c6c\u65bc5\u548c7\u7684\u500d\u6578
Itsa - [C_MM29-\u6613] \u6700\u5927\u8cea\u6578\u554f\u984c
Itsa - [C_MM30-\u6613] \u8cea\u6578\u5224\u5225
Itsa - [C_MM31-\u6613] \u8a08\u7b971~N\u5167\u80fd\u88ab2\u8ddf3\u6574\u9664\uff0c\u4f46\u4e0d\u80fd\u88ab12\u6574\u9664\u7684\u6574\u6578\u7e3d\u548c
Itsa - [C_MM40-\u6613] 1~N\u4e4b\u9593\u7684\u7e3d\u548c
Itsa - [C_MM49-\u6613] \u9023\u7e8c1\u7684\u500d\u6578
Itsa - [C_ST09-\u6613] \u661f\u865f\u77e9\u5f62\u8f38\u51fa
Itsa - [C_ST11-\u6613] \u661f\u865f\u83f1\u5f62\u8f38\u51fa
Itsa - [C_ST14-\u6613] \u6578\u5b57\u76f4\u89d2\u4e09\u89d2\u5f62\u8f38\u51fa
@EditTime : 2024-01-30 21:51
"},{"location":"fundamental/python/say_hello/","title":"Say Hello to Python!","text":""},{"location":"fundamental/python/say_hello/#first_program","title":"First Program","text":"\u55e8\uff0c\u9019\u662f\u4f60\u7b2c\u4e00\u500b Python \u7a0b\u5f0f\uff0c\u5728 Pycharm \u4e2d\u5efa\u7acb\u4e00\u500b main.py
\u6a94\u6848\uff0c\u4e26\u5728\u6a94\u6848\u4e2d\u6253\u4e0a :
# print(\"Hello Cat!\")\nprint(\"Hello World!\")\nprint('Hello Python!')\nprint(\"cheung4843\")\n
\u6309\u4e0b\u57f7\u884c\u5f8c\uff0c\u4f60\u7684\u63a7\u5236\u53f0(Console)\u5c07\u6703\u5370\u51fa Hello World!
\u63a5\u8457\u63db\u884c\uff0c\u518d\u5370\u51fa Hello Python!
\uff0c\u63a5\u8457\u518d\u5370\u51fa\u6211 cheung4843
\uff0c\u518d\u63db\u884c\u3002
\u90a3 # print(\"Hello Cat!\")
\u662f\u4ec0\u9ebc?\u4ee5\u4e95\u5b57\u865f\u70ba\u958b\u982d\u7684\uff0c\u88ab\u7a31\u70ba\u8a3b\u89e3\uff0c\u4ed6\u4e0d\u6703\u5728\u7a0b\u5f0f\u4e2d\u88ab\u57f7\u884c\uff0c\u4ed6\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7406\u89e3\u7a0b\u5f0f\u78bc\uff0c\u6216\u8005\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002
\u4e0d\u66c9\u5f97\u4f60\u662f\u5426\u6709\u767c\u73fe \"Hello World!\"
\u8207 'Hello Python!'
\u7684\u5dee\u7570?\u7576\u7136\u9664\u4e86\u5b57\u6bcd\u4e0d\u4e00\u6a23\u4e4b\u5916\uff0c\u9084\u6709\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u7684\u5dee\u5225\uff0c\u4f46\u4ed6\u5011\u90fd\u88ab\u7a31\u70ba\u5b57\u4e32(String)\uff0c\u5728 Python \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6210\u5c0d\u7684\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u4f86\u8868\u9054\u4e00\u500b\u5b57\u4e32\uff0c\u4f46\u662f\u4e0d\u80fd\u6df7\u7528\uff0c\u4f8b\u5982 :
print(\"I love cats')\n
\u57f7\u884c\u5f8c\uff0c\u4f60\u6703\u767c\u73fe\u63a7\u5236\u53f0\u8ddf\u4f60\u5831\u544a\u4e86\u932f\u8aa4\uff0c\u9019\u662f\u8a9e\u6cd5\u932f\u8aa4\uff0c\u800c\u5f8c\u7e8c\u5beb\u7a0b\u5f0f\u7684\u904e\u7a0b\u4e2d\uff0c\u4f60\u5c07\u906d\u9047\u8a31\u591a\u8a9e\u610f\u932f\u8aa4\u3002
\u90a3\u4f60\u77e5\u9053\u4ec0\u9ebc\u662f\u7a7a\u5b57\u4e32\u55ce?
print('')\nprint(\"\")\nprint(\"Above are nothing, right?\")\n
\u57f7\u884c\u5b8c\u5f8c\uff0c\u4f60\u5c31\u77e5\u9053\u7a7a\u5b57\u4e32\u662f\u4ec0\u9ebc\u4e86\u5427?
\u63a5\u8457\u6211\u5011\u4f86\u8b1b\u8b1b print
\u9019\u500b\u6771\u897f\uff0c\u6211\u5011\u5728 Python \u4e2d\u7a31\u4ed6\u70ba\u51fd\u5f0f(Function)\uff0c\u6211\u5011\u8981\u5982\u4f55\u4f7f\u7528\u5462?\u4e5f\u5c31\u662f\u5728\u51fd\u5f0f\u7684\u540d\u7a31\u5f8c\u52a0\u4e0a\u4e00\u500b\u62ec\u865f()
\u3002\u800c print
\u662f Python \u5167\u5efa\u7d66\u6211\u5011\u7684\u300c\u5de5\u5177\u300d\uff0c\u5c31\u662f\u8aaa\uff0c\u6211\u5011\u4e0d\u77e5\u9053\u4ed6\u662f\u600e\u9ebc\u88ab\u9020\u51fa\u4f86\u7684\uff0c\u6211\u5011\u73fe\u968e\u6bb5\u53ea\u8981\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u6216\u8a31\u4f60\u53ef\u80fd\u807d\u904e\u4e00\u500b\u8001\u6389\u7259\u7684\u6bd4\u55bb\uff0c\u7a31\u51fd\u5f0f\u5c31\u50cf\u662f\u4e00\u500b\u9ed1\u76d2\u5b50\u3002
\u95dc\u65bc\u51fd\u5f0f(Function)\uff0c\u6211\u6703\u5728\u5f80\u5f8c\u7684\u7ae0\u7bc0\u8ddf\u4f60\u4ecb\u7d39\uff0c\u800c\u73fe\u5728\u4f60\u53ea\u8981\u77e5\u9053 print()
\u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u5370\u51fa\u4f86\uff0c\u4e26\u63db\u884c\u3002
\u90a3\u5982\u679c\u6211\u4e0d\u60f3\u63db\u884c\u5462?
print(\"You\")\nprint(\"are\")\nprint(\"my\", end=' ')\nprint(\"special\", end='\\n')\nprint(\"1234\", end=\"5\")\nprint(\"---------\")\n
ouputYou\nare\nmy special\n12345---------\n
\u5728\u9019\u500b\u5947\u602a\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u4f60\u767c\u73fe\u6211\u5728 print
\u7684\u62ec\u865f\u4e2d\u591a\u52a0\u4e86\u4e00\u500b end=
\u7684\u6771\u897f\uff0c\u8b93 print
\u5370\u5b8c\u524d\u9762\u7684\u6771\u897f\u5f8c\uff0c\u518d\u5370\u51fa\u7b49\u65bc\u5f8c\u7684\u6771\u897f\u3002
You are my special
\u90a3 \\n
\u662f\u4ec0\u9ebc\u6771\u897f?\u4ed6\u662f\u8df3\u812b\u5b57\u5143(Escape Character)\u5bb6\u65cf\u4e2d\u7684\u4e00\u54e1\uff0c\u4f60\u73fe\u5728\u53ea\u8981\u77e5\u9053\u4ed6\u80fd\u5920\u63db\u884c\u3002
Note
\"cheung4843\"
\u8207 '114514 + 1919810'
\uff0c\u4f46\u8a18\u4f4f\u55ae\u96d9\u5f15\u865f\u4e0d\u5f97\u6df7\u7528\u3002print(x, end=y)
\u6703\u5370\u51fa x
\uff0c\u518d\u5370\u51fa y
\uff0c\u800c y
\u9810\u8a2d\u70ba \\n
\u4e5f\u5c31\u662f\u63db\u884c\u3002@EditTime : 2024-01-25 19:23
Question
\"'\"
\u8207 \"\"\"
\u662f\u5408\u6cd5\u7684\u5b57\u4e32\u55ce?print()
\u62ec\u865f\u5167\u6c92\u6709\u653e\u6771\u897f\u6703\u5370\u51fa\u4ec0\u9ebc?@EditTime : 2024-01-25 19:55
"},{"location":"fundamental/python/selection_structures/","title":"Selection Structures","text":""},{"location":"fundamental/python/selection_structures/#introduction","title":"Introduction","text":"\u5728\u6211\u5011\u4eba\u751f\u4e2d\uff0c\u6211\u5011\u6703\u9762\u81e8\u5f88\u591a\u9078\u64c7\uff0c\u6703\u6839\u64da\u7576\u4e0b\u7684\u60c5\u6cc1\uff0c\u505a\u51fa\u4e0d\u540c\u7684\u6c7a\u5b9a\uff0c\u800c\u7a0b\u5f0f\u4e5f\u662f\u4e00\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u60c5\u6cc1\uff0c\u57f7\u884c\u4e0d\u540c\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u7a31\u70ba\u9078\u64c7\u7d50\u69cb(Selection Structures)\uff0c\u662f\u6d41\u7a0b\u63a7\u5236\u7684\u4e00\u7a2e\u3002
\u4f8b\u5982\uff0c\u6211\u6839\u64da\u5b78\u6e2c\u6210\u7e3e\uff0c\u4f86\u6c7a\u5b9a\u8981\u586b\u54ea\u4e9b\u5fd7\u9858\uff0c\u6216\u662f\u6211\u6839\u64da\u5929\u6c23\uff0c\u4f86\u6c7a\u5b9a\u8981\u4e0d\u8981\u5e36\u5098\u3002
"},{"location":"fundamental/python/selection_structures/#if_elif_else","title":"if ... elif ... else","text":"\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u56de\u60f3 Operators - Comparison Operators \u7684\u5167\u5bb9\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u6bd4\u8f03\u904b\u7b97\u5b50\u4f86\u5f97\u5230\u4e00\u500b\u5e03\u6797\u503c\uff0c\u800c if
\u6703\u6839\u64da True
\u6216 False
\u4f86\u6c7a\u5b9a\u662f\u5426\u57f7\u884c\u67d0\u6bb5\u7a0b\u5f0f\u78bc\u3002
\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50:
\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u8f38\u51fa\u4ed6\u7684\u7d55\u5c0d\u503c\u3002
num = int(input(\"Enter a number: \"))\n\n# get the absolute value of the input\nif num < 0:\n num = -num\n\nprint(num)\n
input-1984\n1984\n
ouput1984\n1984\n
Ivan Torrent - \"1984\" Lyrics Video
\u908f\u8f2f\u5f88\u7c21\u55ae\uff0c\u5982\u679c\u8f38\u5165\u7684\u6578\u5b57\u5c0f\u65bc\u96f6\uff0c\u90a3\u9ebc\u5c31\u5c07\u4ed6\u653e\u4e0a\u8ca0\u865f\u3002\u63d0\u9192\u4f60\u4e00\u4e0b\uff0c\u9019\u88e1\u7684 -
\u8207 not
\u4e00\u6a23\uff0c\u90fd\u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c
\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u53ea\u6709\u7528\u5230 if
\uff0c\u63a5\u8457\u6211\u5011\u6709\u8acb else
\u767b\u5834\u3002
\u8acb\u770b\u4e0b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u5224\u65b7\u4ed6\u662f\u5947\u6578\u9084\u662f\u5076\u6578\u3002
num = int(input(\"Enter a number: \"))\n\n# odd or even\nif num % 2 == 0:\n print(\"Even\")\nelse:\n print(\"Odd\")\n
input1983\n-1982\n
ouputOdd\nEven\n
Timecop1983 - On the Run
\u9019\u88e1\u6211\u5011\u7528\u5230\u4e86 else
\uff0c\u7576 if
\u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c else
\u7684\u5167\u5bb9\u3002
\u6211\u5011\u518d\u4f86\u770b\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4f60\u7684\u5206\u6578\uff0c\u8f38\u51fa\u4f60\u7684\u8a55\u50f9\uff0c\u56e0\u70ba\u6709\u5f88\u591a\u7a2e\u8a55\u50f9\uff0c\u6240\u4ee5\u9700\u8981\u7528\u5230 elif
\u3002
\u7576 if
\u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u9762 elif
\u7684\u689d\u4ef6\uff0c\u5982\u679c elif
\u7684\u689d\u4ef6\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c elif
\u7684\u5167\u5bb9\uff0c\u5982\u679c\u76ee\u524d\u7684 elif
\u7684\u689d\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u4e00\u500b elif
\u7684\u689d\u4ef6\uff0c\u5982\u679c\u6240\u6709\u7684 elif
\u7684\u689d\u4ef6\u90fd\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c else
\u7684\u5167\u5bb9\u3002
\u9019\u6a23\u8b1b\u6216\u8a31\u6709\u9ede\u7e5e\u53e3\uff0c\u4f60\u53ef\u4ee5\u770b\u770b\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u61c9\u8a72\u5c31\u80fd\u7406\u89e3\u4e86\u3002
score = int(input(\"Enter your score: \"))\n\nif score >= 90:\n print(\"A\")\n print(\"Excellent!\")\nelif score >= 80:\n print(\"B\")\nelif score >= 70:\n print(\"C\")\nelif score >= 60:\n print(\"D\")\nelse:\n print(\"F\")\n
input60\n90\n49\n
ouputD\nA\nExcellent!\nF\n
\u63a5\u4e0b\u4f86\u7d66\u4f60\u4e00\u500b\u53ef\u80fd\u6703\u7591\u60d1\u7684\u4f8b\u5b50:
score = int(input(\"Enter your score: \"))\nif score >= 60:\n print(\"D\")\nelif score >= 70:\n print(\"C\")\nelif score >= 80:\n print(\"B\")\nelif score >= 90:\n print(\"A\")\n print(\"Excellent!\")\nelse:\n print(\"F\")\n
input90\n
ouputD\n
\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u5947\u602a\uff0c\u70ba\u4ec0\u9ebc\u8f38\u5165 90
\u6703\u5370\u51fa D
\uff0c\u800c\u4e0d\u662f A
\uff0c\u9019\u662f\u56e0\u70ba if
\u7684\u689d\u4ef6\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c if
\u7684\u5167\u5bb9\uff0c\u5c31\u4e0d\u6703\u6aa2\u67e5 elif
\u7684\u689d\u4ef6\u4e86\u3002
\u6240\u4ee5\uff0c\u4f60\u5728\u64b0\u5beb\u689d\u4ef6\u5f0f\u7684\u6642\u5019\uff0c\u8981\u6ce8\u610f\u9806\u5e8f\uff0c\u4ee5\u53ca\u78ba\u4fdd\u6bcf\u500b\u689d\u4ef6\u662f\u5426\u662f\u4e92\u65a5\u7684\u3002
score = int(input(\"Enter your score: \"))\n\nif 70 > score >= 60:\n print(\"D\")\nelif 80 > score >= 70:\n print(\"C\")\nelif 90 > score >= 80:\n print(\"B\")\nelif 100 >= score >= 90:\n print(\"A\")\n print(\"Excellent!\")\nelse:\n print(\"F\")\n
input90\n4843\n55\n
ouputA\nExcellent!\nF\nF\n
\u9019\u6a23\u5c31\u4e0d\u6703\u6709\u554f\u984c\u4e86\uff0c\u4f46\u662f\uff0c\u7576\u6211\u8f38\u5165\u8d85\u904e 100
\u7684\u5206\u6578\u6642\uff0c\u662f\u6703\u5370\u51fa F
\u7684\uff0c\u4f60\u8a72\u600e\u9ebc\u89e3\u6c7a\u5462?
if
\u7684\u5167\u5bb9\u53ef\u4ee5\u662f\u53e6\u4e00\u500b if
\uff0c\u9019\u7a2e\u7d50\u69cb\u7a31\u70ba\u5de2\u72c0\u689d\u4ef6\u5f0f(Nested if)\u3002
\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6709\u4e09\u500b\u6574\u6578 a, b, c
\uff0c\u8acb\u4f60\u8f38\u51fa\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002
a, b, c = map(int, input().split())\nmax_one = None\n\nif a > b:\n max_one = a\n if c > max_one:\n max_one = c\nelse:\n max_one = b\n if c > max_one:\n max_one = c\n\nprint(max_one)\n
input10 20 30\n12 12 12\n-3 -4 -5\n
ouput30\n12\n-3\n
\u60f3\u6cd5\u5f88\u7c21\u55ae\uff0c\u5148\u8b93 a, b
\u9032\u884c\u6bd4\u8f03\uff0c\u5f97\u5230\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\uff0c\u518d\u8b93 c
\u8207 max_one
\u6bd4\u8f03\uff0c\u5f97\u5230\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002
\u9019\u88e1\u5077\u5077\u544a\u8a34\u4f60\u4e00\u500b\u795e\u5947\u5999\u5999\u5de5\u5177\uff0cmax()
\uff0c\u4ed6\u6703\u627e\u51fa\u62ec\u865f\u5167\u7684\u6771\u897f\u4e2d\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002
a, b, c = map(int, input().split())\nprint(max(a, b, c))\n
input114514 1919 810\n
ouput114514\n
"},{"location":"fundamental/python/selection_structures/#match_case","title":"match ... case","text":"\u5728 Python 3.10 \u4e2d\uff0c\u65b0\u589e\u4e86 match ... case
\uff0c\u4f46\u907a\u61be\u7684\u662f\u4e00\u4e9b\u53e4\u8001\u7684 Online Judge \u7684 Python \u7248\u672c\u592a\u820a\u4e0d\u80fd\u7528\uff0c\u4f46\u6211\u9084\u662f\u60f3\u4ecb\u7d39\u7d66\u4f60\u3002
\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5728 if ... elif ... else
\u4e2d\u7684\u4f8b\u5b50:
month = int(input(\"Enter a month (1-12): \"))\n\n# which season?\nif 3 <= month <= 5:\n print(\"Spring\")\nelif 6 <= month <= 8:\n print(\"Summer\")\nelif 9 <= month <= 11:\n print(\"Fall\")\nelif month == 12 or month == 1 or month == 2:\n print(\"Winter\")\nelse:\n print(\"Invalid month\")\n
input4\n7\n10\n1\n13\n
ouputSpring\nSummer\nFall\nWinter\nInvalid month\n
\u518d\u4f86\u770b\u770b match ... case
\u7684\u7248\u672c:
month = int(input(\"Enter a month (1-12): \"))\n\nmatch month:\n case 3 | 4 | 5:\n print(\"Spring\")\n case 6 | 7 | 8:\n print(\"Summer\")\n case 9 | 10 | 11:\n print(\"Autumn\")\n case 12 | 1 | 2:\n print(\"Winter\")\n case _:\n print(\"Invalid month\")\n
input270\n11\n3\n
ouputInvalid month\nAutumn\nSpring\n
\u662f\u4e0d\u662f\u5f88\u7c21\u6f54\u5462?\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u50cf\u662f C, C++, Java
\u7b49\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u770b\u904e switch .. case
\uff0c\u4f46\u662f match ... case
\u66f4\u5f37\u5927!
\u95dc\u65bc match ... case
\u6211\u5c31\u4ecb\u7d39\u5230\u9019\u88e1\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u4f60\u8981\u77e5\u9053 if ... elif ... else
\u7684\u7528\u6cd5\u3002
@EditTime : 2024-01-29 12:32
"},{"location":"fundamental/python/selection_structures/#practice","title":"Practice","text":"Itsa - [C_MM09-\u6613] \u8a08\u7b97 i \u6b21\u65b9\u7684\u503c
Reference codei = int(input())\nif i > 31:\n print(\"Value of more than 31\")\nelse:\n print(1 << i)\n
Itsa - [C_MM13-\u6613] \u505c\u8eca\u8cbb\u8a08\u7b97
Reference codeh1, m1 = map(int, input().split())\nh2, m2 = map(int, input().split())\n\npaid = 0\nminutes = (h2 - h1) * 60 + (m2 - m1)\nif minutes > 240:\n paid += ((minutes - 240) // 30) * 60\n minutes = 240\nif minutes > 120:\n paid += ((minutes - 120) // 30) * 40\n minutes = 120\npaid += (minutes // 30) * 30\n\nprint(paid)\n
Itsa - [C_MM15-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u6b63\u65b9\u5f62\u7684\u7bc4\u570d\u5167
Reference codex, y = map(int, input().split())\n\nif 0 <= x <= 100 and 0 <= y <= 100:\n print(\"inside\")\nelse:\n print(\"outside\")\n
"},{"location":"fundamental/python/selection_structures/#assignment","title":"Assignment","text":"Itsa - [C_MM16-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u5713\u5f62\u7684\u7bc4\u570d\u5167
Itsa - [C_MM19-\u6613] \u96fb\u8a71\u8cbb\u8a08\u7b97
Itsa - [C_MM24-\u6613] \u8a08\u7b97\u85aa\u6c34
Itsa - [C_MM32-\u6613] Armstrong\u6578
Itsa - [C_MM35-\u6613] \u5e73\u3001\u958f\u5e74\u5224\u5b9a
Itsa - [C_MM36-\u6613] \u5b63\u7bc0\u5224\u5b9a
Itsa - [C_MM37-\u6613] \u5224\u65b7\u5ea7\u6a19\u4f4d\u65bc\u4f55\u8655
Itsa - [C_MM38-\u6613] \u5224\u65b73\u6574\u6578\u662f\u5426\u80fd\u69cb\u6210\u4e09\u89d2\u5f62\u4e4b\u4e09\u908a\u9577
Itsa - [C_MM39-\u6613] \u5224\u65b7\u662f\u4f55\u7a2e\u4e09\u89d2\u5f62
Itsa - [C_MM46-\u6613] \u8907\u6578\u904b\u7b97
Itsa - [C_AR36-\u6613] \u661f\u5ea7\u67e5\u8a62
@EditTime : 2024-01-29 14:38
"},{"location":"fundamental/python/sets/","title":"Sets","text":""},{"location":"fundamental/python/sets/#introduction","title":"Introduction","text":"\u5982\u679c\u7d66\u4f60\u4e00\u500b\u4e32\u5217\uff0c\u8acb\u4f60\u6aa2\u67e5\u88e1\u9762\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?\u4f60\u6703\u600e\u9ebc\u6a23\u78ba\u4fdd\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\u5462?
\u5728\u9019\u4e00\u7ae0\uff0c\u6211\u5011\u5c07\u6703\u5b78\u7fd2\u5230\u96c6\u5408(Set)\uff0c\u5b83\u6709\u4ee5\u4e0b\u7279\u6027:
{}
\u4f86\u8868\u793a\uff0c\u88e1\u9762\u7684\u5143\u7d20\u7528\u9017\u865f,
\u9694\u958b\u4f60\u53ef\u4ee5\u7528set()
\u4f86\u5efa\u7acb\u4e00\u500b\u96c6\u5408\uff0c\u6216\u8005\u7528\u5927\u62ec\u865f{}
\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:
a = set(\"FOREVER\")\nprint(a)\n\nb = [4, 8, 4, 3]\nc = set(b)\nprint(c)\n\nd = {(0, 1), (1, 2), (2, 3)}\nprint(d)\nprint(type(d))\n\ne = {}\nprint(type(e))\n
Output{'R', 'F', 'O', 'E', 'V'}\n{8, 3, 4}\n{(2, 3), (1, 2), (0, 1)}\n<class 'set'>\n<class 'dict'>\n
SUHO(\uc218\ud638) _ FOREVER
\u4f60\u5f97\u6ce8\u610f\uff0c\u5efa\u7acb\u7a7a\u96c6\u5408\u7684\u6642\u5019\uff0c\u4f60\u5fc5\u9808\u7528set()
\uff0c\u56e0\u70ba{}
\u662f\u7528\u4f86\u5efa\u7acb\u7a7a\u5b57\u5178\u7684\u3002
\u4ee5\u53ca\u6240\u8b02\u7684\u7121\u5e8f\uff0c\u4f60\u8a66\u8457\u591a\u57f7\u884c\u5e7e\u6b21\u7a0b\u5f0f\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u6bcf\u6b21\u8f38\u51fa\u7684\u7d50\u679c\u90fd\u4e0d\u4e00\u6a23\u3002
\u4f60\u751a\u81f3\u53ef\u4ee5\u7528 Comprehension \u4f86\u5efa\u7acb\u96c6\u5408\u3002
a = {1, 2, 3}\nb = {3, 4, 5}\ndiff_b = {x for x in b if x not in a}\nprint(diff_b)\n\nc = {x ** 2 for x in range(-3, 4)}\nprint(c)\n
Output{4, 5}\n{0, 9, 4, 1}\n
"},{"location":"fundamental/python/sets/#operations","title":"Operations","text":""},{"location":"fundamental/python/sets/#accessing_elements","title":"Accessing elements","text":"\u56e0\u70ba\u96c6\u5408\u662f\u7121\u5e8f\u7684\uff0c\u6240\u4ee5\u4f60\u4e0d\u80fd\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5143\u7d20\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u7528in
\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u53ca\u642d\u914dfor
\u8ff4\u5708\u4f86\u904d\u6b77\u96c6\u5408\u3002
perfect_nums_set = {6, 28, 496, 8128, 33550336}\nprint(4843 in perfect_nums_set)\n\nfor num in perfect_nums_set:\n print(num, end=' ')\n
OutputFalse\n33550336 8128 496 6 28 \n
Question
\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u96c6\u5408\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728?
\u7b54\u6848\u53ef\u80fd\u6703\u8b93\u4f60\u9a5a\u8a1d\uff0c\u4e0d\u9700\u8981 \\(n\\) \u500b\u6b65\u9a5f\uff0c\u53ea\u8981\u5e7e\u500b\u6b65\u9a5f\u5c31\u597d\uff0c\u53ef\u4ee5\u8aaa\u662f\u99ac\u4e0a\u3002
"},{"location":"fundamental/python/sets/#union","title":"Union","text":"\u4f60\u53ef\u4ee5\u7528|
\u6216\u8005 union()
\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u806f\u96c6\u3002
a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.union(b))\nc = a | b\nprint(c)\n\na |= b\nprint(a)\n
Output{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n
"},{"location":"fundamental/python/sets/#intersection","title":"Intersection","text":"\u4f60\u53ef\u4ee5\u7528&
\u6216\u8005 intersection()
\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\u3002
a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.intersection(b))\nc = a & b\nprint(c)\n\na &= b\nprint(a)\n
Output{5}\n{5}\n{5}\n
"},{"location":"fundamental/python/sets/#difference","title":"Difference","text":"\u4f60\u53ef\u4ee5\u7528-
\u6216\u8005 difference()
\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u5dee\u96c6\u3002
a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.difference(b))\nc = b - a\nprint(c)\n\na -= b\nprint(a)\n
Output{1, 3}\n{2, 4, 6}\n{1, 3}\n
\u6211\u5c31\u8209\u9019\u4e09\u500b\u4f8b\u5b50\uff0c\u5176\u4ed6\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u3002
"},{"location":"fundamental/python/sets/#methods","title":"Methods","text":""},{"location":"fundamental/python/sets/#adding_elements","title":"Adding elements","text":"\u4f60\u53ef\u4ee5\u7528 add()
\u4f86\u65b0\u589e\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002
nums_set = {1, 2, 3, 4, 5}\nnums_set.add(6)\nnums_set.add(3)\nprint(nums_set)\n
Output{1, 2, 3, 4, 5, 6}\n
\u4e5f\u53ef\u4ee5\u7528 update()
\u4f86\u65b0\u589e\u591a\u500b\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002
\u5176\u5be6\u5c31\u8ddf |=
\u4e00\u6a23\u3002
nums_set = {1, 2, 3, 4, 5}\nnums_set.update({6, 7})\nprint(nums_set)\n\nnums_set.update([1, 8, 9], {10, 11})\nprint(nums_set)\n
Output{1, 2, 3, 4, 5, 6, 7}\n{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\n
"},{"location":"fundamental/python/sets/#removing_elements","title":"Removing elements","text":"\u4f60\u53ef\u4ee5\u7528 remove()
\u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u6703\u62cb\u51fa\u932f\u8aa4\u3002
a = {1, 2, 3}\na.remove(1)\nprint(a)\na.remove(1)\nprint(a)\n
Output{2, 3}\nKeyError: 1\n
\u6216\u8005\u7528 discard()
\u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4e0d\u6703\u62cb\u51fa\u932f\u8aa4\u3002
a = {1, 2, 3}\na.discard(2)\nprint(a)\na.discard(2)\nprint(a)\n
Output{1, 3}\n{1, 3}\n
\u90a3\u600e\u9ebc\u6e05\u7a7a\u5462?\u4f60\u53ef\u4ee5\u7528 clear()
\u4f86\u6e05\u7a7a\u96c6\u5408\u3002
a = {1, 2, 3}\na.clear()\nprint(len(a))\n
Output0\n
\u9084\u6709\u66f4\u591a\u7684\u65b9\u6cd5\uff0c\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\uff0c\u4f46\u9019\u4e9b\u76ee\u524d\u61c9\u8a72\u5920\u4f60\u7528\u3002
"},{"location":"fundamental/python/sets/#practice","title":"Practice","text":"Itsa - [C_AR20-\u6613] \u6aa2\u67e5\u6578\u503c\u662f\u5426\u6709\u91cd\u8907
Reference coden = int(input())\nnum_set = set(input().split())\n\nif len(num_set) == n:\n print(1)\nelse:\n print(0)\n
\u6aa2\u67e5\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u9019\u500b\u554f\u984c\u5c31\u5f88\u9069\u5408\u7528\u96c6\u5408\u4f86\u89e3\u6c7a\uff0c\u56e0\u70ba\u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u53ea\u8981\u6aa2\u67e5\u96c6\u5408\u7684\u9577\u5ea6\u662f\u5426\u7b49\u65bc\u8f38\u5165\u4e32\u5217\u7684\u9577\u5ea6\u5c31\u597d\u3002"},{"location":"fundamental/python/sets/#assignment","title":"Assignment","text":"\u5728 Itsa \u4e0a\u4e0d\u592a\u597d\u627e\u984c\u76ee\uff0c\u6240\u4ee5\u4e7e\u8106\u9644\u4e0a\u89e3\u7b54\u3002
Itsa - [C_ST23-\u6613] \u76f8\u540c\u5b57\u96c6\u7684\u5b57\u4e32
Reference codea, b = input().split(',')\na_set = set(str.lower(a))\na_set.discard(\" \")\nb_set = set(str.lower(b))\nb_set.discard(\" \")\n\nif a_set == b_set:\n print(1)\nelse:\n print(0)\n
\u9019\u88e1\u61c9\u8a72\u80fd\u611f\u53d7 discard()
\u7684\u7528\u8655\uff0c\u984c\u76ee\u8981\u6c42\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\uff0c\u6240\u4ee5\u6211\u5011\u5148\u628a\u5b57\u4e32\u8f49\u6210\u5c0f\u5beb\uff0c\u7136\u5f8c\u518d\u628a\u7a7a\u767d\u5b57\u5143\u79fb\u9664(\u4e0d\u7ba1\u6709\u6c92\u6709)\uff0c\u6700\u5f8c\u518d\u6aa2\u67e5\u5169\u500b\u96c6\u5408\u662f\u5426\u76f8\u7b49\u3002 Itsa - [C_ST82-\u6613] \u4ea4\u96c6
Itsa - [C_ST83-\u6613] \u806f\u96c6
Itsa - [C_ST84-\u6613] \u5dee\u96c6
Itsa - [C_AR192-\u6613] \u5224\u65ad\u4efb\u610f\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\u662f\u5426\u6709\u91cd\u8907
@EditTime : 2024-02-06 23:16
"},{"location":"fundamental/python/tuples/","title":"Tuples","text":""},{"location":"fundamental/python/tuples/#introduction","title":"Introduction","text":"\u6709\u4e86\u4e0a\u4e00\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u6211\u76f8\u4fe1\u5e8f\u5c0d(Tuple)\u5c0d\u4f60\u4f86\u8aaa\u4e0d\u6703\u56f0\u96e3\uff0c\u56e0\u70ba\u5b83\u8ddf\u4e32\u5217(List)\u5f88\u50cf\uff0c\u53ea\u662f\u5b83\u662f\u7528\u5c0f\u62ec\u865f()
\u4f86\u8868\u793a\uff0c\u800c\u4e14\u5b83\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\u3002\u9019\u8868\u793a\u4f60\u4e0d\u80fd\u65b0\u589e\u3001\u522a\u9664\u6216\u4fee\u6539\u88e1\u9762\u7684\u5143\u7d20\u3002\u90a3\u65e2\u7136\u5982\u6b64\uff0c\u70ba\u4ec0\u9ebc\u9084\u8981\u7528\u5b83\u5462?
\u8981\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\u6709\u5f88\u591a\u7a2e\u65b9\u6cd5\uff0c\u6700\u7c21\u55ae\u7684\u5c31\u662f\u7528\u5c0f\u62ec\u865f()
\u628a\u5143\u7d20\u5305\u8d77\u4f86\uff0c\u7528\u9017\u865f,
\u9694\u958b\u5c31\u597d\u4e86\u3002
\u4f46\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u53ea\u6709\u4e00\u500b\u5143\u7d20\u6642\uff0c\u4f60\u5fc5\u9808\u5728\u5f8c\u9762\u52a0\u4e0a\u9017\u865f\u3002\u9019\u908a\u4f60\u81ea\u5df1\u8a66\u8a66\u770b\uff0c\u61c9\u8a72\u90fd\u80fd\u7406\u89e3\u3002
empty = ()\none = (1,)\ntwo = (1, 2)\nprint(len(empty), len(one), len(two))\nprint(type(empty))\n\nodd = 1, 3, 5\nprint(odd)\nprint(type(odd))\n
Output0 1 2\n<class 'tuple'>\n(1, 3, 5)\n(1, 3, 5)\n<class 'tuple'>\n
\u4f60\u4e5f\u53ef\u4ee5\u7528tuple()
\u4f86\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:
lst = [1, 2, 3, 4, 5]\ntup = tuple(lst)\nprint(tup)\n\ns = \"know me...\"\ns_tup = tuple(s)\nprint(s_tup)\n
Output(1, 2, 3, 4, 5)\n('k', 'n', 'o', 'w', ' ', 'm', 'e', '.', '.', '.')\n
\u516b\u6728\u6d77\u8389 \u300eknow me...\u300f
\u9084\u8a18\u5f97 List Comprehension \u55ce?\u6211\u5011\u5c07\u4e2d\u62ec\u865f\u6539\u6210\u5c0f\u62ec\u865f\u8a66\u8a66\u770b:
gen = (x ** 2 for x in range(5))\nprint(type(gen))\ntup = tuple(gen)\nprint(tup)\n
Output<class 'generator'>\n(0, 1, 4, 9, 16)\n
\u4f60\u6703\u767c\u73fe\uff0c\u7d50\u679c\u4e26\u975e\u662f\u4f60\u9810\u671f\u7684\u5e8f\u5c0d\uff0c\u800c\u662f\u7522\u751f\u5668(Generator)\uff0c\u4f60\u9084\u9700\u8981\u4f7f\u7528tuple()
\u4f86\u8f49\u63db\u3002
\u81f3\u65bc\u70ba\u4ec0\u9ebc\u8981\u7528\u7522\u751f\u5668(Generator)\uff0c\u6211\u6703\u5728\u672a\u4f86\u7684\u7ae0\u7bc0\u8ddf\u4f60\u8aaa\u660e\uff0c\u53c8\u6316\u5751\u4e86\u3002
\u90a3\u9ebc\u52a0\u6cd5\u8ddf\u4e58\u6cd5\u5462?\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u52a0\u6cd5\u4f86\u5408\u4f75\u5169\u500b\u5e8f\u5c0d\uff0c\u7528\u4e58\u6cd5\u4f86\u8907\u88fd\u5e8f\u5c0d\u3002
a = (1, 2, 3)\nb = 4, 5, 6\nc = a + b\nprint(c)\n\nd = a * 2 + b\nprint(d)\n
Output(1, 2, 3, 4, 5, 6)\n(1, 2, 3, 1, 2, 3, 4, 5, 6)\n
"},{"location":"fundamental/python/tuples/#operations","title":"Operations","text":""},{"location":"fundamental/python/tuples/#accessing_elements","title":"Accessing elements","text":"\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u7528\u8ca0\u7d22\u5f15\u4f86\u5f9e\u5f8c\u5b58\u53d6\u3002
t = (1, 2, 3, 4, 5)\nprint(t[0], t[-1])\n
Output1 5\n
"},{"location":"fundamental/python/tuples/#slicing","title":"Slicing","text":"\u540c\u6a23\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u5207\u7247\u4f86\u53d6\u5f97\u5e8f\u5c0d\u4e2d\u7684\u5b50\u5e8f\u5c0d\u3002
t = (1, 2, 3, 4, 5)\nprint(t[1:3])\nprint(t[:3])\nprint(t[3:])\nprint(t[:])\n
Output(2, 3)\n(1, 2, 3)\n(4, 5)\n(1, 2, 3, 4, 5)\n
"},{"location":"fundamental/python/tuples/#modifying_elements","title":"Modifying elements","text":"\u4f46\u662f\u4f60\u4e0d\u80fd\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u9019\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u662f\u6709\u6298\u8877\u7684\u65b9\u6cd5\u5566\uff0c\u5c31\u662f\u628a\u5e8f\u5c0d\u8f49\u63db\u6210\u4e32\u5217\uff0c\u518d\u8f49\u63db\u56de\u4f86\u3002
cat_tup = (\"\ud83d\ude38\", \"\ud83d\ude3a\", \"\ud83d\ude3b\", [\"\ud83d\ude3f\", \"\ud83d\ude40\"])\n\ncat_lst = list(cat_tup)\ncat_lst[1] = \"\ud83d\ude3c\"\ncat_tup = tuple(cat_lst)\nprint(cat_tup)\n\ncat_tup[3][0] = \"\ud83d\ude3e\"\nprint(cat_tup)\n\ncat_tup[1] = \"\ud83d\ude3d\"\nprint(cat_tup)\n
Output('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3f', '\ud83d\ude40'])\n('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3e', '\ud83d\ude40'])\nTypeError: 'tuple' object does not support item assignment\n
\u4f46\u4f60\u6709\u6c92\u6709\u89ba\u5f97\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u6211\u53ef\u4ee5\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u4e32\u5217\u5462?
"},{"location":"fundamental/python/tuples/#checking_elements","title":"Checking elements","text":"\u4f60\u53ef\u4ee5\u7528 in
\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5728\u5e8f\u5c0d\u4e2d\uff0c\u9019\u8ddf\u4e32\u5217\u662f\u4e00\u6a23\u7684\u3002
fib = (0, 1, 1, 2, 3, 5, 8)\nprint(0 not in fib)\nprint(5 in fib)\n
OutputFalse\nTrue\n
Question
\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u5e8f\u5c0d\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u5e8f\u5c0d\u4e2d\uff0c\u6700\u597d\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?
\u53c8\u662f\u719f\u6089\u7684\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u7684\u3002
"},{"location":"fundamental/python/tuples/#methods","title":"Methods","text":"\u56e0\u70ba\u5e8f\u5c0d\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u6240\u4ee5\u53ea\u6709\u5169\u500b\u65b9\u6cd5\uff0c\u4e00\u500b\u662fcount()
\uff0c\u4e00\u500b\u662findex()
\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u8a66\u8a66\u770b\u3002
count()
\u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u7576\u7136 List \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\uff0c\u4f46\u524d\u9762\u6211\u4e26\u6c92\u6709\u63d0\uff0c\u56e0\u70ba\u53ef\u4ee5\u653e\u5728\u9019\u88e1\u6c34\u5167\u5bb9(X
t = ((1, 2), (3, 4), 6, 6, [7, 8])\nprint(t.count(1))\nprint(t.count([7, 8]))\nprint(t.count(6))\n
Output0\n1\n2\n
"},{"location":"fundamental/python/tuples/#index","title":"index","text":"index()
\u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u7684\u7d22\u5f15\uff0c\u4e00\u6a23\u7684\uff0cList \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\u3002
t = (\"Love Me Again\", \"John Newman\", 2013)\nprint(t.index(\"John Newman\"))\nprint(t.index(2014))\n
Output1\nValueError: tuple.index(x): x not in tuple\n
John Newman - Love Me Again
"},{"location":"fundamental/python/tuples/#tuple_vs_list","title":"Tuple vs List","text":"[]
\u8868\u793a()
\u8868\u793a\u95dc\u65bc\u300c\u53ef\u96dc\u6e4a\u7684(hashable)\u300d\uff0c\u6211\u6703\u5728\u5b57\u5178(Dict)\u9019\u7ae0\u4e2d\u8ddf\u4f60\u8aaa\u660e\u3002
"},{"location":"fundamental/python/tuples/#practice","title":"Practice","text":"Itsa - [C_AR04-\u6613] \u908a\u7de3\u5075\u6e2c
Reference codeN = int(input())\n\ndirs = ((0, 1), (0, -1), (1, 0), (-1, 0))\n\nfor k in range(N):\n n, m = map(int, input().split())\n pic = []\n for _ in range(n):\n pic.append(input().split())\n\n for i in range(n):\n for j in range(m):\n if pic[i][j] == '0':\n print('_ ', end='')\n else:\n is_edge = False\n for d in dirs:\n x, y = i + d[0], j + d[1]\n if 0 <= x < n and 0 <= y < m and pic[x][y] == '0':\n is_edge = True\n break\n if is_edge:\n print('0 ', end='')\n else:\n print('_ ', end='')\n print()\n\n if k != N - 1:\n print()\n
\u65b9\u5411 dirs
\u5c31\u5f88\u9069\u7528\u5e8f\u5c0d\u4f86\u8868\u793a\uff0c\u56e0\u70ba\u5b83\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u6703\u8b8a\u52d5\u3002 \u672a\u4f86\u5728\u5b78\u5716\u5f62\u8d70\u8a2a\u7684\u6642\u5019\uff0c\u4f60\u6703\u4e00\u76f4\u770b\u5230\u9019\u7a2e\u5beb\u6cd5\u3002
\u5c0d\u4e86\uff0c \u8ff4\u5708\u8b8a\u6578 _
\u88ab\u7a31\u70ba\u6368\u68c4\u8b8a\u6578\uff0c\u8868\u793a\u4e0d\u9700\u8981\u7528\u5230\u9019\u500b\u8b8a\u6578\uff0c\u53ea\u662f\u70ba\u4e86\u914d\u5408\u8ff4\u5708\u8a9e\u6cd5\u800c\u5df2\u3002
Itsa - [C_AR119-\u6613] \u5730\u96f7\u5371\u96aa\u6307\u6578\u8868
Itsa - [C_AR139-\u6613] \u9ec3\u91d1\u63a2\u6e2c
Itsa - [C-AR140-\u6613] \u6c42\u6700\u5927\u7ce7\u98df\u7522\u91cf
@EditTime : 2024-02-04 21:15
"},{"location":"fundamental/python/variable_and_input/","title":"Variable and Input","text":""},{"location":"fundamental/python/variable_and_input/#variable","title":"Variable","text":"\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f86\u770b\u600e\u9ebc\u4f7f\u7528\u8b8a\u6578\uff0c\u4ee5\u53ca\u8f38\u5165\u81ea\u5df1\u60f3\u8981\u7684\u6771\u897f\u3002
x = 4843\nprint(x)\n\nx = \"Memory Reboot\"\nprint(x)\n\nx = '4843'\nprint(x)\n
\u6216\u8a31\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u795e\u5947\uff0c\u70ba\u4ec0\u9ebc x
\u53ef\u4ee5\u8b8a\u6210\u6578\u5b57\uff0c\u53c8\u53ef\u4ee5\u8b8a\u6210\u5b57\u4e32\uff0c\u9084\u6709\u70ba\u4ec0\u9ebc\u7b2c\u4e00\u500b\u8207\u7b2c\u4e09\u500b\u7684\u8f38\u51fa\u6703\u662f\u76f8\u540c\u7684\u3002
\u518d\u4f86\u7528\u4e00\u4e9b old-school \u7684\u6bd4\u55bb\uff0c\u4f60\u53ef\u4ee5\u5c07 x
\u60f3\u50cf\u6210\u4e00\u500b\u7bb1\u5b50\uff0c\u800c\u7b49\u865f\u53f3\u908a\u7684\u300c\u503c\u300d\u5c31\u662f\u7bb1\u5b50\u88e1\u88dd\u7684\u6771\u897f\u3002\u65e2\u7136\u7bb1\u5b50\u88e1\u9762\u7684\u6771\u897f\u53ef\u4ee5\u8b8a\uff0c\u90a3\u6211\u662f\u4e0d\u662f\u53ef\u4ee5\u8aaa x
\u662f\u4e00\u500b\u8b8a\u6578\u5462?
\u4f46\u8acb\u4f60\u5148\u5fd8\u6389\u9019\u500b\u6bd4\u55bb\uff0c\u56e0\u70ba\u9019\u500b\u6bd4\u55bb\u4e26\u4e0d\u5b8c\u7f8e\uff0c\u4f46\u6211\u60f3\u4f60\u61c9\u8a72\u80fd\u5920\u7406\u89e3\uff0cx
\u662f\u4e00\u500b\u8b8a\u6578\uff0c\u800c =
\u662f\u6307\u6d3e\u904b\u7b97\u5b50(Assignment Operator)\uff0c\u4ed6\u6703\u5c07\u7b49\u865f\u53f3\u908a\u7684\u6771\u897f\u6307\u6d3e\u7d66\u7b49\u865f\u5de6\u908a\u7684\u8b8a\u6578\uff0c\u6216\u8005\u8aaa x
\u6703\u6307\u5411\u7b49\u865f\u53f3\u908a\u7684\u7269\u4ef6(Object)\u3002
Quote
\u5728Python\u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u7269\u4ef6\u3002
\u8acb\u89c0\u770b\u4e0b\u9762\u7684\u52d5\u756b\u3002
V\u00d8J, Narvent - Memory Reboot (4K Music Video)
\u518d\u4f86\u4ecb\u7d39\u5e7e\u7a2e\u4e0d\u540c\u7684\u8b8a\u6578\u985e\u578b\u3002
a = 114514\nprint(type(a))\nb = 1919810.0\nprint(type(b))\nc = 10e3\nprint(type(c))\nd = \"cheung4843\"\nprint(type(d))\ne = True\nprint(type(e))\nf = False\nprint(type(f))\nh = None\nprint(type(h))\n
type
\u544a\u8a34\u4f60\u62ec\u865f\u4e2d\u7684\u6771\u897f\u662f\u4ec0\u9ebc\u985e\u5225(Class)\u3002 b
\u8207 c
\u90fd\u662f\u6d6e\u9ede\u6578(Float)\uff0c\u4e5f\u5c31\u662f\u5c0f\u6578\u9ede\u7684\u6578\u5b57\uff0c\u800c d
\u5247\u662f\u5b57\u4e32\uff0ce
\u8207 f
\u5247\u662f\u5e03\u6797(Boolean)\uff0c\u800c h
\u5247\u662f\u7a7a\u503c\u3002
Note
type(x)
\u56de\u50b3 x
\u7684\u985e\u5225\u3002int
\u6574\u6578\u3002float
\u6d6e\u9ede\u6578\u3002str
\u5b57\u4e32\u3002bool
\u5e03\u6797\u3002None
\u7a7a\u503c\u3002Question
print(type(3 + 4.0))
\u6703\u5370\u51fa\u4ec0\u9ebc?print(type(3 + True))
\u6703\u5370\u51fa\u4ec0\u9ebc?@EditTime : 2024-01-27 16:44
"},{"location":"fundamental/python/variable_and_input/#input","title":"Input","text":"\u4f46\u662f\u5982\u679c\u6bcf\u6b21\u60f3\u8981\u4fee\u6539 x
\u88e1\u9762\u7684\u6771\u897f\uff0c\u96e3\u9053\u90fd\u8981\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4fee\u6539\u55ce?\u80fd\u4e0d\u80fd\u6211\u81ea\u5df1\u4f86\u8f38\u5165\u5462?
x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = int(x) + int(y)\nprint(\"The sum is: \", z)\n
\u5594\u5e79\uff0c\u600e\u9ebc\u4e00\u4e0b\u5b50\u591a\u51fa\u90a3\u9ebc\u591a\u6771\u897f\uff0c\u5225\u614c\uff0c\u6211\u4f86\u89e3\u91cb\uff0c\u4f46\u8acb\u4f60\u5148\u56de\u60f3\u5728\u524d\u4e00\u7bc0\u4e2d\u5b78\u904e\u7684\u6771\u897f\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe input, int,
\u90fd\u662f\u51fd\u5f0f\u3002\u800c input
\u62ec\u865f\u4e2d\u7684\u5b57\u4e32\u6703\u986f\u793a\u5728\u63a7\u5236\u53f0\u4e2d\u63d0\u793a\u4f60\u8981\u8f38\u5165\u4ec0\u9ebc\uff0cint
\u5247\u662f\u628a\u62ec\u865f\u4e2d\u7684\u6771\u897f\u7684\u985e\u5225\u8f49\u63db\u6210\u6574\u6578\u3002
\u90a3\u70ba\u4ec0\u9ebc\u5370\u51fatype(x)
\u5f97\u5230 <class 'str'>
\u5462?\u4ee3\u8868 x
\u662f\u4e00\u500b\u5b57\u4e32\uff0c\u9019\u662f\u56e0\u70ba input
\u7e3d\u662f\u5c07\u4f60\u8f38\u5165\u9032\u4f86\u7684\u6771\u897f\u7576\u6210\u5b57\u4e32\uff0c\u4f46\u6211\u60f3\u8981\u8b93 z = x + y
\u9019\u500b\u6578\u5b78\u5f0f\u5b50\u6210\u7acb\uff0c\u6240\u4ee5\u9700\u8981\u7528 int
\u4f86\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\u518d\u9032\u884c\u904b\u7b97\u3002
\u90a3\u4e0b\u9762\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u662f\u4ec0\u9ebc\u5462?
x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = x + y\nprint(\"The sum is: \", z)\n
\u6c92\u932f\uff0c\u5b57\u4e32\u7684\u76f8\u52a0\uff0c\u5c31\u662f\u76f8\u9023\u3002\u90a3\u4f60\u8981\u4e0d\u8981\u8a66\u8a66\u770b\u76f8\u6e1b?
\u63a5\u4e0b\u4f86\u6211\u5011\u4f86\u505a\u500b\u6709\u8da3\u7684\u5be6\u9a57\uff0c\u9806\u4fbf\u8a8d\u8b58\u4e00\u4e0b f-string
a = 3.5\nb = int(a)\nprint(f'The value of b is {b}, and its type is {type(b)}')\nc = float(b)\nprint(f'The value of c is {c}, and its type is {type(c)}')\nprint(b == c)\n
\u90a3 f-string
\u662f\u4ec0\u9ebc\u6771\u897f\u5462?\u4ed6\u662f\u4e00\u7a2e\u5b57\u4e32\u683c\u5f0f\u5316(String Formatting)\u7684\u65b9\u6cd5\uff0c\u4ed6\u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u5b57\u4e32\uff0c\u4e26\u5c07\u5b57\u4e32\u4e2d\u7684 {}
\u66ff\u63db\u6210\u62ec\u865f\u5167\u7684\u6771\u897f\u3002
\u800c\u57f7\u884c\u5b8c\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u4f60\u6703\u767c\u73fe b
\u8207 c
\u7684\u985e\u5225\u4e0d\u540c\uff0c\u4f46\u4ed6\u5011\u7684\u503c\u537b\u76f8\u540c\uff0c\u9019\u662f\u56e0\u70ba int()
\u8207 float()
\u90fd\u662f\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u6574\u6578\u8207\u6d6e\u9ede\u6578\uff0c\u800c int()
\u6703\u5c07\u6d6e\u9ede\u6578\u7684\u5c0f\u6578\u9ede\u6368\u53bb\uff0c\u800c float()
\u5247\u6703\u5c07\u6574\u6578\u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002
\u90a3\u9ebc b == c
\u662f\u4ec0\u9ebc\u610f\u601d\u5462?\u5176\u4e2d ==
\u662f\u6bd4\u8f03\u904b\u7b97\u5b50(Comparison Operator)\uff0c\u4ed6\u6703\u6bd4\u8f03\u7b49\u865f\u5de6\u53f3\u5169\u908a\u7684\u6771\u897f\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5247\u56de\u50b3 True
\uff0c\u5426\u5247\u56de\u50b3 False
\u3002
\u90a3\u5982\u679c\u6211\u4eca\u5929\u60f3\u8981\u4e00\u6b21\u8f38\u5165\u597d\u5e7e\u500b\u5b57\u4e32\uff0c\u6bcf\u4e00\u500b\u5b57\u4e32\u4ee5\u7a7a\u683c\u4f86\u9694\u958b\u5462? \u4f46\u5728\u9019\u4e4b\u524d\uff0c\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5c0f\u7a0b\u5f0f :
a, b = \"Hello World\".split()\nprint(a)\nprint(b)\n
\u6211\u77e5\u9053\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u5b57\u4e32\u5f8c\u9762\u63a5\u4e86\u4e00\u500b .split()
\u5462?\u5728\u9019\u88e1\u4ed6\u5f88\u50cf\u662f\u51fd\u5f0f\uff0c\u4f46\u53c8\u4e0d\u662f\u51fd\u5f0f\uff0c\u90a3\u4ed6\u53eb\u4ec0\u9ebc\u5462?\u4ed6\u88ab\u7a31\u70ba \"Hello World\"
\u9019\u500b\u5b57\u4e32\u7684\u65b9\u6cd5(Method)\uff0c\u4f46\u672a\u4f86\u4f60\u53ef\u80fd\u9084\u6703\u807d\u5230\u985e\u5225\u65b9\u6cd5(Class Method)\uff0c\u4ee5\u53ca\u975c\u614b\u65b9\u6cd5(Static Method) \u7b49\u540d\u8a5e\uff0c\u6211\u6015\u4f60\u6703\u641e\u6df7\uff0c\u6240\u4ee5\u4f60\u5c31\u5148\u8a8d\u9017\u9ede\u5f8c\u9762\u7684\u662f\u300c\u65b9\u6cd5\u300d\u5c31\u597d\u4e86\u3002
\u800c .split()
\u6703\u628a\u5b57\u4e32\u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\u4e26\u56de\u50b3(\u90a3\u4f60\u60f3\u60f3\u62ec\u865f\u5167\u4ec0\u9ebc\u90fd\u6c92\u653e\uff0c\u9810\u8a2d\u6703\u662f\u4ec0\u9ebc?)\uff0c\u800c\u770b\u770b\u7b49\u865f\u5de6\u908a\uff0c\u6211\u7528 a, b
\u53bb\u63a5\u8457\uff0c\u9019\u7a31\u70ba\u958b\u7bb1(Unpacking)
\u90a3\u73fe\u5728\u4f60\u61c9\u8a72\u80fd\u770b\u61c2\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u4e86\uff0c\u56e0\u70ba input()
\u4e5f\u6703\u56de\u50b3\u4e00\u500b\u5b57\u4e32\uff0c\u56e0\u6b64\u4ed6\u4e5f\u80fd\u5920\u4f7f\u7528 .split()
a, b = input().split()\nprint(a)\nprint(b)\n
Note
input()
\u63a5\u53d7\u8f38\u5165\uff0c\u56de\u50b3\u4e00\u500b\u5b57\u4e32\u3002int(x)
\u5c07 x
\u8f49\u63db\u6210\u6574\u6578\u3002float(x)
\u5c07 x
\u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002str.split()
\u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\uff0c\u4e26\u56de\u50b3\u3002@EditTime : 2024-01-25 22:13
Question
print(int(input()) + int(input()))
\u6703\u5370\u51fa\u4ec0\u9ebc?124.spilt(\"1\")
\u662f\u5408\u6cd5\u7684\u55ce?bool(0)
\u8207 bool(1)
\u6703\u56de\u50b3\u4ec0\u9ebc?a
\u8207 b
\u6210\u529f\u4e92\u63db\u4e86\u5462?a, b = 4, 5\nprint(a, b)\na, b = b, a\nprint(a, b)\n
a, b = 4, 5\nprint(a, b)\ntmp = a\na = b\nb = tmp\nprint(a, b)\n
@EditTime : 2024-01-25 22:17
"},{"location":"fundamental/python/variable_and_input/#practice","title":"Practice","text":"\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:
Info
print()
\u8207 input()
\u3002type(x)
\u8207 int(x), float(x)
\u7684\u7528\u9014\u3002str.split()
\u7684\u7528\u9014\u3002f-string
\u3002\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002
ZeroJudge - a001. \u54c8\u56c9
Reference codeword = input()\nprint(f'hello, {word}')\n
@EditTime : 2024-01-27 17:02
"},{"location":"blog/archive/2024/","title":"2024","text":""},{"location":"blog/category/blog/","title":"Blog","text":""}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Hello LMcps Book!","text":""},{"location":"#welcome_to_lmcps","title":"Welcome to LMcps!","text":"\u9ece\u660e\u8cc7\u8a0a\u793e(LMcps)\u5275\u7acb\u65bc2018\u5e74\uff0c\u6307\u5c0e\u8005\u70ba\u674e\u60e0\u6587\u8001\u5e2b\u3002
\u672c\u66f8\u7684\u6b63\u78ba\u6253\u958b\u65b9\u5f0f\u662f\u6697\u8272\u6a21\u5f0f\u3002
print(\"Hello LMcps Book!\")\nprint(\"Welcome to LMcps!\")\n
"},{"location":"#why_does_it_start","title":"Why does it start?","text":"\u57282024/1/25\uff0c\u662f\u75ab\u60c5\u5f8c\uff0c\u7b2c\u4e00\u6b21\u6b77\u5c46\u793e\u54e1\u8fd4\u56de\u6bcd\u6821\u8207\u5b78\u5f1f\u59b9\u5011\u5206\u4eab\u7d93\u9a57\u7684\u65e5\u5b50\uff0c\u5728\u8207\u8001\u5e2b\u53ca\u5b78\u5f1f\u59b9\u5011\u4ea4\u6d41\u5f8c\uff0c\u767c\u73fe\u4e86\u76ee\u524d\u8cc7\u8a0a\u6559\u80b2\u7684\u7a98\u56f0\uff0c\u8af8\u5982\u7db2\u8def\u4e0a\u7684\u8cc7\u6e90\u96d6\u591a\uff0c\u4f46\u4e0d\u6613\u5165\u9580\uff0c\u65bc\u662f\u6c7a\u5b9a\u5beb\u4e00\u672c\u66f8\uff0c\u4ee5\u5927\u91cf\u7684\u5716\u7247\u53ca\u7c21\u55ae\u7684\u6587\u5b57\uff0c\u8b93\u5b78\u5f1f\u59b9\u5011\u80fd\u5920\u5feb\u901f\u5165\u9580\uff0c\u4e26\u4e14\u80fd\u5920\u5728\u77ed\u6642\u9593\u5167\uff0c\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\u8207\u81ea\u4fe1\u3002
"},{"location":"#content","title":"Content","text":"\u9019\u672c\u66f8\u8457\u91cd\u65bc\u61c9\u7528\u9762\uff0c\u4e26\u4e14\u5be6\u4f5c\u70ba\u4e3b\uff0c\u4e5f\u4e26\u975e\u5e36\u4f60\u6253\u7af6\u7a0b\uff0c\u800c\u662f\u5e36\u4f60\u4e86\u89e3\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u3002\u5be6\u4f5c\u8a9e\u8a00\u4ee5Python\u70ba\u4e3b\uff0c\u6703\u6709\u5c11\u6578C++\u7684\u5be6\u4f5c\u3002
"},{"location":"#wait_to_be_done","title":"Wait to be done","text":"MIT License
Copyright (c) 2023 Cheung4843
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"},{"location":"blog/","title":"Blog","text":""},{"location":"blog/2024/02/06/hello-blog-/","title":"Hello Blog !","text":"Hi, this is a test post.
For the better experience of the development, I think recording the process of the development is a good idea.
If you are interested in the project, you can follow the blog to get the latest information about the project.
Also, plaease contact me if you wanna join the project. I am looking forward to working with you.
"},{"location":"fundamental/","title":"Fundamental","text":"\u9019\u88e1\u5c07\u6703\u6559\u4f60\u57fa\u790e\u7684Python\u8207C++\u7684\u57fa\u790e\u8a9e\u6cd5\u3002
"},{"location":"fundamental/cpp/","title":"C++","text":"\u6211\u662fc++\u7cfb\u5217\u7684\u7b46\u8005bloodnighttw\uff0c\u6211\u9810\u8a08\u5728\u6b64\u6587\u7ae0\u5206\u4e0b\u5217\u5e7e\u500b\u7ae0\u7bc0\uff0c\u4e26\u76e1\u91cf\u4ee5\u521d\u5b78\u8005\u7684\u89d2\u5ea6\u5beb\u51fa\u5167\u5bb9\u3002
\u5982\u679c\u5728\u64b0\u5beb\u4e0a\u6709\u4efb\u4f55\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u5bc4\u96fb\u5b50\u90f5\u4ef6\u5230 bbeenn1227@gmail.com
\u6216\u8005 emails@bntw.dev
\u4f86\u505a\u8a62\u554f(\u4e0d\u904e\u8acb\u8868\u660e\u4f60\u7684\u8eab\u4efd\uff0c\u4e0d\u7136\u6211\u53ef\u80fd\u6703\u5ffd\u7565)\uff0c\u6216\u8005\u53ef\u4ee5\u5728\u6211\u66f4\u5e38\u51fa\u73fe\u7684discord\u8a62\u554f\u6211\u3002
\u6b64\u9023\u7d50\u70ba\u9ece\u660e\u8cc7\u8a0a\u793e\u7684discord\u7fa4\u7d44\u9023\u7d50\uff0c\u52a0\u5165\u4e4b\u5f8c\u53ef\u4ee5tag @bloodnighttw
\uff0c\u53ea\u8981\u6211\u5728\u7dda\u4e0a\u4e14\u6709\u6642\u9593\u6211\u90fd\u6703\u76e1\u91cf\u5e6b\u4f60\u56de\u7b54\u3002
\u53e6\u5916\u6709\u4efb\u4f55\u932f\u5b57\u6216\u53ef\u4ee5\u6539\u5584\u7684\u5730\u65b9\u8acb\u767cissues\uff0c\u6211\u5011\u6703\u518d\u8a0e\u8ad6\u904e\u5f8c\u9069\u6642\u5730\u505a\u51fa\u4fee\u6b63\u3002
"},{"location":"fundamental/cpp/create_env/","title":"0. \u74b0\u5883\u67b6\u8a2d","text":"\u6b63\u6240\u8b02\u5de5\u6b32\u5584\u5176\u4e8b\uff0c\u5fc5\u5148\u5229\u5176\u5668\uff0c\u6b64\u7ae0\u6211\u5011\u5206\u5225\u6703\u5728\u5206\u5225\u8b1b\u8ff0\u5982\u4f55\u5728windows\u3001macos\u4e0a\u5b89\u88dd\u76f8\u95dc\u5957\u4ef6\u3002
\u6211\u5011\u9810\u8a08\u8981\u4f7f\u7528GNU C++ Compiler\u9032\u884c\u7de8\u8b6f\uff0c\u4ee5\u53ca\u4f7f\u7528Visual studio code + clangd\u8207c++11
\u7684\u6a19\u6e96\u64b0\u5beb\u7a0b\u5f0f\uff0c\u4e0b\u9762\u6703\u8a73\u8ff0\u5b89\u88dd\u6d41\u7a0b\u3002
\u7576\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u50cf\u662fdev c++
\u3001codeblock
\u9019\u985e\u7684\u8edf\u9ad4\u9032\u884c\u64b0\u5beb\uff0c\u53ea\u4e0d\u904e\u6709\u4e00\u9ede\u9700\u8981\u6ce8\u610f\uff0c\u820a\u7248\u7684dev c++
\u9810\u8a2d\u6c92\u6709\u555f\u7528c++11\u6a19\u6e96\u7684\u652f\u63f4\uff0c\u4f60\u5fc5\u9808\u8981\u5728\u8a2d\u5b9a\u88e1\u9762\u5c0b\u627e\u958b\u555f\u7684\u65b9\u5f0f\u3002
GNU C++ Compiler (G++)\u672c\u8eab\u662f\u6c92\u6709\u652f\u63f4Windows\u7684\uff0c\u4f46\u6211\u5011\u53ef\u4ee5\u900f\u904e\u4f7f\u7528wsl2\u6216\u8005\u5225\u4eba\u7528\u597d\u7684\u79fb\u690d\u7248\u672c\u9032\u884c\u4f7f\u7528\uff0c\u9019\u908a\u6211\u5011\u6703\u5b89\u88dd\u5225\u4eba\u5beb\u597d\u7684\u79fb\u690d\u7248\u3002
"},{"location":"fundamental/cpp/create_env/#1mingw","title":"1.\u4e0b\u8f09\u4e26\u5b89\u88ddmingw","text":"mingw32-gcc-g++
installation
> apply change
\u6309\u4e0bapply
\u5b89\u88dd
C:\\MinGW
\u88e1\u9762\uff0c\u627e\u5230\u88e1\u9762\u7684bin
\u8cc7\u6599\u593e\uff0c\u9ede\u9032\u53bb\u4e26\u8907\u88fd\u8def\u5f91\u3002 \u9ede\u5165 \u9032\u968e
>\u74b0\u5883\u8b8a\u6578
\uff0c\u4e26\u5728Path\u4e2d\u8cbc\u4e0a\u525b\u624d\u8907\u88fd\u7684\u8b8a\u6578\uff0c\u7136\u5f8c\u4fdd\u5b58\u9000\u51fa\u3002
\u958b\u555f\u63d0\u793a\u547d\u4ee4\u5b57\u5143\u5f8c\uff0c\u6253\u5165g++ -v
\uff0c\u5982\u679c\u6709\u7248\u672c\u8a0a\u606f\u4ee3\u8868\u5b89\u88dd\u6210\u529f\u3002
macos\u672c\u8eab\u5c31\u6709\u63d0\u4f9bclangd\u4f5c\u70ba\u7de8\u8b6f\u5668\u4f7f\u7528\uff0c\u5728\u5b89\u88ddxcode\u5f8c\u61c9\u8a72\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982\u679c\u9084\u662f\u9700\u8981GNU C++ Compiler\uff0c\u8acb\u53c3\u8003\u4e0b\u9762\u6b65\u9a5f\u3002
"},{"location":"fundamental/cpp/create_env/#1homwbrew","title":"1.\u5b89\u88ddhomwbrew\u5957\u4ef6\u7ba1\u7406\u5668","text":"\u6839\u64da\u5b98\u7db2\u7684\u6307\u793a\u5b89\u88ddhomebrew\u3002
"},{"location":"fundamental/cpp/create_env/#2g","title":"2.\u4f7f\u7528\u7ba1\u7406\u5668\u5b89\u88ddg++\u5957\u4ef6","text":"brew search gcc
\u641c\u5c0b\u76f8\u95dc\u5957\u4ef6brew install gcc
\u5b89\u88ddCompilergcc-13 -v
\u6216g++-13
\u78ba\u8a8d\u662f\u5426\u6210\u529f\u5b89\u88ddWarning
\u8acb\u6ce8\u610f\uff0c\u5728macos\u4e0a\u9810\u8a2d\u4f7f\u7528\u7684g++
\u6307\u4ee4\u70baclang
\u800c\u4e0d\u662fGNU C++ Compiler
\uff0c\u8acb\u6539\u4f7f\u7528g++-XX
\u4f86\u64cd\u4f5cg++\u7de8\u8b6f\u5668\u3002
\u524d\u5f80\u9019\u500b\u7db2\u7ad9\u4e0b\u8f09\u5c6c\u65bc\u4f60\u7cfb\u7d71\u7684visual studio code\u5b89\u88dd\u6a94\u3002
"},{"location":"fundamental/cpp/create_env/#2_extension","title":"2. \u5b89\u88dd extension","text":"C/C++
clangd
\u9019\u5169\u500bextension\uff0c\u4e26\u91cd\u555fvscode Disable Intellisence
\u5982\u679c\u9047\u5230\u5b57\u9ad4\u975e\u7b49\u5bec\uff0c\u8acb\u5c0b\u627e\u4e00\u500b\u7b49\u5bec\u5b57\u9ad4\u4e26\u4f7f\u7528\uff0c\u7b49\u5bec\u5b57\u9ad4\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002
"},{"location":"fundamental/cpp/hello_world/","title":"1. Hello World","text":""},{"location":"fundamental/cpp/hello_world/#hello_world","title":"Hello World (\u8f38\u51fa)","text":""},{"location":"fundamental/cpp/hello_world/#_1","title":"\u521d\u5b78\u8005\u7684\u7b2c\u4e00\u6b65","text":"\u9996\u5148\uff0c\u8acb\u5728vscode (Visual studio code)\u4e2d\u65b0\u589e\u4e00\u500b\u540d\u70bahello_world.cpp\uff0c\u4e26\u6253\u5165\u4e0b\u5217\u7a0b\u5f0f\u78bc\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\" << endl;\n return 0;\n}\n
\u78ba\u8a8d\u597d\u5b58\u6a94\u904e\u5f8c\uff0c\u6309\u4e0b\u53f3\u4e0a\u89d2\u7684\u57f7\u884c\u6309\u9375\uff0c\u4f60\u5c31\u6703\u6210\u529f\u770b\u5230\u4f60\u5beb\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\u3002 \u63a5\u8457\uff0c\u6211\u5011\u5c07\u66f4\u6df1\u5165\u8a0e\u8ad6\u9019\u6bb5\u7a0b\u5f0f\uff0c\u5230\u5e95\u767c\u751f\u4e86\u4ec0\u9ebc\u4e8b\u60c5\u3002
"},{"location":"fundamental/cpp/hello_world/#hello_world_1","title":"Hello World\u7a0b\u5f0f\u89e3\u91cb","text":"\u5728\u9019\u6bb5\u7a0b\u5f0f\u4e2d\uff0c\u5df2\u7d93\u6709\u8a31\u591a\u7a0d\u5fae\u8907\u96dc\u7684\u6982\u5ff5\uff0c\u5148\u5225\u6015\uff0c\u6211\u5011\u5c31\u53ea\u8981\u4e86\u89e3\u5230:
int main(){......}
\u88e1\u9762\u958b\u59cb\u57f7\u884c\u7684return 0;
\u4ee3\u8868\u7a0b\u5f0f\u6210\u529f\u7d50\u675f\u3002\u8b93\u6211\u5011\u7126\u9ede\u653e\u5728\u7b2c\u516d\u884c:
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\" << endl;\n return 0;\n}\n
\u9019\u908a\u6709\u4e86\u6211\u5011\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\uff0c\u4f60\u53ef\u4ee5\u628acout
\u7576\u6210\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\uff0c\u628a<<
\u7576\u6210\u50b3\u905e\u7684\u7bad\u982d\uff0c\u800c\u9019\u6bb5\u7a0b\u5f0f\u5c31\u662f\u8981\u628a\u9019\u6bb5\u5b57\u50b3\u7d66\u96fb\u8166\u5370\u51fa\u3002
\u9019\u908a\u7684endl
\u4ee3\u8868\u8457\u63db\u884c\u7684\u610f\u601d\uff0c\u900f\u904e<<
\u50b3\u7d66\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\u3002
\u9019\u908a\u63d0\u4f9b\u53e6\u5916\u4e00\u500b\u63db\u884c\u7684\u5beb\u6cd5\uff0c\u5177\u9ad4\u5dee\u7570\u5728\u719f\u6089\u8a9e\u6cd5\u5f8c\uff0c\u6703\u5728\u53e6\u5916\u4e00\u7bc7\u6587\u7ae0\u505a\u8aaa\u660e\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\\n\";\n return 0;\n}\n
\\n
\u70ba\u63db\u884c\u5b57\u5143\uff0c\u5728\u5b57\u5143\u4e2d\u6709\u8a31\u591a\u5b57\u5143\u6703\u6709\u7279\u5b9a\u529f\u7528\uff0c\u6703\u4ee5\\
\u958b\u982d\uff0c\u9023\u7d50\u4e00\u500b\u5b57\u6bcd\u6216\u6578\u5b57\uff0c\u4ee3\u8868\u8457\u8df3\u812b\u5b57\u5143\uff0c\u4f5c\u70ba\u7279\u6b8a\u7528\u9014\u4f7f\u7528\u3002 \u7d30\u5fc3\u5982\u4f60\uff0c\u9019\u662f\u53c8\u8981\u63d0\u554f\u4e86\uff0c\u5206\u865f;
\u53c8\u4ee3\u8868\u4ec0\u9ebc\u610f\u601d\uff1f
\u5206\u865f;
\u662f\u544a\u8a34\u96fb\u8166\u8aaa\uff0c\u9019\u4e9b\u6307\u4ee4\u8ddf\u4e0b\u4e00\u500b\u5b57\u5143\u6216\u4e0b\u4e00\u884c\u662f\u5206\u958b\u7684\uff0c\u4e0d\u8981\u628a\u4ed6\u9023\u5728\u4e00\u8d77\uff0c\u4e5f\u56e0\u70ba\u6709\u9019\u6771\u897f\uff0c\u6211\u5011\u7a0b\u5f0f\u53ef\u4ee5\u9019\u6a23\u5beb\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n cout << \"hello world!\\n\"; cout << endl;\n return 0;\n}\n
Danger
\u5341\u5206\u4e0d\u5efa\u8b70\u4f60\u9019\u6a23\u5beb\uff0c\u5c0d\u65bc\u7a0b\u5f0f\u719f\u7df4\u7684\u4eba\u4f86\u8aaa\uff0c\u9019\u6a23\u7684\u6392\u7248\uff0c\u7a0b\u5f0f\u78bc\u8b80\u8d77\u4f86\u6703\u975e\u5e38\u75db\u82e6\uff0c\u5c0d\u65bc\u521d\u5b78\u8005\uff0c\u5247\u5bb9\u6613\u8aa4\u89e3\u7a0b\u5f0f\u78bc\u3002
"},{"location":"fundamental/cpp/hello_world/#_2","title":"\u52a0\u5165\u8b8a\u6578 (\u8f38\u5165)","text":""},{"location":"fundamental/cpp/hello_world/#_3","title":"\u8b80\u53d6\u5b57\u4e32","text":"#include <iostream>\n\nusing namespace std;\n\nint main(){\n string str;\n cin >> str;\n cout << str << endl;\n return 0;\n}\n
\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u4e32\u6587\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u6587\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002
"},{"location":"fundamental/cpp/hello_world/#_4","title":"\u8b80\u53d6\u6578\u5b57","text":"\u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23:
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp;\n cin >> temp;\n cout << temp << endl;\n return 0;\n}\n
\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u500b\u6578\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u4e00\u4e32\u6578\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002
Info
\u5982\u679c\u4eca\u5929\u6253\u5165\u7684\u6578\u5b57\u975e\u5e38\u975e\u5e38\u7684\u5927\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u5370\u51fa\u4f86\u7684\u6578\u5b57\u8ddf\u6253\u5165\u7684\u6578\u5b57\u4e0d\u4e00\u6a23\uff0c\u5176\u539f\u56e0\u662f\u6ea2\u4f4d\uff0c\u9019\u9ede\u6211\u5011\u6703\u5728\u4e0b\u4e00\u7ae0\u8ac7\u5230\u5176\u539f\u56e0\u8207\u89e3\u6cd5\u3002
"},{"location":"fundamental/cpp/hello_world/#_5","title":"\u8f38\u5165\u7a0b\u5f0f\u89e3\u91cb","text":"\u6b63\u5982cout
\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u628acin
\u7576\u6210\u4f60\u7684\u9375\u76e4\uff0c\u900f\u904e>>
\u544a\u8a34\u96fb\u8166\u5f9e\u9375\u76e4\u8f38\u5165\u81f3\u53f3\u908a\u7684\u8b8a\u6578\u3002
\u5e38\u898b\u7684\u8aa4\u5340
\u5c0d\u65bc\u521d\u5b78\u8005\u4f86\u8aaa\uff0c\u53ef\u80fd\u6703\u4e0d\u5c0f\u5fc3\u5beb\u51fa\u9019\u6a23\u7684\u7a0b\u5f0f\uff1a
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp1,temp2;\n cin << temp1 << temp2;\n cout << temp1 << \"!=\" << temp2 << endl;\n return 0;\n}\n
\u6b64\u6642\u4f60\u6703\u767c\u73fe\u7121\u6cd5\u7de8\u8b6f\uff0c\u5176\u539f\u56e0\u662f\u4f60\u7684cin
\u7684<<
\u61c9\u8a72\u8981\u662f>>
\u624d\u5c0d \u3002
\u5e38\u898b\u7684\u932f\u8aa4\u4e5f\u5305\u62ec\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a
cout
\u7684<<
\u65b9\u5411\u932f\u8aa4 #include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp1,temp2;\n cin >> temp1 >> temp2;\n cout >> temp1 >> temp2;\n return 0;\n}\n
cout
\u7684\u4f4d\u7f6e\u932f\u8aa4
#include <iostream>\n\nusing namespace std;\n\nint main(){\n \"error\" >> cout;\n return 0;\n}\n
cout
\u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002 cin
\u7684\u4f4d\u7f6e\u932f\u8aa4
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int a;\n a << cin;\n return 0;\n}\n
\u539f\u56e0\u540c\u4e0a\uff0ccin
\u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002 \u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u4e00\u9023\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n int temp1;\n string temp2;\n cin >> temp1 >> temp2;\n cout << temp1 << \"!=\" << temp2 << endl;\n return 0;\n}\n
input1234 4321\n
output1234!=4321\n
\u4f60\u6703\u767c\u73fe\u4e00\u4ef6\u4e8b\u60c5\uff0ccin
\u662f\u900f\u904e\u7a7a\u683c\u6216\u8005\u63db\u884c\u4f86\u5206\u958b\u7684\uff0c\u4eca\u5929\u5982\u679c\u60f3\u8981\u8b80\u53d6\u4e00\u884c\uff0c\u8acb\u53c3\u8003\u4e0b\u65b9\u7a0b\u5f0f\u3002
\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff0c\u5176\u4e2dtemp2\u8acb\u6539\u6210\u8b8a\u6578\u7684\u540d\u7a31\u3002
#include <iostream>\n\nusing namespace std;\n\nint main(){\n string temp2;\n getline(cin,temp2);\n cout << temp2 << endl;\n return 0;\n}\n
"},{"location":"fundamental/cpp/hello_world/#practice","title":"Practice","text":"\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:
Info
\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002
ZeroJudge - a001. \u54c8\u56c9
Reference code#include <iostream>\n\nint main(){\n string word;\n cin >> word;\n cout << \"hello, \" << word << endl;\n\n return 0;\n}\n
"},{"location":"fundamental/python/","title":"Python","text":"\u55e8\uff0c\u6211\u662f @cheung4843\uff0c\u662fPython\u7cfb\u5217\u7684\u4f5c\u8005\u3002\u5728\u4e00\u958b\u59cb\u57fa\u790e\u8a9e\u6cd5\u7684\u90e8\u5206\u6703\u6559\u5f97\u7279\u5225\u8a73\u7d30\uff0c\u6587\u7ae0\u4e2d\u7684\u5c08\u6709\u540d\u8a5e\u5c07\u6703\u4ee5\u9ec3\u8272\u8207\u7da0\u8272\u6a19\u793a\uff0c\u8acb\u4f60\u7a0d\u5fae\u6ce8\u610f\u4e00\u4e0b\uff0c\u82e5\u4e0d\u61c2\u4e5f\u6c92\u95dc\u4fc2\uff0c\u6211\u6703\u5728\u5f8c\u9762\u7ae0\u7bc0\u518d\u6b21\u63d0\u5230\u3002
\u5728\u6bcf\u4e00\u500b\u5c0f\u7bc0\u7684\u5f8c\u9762\u90fd\u6703\u6709\u7b46\u8a18\u5340\u4ee5\u53ca\u554f\u984c\u5340\uff0c\u5c24\u5176\u662f\u554f\u984c\u5340\uff0c\u8acb\u4f60\u597d\u597d\u601d\u8003\u3002\u800c\u5728\u6bcf\u4e00\u7bc7\u6587\u7ae0\u6700\u5f8c\u90fd\u6703\u653e\u4e0a\u4e00\u4e9b\u7df4\u7fd2\u984c\uff0c\u8acb\u5617\u8a66\u5b8c\u6210\u4ed6\u5011!
"},{"location":"fundamental/python/classes/","title":"Classes","text":""},{"location":"fundamental/python/classes/#introduction","title":"Introduction","text":""},{"location":"fundamental/python/dictionaries/","title":"Dictionaries","text":""},{"location":"fundamental/python/dictionaries/#introduction","title":"Introduction","text":"\u4f86\u4ecb\u7d39\u672c\u4e3b\u984c\u4e2d\u6700\u5f8c\u4e00\u500b\u8cc7\u6599\u7d50\u69cb\uff1a\u5b57\u5178(Dictionaries)\uff0c\u662f\u7531\u9375(Key)\u548c\u503c(Value)\u6240\u7d44\u6210\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u7d93\u7531\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u9375\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u800c\u503c\u5247\u53ef\u4ee5\u662f\u4efb\u4f55\u578b\u614b\u7684\u8cc7\u6599\u3002
Question
\u7559\u7d66\u4f60\u601d\u8003\uff0c\u4f60\u4e00\u5b9a\u77e5\u9053\u7b54\u6848\uff0c\u5c31\u7b97\u4f60\u6c92\u8fa6\u6cd5\u99ac\u4e0a\u77e5\u9053\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5beb\u7a0b\u5f0f\u4f86\u6e2c\u8a66\u3002
"},{"location":"fundamental/python/dictionaries/#create_a_dictionary","title":"Create a Dictionary","text":"\u4f60\u53ef\u4ee5\u7528{}
\u3001dict()
\u6216\u8005 Comprehension \u4f86\u5efa\u7acb\u4e00\u500b\u5b57\u5178\u3002
scores = {\"Compiler\": 100, \"AWS\": 95, \"Data Science\": 92}\nprint(scores)\n\n# when keys are strings, using keyword arguments\ndirections = dict(North=\"\u2191\", South=\"\u2193\", East=\"\u2192\", West=\"\u2190\")\nprint(directions)\n\ncapital = dict([(\"Taiwan\", \"Taipei\"), (\"Japan\", \"Tokyo\"), (\"Korea\", \"Seoul\")])\nprint(capital)\n\n# comprehension\ncube = {x: x ** 3 for x in range(-2, 3)}\nprint(cube)\n
Output{'Compiler': 100, 'AWS': 95, 'Data Science': 92}\n{'North': '\u2191', 'South': '\u2193', 'East': '\u2192', 'West': '\u2190'}\n{'Taiwan': 'Taipei', 'Japan': 'Tokyo', 'Korea': 'Seoul'}\n{-2: -8, -1: -1, 0: 0, 1: 1, 2: 8}\n
\u90a3\u70ba\u4ec0\u9ebc\u8aaa\u5b57\u5178\u662f\u6709\u5e8f\u7684\u5462?\u4f60\u591a\u57f7\u884c\u5e7e\u6b21\uff0c\u8f38\u51fa\u7684\u9806\u5e8f\u8207\u4f60\u5efa\u7acb\u7684\u9806\u5e8f\u662f\u4e00\u6a23\u7684\u55ce?
"},{"location":"fundamental/python/dictionaries/#operations","title":"Operations","text":""},{"location":"fundamental/python/dictionaries/#accessing_and_modifying_elements","title":"Accessing and Modifying elements","text":"\u4f60\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u4e5f\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u4fee\u6539\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u6703\u5f97\u5230 KeyError
\u3002
recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"King Gnu\"] = \"SPECIALZ\"\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"HEALTH\"] = \"Blue Monday\"\nprint(recent_listening[\"HEALTH\"])\n\nprint(recent_listening[\"ALI\"])\n
Output\u98db\u884c\u8247\nSPECIALZ\nBlue Monday\nKeyError: 'ALI'\n
King Gnu - \u98db\u884c\u8247
Blue Monday
\u90a3\u9ebc\u8a72\u5982\u4f55\u6aa2\u67e5\u9375\u662f\u5426\u5b58\u5728\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 in
\u4f86\u6aa2\u67e5\uff0c\u4f86\u907f\u514d\u932f\u8aa4\u3002
recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\n\nprint(\"King Gnu\" in recent_listening)\nprint(\"ALI\" in recent_listening)\n
OutputTrue\nFalse\n
Question
\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u5b57\u5178\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u9375\u5b58\u5728?
"},{"location":"fundamental/python/dictionaries/#removing_elements","title":"Removing elements","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 del
\u4f86\u522a\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002
\u4f8b\u5982\u6211\u9019\u9663\u5b50\u90fd\u4e0d\u5beb Java \u4e86\uff0c\u6240\u4ee5\u628a Java \u5f9e\u6211\u7684\u5fc3\u4e2d\u522a\u9664\u3002
my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\nprint(my_love)\n\ndel my_love[\"Java\"]\nprint(my_love)\n
Output{'python': 100, 'Java': 70, 'Js': 60}\n{'python': 100, 'Js': 60}\n
"},{"location":"fundamental/python/dictionaries/#iterating","title":"Iterating","text":"\u76f4\u63a5\u4f86\u770b\u4f8b\u5b50\u5427\u3002
my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love:\n print(key, my_love[key])\n
Outputpython 100\nJava 70\nJs 60\n
\u5982\u679c\u4f60\u53ea\u60f3\u8981\u53d6\u5f97\u9375\u6216\u8005\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 keys()
\u6216\u8005 values()
\u3002
my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love.keys():\n print(key)\n\nprint(my_love.keys())\n\nfor value in my_love.values():\n print(value)\n\nprint(list(my_love.values()))\n
Outputpython\nJava\nJs\ndict_keys(['python', 'Java', 'Js'])\n100\n70\n60\n[100, 70, 60]\n
\u90a3\u5982\u679c\u4f60\u60f3\u8981\u540c\u6642\u53d6\u5f97\u9375\u548c\u503c\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 items()
\u3002
my_cat = {\"name\": \"Fat Orange\", \"age\": 12, \"is_cute\": True}\n\nfor key, value in my_cat.items():\n print(f\"{key}: {value}\")\n
Outputname: Fat Orange\nage: 12\nis_cute: True\n
\u6211\u611b\u6211\u7684\u6a58\u8c93\u3002
"},{"location":"fundamental/python/dictionaries/#methods","title":"Methods","text":""},{"location":"fundamental/python/dictionaries/#get","title":"get","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 get()
\u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u5f88\u65b9\u4fbf\u5594\u3002
my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info['name'])\nprint(my_info.get('hobbies')[1])\n\n# print(my_info['is_handsome']) # This will cause an error\nprint(my_info.get('is_handsome', 'Of course I am!'))\n
OutputSean\nworking out\nOf course I am!\n
\u5e0c\u671b\u4f60\u4e0d\u8981\u89ba\u5f97\u6211\u81ea\u6200\u3002
"},{"location":"fundamental/python/dictionaries/#pop","title":"pop","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 pop()
\u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u540c\u6642\u522a\u9664\u8a72\u9375\u503c\u5c0d\u3002
my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info.pop('age'))\nprint(my_info)\n
Output20\n{'name': 'Sean', 'hobbies': ['coding', 'working out']}\n
"},{"location":"fundamental/python/dictionaries/#clear","title":"clear","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 clear()
\u4f86\u6e05\u7a7a\u5b57\u5178\u3002
my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nmy_info.clear()\nprint(my_info)\n
Output{}\n
umm... \u518d\u904e\u4e0d\u4e45\u6211\u5c31\u8981 21 \u6b72\u4e86\u3002
\u6211\u8a8d\u70ba\u9019\u4e9b\u65b9\u6cd5\u5c31\u5920\u7528\u4e86\uff0c\u5176\u4ed6\u7684\u4f60\u53ef\u4ee5\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u56c9~
"},{"location":"fundamental/python/dictionaries/#practice","title":"Practice","text":"Itsa - [C_ST19-\u6613] \u6642\u9593\u8f49\u63db
Reference coden = int(input())\n\ntime_diff = {\"TW\": 0, \"JA\": 60, \"USE\": -720, \"USC\": -780, \"USW\": -840, \"UK\": -480}\n\nfor _ in range(n):\n time, cur, to = input().split()\n minutes = int(time[:2]) * 60 + int(time[3:])\n minutes += time_diff[to] - time_diff[cur]\n\n if minutes < 0:\n minutes += 1440\n minutes %= 1440\n\n h, m = divmod(minutes, 60)\n print(f\"{h:02d}{m:02d} {to}\")\n
\u4e00\u5f8b\u5148\u8f49\u63db\u6210\u5206\u9418\uff0c\u518d\u9032\u884c\u6642\u5340\u8f49\u63db\uff0c\u6700\u5f8c\u518d\u8f49\u63db\u6210\u5c0f\u6642\u548c\u5206\u9418\u3002 divmod
\u56de\u50b3\u4e00\u500b\u5e8f\u5c0d\uff0c\u5546\u8207\u9918\u6578\u3002
Itsa - [C_AR111-\u6613] \u5c0d\u8a71\u6a5f\u5668\u4eba
Itsa - [C_AR152-\u6613] \u6b63\u6574\u6578\u7d71\u8a08
Tipsorted()
and key
Itsa - [C_AR42-\u6613] \u904e\u534a\u5143\u7d20
Itsa - [C_AR188-\u6613] \u9663\u5217\u5143\u7d20
Tip\u9664\u4e86\u7528\u5b57\u5178\u4f86\u89e3\u984c\u4e4b\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5b78 Boyer\u2013Moore majority vote algorithm
"},{"location":"fundamental/python/functions/","title":"Functions","text":""},{"location":"fundamental/python/functions/#introduction","title":"Introduction","text":"\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u56de\u60f3 Repetition Structures - Nested Loop \u4e2d\u7684\u4f8b\u5b50:
\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\([1, n]\\) \u9593\u7684\u6700\u5927\u8cea\u6578\u3002
n = int(input())\n\nfor i in range(n, 0, -1):\n is_prime = True\n for j in range(2, i):\n if i % j == 0:\n is_prime = False\n break\n\n if is_prime:\n print(f\"{i} is the largest prime in [1, {n}]\")\n break\n
\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u529f\u80fd\u662f\u6b63\u78ba\u7684\uff0c\u4f46\u662f\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u4e0d\u9ad8\uff0c\u56e0\u70ba\u7a0b\u5f0f\u78bc\u7684\u9577\u5ea6\u592a\u9577\uff0c\u4e26\u4e14\u7a0b\u5f0f\u78bc\u7684\u908f\u8f2f\u4e0d\u5920\u6e05\u6670\u3002
\u6211\u5011\u53ef\u4ee5\u628a\u5224\u65b7\u8cea\u6578\u7684\u90e8\u5206\u62bd\u51fa\u4f86\uff0c\u4e26\u4e14\u5c07\u4ed6\u5305\u88dd\u6210\u4e00\u500b\u51fd\u5f0f:
def is_prime(x):\n for i in range(2, x):\n if x % i == 0:\n return False\n return True\n\n\nn = int(input())\n\nfor i in range(n, 0, -1):\n if is_prime(i):\n print(f\"{i} is the largest prime in [1, {n}]\")\n break\n
\u9019\u6a23\u7684\u597d\u8655\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u76f8\u540c\u7684\u7a0b\u5f0f\u78bc\u91cd\u8907\u4f7f\u7528\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u3002
\u90a3\u5982\u679c\u6211\u60f3\u8981\u9032\u4e00\u6b65\u7a0b\u5f0f\u78bc\u5c01\u88dd\u6210\u4e00\u500b\u63a5\u53d7\u6b63\u6574\u6578 \\(n\\) \u7684\u51fd\u5f0f\uff0c\u4e26\u4e14\u56de\u50b3 \\([1, n]\\) \u9593\u7684\u6700\u5927\u8cea\u6578\u5462?
def is_prime(x):\n for i in range(2, x):\n if x % i == 0:\n return False\n return True\n\n\ndef largest_prime(n):\n for i in range(n, 0, -1):\n if is_prime(i):\n return i\n\n\nn = int(input())\n\nprint(f\"{largest_prime(n)} is the largest prime in [1, {n}]\")\n
\u4e0d\u66c9\u5f97\u4f60\u6709\u6c92\u6709\u611f\u53d7\u5230\uff0c\u7576\u7a0b\u5f0f\u78bc\u8d8a\u4f86\u8d8a\u9577\uff0c\u6211\u5011\u5c31\u8d8a\u9700\u8981\u51fd\u5f0f\u4f86\u5e6b\u52a9\u6211\u5011\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5\u66f4\u5bb9\u6613\u7684\u95b1\u8b80\u7a0b\u5f0f\u78bc\u3002
\u9019\u500b\u5f15\u5b50\u544a\u8a34\u6211\u5011\uff0c\u51fd\u5f0f\u662f\u4e00\u500b\u53ef\u4ee5\u5c07\u7a0b\u5f0f\u78bc\u5305\u88dd\u6210\u4e00\u500b\u7368\u7acb\u7684\u55ae\u4f4d\uff0c\u4e26\u4e14\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u7684\u5de5\u5177\u3002
"},{"location":"fundamental/python/functions/#define_a_function","title":"Define a Function","text":"\u51fd\u5f0f\u7684\u5b9a\u7fa9\u662f\u4ee5 def
\u958b\u982d\uff0c\u5f8c\u9762\u63a5\u8457\u51fd\u5f0f\u7684\u540d\u7a31\uff0c\u4ee5\u53ca\u62ec\u865f\u5167\u7684\u53c3\u6578(Parameter)\uff0c\u7e2e\u6392\u5167\u7684\u7a0b\u5f0f\u78bc\u5c31\u662f\u51fd\u5f0f\u7684\u5167\u5bb9\u3002
\u4f60\u53ef\u4ee5\u9078\u64c7\u662f\u5426\u8981\u5728\u51fd\u5f0f\u4e2d\u52a0\u4e0a return
\u4f86\u56de\u50b3\u503c\uff0c\u5982\u679c\u6c92\u6709\uff0c\u51fd\u5f0f\u5247\u6703\u56de\u50b3 None
\u3002
\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f greet
\uff0c\u4ed6\u63a5\u53d7\u4e00\u500b\u53c3\u6578 name
\uff0c\u4e26\u5370\u51fa Hello, {name}
:
def greet(name):\n print(f\"Hello, {name}\")\n\n\nprint(greet(\"World\"))\n
OutputHello, World\nNone\n
greet(\"World\")
\u6703\u5148\u5370\u51fa Hello, World
\uff0c\u63a5\u8457\u56de\u50b3 None
\u3002
\u5b57\u4e32 \"World\"
\u88ab\u7a31\u70ba\u5f15\u6578(Argument)\uff0c\u800c name
\u5247\u88ab\u7a31\u70ba\u53c3\u6578(Parameter)\u3002\u4f46\u4e5f\u4e0d\u7528\u592a\u62d8\u6ce5\u3002
\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f freq(x)
\u63a5\u53d7\u4e00\u500b\u6578\u5b57\u5b57\u4e32 x
\uff0c\u56de\u50b3 0-9
\u7684\u51fa\u73fe\u6b21\u6578\uff0c\u4ee5\u5e8f\u5c0d\u7684\u65b9\u5f0f\u56de\u50b3:
def freq(x: str) -> tuple:\n table = [0] * 10\n for digit in x:\n table[int(digit)] += 1\n\n return tuple(table)\n\n\na, b = \"114514\", \"111445\"\nprint(freq(a) == freq(b))\n
OutputTrue\n
\u6211\u5011\u53ef\u4ee5\u5728\u53c3\u6578\u5f8c\u9762\u52a0\u4e0a :
\u4f86\u6307\u5b9a\u53c3\u6578\u7684\u578b\u5225\uff0c\u4e26\u4e14\u5728\u51fd\u5f0f\u5f8c\u9762\u52a0\u4e0a ->
\u4f86\u6307\u5b9a\u56de\u50b3\u503c\u7684\u578b\u5225\u3002
\u6709\u6642\u5019\u6211\u5011\u6703\u5e0c\u671b\u51fd\u5f0f\u7684\u53c3\u6578\u6709\u9810\u8a2d\u503c\uff0c\u9019\u6a23\u5728\u547c\u53eb\u51fd\u5f0f\u6642\u5c31\u4e0d\u9700\u8981\u586b\u5165\u5f15\u6578\u3002
\u4f8b\u5982\uff0c\u51fd\u5f0f max_of(x, k=1)
\u63a5\u53d7\u5169\u500b\u53c3\u6578 x
\u4e32\u5217\u8207 k
\u6578\u5b57\uff0c\u56de\u50b3 x
\u4e2d\u6700\u5927\u7684 k
\u500b\u6578\u5b57\uff0c\u4ee5\u4e32\u5217\u7684\u65b9\u5f0f\u56de\u50b3\uff0c\u800c k
\u7684\u9810\u8a2d\u503c\u70ba 1
:
def max_of(x: list[int], k=1):\n return sorted(x, reverse=True)[:k]\n\n\ny = [4, 8, 5, 3, 9]\nprint(max_of(y))\nprint(max_of(y, 2))\nprint(max_of(y, k=3))\n
Output[9]\n[9, 8]\n[9, 8, 5]\n
\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u51fd\u5f0f weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5)
\u63a5\u53d7\u4e09\u500b\u53c3\u6578 math
, english
, programming
\u4ee5\u53ca\u4e09\u500b\u9810\u8a2d\u503c math_weight=0.25
, english_weight=0.25
, programming_weight=0.5
\uff0c\u56de\u50b3\u52a0\u6b0a\u5206\u6578:
def weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5):\n return math * math_weight + english * english_weight + programming * programming_weight\n\n\nsean_score = weight_score(80, 90, 100)\nyaris_score = weight_score(math=82, math_weight=0.3, english=90, english_weight=0.3, programming=100,\n programming_weight=0.4)\nprint(sean_score, yaris_score)\n
Output92.5 91.6\n
\u6709\u9810\u8a2d\u503c\u7684\u53c3\u6578\u5fc5\u9808\u653e\u5728\u6c92\u6709\u9810\u8a2d\u503c\u7684\u53c3\u6578\u5f8c\u9762\u3002\u4f46\u5982\u679c\u4f60\u5728\u547c\u53eb\u51fd\u5f0f\u7684\u6642\u5019\uff0c\u6307\u5b9a\u4e86\u53c3\u6578\u7684\u540d\u7a31\uff0c\u5247\u53ef\u4ee5\u4e0d\u7528\u9075\u5b88\u9019\u500b\u898f\u5247\u3002
\u5176\u5be6\u9084\u6709\u4e00\u4e9b\u795e\u5947\u7684\u7528\u6cd5\uff0c\u4f46\u5c31\u4e0d\u5728\u9019\u88e1\u8a0e\u8ad6\u4e86\u3002
"},{"location":"fundamental/python/functions/#lambda_function","title":"Lambda Function","text":"\u9084\u8a18\u5f97 map
\u55ce? \u4ed6\u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u61c9\u7528\u5230\u4e00\u500b\u4e32\u5217\u4e0a\u3002
\u5148\u56de\u61b6\u4e00\u4e0b\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u4f7f\u7528 map
:
def square(x):\n return x ** 2\n\n\nlst = [1, 2, 3, 4, 5]\nprint(list(map(square, lst)))\n\nsong_name = [\"I\", \"Really\", \"Want\", \"to\", \"Stay\", \"At\", \"Your\", \"House\"]\nprint(list(map(len, song_name)))\n
Output[1, 4, 9, 16, 25]\n[1, 6, 4, 2, 4, 2, 4, 5]\n
I Really Want to Stay At Your House\u201d by Rosa Walton
\u9019\u7a2e\u63a5\u53d7\u51fd\u5f0f\u70ba\u53c3\u6578\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u9ad8\u968e\u51fd\u5f0f(Higher-Order Function)\u3002
\u4f46\u662f\uff0c\u5982\u679c\u51fd\u5f0f\u53ea\u6703\u88ab\u4f7f\u7528\u4e00\u6b21\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 lambda
\u4f86\u5b9a\u7fa9\u4e00\u500b\u533f\u540d\u51fd\u5f0f:
lst = [1, 2, 3, 4, 5]\nprint(list(map(lambda x: x ** 2, lst)))\n
Output[1, 4, 9, 16, 25]\n
\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u7528\u904e min
\u55ce? \u4ed6\u53ef\u4ee5\u627e\u51fa\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u6700\u5c0f\u503c\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u81ea\u8a02\u898f\u5247\uff0c\u4f8b\u5982\u6709\u4e00\u500b\u5ea7\u6a19\u4e32\u5217 points
\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u627e\u51fa\u6700\u9760\u8fd1\u539f\u9ede\u7684\u9ede:
points = [(9, 2), (3, 4), (5, 6), (7, 8), (4, 8), (1, 3)]\nprint(min(points, key=lambda x: x[0] ** 2 + x[1] ** 2))\n
Output(1, 3)\n
\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u5169\u500b\u4e32\u5217 a
\u8207 b
\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u8a08\u7b97\u5169\u500b\u4e32\u5217\u7684\u5167\u7a4d:
a, b = [1, 2, 3], [4, 5, 6]\nprint(sum(map(lambda x, y: x * y, a, b)))\n
Output32\n
"},{"location":"fundamental/python/functions/#factory_function","title":"Factory Function","text":"\u6709\u6642\u5019\u6211\u5011\u6703\u5e0c\u671b\u51fd\u5f0f\u56de\u50b3\u53e6\u4e00\u500b\u51fd\u5f0f\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u5de5\u5ee0\u51fd\u5f0f(Factory Function)\u3002
\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f make_power_fun(n)
\uff0c\u4ed6\u63a5\u53d7\u4e00\u500b\u6578\u5b57 n
\uff0c\u56de\u50b3\u4e00\u500b\u51fd\u5f0f\uff0c\u9019\u500b\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u6578\u5b57 x
\uff0c\u56de\u50b3 x
\u7684 n
\u6b21\u65b9:
def make_power_fun(n):\n return lambda x: x ** n\n\n\npower_fun_2 = make_power_fun(2)\npower_fun_3 = make_power_fun(4)\n\nprint(power_fun_2(3))\nprint(power_fun_3(3))\n
Output9\n81\n
\u9019\u6a23\u7684\u597d\u8655\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5730\u65b9\u4f7f\u7528\u4e0d\u540c\u7684 n
\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u8907\u5b9a\u7fa9\u51fd\u5f0f\u3002
\u51fd\u5f0f\u53ef\u4ee5\u88ab\u5b9a\u7fa9\u5728\u53e6\u4e00\u500b\u51fd\u5f0f\u7684\u5167\u90e8\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u5de2\u72c0\u51fd\u5f0f(Nested Function)\u3002
\u4f8b\u5982\uff0c\u51fd\u5f0f collect_anagrams(words, target)
\u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u4e32\u5217 words
\u8207\u4e00\u500b\u5b57\u4e32 target
\uff0c\u56de\u50b3 words
\u4e2d\u8207\u4e92\u70ba target
\u662f\u76f8\u540c\u5b57\u6bcd\u7570\u5e8f\u8a5e\u7684\u4e32\u5217:
\u65bc\u662f\u6211\u5728\u51fd\u5f0f\u7684\u5167\u90e8\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u8f14\u52a9\u51fd\u5f0f(Helper Function) is_anagram(x, y)
\uff0c\u4ed6\u63a5\u53d7\u5169\u500b\u5b57\u4e32 x
\u8207 y
\uff0c\u56de\u50b3 x
\u8207 y
\u662f\u5426\u70ba\u76f8\u540c\u5b57\u6bcd\u7570\u5e8f\u8a5e:
def collect_anagrams(words, target):\n def is_anagram(x, y):\n return sorted(x) == sorted(y)\n\n return [word for word in words if is_anagram(word, target)]\n\n\nstrs = [\"now\", \"won\", \"own\", \"no\", \"on\", \"www\"]\nprint(collect_anagrams(strs, \"onw\"))\n
Output['now', 'won', 'own']\n
\u9019\u500b\u5728\u5237 LeeCode \u6642\u6703\u5e38\u5e38\u7528\u5230\u3002
"},{"location":"fundamental/python/functions/#recursion","title":"Recursion","text":"\u4f86\u5230\u672c\u7ae0\u7684\u91cd\u982d\u6232\uff0c\u905e\u8ff4(Recursion)\u3002
\u905e\u8ff4\u662f\u4e00\u7a2e\u51fd\u5f0f\u547c\u53eb\u81ea\u5df1\u7684\u6280\u5de7\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u905e\u8ff4\u51fd\u5f0f(Recursive Function)\u3002
\u5148\u4f86\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(n!\\)\uff0c\u8ff4\u5708\u7684\u5beb\u6cd5\u662f\u9019\u6a23\u7684:
def factorial(n):\n result = 1\n for i in range(1, n + 1):\n result *= i\n return result\n\n\nprint(factorial(0))\nprint(factorial(5))\n
Output1\n120\n
\u8a18\u5f97\u9ad8\u4e2d\u6578\u5b78\u8ab2\u672c\u4e0a\u7684\u5b9a\u7fa9\u55ce?
\\[ \\text{factorial}(n) = \\begin{cases} 1 & \\text{if } n = 0 \\text{, base case}\\\\ n \\times \\text{factorial}(n-1) & \\text{if } n > 0 \\text{, recursive case}\\end{cases} \\]\u9019\u6a23\u7684\u5b9a\u7fa9\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ffb\u8b6f\u6210\u7a0b\u5f0f\u78bc:
def factorial(n):\n if n == 0:\n return 1\n else:\n return n * factorial(n - 1)\n\n\nprint(factorial(0))\nprint(factorial(5))\n
Output1\n120\n
\u7576 n == 0
\u6642\uff0c\u6211\u5011\u7a31\u70ba\u57fa\u672c\u60c5\u6cc1(Base Case)\uff0c\u9019\u662f\u905e\u8ff4\u7684\u7d42\u6b62\u689d\u4ef6\uff0c\u7576 n > 0
\u6642\uff0c\u6211\u5011\u7a31\u70ba\u905e\u8ff4\u60c5\u6cc1(Recursive Case)\uff0c\u9019\u662f\u905e\u8ff4\u7684\u57f7\u884c\u689d\u4ef6\u3002
\u6bcf\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f\u90fd\u61c9\u8a72\u6709\u4e00\u500b\u57fa\u672c\u60c5\u6cc1\uff0c\u9019\u6a23\u7684\u905e\u8ff4\u624d\u6703\u7d42\u6b62\uff0c\u5426\u5247\u5c31\u6703\u9677\u5165\u7121\u7aae\u905e\u8ff4\uff0c\u54a6?\u6709\u6c92\u6709\u5f88\u719f\u6089?\u9084\u8a18\u5f97\u7121\u7aae\u8ff4\u5708\u55ce?
\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97\u8cbb\u6c0f\u6578\u5217\u7684\u7b2c \\(n\\) \u9805\uff0c\u8ff4\u5708\u7684\u5beb\u6cd5\u662f\u9019\u6a23\u7684:
def fibonacci(n):\n a, b = 0, 1\n for _ in range(n):\n a, b = b, a + b\n return a\n\n\nprint(fibonacci(0))\nprint(fibonacci(10))\n
Output0\n55\n
\u540c\u6a23\u7684\uff0c\u9084\u8a18\u5f97\u9ad8\u4e2d\u6578\u5b78\u8ab2\u672c\u4e0a\u7684\u5b9a\u7fa9\u55ce?
\\[ \\text{fibonacci}(n) = \\begin{cases} 0 & \\text{if } n = 0 \\text{, base case}\\\\ 1 & \\text{if } n = 1 \\text{, base case}\\\\ \\text{fibonacci}(n-1) + \\text{fibonacci}(n-2) & \\text{if } n > 1 \\text{, recursive case}\\end{cases} \\]\u9019\u6a23\u7684\u5b9a\u7fa9\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ffb\u8b6f\u6210\u7a0b\u5f0f\u78bc:
def fibonacci(n):\n if n == 0:\n return 0\n elif n == 1:\n return 1\n else:\n return fibonacci(n - 1) + fibonacci(n - 2)\n\n\nprint(fibonacci(0))\nprint(fibonacci(10))\n
Output0\n55\n
\u4f46\u662f\uff0c\u905e\u8ff4\u7684\u6548\u7387\u901a\u5e38\u6bd4\u8ff4\u5708\u4f4e\uff0c\u56e0\u70ba\u905e\u8ff4\u6703\u9020\u6210\u5927\u91cf\u7684\u51fd\u5f0f\u547c\u53eb\uff0c\u800c\u4e14\u5bb9\u6613\u9020\u6210\u5927\u91cf\u7684\u91cd\u8907\u8a08\u7b97\u3002
\u518d\u4f86\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8a08\u7b97\u6574\u6578 a, b
\u7684\u6700\u5927\u516c\u56e0\u6578\uff0c\u4f60\u53ef\u80fd\u6709\u807d\u904e\u8f3e\u8f49\u76f8\u9664\u6cd5\uff0c\u53c8\u7a31\u70ba\u6b50\u5e7e\u91cc\u5f97\u6f14\u7b97\u6cd5(Euclidean algorithm)\uff0c\u4ed6\u7684\u5b9a\u7fa9\u662f\u9019\u6a23\u7684:
\u76f4\u63a5\u5beb\u6210\u7a0b\u5f0f\u78bc:
def gcd(a, b):\n if b == 0:\n return a\n else:\n return gcd(b, a % b)\n\n\nprint(gcd(12, 18))\nprint(gcd(18, 12))\nprint(gcd(4843, 1234))\n
Output6\n6\n1\n
\u9019\u500b\u8981\u5beb\u6210\u8fed\u4ee3(Iterative)\u7248\u672c\u9084\u6bd4\u8f03\u96e3\u3002
\u95dc\u65bc\u905e\u8ff4\uff0c\u5c31\u5148\u8b1b\u5230\u9019\u88e1\uff0c\u672a\u4f86\u9032\u5165\u6f14\u7b97\u6cd5\u7684\u7ae0\u7bc0\u6642\uff0c\u6703\u518d\u6df1\u5165\u8a0e\u8ad6\uff0c\u800c\u4e14\u6703\u6709\u66f4\u591a\u7684\u4f8b\u5b50\u3002
"},{"location":"fundamental/python/functions/#practice","title":"Practice","text":"Itsa - [C_MM48-\u6613] F91
Reference codedef f91(n):\n if n <= 100:\n return f91(f91(n + 11))\n elif n >= 101:\n return n - 10\n\n\nk = int(input())\nn = list(map(int, input().split()))\nfor x in n:\n print(f91(x))\n
\u4f60\u4ed4\u7d30\u89c0\u5bdf\u4e00\u4e0b\uff0c\u5176\u5be6\u53ef\u4ee5\u5beb\u6210\u9019\u6a23:
def f91(z): \n if z >= 101: \n return z - 10 \n else: \n return 91 \n\n\nk = int(input()) \nn = list(map(int, input().split())) \nfor x in n: \n print(f91(x)) \n
Itsa - [C_RU13-\u6613] \u5927\u4e00\u9ede\u7684Fibonacci
Reference codeMAX = 47\nfibonacci = [0] * MAX\nfibonacci[1] = 1\n\nfor i in range(2, MAX):\n fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]\n\nwhile True:\n n = int(input())\n if n == -1:\n break\n print(fibonacci[n + 1])\n
\u9019\u7a2e\u5beb\u6cd5\u53eb\u505a\u52d5\u614b\u898f\u5283(Dynamic Programming)\uff0c\u672a\u4f86\u6703\u518d\u8a0e\u8ad6\uff0c\u800c\u4e14\u6703\u5f88\u982d\u75db\u3002"},{"location":"fundamental/python/functions/#assignment","title":"Assignment","text":"Itsa - [C_MM103-\u6613] \u8cbb\u5f0f\u6578\u5217
Itsa - [C_MM143-\u6613] \u6c42Emirp
Itsa - [C_MM144-\u6613] \u6c42\u7d44\u5408\u6578C(n,r)
Itsa - \u984c\u76ee10. \u8f3e\u8f49\u76f8\u9664\u6cd5
Itsa - [C_RU10-\u4e2d] \u722c\u6a13\u68af
Itsa - [C_RU14-\u6613] \u597d\u9ad8\u7684\u6c34\u6676\u5854
Itsa - [C_RU23-\u6613] \u905e\u8ff4\u7df4\u7fd22f(n)=f(n-1)+2
Itsa - [C_RU24-\u6613] \u905e\u8ff4\u7df4\u7fd2f(n)\uff1d2f(n-1)/(1+2f(n-1))
Itsa - [C_RU25-\u6613] \u905e\u8ff4\u7df4\u7fd2f(n)\uff1d3f(n-1)+g(n-1)\ufe50g(n)\uff1d-f(n-1)+g(n-1)
"},{"location":"fundamental/python/lists/","title":"Lists","text":""},{"location":"fundamental/python/lists/#introduction","title":"Introduction","text":"\u4e0d\u66c9\u5f97\u4f60\u770b\u5230\u9019\u88e1\u6709\u6c92\u6709\u767c\u73fe\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u90fd\u662f\u7528\u4e00\u500b\u4e00\u500b\u7684\u8b8a\u6578\u4f86\u5132\u5b58\u8cc7\u6599\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u8981\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u9019\u6a23\u7684\u65b9\u5f0f\u5c31\u6703\u8b8a\u5f97\u5f88\u9ebb\u7169\u3002
\u6240\u4ee5\u9019\u88e1\u8ddf\u4f60\u4ecb\u7d39 List(\u4e32\u5217)\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u800c\u4e14\u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u4f8b\u5982: \u6578\u5b57\u3001\u5b57\u4e32\u3001\u5e03\u6797\u503c\u7b49\u7b49\u3002
"},{"location":"fundamental/python/lists/#create_a_list","title":"Create a list","text":"\u8981\u5efa\u7acb\u4e00\u500b List\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []
\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u6211\u5011\u8981\u5132\u5b58\u7684\u8cc7\u6599\uff0c\u8cc7\u6599\u4e4b\u9593\u7528\u9017\u865f ,
\u9694\u958b\u3002
\u4f86\u8209\u500b\u4f8b\u5b50\uff0c\u5efa\u7acb\u4e09\u500b List\uff0c\u4e00\u500b\u90fd\u5132\u5b58\u6574\u6578\uff0c\u4e00\u500b\u662f\u90fd\u5132\u5b58\u5b57\u4e32\uff1b\u6700\u5f8c\u4e00\u500b\u5247\u653e\u4e86\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\u3002
numbers = [1, 2, 3, 4, 5]\nprint(numbers)\n\nwhat_i_learned = [\"C\", \"C++\", \"Python\", \"Java\", \"C#\", \"JavaScript\", \"TypeScript\"]\nprint(what_i_learned)\n\nstudent_info = [\"Sean\", 20, \"Computer Science\", 4.0, True, \"aLIEz\"]\nprint(student_info)\n\nprint(type(numbers), type(what_i_learned), type(student_info))\n
ouput[1, 2, 3, 4, 5]\n['C', 'C++', 'Python', 'Java', 'C#', 'JavaScript', 'TypeScript']\n['Sean', 20, 'Computer Science', 4.0, True, 'aLIEz']\n<class 'list'> <class 'list'> <class 'list'>\n
Sawano Hiroyuki - aLIEz Aldnoah.Zero Full Lyrics
\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u6703\u807d\u904e Array(\u9663\u5217)\uff0cList \u5c31\u662f Python \u4e2d\u7684 Array\uff0c\u4f46\u53c8\u66f4\u5f37\u5927\uff0c\u56e0\u70ba List \u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u800c\u901a\u5e38 Array \u53ea\u80fd\u5132\u5b58\u4e00\u7a2e\u8cc7\u6599\u578b\u614b\u3002
\u4f46 Python \u4e5f\u6709\u63d0\u4f9b\u771f\u6b63\u7684\u300c\u9663\u5217\u300d\uff0c\u4f46\u6211\u5148\u4e0d\u63d0\uff0c\u8acb\u4f60\u7a0d\u5fae\u6709\u500b\u5370\u8c61\u5c31\u597d\u3002
\u984c\u5916\u8a71\uff0c\u7576\u521d\u5f9e C, C++, Java \u8f49\u5230 Python \u7684\u6642\u5019\uff0c\u771f\u5fc3\u89ba\u5f97\u600e\u9ebc\u53ef\u4ee5\u9019\u9ebc\u96a8\u4fbf\uff0c\u9023\u578b\u614b\u90fd\u4e0d\u7528\u5ba3\u544a\uff0c\u73fe\u5728\u89ba\u5f97\u771f\u9999\u3002
\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u6709\u63d0\u5230 range
\u9019\u500b\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6574\u6578\u7684\u5e8f\u5217\uff0c\u4f8b\u5982: range(5)
\u6703\u7522\u751f\u4e00\u500b\u5f9e \\(0\\) \u5230 \\(4\\) \u7684\u6574\u6578\u5e8f\u5217\u3002
list(range(5))
\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 range
\u7269\u4ef6\u8f49\u63db\u6210 List\u3002
x = range(5)\nprint(type(x))\nnumbers = list(x)\nprint(numbers)\n
ouput<class 'range'>\n[0, 1, 2, 3, 4]\n
\u90a3\u80fd\u5426\u628a\u5b57\u4e32\u8f49\u63db\u6210 List \u5462? \u7576\u7136\u53ef\u4ee5!
x = \"Signals\"\nprint(type(x))\nwords = list(x)\nprint(words)\n
ouput<class 'str'>\n['S', 'i', 'g', 'n', 'a', 'l', 's']\n
Lazer Boomerang - Signals (Official Audio)
\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u4f55\u521d\u59cb\u5316\u4e00\u500b\u5143\u7d20\u90fd\u662f \\(0\\) \u7684\u4e32\u5217\u3002
n = 5\nzeros = [0] * n\nprint(zeros)\n
ouput[0, 0, 0, 0, 0]\n
\u5982\u4f55\u5c07\u5169\u500b\u4e32\u5217\u5408\u4f75\u6210\u4e00\u500b\u4e32\u5217\u5462?
a = [1, 2, 3]\nb = [4, 5, 6]\nc = a + b\nprint(c)\n
ouput[1, 2, 3, 4, 5, 6]\n
\u9084\u6709\u4e00\u500b\u65b9\u6cd5\uff0c\u5c31\u662f\u4f7f\u7528 extend
\u9019\u500b\u65b9\u6cd5\uff0c\u9019\u5728\u5f8c\u9762\u6703\u4ecb\u7d39\u5230\u3002
\u6211\u76f8\u4fe1\u4ee5\u4e0a\u9019\u4e9b\u4f8b\u5b50\u80fd\u8b93\u4f60\u6293\u5230\u4e00\u4e9b\u6982\u5ff5\uff0c\u63a5\u4e0b\u4f86\u5c07\u4ecb\u7d39\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u8207\u65b9\u6cd5\uff0c\u8b93\u4f60\u66f4\u719f\u6089 List\u3002
"},{"location":"fundamental/python/lists/#operations","title":"Operations","text":""},{"location":"fundamental/python/lists/#accessing_elements","title":"Accessing elements","text":"\u8981\u5b58\u53d6 List \u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []
\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7d22\u5f15\u503c\u5f9e \\(0\\) \u958b\u59cb\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528\u8ca0\u6578\uff0c\u8ca0\u6578\u7684\u7d22\u5f15\u503c\u662f\u5f9e\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u958b\u59cb\u7b97\u8d77\u3002
numbers = [1, 2, 3, 4, 5]\n\n# First element\nprint(numbers[0])\n# Last element\nprint(numbers[-1])\nprint(numbers[4])\n
ouput1\n5\n5\n
\u8b93\u6211\u5011\u914d\u5408\u8ff4\u5708\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\n\nfor i in range(len(numbers)):\n print(numbers[i], end=' ')\n
ouput1 2 3 4 5\n
len
\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u4e32\u5217\u7684\u9577\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e32\u5217\u4e2d\u5143\u7d20\u7684\u500b\u6578\u3002
\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97 Repetiton Structures - Foreach \u7684\u5167\u5bb9\uff0c\u6211\u5011\u7528\u9019\u500b\u6982\u5ff5\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\n\nfor number in numbers:\n print(number, end=' ')\n
ouput1 2 3 4 5\n
"},{"location":"fundamental/python/lists/#slicing","title":"Slicing","text":"\u5207\u7247(Slicing)\u53ef\u4ee5\u7528\u4f86\u53d6\u5f97\u4e32\u5217\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982:
numbers = [1, 2, 3, 4, 5, 6]\n\nprint(numbers[0:4])\nprint(numbers[:4])\nprint(numbers[:-2])\nprint(numbers[1:3])\nprint(numbers[:6:2])\nprint(numbers[::-1])\nprint(numbers[:])\n
ouput[1, 2, 3, 4]\n[1, 2, 3, 4]\n[1, 2, 3, 4]\n[2, 3]\n[1, 3, 5]\n[6, 5, 4, 3, 2, 1]\n[1, 2, 3, 4, 5, 6]\n
@EditTime : 2024-01-31 21:56
\u4f60\u5f88\u8070\u660e\uff0c\u61c9\u8a72\u767c\u73fe\u4e86\u7528\u6cd5\u8207 range
\u4e00\u6a23\uff0c\u5206\u5225\u662f\u8d77\u59cb\u7d22\u5f15\u503c\u3001\u7d50\u675f\u7d22\u5f15\u503c\u3001\u9593\u9694\uff0c\u5982\u679c\u4e0d\u5beb\u7684\u8a71\uff0c\u9810\u8a2d\u503c\u5206\u5225\u662f \\(0\\)\u3001\u4e32\u5217\u9577\u5ea6\u3001\\(1\\)\u3002
\u8981\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []
\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7136\u5f8c\u518d\u6307\u5b9a\u65b0\u7684\u503c\u3002
music_info = [\"Time_To_Pretend\", \"Lazer Boomerang\", \" 2019/4/26\", 1, False]\nmusic_info[0] = \"Time To Pretend\"\nmusic_info[3] = 2\nmusic_info[-1] = True\nprint(music_info)\n
ouput['Time To Pretend', 'Lazer Boomerang', ' 2019/4/26', 2, True]\n
Lazer Boomerang - Time To Pretend (Official Audio)
\u6211\u5011\u53ef\u4ee5\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u503c\uff0c\u9019\u7a31\u70ba \u53ef\u8b8a\u52d5\u7684(mutable)\uff0c\u800c\u5b57\u4e32\u5247\u4e0d\u884c\uff0c\u9019\u7a31\u70ba \u4e0d\u53ef\u8b8a\u52d5\u7684(immutable)\u3002
sad_cat = \"\ud83d\ude3f\"\nsad_cat[0] = \"\ud83d\ude38\"\n
ouputTypeError: 'str' object does not support item assignment\n
"},{"location":"fundamental/python/lists/#checking_elements","title":"Checking elements","text":"\u8981\u6aa2\u67e5\u4e32\u5217\u4e2d\u662f\u5426\u6709\u67d0\u500b\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 in
\u9019\u500b\u904b\u7b97\u5b50\u3002
numbers = [1, 2, 3, 4, 5]\nprint(1 in numbers)\nprint(6 not in numbers)\n
ouputTrue\nTrue\n
Question
\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u4e32\u5217\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u6700\u58de\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?
\u7b54\u6848\u662f \\(n\\) \u6b21\u5594\uff0c\u56e0\u70ba\u4e00\u500b\u4e00\u500b\u627e\uff0c\u76f4\u5230\u627e\u5230\u6216\u662f\u627e\u5b8c\u70ba\u6b62\uff0c\u50cf\u662f\u4f8b\u5b50\u4e2d\u7684 6
\u3002
\u5728\u6f14\u7b97\u6cd5(Algorithm)\u7684\u7ae0\u7bc0\u4e2d\u6703\u63d0\u5230\u6642\u9593\u8907\u96dc\u5ea6(Time Complexity)\u9019\u500b\u89c0\u5ff5\uff0c\u9019\u88e1\u5c31\u5148\u7d66\u4f60\u4e00\u500b\u5370\u8c61\u5c31\u597d\u4e86~
"},{"location":"fundamental/python/lists/#methods","title":"Methods","text":"\u4f86\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u4e32\u5217\u65b9\u6cd5\u3002
"},{"location":"fundamental/python/lists/#append","title":"append","text":"append
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u4e00\u500b\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.append(6)\nprint(numbers)\nnumbers.append([7, 8, 9])\nprint(numbers)\n
ouput[1, 2, 3, 4, 5, 6]\n[1, 2, 3, 4, 5, 6, [7, 8, 9]]\n
\u5e0c\u671b\u7b2c\u4e8c\u500b append
\u6c92\u6709\u5687\u5230\u4f60\uff0c\u5b83\u53ef\u4ee5\u65b0\u589e\u4e00\u500b\u4e32\u5217\uff0c\u4f46\u662f\u9019\u500b\u4e32\u5217\u6703\u8b8a\u6210\u4e32\u5217\u4e2d\u7684\u4e00\u500b\u5143\u7d20\u3002\u5982\u679c\u4f60\u662f\u60f3\u8981\u628a 7
\u30018
\u30019
\u9019\u4e09\u500b\u5143\u7d20\u52a0\u5230\u4e32\u5217\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 extend
\u9019\u500b\u65b9\u6cd5\u3002
extend
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u53e6\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.extend([6, 7, 8])\nprint(numbers)\n
ouput[1, 2, 3, 4, 5, 6, 7, 8]\n
"},{"location":"fundamental/python/lists/#pop","title":"pop","text":"pop
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u88ab\u79fb\u9664\u7684\u5143\u7d20\u3002
fruits = [\"apple\", \"banana\", \"cherry\", \"durian\", \"elderberry\"]\nprint(fruits.pop())\nprint(fruits)\nprint(fruits.pop(1))\nprint(fruits)\n
ouputelderberry\n['apple', 'banana', 'cherry', 'durian']\nbanana\n['apple', 'cherry', 'durian']\n
\u901a\u5e38\uff0c\u6211\u5011\u6703\u4f7f\u7528 pop
\u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u56e0\u70ba\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u7d22\u5f15\u503c\u8d85\u51fa\u7bc4\u570d\u7684\u932f\u8aa4\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7d22\u5f15\u503c\uff0c\u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u4e0d\u5efa\u8b70\u3002
clear
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.clear()\nprint(numbers)\n
ouput[]\n
"},{"location":"fundamental/python/lists/#reverse","title":"reverse","text":"reverse
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u53cd\u8f49\u4e32\u5217\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers.reverse()\nprint(numbers)\n
ouput[5, 4, 3, 2, 1]\n
"},{"location":"fundamental/python/lists/#sort","title":"sort","text":"sort
\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u6392\u5e8f\u4e32\u5217\uff0c\u9810\u8a2d\u662f\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u5982\u679c\u8981\u7531\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u53ef\u4ee5\u6307\u5b9a reverse=True
\u3002
numbers = [5, 4, 3, 2, 1]\nnumbers.sort()\nprint(numbers)\nnumbers.sort(reverse=True)\nprint(numbers)\n
ouput[1, 2, 3, 4, 5]\n[5, 4, 3, 2, 1]\n
\u751a\u81f3\u4f60\u53ef\u4ee5\u8a2d\u5b9a key
\uff0c\u4f86\u6c7a\u5b9a\u6392\u5e8f\u7684\u4f9d\u64da\uff0c\u4f8b\u5982: key=len
\uff0c\u5c31\u662f\u6839\u64da\u5143\u7d20 x
\u7684 len(x)
\u4f86\u6392\u5e8f\u3002
fruits = [\"apple\", \"banana\", \"watermelon\", \"pineapple\"]\nfruits.sort(key=len)\nprint(fruits)\n
ouput['apple', 'banana', 'pineapple', 'watermelon']\n
"},{"location":"fundamental/python/lists/#copy","title":"copy","text":"copy
\u9019\u500b\u65b9\u6cd5\u56de\u50b3\u4e00\u500b\u6dfa\u8907\u88fd(Shallow Copy)\u7684\u4e32\u5217\u3002
numbers = [1, 2, 3, 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1] = 10\nprint(numbers_copy)\nprint(numbers)\n
ouput[1, 2, 3, 4, 5]\n[1, 2, 3, 4, 5]\nAfter changing numbers[1]\n[1, 2, 3, 4, 5]\n[1, 10, 3, 4, 5]\n
\u9019\u500b\u4f8b\u5b50\u9084\u4e0d\u80fd\u770b\u51fa\u6dfa\u8907\u88fd\uff0c\u4fee\u6539\u4e86 numbers
\u7684\u7b2c\u4e8c\u500b\u5143\u7d20\uff0cnumbers_copy
\u4e0d\u6703\u8ddf\u8457\u6539\u8b8a\u3002
numbers = [1, [2, 3], 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1][0] = 6\nprint(numbers_copy)\nprint(numbers)\n
ouput[1, [2, 3], 4, 5]\n[1, [2, 3], 4, 5]\nAfter changing numbers[1]\n[1, [6, 3], 4, 5]\n[1, [6, 3], 4, 5]\n
\u90a3\u4ec0\u9ebc\u53c8\u662f\u6df1\u8907\u88fd(Deep Copy)\u5462? \u9019\u500b\u5c31\u8981\u7b49\u5230\u6211\u5011\u5b78\u5230\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08(Object-Oriented Programming)\u7684\u6642\u5019\uff0c\u518d\u4f86\u8ddf\u4f60\u4ecb\u7d39\uff0c\u6211\u4e0d\u61c9\u8a72\u6316\u5751\u7684\u3002
\u9084\u6709\u66f4\u591a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u53c3\u8003 Python Documentation - List\uff0c\u4f46\u6211\u4e0a\u9762\u4ecb\u7d39\u7684\u65b9\u6cd5\uff0c\u4f60\u81f3\u5c11\u8981\u6703\uff0c\u4f60\u4e5f\u8981\u990a\u6210\u770b\u5b98\u65b9\u6587\u4ef6\u7684\u7fd2\u6163\u3002
"},{"location":"fundamental/python/lists/#list_comprehension","title":"List Comprehension","text":"\u4e32\u5217\u7d9c\u5408\u904b\u7b97(List comprehension)\u8b93\u4f60\u7528\u4e00\u884c\u7a0b\u5f0f\u78bc\uff0c\u5c31\u53ef\u4ee5\u5efa\u7acb\u51fa\u64c1\u6709\u67d0\u9805\u6027\u8cea\u5143\u7d20\u7684\u4e32\u5217\u3002
\u8209\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a
\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b
\uff0c\u5176\u4e2d b[i] = a[i] ** 2
\uff0c\u4e5f\u5c31\u662f b
\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0d\u61c9 a
\u5143\u7d20\u7684\u5e73\u65b9\u3002
\u6211\u5011\u5148\u4f7f\u7528\u8ff4\u5708\u4f86\u5be6\u4f5c:
a = [1, 2, 3, 4, 5]\nb = []\n\nfor num in a:\n b.append(num ** 2)\n\nprint(b)\n
ouput[1, 4, 9, 16, 25]\n
\u518d\u4f86\u4f7f\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97:
a = [1, 2, 3, 4, 5]\nb = [num ** 2 for num in a]\n\nprint(b)\n
ouput[1, 4, 9, 16, 25]\n
\u975e\u5e38\u5730\u512a\u96c5\uff0c\u6211\u5011\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a
\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b
\uff0c\u5176\u4e2d b
\u7684\u5143\u7d20\u90fd\u662f a
\u5143\u7d20\u4e2d\u7684\u5076\u6578\u3002
a = [1, 2, 3, 4, 5]\nb = [num for num in a if num % 2 == 0]\n\nprint(b)\n
ouput[2, 4]\n
\u9084\u8a18\u5f97 Operators - Bonus: map for input \u63d0\u5230\u7684 map
\u55ce?
\u5148\u4f86\u8907\u7fd2\u4e00\u4e0b\uff0cmap
\u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u5957\u7528\u5230\u4e00\u500b\u5e8f\u5217\u7684\u6bcf\u4e00\u500b\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u4e00\u500b map
\u7269\u4ef6\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 list
\u5c07 map
\u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\u3002
a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = map(int, a)\nprint(type(b))\n\nc = list(b)\nprint(c)\n
ouput<class 'map'>\n[1, 2, 3, 4, 5]\n
@EditTime : 2024-02-01 23:21
\u90a3\u80fd\u4e0d\u80fd\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97\u4f86\u5be6\u4f5c\u5462?
a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = [int(num) for num in a]\nprint(b)\n
ouput[1, 2, 3, 4, 5]\n
\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002
\u9084\u6709\u5f88\u591a\u61c9\u7528\uff0c\u4f8b\u5982: \u4e32\u5217\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0f\u5beb\uff0c\u6211\u5011\u60f3\u8981\u628a\u5b83\u5011\u8f49\u63db\u6210\u5927\u5beb\u3002
lowercase = [\"a\", \"b\", \"c\", \"d\", \"e\"]\nuppercase = [letter.upper() for letter in lowercase]\nprint(uppercase)\n
ouput['A', 'B', 'C', 'D', 'E']\n
\u6ce8\u610f\u56c9\uff0c\u9019\u908a\u7528\u5230\u7684\u662f\u65b9\u6cd5\u5594\u3002
"},{"location":"fundamental/python/lists/#nested_lists","title":"Nested Lists","text":"\u5de2\u72c0\u4e32\u5217(Nested Lists)\uff0c\u5c31\u662f\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u4e5f\u662f\u4e32\u5217\u3002
"},{"location":"fundamental/python/lists/#2d","title":"2D","text":"\u5148\u4f86\u8209\u4e00\u500b\u4f60\u53ef\u80fd\u807d\u904e\u7684\u540d\u8a5e\uff0c\u4e8c\u7dad\u9663\u5217(2D Array)\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5de2\u72c0\u4e32\u5217\u4f86\u5be6\u4f5c\u4e8c\u7dad\u9663\u5217\uff0c\u4f8b\u5982\u5169\u500b \\(3 \\times 3\\) \u7684\u4e8c\u7dad\u9663\u5217\uff0c\u4f86\u9032\u884c\u77e9\u9663\u52a0\u6cd5\u3002
matrix_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nmatrix_2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]\n\nmatrix_3 = [[0 for i in range(3)] for j in range(3)]\n\nfor i in range(3):\n for j in range(3):\n matrix_3[i][j] = matrix_1[i][j] + matrix_2[i][j]\n\nprint(matrix_3)\n
ouput[[10, 10, 10], [10, 10, 10], [10, 10, 10]]\n
\u6211\u5e0c\u671b matrix_3
\u7684\u521d\u59cb\u5316\u6c92\u6709\u5687\u5230\u4f60\uff0c\u8acb\u4f60\u628a [0 for i in range(3)]
\u7576\u6210\u4e00\u500b\u6574\u9ad4 E
\uff0c\u518d\u628a [E for j in range(3)]
\u7576\u6210\u4e00\u500b\u6574\u9ad4\uff0c\u9019\u6a23\u5c31\u4e0d\u6703\u89ba\u5f97\u5f88\u8907\u96dc\u4e86\uff0ci, j
\u90fd\u53ea\u662f\u8a08\u6578\u7528\u7684\u800c\u5df2\uff0c\u4e0d\u8981\u88ab\u5b83\u5011\u5687\u5230\u3002
\u92f8\u9f52\u72c0\u4e32\u5217(Jagged Lists)\uff0c\u5c31\u662f\u5141\u8a31\u5de2\u72c0\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7684\u9577\u5ea6\u4e0d\u4e00\u6a23\u3002
\u984c\u5916\u8a71\uff0c\u4e4b\u524d\u5728\u5b78 C \u7684\u6642\u5019\uff0c\u53ea\u80fd\u7528\u6307\u6a19\u4f86\u5be6\u4f5c\uff0c\u4f46\u7528\u8d77\u4f86\u771f\u7684\u5f88\u9ebb\u7169\u3002
jagged = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]\n\nfor row in jagged:\n for elem in row:\n print(elem, end=' ')\n print()\n
ouput1 2 3 \n4 5 \n6 7 8 9 \n
\u5e0c\u671b\u9019\u500b\u8ff4\u5708\u7684\u5beb\u6cd5\u4e0d\u6703\u5687\u5230\u4f60\uff0crow
\u662f jagged
\u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u4e32\u5217\uff0celem
\u662f row
\u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u6574\u6578\u3002
Python \u7684\u8a9e\u6cd5\u771f\u7684\u5f88\u9748\u6d3b\uff0c\u4f46\u4e5f\u8981\u5f88\u5c0f\u5fc3\uff0c\u4ed6\u63d0\u4f9b\u4e86\u5f88\u591a\u5de5\u5177\uff0c\u4f46\u5982\u679c\u4f60\u4e0d\u6e05\u695a\u4ed6\u662f\u600e\u9ebc\u904b\u4f5c\u7684\uff0c\u5c31\u6703\u5bb9\u6613\u6703\u5beb\u51fa\u6548\u7387\u4f4e\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u4e5f\u662f\u96d9\u9762\u5203\uff0c\u6240\u4ee5\u8981\u591a\u591a\u7df4\u7fd2\uff0c\u591a\u591a\u601d\u8003\uff0c\u4f60\u53ef\u4ee5\u7684\u3002
"},{"location":"fundamental/python/lists/#practice","title":"Practice","text":"Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578
Reference codetable = []\nfor i in range(6, 2 ** 13, 2):\n s = 0\n for j in range(1, i):\n if i % j == 0:\n s += j\n if s == i:\n table.append(i)\n\nwhile True:\n N = int(input())\n print(' '.join([str(x) for x in table if x <= N]))\n
\u5efa\u7acb\u4e00\u500b\u4e32\u5217 table
\uff0c\u88e1\u9762\u653e\u4e86 \\(6\\) \u5230 \\(2^{13}\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\uff0c\u63a5\u8457\u8f38\u5165\u4e00\u500b\u6574\u6578 \\(N\\)\uff0c\u8f38\u51fa \\(1\\) \u5230 \\(N\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\u3002 \u90a3\u9ebc \\(2^{13}\\) \u662f\u600e\u9ebc\u4f86\u7684?\u311c\uff0c\u6211\u5237\u5f8c\u53f0\u6e2c\u8cc7\u63a8\u6572\u4f86\u7684\u3002
join
\u65b9\u6cd5\u53ef\u4ee5\u5c07\u5b57\u4e32\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7528\u6307\u5b9a\u7684\u5b57\u4e32\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u908a\u7528\u7a7a\u767d\u5b57\u4e32 ' '
\u4f86\u9023\u63a5\u3002
\u9019\u7a2e\u6280\u5de7\u7a31\u70ba\u300c\u5efa\u8868\u300d\uff0c\u4e8b\u5be6\u4e0a\u5b8c\u7f8e\u6578\u5f88\u758f\u6563\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u81ea\u5df1\u5efa\u5b8c\u8868\u5f8c\u76f4\u63a5\u5beb\u6b7b\uff0c\u4f46\u9019\u6a23\u7684\u8a71\uff0c\u7a0b\u5f0f\u78bc\u5c31\u4e0d\u5177\u6709\u901a\u7528\u6027\u3002
table = [6, 28, 496, 8128]\n\nN = int(input())\nprint(' '.join(str(x) for x in table if x <= N))\n
\u5982\u679c\u5c31\u55ae\u7d14\u89e3\u984c\u7684\u8a71\uff0c\u9019\u6a23\u4e5f\u662f\u53ef\u4ee5\u7684\u3002
\u6b63\u898f\u7684\u984c\u76ee\u90fd\u6703\u898f\u7bc4\u6e2c\u8cc7\u7bc4\u570d\uff0c\u5982\u679c\u4f60\u4ee5\u5f8c\u4e0a\u5927\u5b78\u5f8c\u9047\u5230\u4e0d\u592a\u9748\u5149\u7684\u52a9\u6559\u6c92\u6709\u6a19\u6e2c\u8cc7\u7bc4\u570d\uff0c\u8a18\u5f97\u96fb\u6b7b\u4ed6\u5011\u3002
Itsa - [C_MM44-\u6613] The Numbers
Reference codeN, M = input().split()\n\ncnt = 0\nfor i in range(0, 6):\n if M[i] == N[0] and M[i + 1] == N[1]:\n cnt += 1\n\nprint(cnt)\n
\u5b57\u4e32\u4e5f\u53ef\u4ee5\u50cf\u662f\u4e32\u5217\u4e00\u6a23\uff0c\u4f7f\u7528\u7d22\u5f15\u503c\u4f86\u5b58\u53d6\u5143\u7d20\u3002
Itsa - [C_AR01-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 I
Reference codearr = input().split()\nprint(' '.join(arr[::-1]))\n
arr.reverse()
\u4e5f\u884c\uff0c\u4f46\u662f\u9019\u6a23\u6703\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5c31\u4f7f\u7528 arr[::-1]
\u3002
arr = input().split()\narr.reverse()\nprint(' '.join(arr))\n
Itsa - [C_AR03-\u6613] \u8a08\u7b97\u9663\u5217\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7acb\u65b9\u548c
Reference codearr = map(int, input().split())\nans = 0\n\nfor num in arr:\n ans += num ** 3\n\nprint(ans)\n
\u6211\u4e26\u6c92\u6709\u628a map
\u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\uff0c\u56e0\u70ba\u6211\u53ea\u9700\u8981\u4e00\u6b21\u8fed\u4ee3\uff0c\u6240\u4ee5\u76f4\u63a5\u7528 map
\u7269\u4ef6\u5c31\u597d\u3002
\u4f86\u770b\u4e00\u884c\u89e3:
print(sum(int(x) ** 3 for x in input().split()))\n
\u53c3\u8003\u5c31\u597d\u3002
Itsa - [C_AR09-\u6613] \u5169\u6578\u5dee\u503c
Reference codearr1 = list(map(int, input().split(',')))\narr1.sort()\narr2 = arr1.copy()\narr2.sort(reverse=True)\n\nmax_num,min_num = 0, 0\nfor i in range(len(arr1) - 1, -1, -1):\n # convert list to number\n max_num = max_num * 10 + arr1[i]\n min_num = min_num * 10 + arr2[i]\n\nprint(max_num - min_num)\n
\u4e0d\u61c2\u6392\u5e8f\u6c92\u95dc\u4fc2\uff0c\u6211\u5011\u5148\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u4ee5\u5f8c\u6211\u6703\u6559\u4f60\u539f\u7406\u3002
\u800c\u9019\u984c\u7684\u91cd\u9ede\u662f\uff0c\u5982\u4f55\u5c07\u4e32\u5217\u8f49\u63db\u6210\u6578\u5b57\u3002\u5f9e\u500b\u4f4d\u6578\u958b\u59cb\uff0c\u6bcf\u6b21\u4e58 \\(10\\)\uff0c\u518d\u52a0\u4e0a\u4e0b\u4e00\u500b\u6578\u5b57\u3002
Itsa - [C_AR022-\u6613] \u5b57\u6bcd\u51fa\u73fe\u7684\u983b\u7387
Reference codestring = input()\n\nfreq = [0] * 26\n\nfor c in string:\n if c.isalpha():\n freq[ord(c.lower()) - ord('a')] += 1\n\nprint(' '.join(str(x) for x in freq))\n
\u9019\u984c\u9700\u8981\u4e00\u500b\u5148\u5099\u77e5\u8b58\uff0cASCII \u78bc\uff0cord
\u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u5b57\u5143\u7684 ASCII \u78bc\u3002
\u518d\u5229\u7528 isalpha
\u65b9\u6cd5\uff0c\u4f86\u5224\u65b7\u5b57\u5143\u662f\u5426\u70ba\u5b57\u6bcd\uff0c\u4ee5\u53ca lower
\u65b9\u6cd5\uff0c\u4f86\u5c07\u5b57\u6bcd\u8f49\u63db\u6210\u5c0f\u5beb\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u5b57\u6bcd\u6620\u5c04\u5230 0
\u5230 25
\u4e4b\u9593\u3002
\u95dc\u65bc\u5b57\u4e32\u7684\u4e3b\u984c\uff0c\u5f8c\u9762\u6703\u518d\u8ddf\u4f60\u4ecb\u7d39\u3002
Itsa - [C_AR025-\u6613] \u8a08\u7b97ASCII\u5b57\u5143
Reference codestring = input()\n\nfreq = [0] * 128\n\n\nfor i in range(len(string)):\n freq[ord(string[i])] += 1\n\nfor i in range(len(freq) - 1, -1, -1):\n if freq[i] > 0:\n print(f'{i} {freq[i]}')\n
\u9019\u984c\u8ddf\u4e0a\u4e00\u984c\u5f88\u50cf\u3002
Itsa - [C_AR029-\u96e3] \u6587\u5b57\u7de8\u78bc
Reference codeplain_text = input()\n\nN = len(plain_text)\nM = 1\nwhile M * M < N:\n M += 1\n\narr = [[' ' for _ in range(M)] for _ in range(M)]\n\nfor i in range(N):\n arr[i // M][i % M] = plain_text[i]\n\ncipher_text = []\nfor i in range(M):\n for j in range(M):\n cipher_text.append(arr[j][i])\n\nprint(''.join(cipher_text))\n
\u77e9\u9663\u8f49\u7f6e\uff0c\u6240\u4ee5 arr[j][i]
\u8b8a\u6210 arr[i][j]
\u3002
Itsa - [C_MM42-\u4e2d] \u6c42(-1)^(n+1)x[1/(2n-1)]\u7684\u548c
Itsa - [C_AR02-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 II
Itsa - [C_AR10-\u4e2d] \u65b0\u901a\u8a71\u8cbb\u7387
Itsa - [C_AR021-\u6613] \u6210\u7e3e\u7d71\u8a08
Itsa - [C_AR023-\u6613] \u5b57\u6839\u8207\u5b50\u5b57\u4e32
Itsa - [C_AR031-\u4e2d] \u4e00\u7dad\u77e9\u9663\u8868\u793a\u4e8c\u7dad\u5e73\u9762\u7a7a\u9593
Itsa - [C_AR33-\u6613] \u8f49\u7f6e\u77e9\u9663
Itsa - [C_AR34-\u6613] \u8eab\u5206\u8b49\u9a57\u8b49\u5668
Itsa - [C_AR35-\u6613] \u751f\u8096\u554f\u984c
Itsa - [C_AR41-\u6613] \u4e00\u6574\u6578\u5e8f\u5217\u6240\u542b\u4e4b\u6574\u6578\u500b\u6578\u53ca\u5e73\u5747\u503c
Itsa - [C_AR46-\u6613] \u9663\u5217\u5e73\u65b9\u548c\u554f\u984c
Itsa - [C_AR48-\u6613] \u6578\u5b57\u52a0\u5bc6
@EditTime : 2024-02-03 21:30
"},{"location":"fundamental/python/operators/","title":"Operators","text":""},{"location":"fundamental/python/operators/#introduction","title":"Introduction","text":"\u5728\u9019\u500b\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u904b\u7b97\u5b50\uff0c\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u5c0d\u8b8a\u6578\u505a\u904b\u7b97\uff0c\u4f8b\u5982: \u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u7b49\u7b49\u3002
"},{"location":"fundamental/python/operators/#arithmetic_operators","title":"Arithmetic Operators","text":"\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u6211\u5011\u719f\u6089\u7684\u56db\u5247\u904b\u7b97\u5c31\u662f\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u5206\u5225\u662f +
\u3001-
\u3001*
\u3001/
\uff0c\u5206\u5225\u4ee3\u8868\u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3002
x, y = 10, 3\nprint(x + y)\nprint(x - y)\nprint(x * y)\nprint(x / y)\nz = x // y\nprint(z)\nprint(type(z))\nprint(x % y)\nprint(x ** y)\n
ouput13\n7\n30\n3.3333333333333335\n3\n<class 'int'>\n1\n1000\n
\u6211\u76f8\u4fe1\u770b\u5b8c\u8f38\u51fa\u5f8c\uff0c\u4f60\u80fd\u5927\u81f4\u660e\u77ad\u5404\u500b\u904b\u7b97\u5b50\u7684\u4f5c\u7528\uff0c\u5176\u4e2d\u60f3\u8acb\u4f60\u7559\u610f *
\u4e58\u6cd5\u904b\u7b97\u5b50\uff0c\u8acb\u4f60\u4e0d\u8981\u6253\u6210 x
\uff0c\u4ee5\u53ca /
\u8207 //
\u7684\u5dee\u5225\uff0c\u5f8c\u8005\u6703\u5c07\u7d50\u679c\u5411\u4e0b\u53d6\u6574\u3002\u9084\u6709\u6bd4\u8f03\u7279\u5225\u7684 %
\uff0c\u7559\u610f\u5230 10 = 3 * 3 + 1
\uff0c\u5c31\u662f\u53d6\u9918\u6578\uff1b\u800c\u770b\u8d77\u4f86\u6700\u5947\u7279\u7684 **
\uff0c\u4ed6\u7684\u4f5c\u7528\u662f\u6c42\u51aa\u3002
\u6211\u5011\u4f86\u8907\u7fd2\u4e00\u4e0b\u570b\u5c0f\u6578\u5b78:
# Upper case for constant\nPI = 3.14\nr = 2\narea = PI * (r ** 2)\nperimeter = 2 * PI * r\nprint(f'Area: {area}, Perimeter: {perimeter}')\n\n# Sum of 1 to 100\ntotal = (1 + 100) * 100 // 2\nprint(f'The sum of 1 to 100 is {total}')\n
ouputArea: 12.56, Perimeter: 12.56\nThe sum of 1 to 100 is 5050\n
\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c\u904b\u7b97\u5b50\u4e4b\u9593\u6709\u512a\u5148\u9806\u5e8f\uff0c\u7576\u4f60\u4e0d\u78ba\u5b9a\u7d50\u679c\u7684\u6642\u5019\uff0c\u8acb\u5584\u7528\u62ec\u865f\u3002
x = 'A' + 'n' + 'i' + 'm' + 'a' + 'l' + 's'\nprint(x)\n
ouputAnimals\n
Martin Garrix - Animals (Official Video)
\u518d\u8acb\u4f60\u7559\u610f\u4e00\u4ef6\u4e8b\uff0c\u4e0d\u540c\u985e\u5225\u5c0d\u65bc\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4e0d\u540c\u7684\uff0c\u4f8b\u5982\u5b57\u4e32\u7684 +
\u6703\u5c07\u5169\u500b\u5b57\u4e32\u76f8\u9023\uff0c\u800c\u6574\u6578\u7684 +
\u5247\u6703\u5c07\u5169\u500b\u6574\u6578\u76f8\u52a0\u3002
Question
2 ** 3 ** 2
\u7684\u7d50\u679c\u662f\u591a\u5c11?2 ** (3 ** 2)
\u7684\u7d50\u679c\u662f\u591a\u5c11?(2 ** 3) ** 2
\u7684\u7d50\u679c\u662f\u591a\u5c11?\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7d50\u679c\u6703\u662f True
\u6216 False
\uff0c\u9019\u500b\u7d50\u679c\u6211\u5011\u7a31\u70ba\u5e03\u6797\u503c\u3002
\u6211\u5011\u4f86\u4ee5\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u6027\u8cea\u4f86\u505a\u4e00\u4e9b\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7df4\u7fd2\u3002
a, b, c = 3, 4, 5\n\nprint(a < b)\nprint(a > b)\nprint(a <= b)\nprint(a >= b)\nprint(a ** 2 + b ** 2 == c ** 2)\nprint(a ** 2 + b ** 2 != c ** 2)\nprint((a + b) > c)\n
ouputFalse\nTrue\nFalse\nTrue\nFalse\nTrue\n
\u9019\u908a\u8981\u63d0\u9192\u4f60\uff0c ==
\u662f\u6bd4\u8f03\u904b\u7b97\u5b50\uff0c\u800c =
\u662f\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u7684\u610f\u601d\u662f\u4e0d\u4e00\u6a23\u7684\u3002
\u6700\u5f8c\u4f86\u770b\u4e00\u500b\u4f8b\u5b50:
age = 14\nprint(12 <= age <= 18)\n\nx, y, z = 1, 2, 3\nprint(x < y < z)\nprint(x < y and y < z)\n
ouputTrue\nTrue\nTrue\n
\u5728Python\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6bd4\u8f03\u904b\u7b97\u5b50\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u6a23\u7684\u5beb\u6cd5\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002
Question
print(1 < 2 < 3 < 4 < 5)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(1 < 2 < 3 < 4 > 5)
\u6703\u5370\u51fa\u4ec0\u9ebc?\u6211\u5011\u7e7c\u7e8c\u5f80\u4e0b\u770b\uff0c\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u908f\u8f2f\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u591a\u500b\u5e03\u6797\u503c\u7d50\u5408\u6210\u4e00\u500b\u5e03\u6797\u503c\u3002
has_license = True\nis_drunk = False\nage = 18\n\nprint(f\"Can I drive a car? {has_license and not is_drunk and age >= 18}\")\nprint(not has_license)\nprint(not is_drunk)\nprint(age >= 18 or is_drunk or not has_license)\n
ouputCan I drive a car? True\nFalse\nTrue\nTrue\n
\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c and
\u53ea\u6709\u5728\u6240\u6709\u5e03\u6797\u503c\u90fd\u662f True
\u7684\u6642\u5019\uff0c\u7d50\u679c\u624d\u6703\u662f True
\uff0c\u800c or
\u53ea\u8981\u6709\u4e00\u500b\u5e03\u6797\u503c\u662f True
\uff0c\u7d50\u679c\u5c31\u6703\u662f True
\u3002
\u56e0\u6b64\u6709\u6240\u8b02\u7684\u77ed\u8def\u6c42\u503c(Short-circuit Evaluation)\uff0c\u7576 and
\u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f False
\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a08\u7b97\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f False
\uff1b\u800c or
\u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f True
\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a55\u4f30\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f True
\u3002
\u800c not
\u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u53ea\u6703\u5c07\u5e03\u6797\u503c\u53cd\u8f49\u3002
Question
print(not True and False)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(not True or False)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(not True and not False)
\u6703\u5370\u51fa\u4ec0\u9ebc?print(not True or not False)
\u6703\u5370\u51fa\u4ec0\u9ebc?\u5728\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u4f4d\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u662f\u5c0d\u4e8c\u9032\u4f4d\u7684\u904b\u7b97\uff0c\u6211\u5011\u53ef\u4ee5\u7528 bin()
\u4f86\u89c0\u5bdf\u4e8c\u9032\u4f4d\u7684\u7d50\u679c\u3002
a, b = 2, 3\nprint(f\"a={bin(a)}\")\nprint(f\"b={bin(b)}\")\n\nprint(a & b, bin(a & b))\nprint(a | b, bin(a | b))\nprint(a ^ b, bin(a ^ b))\nprint(~a, bin(~a))\n\nprint(a << 1, bin(a << 1))\nprint(a >> 1, bin(a >> 1))\n
ouputa=0b10\nb=0b11\n2 0b10\n3 0b11\n1 0b1\n-3 -0b11\n4 0b100\n1 0b1\n
&
\u662f\u4f4d\u5143\u7684 and
\uff0c |
\u662f\u4f4d\u5143\u7684 or
\uff0c ^
\u662f\u4f4d\u5143\u9593\u7684\u9032\u884c\u4e92\u65a5\u6216\u904b\u7b97\uff0c ~
\u662f\u4f4d\u5143\u7684 not
\uff0c <<
\u662f\u4f4d\u5143\u7684\u5de6\u79fb\uff0c >>
\u662f\u4f4d\u5143\u7684\u53f3\u79fb\u3002
\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u60f3\u60f3\u6211\u5011\u8a72\u5982\u4f55\u5224\u65b7\u4e00\u500b\u6578\u5b57\u662f\u5947\u6578\u9084\u662f\u5076\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 %
\u4f86\u5224\u65b7\uff0c\u4f46\u662f\u6211\u5011\u4e5f\u53ef\u4ee5\u7528\u4f4d\u5143\u904b\u7b97\u4f86\u5224\u65b7\u3002
a, b = 5678, 4843\nprint(f\"Is a even? {a % 2 == 0}\")\nprint(f\"Is b odd? {b & 1 == 1}\")\n
ouputIs a even? True\nIs b odd? True\n
Question
2
\u7684\u51aa?*
\u4e58\u6cd5\u904b\u7b97\u5b50?\u6211\u5011\u5df2\u7d93\u5b78\u6703\u4e86\u4e00\u4e9b\u904b\u7b97\u5b50\uff0c\u73fe\u5728\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u4e00\u4e9b\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u904b\u7b97\u7d50\u679c\u6307\u5b9a\u7d66\u8b8a\u6578\u3002
\u5148\u4f86\u770b\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 +=
\u4f86\u5c07\u8b8a\u6578\u52a0\u4e0a\u67d0\u500b\u503c\uff0c\u9019\u500b\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002
x = 1\nx = x + 2\ny = 1\ny += 2\nprint(x, y)\n
ouput3 3\n
\u518d\u4f86\u770b\u770b\u5176\u4ed6\u7684\u6307\u6d3e\u904b\u7b97\u5b50\u3002
x = 1\nx += 2\nprint(x)\nx -= 1\nprint(x)\nx *= 3\nprint(x)\nx //= 2\nprint(x)\nx **= 2\nprint(x)\nx <<= 1\nprint(x)\n
ouput3\n2\n6\n3\n9\n18\n
\u5c0d\u4e86\uff0c\u8acb\u4f60\u5225\u5fd8\u8a18 =
\u4e5f\u662f\u4e00\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u3002
x = y = z = 1\nprint(x, y, z)\n
ouput1 1 1\n
\u5e0c\u671b\u9019\u500b\u4f8b\u5b50\u53ef\u4ee5\u8b93\u4f60\u66f4\u719f\u6089\u6307\u6d3e\u904b\u7b97\u5b50\u3002
Question
and=
\u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50?&=
\u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4ec0\u9ebc?\u70ba\u4e86\u80fd\u8b93\u4f60\u7df4\u7fd2\u4e00\u4e9b\u984c\u76ee\uff0c\u6211\u5148\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u5370\u51fa\u6d6e\u9ede\u6578\u5230\u6307\u5b9a\u4f4d\u6578\u3002
\u5728 Say Hello to Python - Input \u4e2d\uff0c\u6709\u7a0d\u5fae\u63d0\u904e\uff0c\u5982\u679c\u4f60\u5b8c\u5168\u6c92\u5370\u8c61\uff0c\u8acb\u4f60\u56de\u53bb\u8907\u7fd2\u4e00\u4e0b\u3002
\u7d66\u4f60\u6d6e\u9ede\u6578 x
\uff0c\u8acb\u4f60\u5370\u51fa x
\u7684\u5e73\u65b9\u6839\u8207\u5e73\u65b9\uff0c\u4e26\u4e14\u53ea\u5370\u51fa\u5c0f\u6578\u9ede\u5f8c\u5169\u4f4d\u3002
x = 3.1415926\nprint(f\"The square root of {x} is {x ** 0.5:.2f}\")\nprint(f\"The square of {x} is {x ** 2:.2f}\")\n
ouputThe square root of 3.1415926 is 1.77\nThe square of 3.1415926 is 9.87\n
@EditTime : 2024-01-27 11:52
"},{"location":"fundamental/python/operators/#bonus_map_for_input","title":"Bonus: map for input","text":"\u518d\u6b21\u8907\u7fd2 Variable and Input - Mutiple Input \u4e2d\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 split()
\u4f86\u5c07\u8f38\u5165\u7684\u5b57\u4e32\u5207\u5272\u6210\u591a\u500b\u5b57\u4e32\u3002
\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u5c07\u9019\u4e9b\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u600e\u9ebc\u505a\u5462?
a, b, c = input().split()\nprint(int(a) + int(b) + int(c))\n
input1 2 3\n
ouput6\n
\u96d6\u7136\u9019\u6a23\u5beb\u4e5f\u53ef\u4ee5\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u8f38\u5165\u5f88\u591a\u500b\u6578\u5b57\uff0c\u9019\u6a23\u5beb\u5c31\u6703\u5f88\u9ebb\u7169\uff0c\u9019\u6642\u5019\u6211\u5011\u53ef\u4ee5\u7528 map()
\u4f86\u5e6b\u52a9\u6211\u5011\u3002
a, b, c = map(int, input().split())\nprint(a + b + c)\n
input4 5 6\n
ouput15\n
map(function, iterable)
\u6703\u5c07 iterable
\u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u4e1f\u9032 function
\u4e2d\uff0c\u5728\u9019\u88e1\u7684 iterable
\u662f input().split()
\uff0c\u800c function
\u662f int
\uff0c\u56e0\u6b64 map(int, input().split())
\u6703\u5c07 input().split()
\u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u8f49\u63db\u6210\u6574\u6578\u3002
\u4f60\u53ef\u4ee5\u5617\u8a66\u5c07\u4f7f\u7528\u5225\u7684\u51fd\u5f0f\uff0c\u4f8b\u5982 float
\u6216 str
\uff0c\u4f46\u8acb\u4f60\u8a18\u5f97\u4e0d\u8981\u52a0\u4e0a\u62ec\u865f\uff0c\u56e0\u70ba\u6211\u5011\u53ea\u662f\u8981\u5c07\u51fd\u5f0f\u7684\u540d\u7a31\u50b3\u9032\u53bb\uff0c\u800c\u4e0d\u662f\u8981\u57f7\u884c\u51fd\u5f0f\u3002
\u6211\u5011\u4f7f\u7528 Unpacking \u7684\u65b9\u5f0f\u4f86\u5c07 map()
\u7684\u7d50\u679c\u6307\u6d3e\u7d66\u8b8a\u6578\u3002\u6211\u76f8\u4fe1\u4f60\u9084\u8a18\u5f97\u4ec0\u9ebc\u662f Unpacking \u5427?
\u5982\u679c\u4f60\u4e0d\u592a\u80fd\u7406\u89e3\uff0c\u4e5f\u6c92\u95dc\u4fc2\uff0c\u5148\u5b78\u6703\u600e\u9ebc\u7528\u5c31\u597d\u3002
"},{"location":"fundamental/python/operators/#practice","title":"Practice","text":"\u6709\u4e86\u672c\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u5176\u5be6\u5df2\u7d93\u53ef\u4ee5\u505a\u5f88\u591a\u984c\u76ee\u4e86\uff0c\u6211\u5011\u4f86\u505a\u4e00\u4e9b\u7df4\u7fd2\u984c\u5427!
Itsa - [C_MM01-\u6613] \u8a08\u7b97\u68af\u578b\u9762\u7a4d
Reference codea, b, h = map(int, input().split())\narea = (a + b) * h / 2\nprint(f\"Trapezoid area:{area}\")\n
Itsa - [C_MM02-\u6613] \u8a08\u7b97\u4e09\u89d2\u5f62\u9762\u7a4d
Reference codea, b = map(int, input().split())\narea = a * b / 2\nprint(area)\n
Itsa - [C_MM04-\u6613] \u8a08\u7b97\u7e3d\u548c\u3001\u4e58\u7a4d\u3001\u5dee\u3001\u5546\u548c\u9918\u6578
Reference codea, b = map(int, input().split())\n\nprint(f\"{a}+{b}={a + b}\")\nprint(f\"{a}*{b}={a * b}\")\nprint(f\"{a}-{b}={a - b}\")\nprint(f\"{a}/{b}={a // b}...{a % b}\")\n
Itsa - [C_MM06-\u6613] \u82f1\u54e9\u8f49\u516c\u91cc
Reference codemile = int(input())\nkm = mile * 1.6\nprint(f\"{km:.1f}\")\n
"},{"location":"fundamental/python/operators/#assignment","title":"Assignment","text":"Itsa - [C_MM07-\u6613] \u8a08\u7b97\u5e73\u65b9\u503c\u8207\u7acb\u65b9\u503c
Itsa - [C_MM08-\u6613] \u8a08\u7b97\u5169\u6578\u548c\u7684\u5e73\u65b9\u503c
Itsa - [C_MM10-\u6613] \u651d\u6c0f\u6eab\u5ea6\u8f49\u83ef\u5f0f\u6eab\u5ea6
Itsa - [C_MM11-\u6613] \u8cfc\u7968\u8a08\u7b97
Itsa - [C_MM12-\u6613] \u76f8\u9047\u6642\u9593\u8a08\u7b97
Itsa - [C_MM14-\u6613] \u8a08\u7b97\u6642\u9593\u7684\u7d44\u5408
@EditTime : 2024-01-28 22:03
"},{"location":"fundamental/python/repetition_structures/","title":"Repetition Structures","text":""},{"location":"fundamental/python/repetition_structures/#introduction","title":"Introduction","text":"\u96fb\u8166\u6700\u6703\u505a\u7684\u4e8b\uff0c\u5c31\u662f\u91cd\u8907\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u9019\u88e1\u8981\u8ddf\u4f60\u4ecb\u7d39\u8ff4\u5708(Loop)\uff0c\u8ff4\u5708\u53ef\u4ee5\u8b93\u6211\u5011\u91cd\u8907\u57f7\u884c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u800c\u4e0d\u7528\u4e00\u884c\u4e00\u884c\u7684\u5beb\u51fa\u4f86\u3002
\u5982\u679c\u4f60\u8981\u5370 \\(100\\) \u6b21 Hello World!
\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?
# ...\nprint(\"Hello World!\")\nprint(\"Hello World!\")\nprint(\"Hello World!\")\n
\u4f60\u6703\u771f\u7684\u5beb \\(100\\) \u884c\u55ce?\u7576\u7136\u4e0d\u6703\uff0c\u4e0d\u7136\u4f60\u6703\u6c23\u6b7b\uff0c\u7136\u5f8c\u628a\u96fb\u8166\u7838\u4e86\u3002
"},{"location":"fundamental/python/repetition_structures/#for_loop","title":"For Loop","text":"\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b for
\u8ff4\u5708\u7684\u7528\u6cd5\u3002\u4e26\u8209\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5370\u51fa \\(1\\) \u5230 \\(5\\):
for i in range(1, 6, 1):\n print(i, end=' ')\n
ouput1 2 3 4 5\n
Time Loop - Purrple Cat
range
\uff0c\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6578\u5217\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u503c\u3001\u7d50\u675f\u503c\u3001\u9593\u9694\u503c\u3002\u9019\u500b\u51fd\u5f0f\u7684\u7528\u6cd5\u5982\u4e0b:
\u5982\u679c\u53ea\u6709\u4e00\u500b\u53c3\u6578\uff0c\u90a3\u5c31\u662f\u7d50\u675f\u503c\uff0c\u8d77\u59cb\u503c\u9810\u8a2d\u70ba \\(0\\)\uff0c\u9593\u9694\u503c\u9810\u8a2d\u70ba \\(1\\)\u3002
range(start=0, stop, step=1)
\u4e26\u4e14\u6ce8\u610f\uff0cstop
\u662f\u4e0d\u5305\u542b\u5728\u6578\u5217\u4e2d\u7684\u3002
\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u60f3\u628a\u5b57\u4e32 \"Speed_of_Light\"
\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u90fd\u5370\u51fa\u4f86\u3002
for c in \"Speed_of_Light\":\n print(c, end=' ')\n
ouput
S p e e d _ o f _ L i g h t \n
DJ OKAWARI feat. \u4e8c\u5bae\u611b \u300cSpeed of Light\u300d \u9019\u88e1\u7684\u8ff4\u5708\u66f4\u63a5\u8fd1\u6982\u5ff5\u4e0a\u7684 Foreach\uff0c\u6bcf\u6b21\u8ff4\u5708\u90fd\u6703\u53d6\u51fa\u5b57\u4e32\u4e2d\u7684\u4e00\u500b\u5b57\u5143\uff0c\u4e26\u4e14\u5c07\u4ed6\u653e\u5230 c
\u4e2d\uff0c\u9019\u500b\u6982\u5ff5\u8acb\u4f60\u8a18\u8d77\u4f86\u3002
\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(\\sum_{i=1}^n{i}=1+2+\\cdots+n\\)
n = int(input())\ntotal = 0\n\n# 1 + 2 + ... + n\nfor i in range(n + 1):\n total += i\n\nprint(total)\n
input100\n
ouput5050\n
\u63a5\u4e0b\u4f86\uff0c\u8ddf\u4f60\u4ecb\u7d39 break
\u8207 continue
\uff0c\u9019\u5169\u500b\u95dc\u9375\u5b57\u53ef\u4ee5\u7528\u4f86\u63a7\u5236\u8ff4\u5708\u7684\u57f7\u884c\u3002
break
\u53ef\u4ee5\u7528\u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u800c continue
\u5247\u662f\u5f37\u5236\u8df3\u5230\u4e0b\u4e00\u6b21\u8ff4\u5708\u3002
for i in range(1, 11):\n if i == 5:\n break\n print(i, end=' ')\n
ouput1 2 3 4\n
\u53ef\u4ee5\u767c\u73fe\uff0ci == 5
\u7684\u6642\u5019\uff0c\u8ff4\u5708\u5c31\u88ab\u5f37\u5236\u4e2d\u65b7\u4e86\u3002
for i in range(1, 11):\n if i == 5:\n continue\n print(i, end=' ')\n
ouput1 2 3 4 6 7 8 9 10\n
\u53ef\u4ee5\u767c\u73fe\uff0c5
\u88ab\u8df3\u904e\u4e86\u3002
\u63a5\u8457\u4ecb\u7d39\u5de2\u72c0\u8ff4\u5708(Nested Loop)\uff0c\u4e5f\u5c31\u662f\u8ff4\u5708\u88e1\u9762\u9084\u6709\u8ff4\u5708\u3002\u505a\u500b\u6bd4\u55bb\u7684\u8a71\uff0c\u5982\u679c\u662f\u5169\u5c64\u7684\u8ff4\u5708\uff0c\u5c31\u50cf\u662f\u6642\u91dd\u8207\u5206\u91dd\uff0c\u5167\u5c64\u8ff4\u5708\u6bcf\u8dd1\u4e00\u5708\uff0c\u5916\u5c64\u8ff4\u5708\u624d\u8dd1\u4e00\u683c\u3002
\u6211\u5011\u4f86\u5370\u4e00\u500b\u76f4\u89d2\u4e09\u89d2\u5f62\u5427\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u5370\u51fa \\(n\\) \u5c64\u7684\u76f4\u89d2\u4e09\u89d2\u5f62\uff0c\u5c07\u6700\u9802\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(0\\)\uff0c\u6700\u5e95\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(n-1\\)\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u5c64\u6709 \\(i+1\\) \u500b\u661f\u661f *
\u3002
n = int(input())\n\nfor i in range(n):\n for j in range(i + 1):\n print('*', end='')\n print()\n
\u6211\u7a31\u5167\u5c64\u7684\u8ff4\u5708\u53eb\u505a j
\u8ff4\u5708\uff0c\u5916\u5c64\u7684\u8ff4\u5708\u53eb\u505a i
\u8ff4\u5708\u3002 j
\u8ff4\u5708\u63a7\u5236\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\uff0c\u800c i
\u8ff4\u5708\u5247\u63a7\u5236\u7e3d\u5171\u6709\u5e7e\u5c64\u3002\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\u90fd\u662f i + 1
\uff0c\u6700\u5f8c\u6703\u63db\u884c\u3002
5\n
output*\n**\n***\n****\n*****\n
\u518d\u4f86\u770b\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u4e5d\u4e5d\u4e58\u6cd5\u8868\u3002
for i in range(1, 10):\n for j in range(1, 10):\n print(f\"{i} * {j} = {i * j}\", end='\\t')\n print()\n
output1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 \n2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 \n3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 \n4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 \n5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 \n6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 \n7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 \n8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 \n9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 \n
i
\u63a7\u5236\u5217(Row)\uff0cj
\u63a7\u5236\u884c(Column)\uff0c\u6bcf\u4e00\u5217\u7684\u6578\u5b57\u90fd\u662f i
\uff0c\u6bcf\u4e00\u884c\u7684\u6578\u5b57\u90fd\u662f j
\uff0c\u6240\u4ee5 i * j
\u5c31\u662f\u76f8\u61c9\u7684\u4e58\u7a4d\u3002
\u71b1\u8eab\u5b8c\u4e86\uff0c\u4f86\u770b\u4e00\u500b\u7a0d\u5fae\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57\uff0c\u5224\u65b7\u4ed6\u662f\u4e0d\u662f\u8cea\u6578\u3002
\u9019\u500b\u4f8b\u5b50\u6703\u662f\u7b49\u7b49\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\u7684\u57fa\u790e\uff0c\u6240\u4ee5\u8acb\u4ed4\u7d30\u770b\u3002
n = int(input())\nis_prime = True\n\nfor i in range(2, n):\n if n % i == 0:\n is_prime = False\n break\n\nif is_prime:\n print(\"Yes\")\nelse:\n print(\"No\")\n
input17\n4843\n
ouputYes\nNo\n
n
\u5982\u679c\u53ef\u4ee5\u6574\u9664 1
\u8207\u81ea\u5df1\u4ee5\u5916\u7684\u6578\u5b57\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 break
\u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8a08\u7b97\u3002
\u6211\u5011\u628a\u554f\u984c\u7528\u5f97\u66f4\u8907\u96dc\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u8f38\u51fa\u5728 \\([1, n]\\) \u4e4b\u9593\u6700\u5927\u7684\u8cea\u6578\u3002
\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u8b93 range
\u5012\u8457\u6578\u56de\u4f86\u3002
n = int(input())\n\nfor i in range(n, 0, -1):\n is_prime = True\n for j in range(2, i):\n if i % j == 0:\n is_prime = False\n break\n\n if is_prime:\n print(f\"{i} is the largest prime in [1, {n}]\")\n break\n
input100\n2\n
output97 is the largest prime in [1, 97]\n2 is the largest prime in [1, 2]\n
i
\u5217\u8209 \\([n, 1]\\) \u7684\u6578\u5b57\uff0c\u800c j
\u5217\u8209 \\([2, i-1]\\) \u7684\u6578\u5b57\uff0c\u5982\u679c i
\u53ef\u4ee5\u6574\u9664 j
\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u99ac\u4e0a\u8df3\u51fa\u5167\u5c64 j
\u8ff4\u5708\uff0c\u7e7c\u7e8c\u5217\u8209\u4e0b\u4e00\u500b\u6578\u5b57\u3002\u5982\u679c i
\u662f\u8cea\u6578\uff0c\u90a3\u5c31\u5370\u51fa\u4f86\uff0c\u4e26\u4e14\u8df3\u51fa\u5916\u5c64 i
\u8ff4\u5708\u3002
\u8acb\u4f60\u4ed4\u7d30\u7aef\u8a73\u9019\u5169\u500b\u4f8b\u5b50\u4e2d\u5f37\u8abf\u7684\u884c\u6578\u3002
@EditTime : 2024-01-30 16:33
"},{"location":"fundamental/python/repetition_structures/#while_loop","title":"While loop","text":"\u63a5\u8457\u4f86\u4ecb\u7d39 while
\u8ff4\u5708\uff0c\u4ed6\u7684\u4f7f\u7528\u5834\u666f\u662f\u7576\u4f60\u4e0d\u77e5\u9053\u8ff4\u5708\u8981\u57f7\u884c\u5e7e\u6b21\u7684\u6642\u5019\uff0c\u5c31\u53ef\u4ee5\u7528 while
\u8ff4\u5708\uff0c\u4f46\u662f\u5225\u5beb\u51fa\u7121\u7aae\u8ff4\u5708(Infinite Loop)\u5594\u3002
\u4f46\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u5148\u4f86\u770b\u5982\u4f55\u7528 while
\u8ff4\u5708\u4f86\u5370\u51fa \\(1\\) \u5230 \\(5\\)\u3002
i = 1\nwhile i < 6:\n print(i, end=\" \")\n i += 1\n
ouput1 2 3 4 5\n
\u56de\u9867\u4e00\u4e0b for loop\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe\u908f\u8f2f\u5176\u5be6\u4e00\u6a23\u3002
\u7576\u689d\u4ef6\u6210\u7acb\u7684\u6642\u5019\uff0c\u5c31\u6703\u57f7\u884c\u8ff4\u5708\uff0c\u76f4\u5230\u689d\u4ef6\u4e0d\u6210\u7acb\u3002
\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\(n!\\)
n = int(input())\ni = 1\nfact = 1\n\nwhile i <= n:\n fact *= i\n i += 1\n\nprint(fact)\n
input5\n1\n
ouput120\n1\n
\u4f46\u76ee\u524d\u9019\u5169\u500b\u4f8b\u5b50\u7121\u6cd5\u770b\u51fa while
\u7684\u9b45\u529b\uff0c\u56e0\u70ba\u4f60\u90fd\u77e5\u9053\u8ff4\u5708\u4ec0\u9ebc\u6642\u5019\u6703\u7d50\u675f\uff0c\u6240\u4ee5\u8ddf\u4f60\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u554f\u984c:
\u8003\u62c9\u8332\u731c\u60f3 wikipedia
\u662f\u6307\u5c0d\u65bc\u6bcf\u4e00\u500b\u6b63\u6574\u6578\uff0c\u5982\u679c\u5b83\u662f\u5947\u6578\uff0c\u5247\u5c0d\u5b83\u4e583\u518d\u52a01\uff0c\u5982\u679c\u5b83\u662f\u5076\u6578\uff0c\u5247\u5c0d\u5b83\u9664\u4ee52\uff0c\u5982\u6b64\u5faa\u74b0\uff0c\u6700\u7d42\u90fd\u80fd\u5920\u5f97\u52301\u3002
\u90a3\u6211\u5011\u8981\u5beb\u7684\u7a0b\u5f0f\u662f\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa\u4ed6\u6703\u5728\u5e7e\u6b65\u5f8c\u8b8a\u6210 \\(1\\)\u3002
\\[ f(n)=\\begin{cases}\\frac{n}{2}, & \\text{if } n \\text{ is even} \\\\ 3n+1, & \\text{if } n \\text{ is odd}\\end{cases} \\]\u4f9d\u7167\u5b9a\u7fa9\uff0c\u6211\u5011\u53ef\u4ee5\u5beb\u51fa\u4ee5\u4e0b\u7a0b\u5f0f\u78bc:
n = int(input())\nstep = 0\n\nwhile n != 1:\n print(n, end=\" -> \")\n if n % 2 == 0:\n n = n // 2\n else:\n n = 3 * n + 1\n step += 1\n\nprint(1)\nprint(f\"step = {step}\")\n
input22\n1\n
ouput22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1\nstep = 15\n1\nstep = 0\n
\u518d\u4f86\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\gt 1\\)\uff0c\u8f38\u51fa \\(n\\) \u7684\u8cea\u56e0\u6578\u5206\u89e3\u3002
\u8acb\u4f60\u5148\u81ea\u5df1\u60f3\u4e00\u4e0b\uff0c\u518d\u770b\u770b\u53c3\u8003\u7a0b\u5f0f\u78bc\u3002
Examplen = int(input())\nfactor = 2\n\nwhile n != 1:\n while n % factor == 0:\n print(factor, end=' ')\n n //= factor\n factor += 1\n
input20\n4843\n
ouput2 2 5\n29 167\n
\u5167\u5c64\u7684 while
\u8ff4\u5708\u6703\u4e00\u76f4\u57f7\u884c\uff0c\u76f4\u5230 n
\u4e0d\u662f factor
\u7684\u500d\u6578\u70ba\u6b62\u3002\u5916\u5c64\u7684 while
\u8ff4\u5708\u5247\u662f\u5217\u8209\u6240\u6709\u7684\u8cea\u56e0\u6578\uff0c\u76f4\u5230 n
\u8b8a\u6210 \\(1\\) \u70ba\u6b62\u3002
\u984c\u5916\u8a71\uff0c\u7576\u521d\u9047\u5230\u9019\u500b\u984c\u76ee\u7684\u6642\u5019\uff0c\u60f3\u4e86\u4e00\u4e0b\u7d50\u679c\u4e00\u6b21\u5c31AC\uff0c\u662f\u5f88\u6709\u6210\u5c31\u611f\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5\u4e5f\u8acb\u4f60\u597d\u597d\u52aa\u529b\uff0c\u9010\u6b65\u57f9\u990a\u81ea\u4fe1\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7684\u3002
@EditTime : 2024-01-30 17:55
"},{"location":"fundamental/python/repetition_structures/#bonus_for_else_and_while_else","title":"Bonus: for ... else and while ... else","text":"for
\u8207 while
\u8ff4\u5708\u90fd\u53ef\u4ee5\u642d\u914d else
\u4f86\u4f7f\u7528\uff0c\u7576\u8ff4\u5708\u6b63\u5e38\u7d50\u675f\u7684\u6642\u5019(\u6c92\u6709 break
)\uff0c\u5c31\u6703\u57f7\u884c else
\u3002
\n
"},{"location":"fundamental/python/repetition_structures/#practice","title":"Practice","text":"Itsa - [C_MM03-\u6613] \u5169\u6578\u7e3d\u548c
Reference codewhile True:\n a, b = map(int, input().split())\n print(a + b)\n
\u9019\u984c\u6703\u51fa\u73fe\u5728\u9019\u88e1\uff0c\u55ae\u7d14\u662f\u56e0\u70ba\u8981\u91cd\u8907\u8f38\u5165\u3002 Itsa - [C_MM26-\u6613] \u8f38\u51fa 1x1\u30012x2\u3001...\u3001NxN\u4e4b\u7d50\u679c
Reference codeN = int(input())\n\nfor i in range(1, N + 1):\n print(f\"{i}*{i}={i * i}\")\n
Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578
Reference codeN = int(input())\n\nfor i in range(6, N + 1, 2):\n s = 0\n for j in range(1, i):\n if i % j == 0:\n s += j\n if s == i:\n if i == 6:\n print(i, end='')\n else:\n print(' ' + str(i), end='')\nprint()\n
\u9019\u662f\u66b4\u529b\u89e3\uff0c\u6703TLE\uff0c\u4e0b\u4e00\u7ae0\u6703\u4ecb\u7d39\u5982\u4f55\u6700\u4f73\u5316\uff0c \u4f46\u5982\u679c\u4f7f\u7528 C++ \u7684\u8a71\uff0c\u76f8\u540c\u908f\u8f2f\u7684\u7a0b\u5f0f\u78bc\u662f\u53ef\u4ee5AC\u7684\u3002 Itsa - [C_MM34-\u6613] \u56e0\u6578\u554f\u984c
Reference codeN = int(input())\n\nprint(1, end=\"\")\nfor i in range(2, N + 1):\n if N % i == 0:\n print(f\" {i}\", end=\"\")\nprint()\n
@EditTime : 2024-01-30 18:40
"},{"location":"fundamental/python/repetition_structures/#assignment","title":"Assignment","text":"Itsa - [C_MM21-\u6613] \u7b97\u968e\u4e58
Itsa - [C_MM25-\u6613] \u8a08\u7b97\u6b63\u6574\u6578\u88ab3\u6574\u9664\u4e4b\u6578\u503c\u4e4b\u7e3d\u548c
Itsa - [C_MM27-\u6613] \u8a08\u7b97\u5169\u6574\u6578\u9593\u6240\u6709\u6574\u6578\u7684\u7e3d\u548c
Itsa - [C_MM28-\u6613] \u8a08\u7b971\u5230N\u4e4b\u9593\u5c6c\u65bc5\u548c7\u7684\u500d\u6578
Itsa - [C_MM29-\u6613] \u6700\u5927\u8cea\u6578\u554f\u984c
Itsa - [C_MM30-\u6613] \u8cea\u6578\u5224\u5225
Itsa - [C_MM31-\u6613] \u8a08\u7b971~N\u5167\u80fd\u88ab2\u8ddf3\u6574\u9664\uff0c\u4f46\u4e0d\u80fd\u88ab12\u6574\u9664\u7684\u6574\u6578\u7e3d\u548c
Itsa - [C_MM40-\u6613] 1~N\u4e4b\u9593\u7684\u7e3d\u548c
Itsa - [C_MM49-\u6613] \u9023\u7e8c1\u7684\u500d\u6578
Itsa - [C_ST09-\u6613] \u661f\u865f\u77e9\u5f62\u8f38\u51fa
Itsa - [C_ST11-\u6613] \u661f\u865f\u83f1\u5f62\u8f38\u51fa
Itsa - [C_ST14-\u6613] \u6578\u5b57\u76f4\u89d2\u4e09\u89d2\u5f62\u8f38\u51fa
@EditTime : 2024-01-30 21:51
"},{"location":"fundamental/python/say_hello/","title":"Say Hello to Python!","text":""},{"location":"fundamental/python/say_hello/#first_program","title":"First Program","text":"\u55e8\uff0c\u9019\u662f\u4f60\u7b2c\u4e00\u500b Python \u7a0b\u5f0f\uff0c\u5728 Pycharm \u4e2d\u5efa\u7acb\u4e00\u500b main.py
\u6a94\u6848\uff0c\u4e26\u5728\u6a94\u6848\u4e2d\u6253\u4e0a :
# print(\"Hello Cat!\")\nprint(\"Hello World!\")\nprint('Hello Python!')\nprint(\"cheung4843\")\n
\u6309\u4e0b\u57f7\u884c\u5f8c\uff0c\u4f60\u7684\u63a7\u5236\u53f0(Console)\u5c07\u6703\u5370\u51fa Hello World!
\u63a5\u8457\u63db\u884c\uff0c\u518d\u5370\u51fa Hello Python!
\uff0c\u63a5\u8457\u518d\u5370\u51fa\u6211 cheung4843
\uff0c\u518d\u63db\u884c\u3002
\u90a3 # print(\"Hello Cat!\")
\u662f\u4ec0\u9ebc?\u4ee5\u4e95\u5b57\u865f\u70ba\u958b\u982d\u7684\uff0c\u88ab\u7a31\u70ba\u8a3b\u89e3\uff0c\u4ed6\u4e0d\u6703\u5728\u7a0b\u5f0f\u4e2d\u88ab\u57f7\u884c\uff0c\u4ed6\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7406\u89e3\u7a0b\u5f0f\u78bc\uff0c\u6216\u8005\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002
\u4e0d\u66c9\u5f97\u4f60\u662f\u5426\u6709\u767c\u73fe \"Hello World!\"
\u8207 'Hello Python!'
\u7684\u5dee\u7570?\u7576\u7136\u9664\u4e86\u5b57\u6bcd\u4e0d\u4e00\u6a23\u4e4b\u5916\uff0c\u9084\u6709\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u7684\u5dee\u5225\uff0c\u4f46\u4ed6\u5011\u90fd\u88ab\u7a31\u70ba\u5b57\u4e32(String)\uff0c\u5728 Python \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6210\u5c0d\u7684\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u4f86\u8868\u9054\u4e00\u500b\u5b57\u4e32\uff0c\u4f46\u662f\u4e0d\u80fd\u6df7\u7528\uff0c\u4f8b\u5982 :
print(\"I love cats')\n
\u57f7\u884c\u5f8c\uff0c\u4f60\u6703\u767c\u73fe\u63a7\u5236\u53f0\u8ddf\u4f60\u5831\u544a\u4e86\u932f\u8aa4\uff0c\u9019\u662f\u8a9e\u6cd5\u932f\u8aa4\uff0c\u800c\u5f8c\u7e8c\u5beb\u7a0b\u5f0f\u7684\u904e\u7a0b\u4e2d\uff0c\u4f60\u5c07\u906d\u9047\u8a31\u591a\u8a9e\u610f\u932f\u8aa4\u3002
\u90a3\u4f60\u77e5\u9053\u4ec0\u9ebc\u662f\u7a7a\u5b57\u4e32\u55ce?
print('')\nprint(\"\")\nprint(\"Above are nothing, right?\")\n
\u57f7\u884c\u5b8c\u5f8c\uff0c\u4f60\u5c31\u77e5\u9053\u7a7a\u5b57\u4e32\u662f\u4ec0\u9ebc\u4e86\u5427?
\u63a5\u8457\u6211\u5011\u4f86\u8b1b\u8b1b print
\u9019\u500b\u6771\u897f\uff0c\u6211\u5011\u5728 Python \u4e2d\u7a31\u4ed6\u70ba\u51fd\u5f0f(Function)\uff0c\u6211\u5011\u8981\u5982\u4f55\u4f7f\u7528\u5462?\u4e5f\u5c31\u662f\u5728\u51fd\u5f0f\u7684\u540d\u7a31\u5f8c\u52a0\u4e0a\u4e00\u500b\u62ec\u865f()
\u3002\u800c print
\u662f Python \u5167\u5efa\u7d66\u6211\u5011\u7684\u300c\u5de5\u5177\u300d\uff0c\u5c31\u662f\u8aaa\uff0c\u6211\u5011\u4e0d\u77e5\u9053\u4ed6\u662f\u600e\u9ebc\u88ab\u9020\u51fa\u4f86\u7684\uff0c\u6211\u5011\u73fe\u968e\u6bb5\u53ea\u8981\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u6216\u8a31\u4f60\u53ef\u80fd\u807d\u904e\u4e00\u500b\u8001\u6389\u7259\u7684\u6bd4\u55bb\uff0c\u7a31\u51fd\u5f0f\u5c31\u50cf\u662f\u4e00\u500b\u9ed1\u76d2\u5b50\u3002
\u95dc\u65bc\u51fd\u5f0f(Function)\uff0c\u6211\u6703\u5728\u5f80\u5f8c\u7684\u7ae0\u7bc0\u8ddf\u4f60\u4ecb\u7d39\uff0c\u800c\u73fe\u5728\u4f60\u53ea\u8981\u77e5\u9053 print()
\u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u5370\u51fa\u4f86\uff0c\u4e26\u63db\u884c\u3002
\u90a3\u5982\u679c\u6211\u4e0d\u60f3\u63db\u884c\u5462?
print(\"You\")\nprint(\"are\")\nprint(\"my\", end=' ')\nprint(\"special\", end='\\n')\nprint(\"1234\", end=\"5\")\nprint(\"---------\")\n
ouputYou\nare\nmy special\n12345---------\n
\u5728\u9019\u500b\u5947\u602a\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u4f60\u767c\u73fe\u6211\u5728 print
\u7684\u62ec\u865f\u4e2d\u591a\u52a0\u4e86\u4e00\u500b end=
\u7684\u6771\u897f\uff0c\u8b93 print
\u5370\u5b8c\u524d\u9762\u7684\u6771\u897f\u5f8c\uff0c\u518d\u5370\u51fa\u7b49\u65bc\u5f8c\u7684\u6771\u897f\u3002
You are my special
\u90a3 \\n
\u662f\u4ec0\u9ebc\u6771\u897f?\u4ed6\u662f\u8df3\u812b\u5b57\u5143(Escape Character)\u5bb6\u65cf\u4e2d\u7684\u4e00\u54e1\uff0c\u4f60\u73fe\u5728\u53ea\u8981\u77e5\u9053\u4ed6\u80fd\u5920\u63db\u884c\u3002
Note
\"cheung4843\"
\u8207 '114514 + 1919810'
\uff0c\u4f46\u8a18\u4f4f\u55ae\u96d9\u5f15\u865f\u4e0d\u5f97\u6df7\u7528\u3002print(x, end=y)
\u6703\u5370\u51fa x
\uff0c\u518d\u5370\u51fa y
\uff0c\u800c y
\u9810\u8a2d\u70ba \\n
\u4e5f\u5c31\u662f\u63db\u884c\u3002@EditTime : 2024-01-25 19:23
Question
\"'\"
\u8207 \"\"\"
\u662f\u5408\u6cd5\u7684\u5b57\u4e32\u55ce?print()
\u62ec\u865f\u5167\u6c92\u6709\u653e\u6771\u897f\u6703\u5370\u51fa\u4ec0\u9ebc?@EditTime : 2024-01-25 19:55
"},{"location":"fundamental/python/selection_structures/","title":"Selection Structures","text":""},{"location":"fundamental/python/selection_structures/#introduction","title":"Introduction","text":"\u5728\u6211\u5011\u4eba\u751f\u4e2d\uff0c\u6211\u5011\u6703\u9762\u81e8\u5f88\u591a\u9078\u64c7\uff0c\u6703\u6839\u64da\u7576\u4e0b\u7684\u60c5\u6cc1\uff0c\u505a\u51fa\u4e0d\u540c\u7684\u6c7a\u5b9a\uff0c\u800c\u7a0b\u5f0f\u4e5f\u662f\u4e00\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u60c5\u6cc1\uff0c\u57f7\u884c\u4e0d\u540c\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u7a31\u70ba\u9078\u64c7\u7d50\u69cb(Selection Structures)\uff0c\u662f\u6d41\u7a0b\u63a7\u5236\u7684\u4e00\u7a2e\u3002
\u4f8b\u5982\uff0c\u6211\u6839\u64da\u5b78\u6e2c\u6210\u7e3e\uff0c\u4f86\u6c7a\u5b9a\u8981\u586b\u54ea\u4e9b\u5fd7\u9858\uff0c\u6216\u662f\u6211\u6839\u64da\u5929\u6c23\uff0c\u4f86\u6c7a\u5b9a\u8981\u4e0d\u8981\u5e36\u5098\u3002
"},{"location":"fundamental/python/selection_structures/#if_elif_else","title":"if ... elif ... else","text":"\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u56de\u60f3 Operators - Comparison Operators \u7684\u5167\u5bb9\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u6bd4\u8f03\u904b\u7b97\u5b50\u4f86\u5f97\u5230\u4e00\u500b\u5e03\u6797\u503c\uff0c\u800c if
\u6703\u6839\u64da True
\u6216 False
\u4f86\u6c7a\u5b9a\u662f\u5426\u57f7\u884c\u67d0\u6bb5\u7a0b\u5f0f\u78bc\u3002
\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50:
\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u8f38\u51fa\u4ed6\u7684\u7d55\u5c0d\u503c\u3002
num = int(input(\"Enter a number: \"))\n\n# get the absolute value of the input\nif num < 0:\n num = -num\n\nprint(num)\n
input-1984\n1984\n
ouput1984\n1984\n
Ivan Torrent - \"1984\" Lyrics Video
\u908f\u8f2f\u5f88\u7c21\u55ae\uff0c\u5982\u679c\u8f38\u5165\u7684\u6578\u5b57\u5c0f\u65bc\u96f6\uff0c\u90a3\u9ebc\u5c31\u5c07\u4ed6\u653e\u4e0a\u8ca0\u865f\u3002\u63d0\u9192\u4f60\u4e00\u4e0b\uff0c\u9019\u88e1\u7684 -
\u8207 not
\u4e00\u6a23\uff0c\u90fd\u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c
\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u53ea\u6709\u7528\u5230 if
\uff0c\u63a5\u8457\u6211\u5011\u6709\u8acb else
\u767b\u5834\u3002
\u8acb\u770b\u4e0b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u5224\u65b7\u4ed6\u662f\u5947\u6578\u9084\u662f\u5076\u6578\u3002
num = int(input(\"Enter a number: \"))\n\n# odd or even\nif num % 2 == 0:\n print(\"Even\")\nelse:\n print(\"Odd\")\n
input1983\n-1982\n
ouputOdd\nEven\n
Timecop1983 - On the Run
\u9019\u88e1\u6211\u5011\u7528\u5230\u4e86 else
\uff0c\u7576 if
\u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c else
\u7684\u5167\u5bb9\u3002
\u6211\u5011\u518d\u4f86\u770b\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4f60\u7684\u5206\u6578\uff0c\u8f38\u51fa\u4f60\u7684\u8a55\u50f9\uff0c\u56e0\u70ba\u6709\u5f88\u591a\u7a2e\u8a55\u50f9\uff0c\u6240\u4ee5\u9700\u8981\u7528\u5230 elif
\u3002
\u7576 if
\u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u9762 elif
\u7684\u689d\u4ef6\uff0c\u5982\u679c elif
\u7684\u689d\u4ef6\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c elif
\u7684\u5167\u5bb9\uff0c\u5982\u679c\u76ee\u524d\u7684 elif
\u7684\u689d\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u4e00\u500b elif
\u7684\u689d\u4ef6\uff0c\u5982\u679c\u6240\u6709\u7684 elif
\u7684\u689d\u4ef6\u90fd\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c else
\u7684\u5167\u5bb9\u3002
\u9019\u6a23\u8b1b\u6216\u8a31\u6709\u9ede\u7e5e\u53e3\uff0c\u4f60\u53ef\u4ee5\u770b\u770b\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u61c9\u8a72\u5c31\u80fd\u7406\u89e3\u4e86\u3002
score = int(input(\"Enter your score: \"))\n\nif score >= 90:\n print(\"A\")\n print(\"Excellent!\")\nelif score >= 80:\n print(\"B\")\nelif score >= 70:\n print(\"C\")\nelif score >= 60:\n print(\"D\")\nelse:\n print(\"F\")\n
input60\n90\n49\n
ouputD\nA\nExcellent!\nF\n
\u63a5\u4e0b\u4f86\u7d66\u4f60\u4e00\u500b\u53ef\u80fd\u6703\u7591\u60d1\u7684\u4f8b\u5b50:
score = int(input(\"Enter your score: \"))\nif score >= 60:\n print(\"D\")\nelif score >= 70:\n print(\"C\")\nelif score >= 80:\n print(\"B\")\nelif score >= 90:\n print(\"A\")\n print(\"Excellent!\")\nelse:\n print(\"F\")\n
input90\n
ouputD\n
\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u5947\u602a\uff0c\u70ba\u4ec0\u9ebc\u8f38\u5165 90
\u6703\u5370\u51fa D
\uff0c\u800c\u4e0d\u662f A
\uff0c\u9019\u662f\u56e0\u70ba if
\u7684\u689d\u4ef6\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c if
\u7684\u5167\u5bb9\uff0c\u5c31\u4e0d\u6703\u6aa2\u67e5 elif
\u7684\u689d\u4ef6\u4e86\u3002
\u6240\u4ee5\uff0c\u4f60\u5728\u64b0\u5beb\u689d\u4ef6\u5f0f\u7684\u6642\u5019\uff0c\u8981\u6ce8\u610f\u9806\u5e8f\uff0c\u4ee5\u53ca\u78ba\u4fdd\u6bcf\u500b\u689d\u4ef6\u662f\u5426\u662f\u4e92\u65a5\u7684\u3002
score = int(input(\"Enter your score: \"))\n\nif 70 > score >= 60:\n print(\"D\")\nelif 80 > score >= 70:\n print(\"C\")\nelif 90 > score >= 80:\n print(\"B\")\nelif 100 >= score >= 90:\n print(\"A\")\n print(\"Excellent!\")\nelse:\n print(\"F\")\n
input90\n4843\n55\n
ouputA\nExcellent!\nF\nF\n
\u9019\u6a23\u5c31\u4e0d\u6703\u6709\u554f\u984c\u4e86\uff0c\u4f46\u662f\uff0c\u7576\u6211\u8f38\u5165\u8d85\u904e 100
\u7684\u5206\u6578\u6642\uff0c\u662f\u6703\u5370\u51fa F
\u7684\uff0c\u4f60\u8a72\u600e\u9ebc\u89e3\u6c7a\u5462?
if
\u7684\u5167\u5bb9\u53ef\u4ee5\u662f\u53e6\u4e00\u500b if
\uff0c\u9019\u7a2e\u7d50\u69cb\u7a31\u70ba\u5de2\u72c0\u689d\u4ef6\u5f0f(Nested if)\u3002
\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6709\u4e09\u500b\u6574\u6578 a, b, c
\uff0c\u8acb\u4f60\u8f38\u51fa\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002
a, b, c = map(int, input().split())\nmax_one = None\n\nif a > b:\n max_one = a\n if c > max_one:\n max_one = c\nelse:\n max_one = b\n if c > max_one:\n max_one = c\n\nprint(max_one)\n
input10 20 30\n12 12 12\n-3 -4 -5\n
ouput30\n12\n-3\n
\u60f3\u6cd5\u5f88\u7c21\u55ae\uff0c\u5148\u8b93 a, b
\u9032\u884c\u6bd4\u8f03\uff0c\u5f97\u5230\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\uff0c\u518d\u8b93 c
\u8207 max_one
\u6bd4\u8f03\uff0c\u5f97\u5230\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002
\u9019\u88e1\u5077\u5077\u544a\u8a34\u4f60\u4e00\u500b\u795e\u5947\u5999\u5999\u5de5\u5177\uff0cmax()
\uff0c\u4ed6\u6703\u627e\u51fa\u62ec\u865f\u5167\u7684\u6771\u897f\u4e2d\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002
a, b, c = map(int, input().split())\nprint(max(a, b, c))\n
input114514 1919 810\n
ouput114514\n
"},{"location":"fundamental/python/selection_structures/#match_case","title":"match ... case","text":"\u5728 Python 3.10 \u4e2d\uff0c\u65b0\u589e\u4e86 match ... case
\uff0c\u4f46\u907a\u61be\u7684\u662f\u4e00\u4e9b\u53e4\u8001\u7684 Online Judge \u7684 Python \u7248\u672c\u592a\u820a\u4e0d\u80fd\u7528\uff0c\u4f46\u6211\u9084\u662f\u60f3\u4ecb\u7d39\u7d66\u4f60\u3002
\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5728 if ... elif ... else
\u4e2d\u7684\u4f8b\u5b50:
month = int(input(\"Enter a month (1-12): \"))\n\n# which season?\nif 3 <= month <= 5:\n print(\"Spring\")\nelif 6 <= month <= 8:\n print(\"Summer\")\nelif 9 <= month <= 11:\n print(\"Fall\")\nelif month == 12 or month == 1 or month == 2:\n print(\"Winter\")\nelse:\n print(\"Invalid month\")\n
input4\n7\n10\n1\n13\n
ouputSpring\nSummer\nFall\nWinter\nInvalid month\n
\u518d\u4f86\u770b\u770b match ... case
\u7684\u7248\u672c:
month = int(input(\"Enter a month (1-12): \"))\n\nmatch month:\n case 3 | 4 | 5:\n print(\"Spring\")\n case 6 | 7 | 8:\n print(\"Summer\")\n case 9 | 10 | 11:\n print(\"Autumn\")\n case 12 | 1 | 2:\n print(\"Winter\")\n case _:\n print(\"Invalid month\")\n
input270\n11\n3\n
ouputInvalid month\nAutumn\nSpring\n
\u662f\u4e0d\u662f\u5f88\u7c21\u6f54\u5462?\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u50cf\u662f C, C++, Java
\u7b49\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u770b\u904e switch .. case
\uff0c\u4f46\u662f match ... case
\u66f4\u5f37\u5927!
\u95dc\u65bc match ... case
\u6211\u5c31\u4ecb\u7d39\u5230\u9019\u88e1\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u4f60\u8981\u77e5\u9053 if ... elif ... else
\u7684\u7528\u6cd5\u3002
@EditTime : 2024-01-29 12:32
"},{"location":"fundamental/python/selection_structures/#practice","title":"Practice","text":"Itsa - [C_MM09-\u6613] \u8a08\u7b97 i \u6b21\u65b9\u7684\u503c
Reference codei = int(input())\nif i > 31:\n print(\"Value of more than 31\")\nelse:\n print(1 << i)\n
Itsa - [C_MM13-\u6613] \u505c\u8eca\u8cbb\u8a08\u7b97
Reference codeh1, m1 = map(int, input().split())\nh2, m2 = map(int, input().split())\n\npaid = 0\nminutes = (h2 - h1) * 60 + (m2 - m1)\nif minutes > 240:\n paid += ((minutes - 240) // 30) * 60\n minutes = 240\nif minutes > 120:\n paid += ((minutes - 120) // 30) * 40\n minutes = 120\npaid += (minutes // 30) * 30\n\nprint(paid)\n
Itsa - [C_MM15-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u6b63\u65b9\u5f62\u7684\u7bc4\u570d\u5167
Reference codex, y = map(int, input().split())\n\nif 0 <= x <= 100 and 0 <= y <= 100:\n print(\"inside\")\nelse:\n print(\"outside\")\n
"},{"location":"fundamental/python/selection_structures/#assignment","title":"Assignment","text":"Itsa - [C_MM16-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u5713\u5f62\u7684\u7bc4\u570d\u5167
Itsa - [C_MM19-\u6613] \u96fb\u8a71\u8cbb\u8a08\u7b97
Itsa - [C_MM24-\u6613] \u8a08\u7b97\u85aa\u6c34
Itsa - [C_MM32-\u6613] Armstrong\u6578
Itsa - [C_MM35-\u6613] \u5e73\u3001\u958f\u5e74\u5224\u5b9a
Itsa - [C_MM36-\u6613] \u5b63\u7bc0\u5224\u5b9a
Itsa - [C_MM37-\u6613] \u5224\u65b7\u5ea7\u6a19\u4f4d\u65bc\u4f55\u8655
Itsa - [C_MM38-\u6613] \u5224\u65b73\u6574\u6578\u662f\u5426\u80fd\u69cb\u6210\u4e09\u89d2\u5f62\u4e4b\u4e09\u908a\u9577
Itsa - [C_MM39-\u6613] \u5224\u65b7\u662f\u4f55\u7a2e\u4e09\u89d2\u5f62
Itsa - [C_MM46-\u6613] \u8907\u6578\u904b\u7b97
Itsa - [C_AR36-\u6613] \u661f\u5ea7\u67e5\u8a62
@EditTime : 2024-01-29 14:38
"},{"location":"fundamental/python/sets/","title":"Sets","text":""},{"location":"fundamental/python/sets/#introduction","title":"Introduction","text":"\u5982\u679c\u7d66\u4f60\u4e00\u500b\u4e32\u5217\uff0c\u8acb\u4f60\u6aa2\u67e5\u88e1\u9762\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?\u4f60\u6703\u600e\u9ebc\u6a23\u78ba\u4fdd\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\u5462?
\u5728\u9019\u4e00\u7ae0\uff0c\u6211\u5011\u5c07\u6703\u5b78\u7fd2\u5230\u96c6\u5408(Set)\uff0c\u5b83\u6709\u4ee5\u4e0b\u7279\u6027:
{}
\u4f86\u8868\u793a\uff0c\u88e1\u9762\u7684\u5143\u7d20\u7528\u9017\u865f,
\u9694\u958b\u4f60\u53ef\u4ee5\u7528set()
\u4f86\u5efa\u7acb\u4e00\u500b\u96c6\u5408\uff0c\u6216\u8005\u7528\u5927\u62ec\u865f{}
\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:
a = set(\"FOREVER\")\nprint(a)\n\nb = [4, 8, 4, 3]\nc = set(b)\nprint(c)\n\nd = {(0, 1), (1, 2), (2, 3)}\nprint(d)\nprint(type(d))\n\ne = {}\nprint(type(e))\n
Output{'R', 'F', 'O', 'E', 'V'}\n{8, 3, 4}\n{(2, 3), (1, 2), (0, 1)}\n<class 'set'>\n<class 'dict'>\n
SUHO(\uc218\ud638) _ FOREVER
\u4f60\u5f97\u6ce8\u610f\uff0c\u5efa\u7acb\u7a7a\u96c6\u5408\u7684\u6642\u5019\uff0c\u4f60\u5fc5\u9808\u7528set()
\uff0c\u56e0\u70ba{}
\u662f\u7528\u4f86\u5efa\u7acb\u7a7a\u5b57\u5178\u7684\u3002
\u4ee5\u53ca\u6240\u8b02\u7684\u7121\u5e8f\uff0c\u4f60\u8a66\u8457\u591a\u57f7\u884c\u5e7e\u6b21\u7a0b\u5f0f\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u6bcf\u6b21\u8f38\u51fa\u7684\u7d50\u679c\u90fd\u4e0d\u4e00\u6a23\u3002
\u4f60\u751a\u81f3\u53ef\u4ee5\u7528 Comprehension \u4f86\u5efa\u7acb\u96c6\u5408\u3002
a = {1, 2, 3}\nb = {3, 4, 5}\ndiff_b = {x for x in b if x not in a}\nprint(diff_b)\n\nc = {x ** 2 for x in range(-3, 4)}\nprint(c)\n
Output{4, 5}\n{0, 9, 4, 1}\n
"},{"location":"fundamental/python/sets/#operations","title":"Operations","text":""},{"location":"fundamental/python/sets/#accessing_elements","title":"Accessing elements","text":"\u56e0\u70ba\u96c6\u5408\u662f\u7121\u5e8f\u7684\uff0c\u6240\u4ee5\u4f60\u4e0d\u80fd\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5143\u7d20\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u7528in
\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u53ca\u642d\u914dfor
\u8ff4\u5708\u4f86\u904d\u6b77\u96c6\u5408\u3002
perfect_nums_set = {6, 28, 496, 8128, 33550336}\nprint(4843 in perfect_nums_set)\n\nfor num in perfect_nums_set:\n print(num, end=' ')\n
OutputFalse\n33550336 8128 496 6 28 \n
Question
\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u96c6\u5408\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728?
\u7b54\u6848\u53ef\u80fd\u6703\u8b93\u4f60\u9a5a\u8a1d\uff0c\u4e0d\u9700\u8981 \\(n\\) \u500b\u6b65\u9a5f\uff0c\u53ea\u8981\u5e7e\u500b\u6b65\u9a5f\u5c31\u597d\uff0c\u53ef\u4ee5\u8aaa\u662f\u99ac\u4e0a\u3002
"},{"location":"fundamental/python/sets/#union","title":"Union","text":"\u4f60\u53ef\u4ee5\u7528|
\u6216\u8005 union()
\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u806f\u96c6\u3002
a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.union(b))\nc = a | b\nprint(c)\n\na |= b\nprint(a)\n
Output{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n
"},{"location":"fundamental/python/sets/#intersection","title":"Intersection","text":"\u4f60\u53ef\u4ee5\u7528&
\u6216\u8005 intersection()
\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\u3002
a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.intersection(b))\nc = a & b\nprint(c)\n\na &= b\nprint(a)\n
Output{5}\n{5}\n{5}\n
"},{"location":"fundamental/python/sets/#difference","title":"Difference","text":"\u4f60\u53ef\u4ee5\u7528-
\u6216\u8005 difference()
\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u5dee\u96c6\u3002
a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.difference(b))\nc = b - a\nprint(c)\n\na -= b\nprint(a)\n
Output{1, 3}\n{2, 4, 6}\n{1, 3}\n
\u6211\u5c31\u8209\u9019\u4e09\u500b\u4f8b\u5b50\uff0c\u5176\u4ed6\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u3002
"},{"location":"fundamental/python/sets/#methods","title":"Methods","text":""},{"location":"fundamental/python/sets/#adding_elements","title":"Adding elements","text":"\u4f60\u53ef\u4ee5\u7528 add()
\u4f86\u65b0\u589e\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002
nums_set = {1, 2, 3, 4, 5}\nnums_set.add(6)\nnums_set.add(3)\nprint(nums_set)\n
Output{1, 2, 3, 4, 5, 6}\n
\u4e5f\u53ef\u4ee5\u7528 update()
\u4f86\u65b0\u589e\u591a\u500b\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002
\u5176\u5be6\u5c31\u8ddf |=
\u4e00\u6a23\u3002
nums_set = {1, 2, 3, 4, 5}\nnums_set.update({6, 7})\nprint(nums_set)\n\nnums_set.update([1, 8, 9], {10, 11})\nprint(nums_set)\n
Output{1, 2, 3, 4, 5, 6, 7}\n{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\n
"},{"location":"fundamental/python/sets/#removing_elements","title":"Removing elements","text":"\u4f60\u53ef\u4ee5\u7528 remove()
\u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u6703\u62cb\u51fa\u932f\u8aa4\u3002
a = {1, 2, 3}\na.remove(1)\nprint(a)\na.remove(1)\nprint(a)\n
Output{2, 3}\nKeyError: 1\n
\u6216\u8005\u7528 discard()
\u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4e0d\u6703\u62cb\u51fa\u932f\u8aa4\u3002
a = {1, 2, 3}\na.discard(2)\nprint(a)\na.discard(2)\nprint(a)\n
Output{1, 3}\n{1, 3}\n
\u90a3\u600e\u9ebc\u6e05\u7a7a\u5462?\u4f60\u53ef\u4ee5\u7528 clear()
\u4f86\u6e05\u7a7a\u96c6\u5408\u3002
a = {1, 2, 3}\na.clear()\nprint(len(a))\n
Output0\n
\u9084\u6709\u66f4\u591a\u7684\u65b9\u6cd5\uff0c\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\uff0c\u4f46\u9019\u4e9b\u76ee\u524d\u61c9\u8a72\u5920\u4f60\u7528\u3002
"},{"location":"fundamental/python/sets/#practice","title":"Practice","text":"Itsa - [C_AR20-\u6613] \u6aa2\u67e5\u6578\u503c\u662f\u5426\u6709\u91cd\u8907
Reference coden = int(input())\nnum_set = set(input().split())\n\nif len(num_set) == n:\n print(1)\nelse:\n print(0)\n
\u6aa2\u67e5\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u9019\u500b\u554f\u984c\u5c31\u5f88\u9069\u5408\u7528\u96c6\u5408\u4f86\u89e3\u6c7a\uff0c\u56e0\u70ba\u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u53ea\u8981\u6aa2\u67e5\u96c6\u5408\u7684\u9577\u5ea6\u662f\u5426\u7b49\u65bc\u8f38\u5165\u4e32\u5217\u7684\u9577\u5ea6\u5c31\u597d\u3002"},{"location":"fundamental/python/sets/#assignment","title":"Assignment","text":"\u5728 Itsa \u4e0a\u4e0d\u592a\u597d\u627e\u984c\u76ee\uff0c\u6240\u4ee5\u4e7e\u8106\u9644\u4e0a\u89e3\u7b54\u3002
Itsa - [C_ST23-\u6613] \u76f8\u540c\u5b57\u96c6\u7684\u5b57\u4e32
Reference codea, b = input().split(',')\na_set = set(str.lower(a))\na_set.discard(\" \")\nb_set = set(str.lower(b))\nb_set.discard(\" \")\n\nif a_set == b_set:\n print(1)\nelse:\n print(0)\n
\u9019\u88e1\u61c9\u8a72\u80fd\u611f\u53d7 discard()
\u7684\u7528\u8655\uff0c\u984c\u76ee\u8981\u6c42\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\uff0c\u6240\u4ee5\u6211\u5011\u5148\u628a\u5b57\u4e32\u8f49\u6210\u5c0f\u5beb\uff0c\u7136\u5f8c\u518d\u628a\u7a7a\u767d\u5b57\u5143\u79fb\u9664(\u4e0d\u7ba1\u6709\u6c92\u6709)\uff0c\u6700\u5f8c\u518d\u6aa2\u67e5\u5169\u500b\u96c6\u5408\u662f\u5426\u76f8\u7b49\u3002 Itsa - [C_ST82-\u6613] \u4ea4\u96c6
Itsa - [C_ST83-\u6613] \u806f\u96c6
Itsa - [C_ST84-\u6613] \u5dee\u96c6
Itsa - [C_AR192-\u6613] \u5224\u65ad\u4efb\u610f\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\u662f\u5426\u6709\u91cd\u8907
@EditTime : 2024-02-06 23:16
"},{"location":"fundamental/python/tuples/","title":"Tuples","text":""},{"location":"fundamental/python/tuples/#introduction","title":"Introduction","text":"\u6709\u4e86\u4e0a\u4e00\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u6211\u76f8\u4fe1\u5e8f\u5c0d(Tuple)\u5c0d\u4f60\u4f86\u8aaa\u4e0d\u6703\u56f0\u96e3\uff0c\u56e0\u70ba\u5b83\u8ddf\u4e32\u5217(List)\u5f88\u50cf\uff0c\u53ea\u662f\u5b83\u662f\u7528\u5c0f\u62ec\u865f()
\u4f86\u8868\u793a\uff0c\u800c\u4e14\u5b83\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\u3002\u9019\u8868\u793a\u4f60\u4e0d\u80fd\u65b0\u589e\u3001\u522a\u9664\u6216\u4fee\u6539\u88e1\u9762\u7684\u5143\u7d20\u3002\u90a3\u65e2\u7136\u5982\u6b64\uff0c\u70ba\u4ec0\u9ebc\u9084\u8981\u7528\u5b83\u5462?
\u8981\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\u6709\u5f88\u591a\u7a2e\u65b9\u6cd5\uff0c\u6700\u7c21\u55ae\u7684\u5c31\u662f\u7528\u5c0f\u62ec\u865f()
\u628a\u5143\u7d20\u5305\u8d77\u4f86\uff0c\u7528\u9017\u865f,
\u9694\u958b\u5c31\u597d\u4e86\u3002
\u4f46\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u53ea\u6709\u4e00\u500b\u5143\u7d20\u6642\uff0c\u4f60\u5fc5\u9808\u5728\u5f8c\u9762\u52a0\u4e0a\u9017\u865f\u3002\u9019\u908a\u4f60\u81ea\u5df1\u8a66\u8a66\u770b\uff0c\u61c9\u8a72\u90fd\u80fd\u7406\u89e3\u3002
empty = ()\none = (1,)\ntwo = (1, 2)\nprint(len(empty), len(one), len(two))\nprint(type(empty))\n\nodd = 1, 3, 5\nprint(odd)\nprint(type(odd))\n
Output0 1 2\n<class 'tuple'>\n(1, 3, 5)\n(1, 3, 5)\n<class 'tuple'>\n
\u4f60\u4e5f\u53ef\u4ee5\u7528tuple()
\u4f86\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:
lst = [1, 2, 3, 4, 5]\ntup = tuple(lst)\nprint(tup)\n\ns = \"know me...\"\ns_tup = tuple(s)\nprint(s_tup)\n
Output(1, 2, 3, 4, 5)\n('k', 'n', 'o', 'w', ' ', 'm', 'e', '.', '.', '.')\n
\u516b\u6728\u6d77\u8389 \u300eknow me...\u300f
\u9084\u8a18\u5f97 List Comprehension \u55ce?\u6211\u5011\u5c07\u4e2d\u62ec\u865f\u6539\u6210\u5c0f\u62ec\u865f\u8a66\u8a66\u770b:
gen = (x ** 2 for x in range(5))\nprint(type(gen))\ntup = tuple(gen)\nprint(tup)\n
Output<class 'generator'>\n(0, 1, 4, 9, 16)\n
\u4f60\u6703\u767c\u73fe\uff0c\u7d50\u679c\u4e26\u975e\u662f\u4f60\u9810\u671f\u7684\u5e8f\u5c0d\uff0c\u800c\u662f\u7522\u751f\u5668(Generator)\uff0c\u4f60\u9084\u9700\u8981\u4f7f\u7528tuple()
\u4f86\u8f49\u63db\u3002
\u81f3\u65bc\u70ba\u4ec0\u9ebc\u8981\u7528\u7522\u751f\u5668(Generator)\uff0c\u6211\u6703\u5728\u672a\u4f86\u7684\u7ae0\u7bc0\u8ddf\u4f60\u8aaa\u660e\uff0c\u53c8\u6316\u5751\u4e86\u3002
\u90a3\u9ebc\u52a0\u6cd5\u8ddf\u4e58\u6cd5\u5462?\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u52a0\u6cd5\u4f86\u5408\u4f75\u5169\u500b\u5e8f\u5c0d\uff0c\u7528\u4e58\u6cd5\u4f86\u8907\u88fd\u5e8f\u5c0d\u3002
a = (1, 2, 3)\nb = 4, 5, 6\nc = a + b\nprint(c)\n\nd = a * 2 + b\nprint(d)\n
Output(1, 2, 3, 4, 5, 6)\n(1, 2, 3, 1, 2, 3, 4, 5, 6)\n
"},{"location":"fundamental/python/tuples/#operations","title":"Operations","text":""},{"location":"fundamental/python/tuples/#accessing_elements","title":"Accessing elements","text":"\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u7528\u8ca0\u7d22\u5f15\u4f86\u5f9e\u5f8c\u5b58\u53d6\u3002
t = (1, 2, 3, 4, 5)\nprint(t[0], t[-1])\n
Output1 5\n
"},{"location":"fundamental/python/tuples/#slicing","title":"Slicing","text":"\u540c\u6a23\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u5207\u7247\u4f86\u53d6\u5f97\u5e8f\u5c0d\u4e2d\u7684\u5b50\u5e8f\u5c0d\u3002
t = (1, 2, 3, 4, 5)\nprint(t[1:3])\nprint(t[:3])\nprint(t[3:])\nprint(t[:])\n
Output(2, 3)\n(1, 2, 3)\n(4, 5)\n(1, 2, 3, 4, 5)\n
"},{"location":"fundamental/python/tuples/#modifying_elements","title":"Modifying elements","text":"\u4f46\u662f\u4f60\u4e0d\u80fd\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u9019\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u662f\u6709\u6298\u8877\u7684\u65b9\u6cd5\u5566\uff0c\u5c31\u662f\u628a\u5e8f\u5c0d\u8f49\u63db\u6210\u4e32\u5217\uff0c\u518d\u8f49\u63db\u56de\u4f86\u3002
cat_tup = (\"\ud83d\ude38\", \"\ud83d\ude3a\", \"\ud83d\ude3b\", [\"\ud83d\ude3f\", \"\ud83d\ude40\"])\n\ncat_lst = list(cat_tup)\ncat_lst[1] = \"\ud83d\ude3c\"\ncat_tup = tuple(cat_lst)\nprint(cat_tup)\n\ncat_tup[3][0] = \"\ud83d\ude3e\"\nprint(cat_tup)\n\ncat_tup[1] = \"\ud83d\ude3d\"\nprint(cat_tup)\n
Output('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3f', '\ud83d\ude40'])\n('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3e', '\ud83d\ude40'])\nTypeError: 'tuple' object does not support item assignment\n
\u4f46\u4f60\u6709\u6c92\u6709\u89ba\u5f97\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u6211\u53ef\u4ee5\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u4e32\u5217\u5462?
"},{"location":"fundamental/python/tuples/#checking_elements","title":"Checking elements","text":"\u4f60\u53ef\u4ee5\u7528 in
\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5728\u5e8f\u5c0d\u4e2d\uff0c\u9019\u8ddf\u4e32\u5217\u662f\u4e00\u6a23\u7684\u3002
fib = (0, 1, 1, 2, 3, 5, 8)\nprint(0 not in fib)\nprint(5 in fib)\n
OutputFalse\nTrue\n
Question
\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u5e8f\u5c0d\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u5e8f\u5c0d\u4e2d\uff0c\u6700\u597d\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?
\u53c8\u662f\u719f\u6089\u7684\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u7684\u3002
"},{"location":"fundamental/python/tuples/#methods","title":"Methods","text":"\u56e0\u70ba\u5e8f\u5c0d\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u6240\u4ee5\u53ea\u6709\u5169\u500b\u65b9\u6cd5\uff0c\u4e00\u500b\u662fcount()
\uff0c\u4e00\u500b\u662findex()
\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u8a66\u8a66\u770b\u3002
count()
\u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u7576\u7136 List \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\uff0c\u4f46\u524d\u9762\u6211\u4e26\u6c92\u6709\u63d0\uff0c\u56e0\u70ba\u53ef\u4ee5\u653e\u5728\u9019\u88e1\u6c34\u5167\u5bb9(X
t = ((1, 2), (3, 4), 6, 6, [7, 8])\nprint(t.count(1))\nprint(t.count([7, 8]))\nprint(t.count(6))\n
Output0\n1\n2\n
"},{"location":"fundamental/python/tuples/#index","title":"index","text":"index()
\u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u7684\u7d22\u5f15\uff0c\u4e00\u6a23\u7684\uff0cList \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\u3002
t = (\"Love Me Again\", \"John Newman\", 2013)\nprint(t.index(\"John Newman\"))\nprint(t.index(2014))\n
Output1\nValueError: tuple.index(x): x not in tuple\n
John Newman - Love Me Again
"},{"location":"fundamental/python/tuples/#tuple_vs_list","title":"Tuple vs List","text":"[]
\u8868\u793a()
\u8868\u793a\u95dc\u65bc\u300c\u53ef\u96dc\u6e4a\u7684(hashable)\u300d\uff0c\u6211\u6703\u5728\u5b57\u5178(Dict)\u9019\u7ae0\u4e2d\u8ddf\u4f60\u8aaa\u660e\u3002
"},{"location":"fundamental/python/tuples/#practice","title":"Practice","text":"Itsa - [C_AR04-\u6613] \u908a\u7de3\u5075\u6e2c
Reference codeN = int(input())\n\ndirs = ((0, 1), (0, -1), (1, 0), (-1, 0))\n\nfor k in range(N):\n n, m = map(int, input().split())\n pic = []\n for _ in range(n):\n pic.append(input().split())\n\n for i in range(n):\n for j in range(m):\n if pic[i][j] == '0':\n print('_ ', end='')\n else:\n is_edge = False\n for d in dirs:\n x, y = i + d[0], j + d[1]\n if 0 <= x < n and 0 <= y < m and pic[x][y] == '0':\n is_edge = True\n break\n if is_edge:\n print('0 ', end='')\n else:\n print('_ ', end='')\n print()\n\n if k != N - 1:\n print()\n
\u65b9\u5411 dirs
\u5c31\u5f88\u9069\u7528\u5e8f\u5c0d\u4f86\u8868\u793a\uff0c\u56e0\u70ba\u5b83\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u6703\u8b8a\u52d5\u3002 \u672a\u4f86\u5728\u5b78\u5716\u5f62\u8d70\u8a2a\u7684\u6642\u5019\uff0c\u4f60\u6703\u4e00\u76f4\u770b\u5230\u9019\u7a2e\u5beb\u6cd5\u3002
\u5c0d\u4e86\uff0c \u8ff4\u5708\u8b8a\u6578 _
\u88ab\u7a31\u70ba\u6368\u68c4\u8b8a\u6578\uff0c\u8868\u793a\u4e0d\u9700\u8981\u7528\u5230\u9019\u500b\u8b8a\u6578\uff0c\u53ea\u662f\u70ba\u4e86\u914d\u5408\u8ff4\u5708\u8a9e\u6cd5\u800c\u5df2\u3002
Itsa - [C_AR119-\u6613] \u5730\u96f7\u5371\u96aa\u6307\u6578\u8868
Itsa - [C_AR139-\u6613] \u9ec3\u91d1\u63a2\u6e2c
Itsa - [C-AR140-\u6613] \u6c42\u6700\u5927\u7ce7\u98df\u7522\u91cf
@EditTime : 2024-02-04 21:15
"},{"location":"fundamental/python/variable_and_input/","title":"Variable and Input","text":""},{"location":"fundamental/python/variable_and_input/#variable","title":"Variable","text":"\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f86\u770b\u600e\u9ebc\u4f7f\u7528\u8b8a\u6578\uff0c\u4ee5\u53ca\u8f38\u5165\u81ea\u5df1\u60f3\u8981\u7684\u6771\u897f\u3002
x = 4843\nprint(x)\n\nx = \"Memory Reboot\"\nprint(x)\n\nx = '4843'\nprint(x)\n
\u6216\u8a31\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u795e\u5947\uff0c\u70ba\u4ec0\u9ebc x
\u53ef\u4ee5\u8b8a\u6210\u6578\u5b57\uff0c\u53c8\u53ef\u4ee5\u8b8a\u6210\u5b57\u4e32\uff0c\u9084\u6709\u70ba\u4ec0\u9ebc\u7b2c\u4e00\u500b\u8207\u7b2c\u4e09\u500b\u7684\u8f38\u51fa\u6703\u662f\u76f8\u540c\u7684\u3002
\u518d\u4f86\u7528\u4e00\u4e9b old-school \u7684\u6bd4\u55bb\uff0c\u4f60\u53ef\u4ee5\u5c07 x
\u60f3\u50cf\u6210\u4e00\u500b\u7bb1\u5b50\uff0c\u800c\u7b49\u865f\u53f3\u908a\u7684\u300c\u503c\u300d\u5c31\u662f\u7bb1\u5b50\u88e1\u88dd\u7684\u6771\u897f\u3002\u65e2\u7136\u7bb1\u5b50\u88e1\u9762\u7684\u6771\u897f\u53ef\u4ee5\u8b8a\uff0c\u90a3\u6211\u662f\u4e0d\u662f\u53ef\u4ee5\u8aaa x
\u662f\u4e00\u500b\u8b8a\u6578\u5462?
\u4f46\u8acb\u4f60\u5148\u5fd8\u6389\u9019\u500b\u6bd4\u55bb\uff0c\u56e0\u70ba\u9019\u500b\u6bd4\u55bb\u4e26\u4e0d\u5b8c\u7f8e\uff0c\u4f46\u6211\u60f3\u4f60\u61c9\u8a72\u80fd\u5920\u7406\u89e3\uff0cx
\u662f\u4e00\u500b\u8b8a\u6578\uff0c\u800c =
\u662f\u6307\u6d3e\u904b\u7b97\u5b50(Assignment Operator)\uff0c\u4ed6\u6703\u5c07\u7b49\u865f\u53f3\u908a\u7684\u6771\u897f\u6307\u6d3e\u7d66\u7b49\u865f\u5de6\u908a\u7684\u8b8a\u6578\uff0c\u6216\u8005\u8aaa x
\u6703\u6307\u5411\u7b49\u865f\u53f3\u908a\u7684\u7269\u4ef6(Object)\u3002
Quote
\u5728Python\u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u7269\u4ef6\u3002
\u8acb\u89c0\u770b\u4e0b\u9762\u7684\u52d5\u756b\u3002
V\u00d8J, Narvent - Memory Reboot (4K Music Video)
\u518d\u4f86\u4ecb\u7d39\u5e7e\u7a2e\u4e0d\u540c\u7684\u8b8a\u6578\u985e\u578b\u3002
a = 114514\nprint(type(a))\nb = 1919810.0\nprint(type(b))\nc = 10e3\nprint(type(c))\nd = \"cheung4843\"\nprint(type(d))\ne = True\nprint(type(e))\nf = False\nprint(type(f))\nh = None\nprint(type(h))\n
type
\u544a\u8a34\u4f60\u62ec\u865f\u4e2d\u7684\u6771\u897f\u662f\u4ec0\u9ebc\u985e\u5225(Class)\u3002 b
\u8207 c
\u90fd\u662f\u6d6e\u9ede\u6578(Float)\uff0c\u4e5f\u5c31\u662f\u5c0f\u6578\u9ede\u7684\u6578\u5b57\uff0c\u800c d
\u5247\u662f\u5b57\u4e32\uff0ce
\u8207 f
\u5247\u662f\u5e03\u6797(Boolean)\uff0c\u800c h
\u5247\u662f\u7a7a\u503c\u3002
Note
type(x)
\u56de\u50b3 x
\u7684\u985e\u5225\u3002int
\u6574\u6578\u3002float
\u6d6e\u9ede\u6578\u3002str
\u5b57\u4e32\u3002bool
\u5e03\u6797\u3002None
\u7a7a\u503c\u3002Question
print(type(3 + 4.0))
\u6703\u5370\u51fa\u4ec0\u9ebc?print(type(3 + True))
\u6703\u5370\u51fa\u4ec0\u9ebc?@EditTime : 2024-01-27 16:44
"},{"location":"fundamental/python/variable_and_input/#input","title":"Input","text":"\u4f46\u662f\u5982\u679c\u6bcf\u6b21\u60f3\u8981\u4fee\u6539 x
\u88e1\u9762\u7684\u6771\u897f\uff0c\u96e3\u9053\u90fd\u8981\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4fee\u6539\u55ce?\u80fd\u4e0d\u80fd\u6211\u81ea\u5df1\u4f86\u8f38\u5165\u5462?
x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = int(x) + int(y)\nprint(\"The sum is: \", z)\n
\u5594\u5e79\uff0c\u600e\u9ebc\u4e00\u4e0b\u5b50\u591a\u51fa\u90a3\u9ebc\u591a\u6771\u897f\uff0c\u5225\u614c\uff0c\u6211\u4f86\u89e3\u91cb\uff0c\u4f46\u8acb\u4f60\u5148\u56de\u60f3\u5728\u524d\u4e00\u7bc0\u4e2d\u5b78\u904e\u7684\u6771\u897f\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe input, int,
\u90fd\u662f\u51fd\u5f0f\u3002\u800c input
\u62ec\u865f\u4e2d\u7684\u5b57\u4e32\u6703\u986f\u793a\u5728\u63a7\u5236\u53f0\u4e2d\u63d0\u793a\u4f60\u8981\u8f38\u5165\u4ec0\u9ebc\uff0cint
\u5247\u662f\u628a\u62ec\u865f\u4e2d\u7684\u6771\u897f\u7684\u985e\u5225\u8f49\u63db\u6210\u6574\u6578\u3002
\u90a3\u70ba\u4ec0\u9ebc\u5370\u51fatype(x)
\u5f97\u5230 <class 'str'>
\u5462?\u4ee3\u8868 x
\u662f\u4e00\u500b\u5b57\u4e32\uff0c\u9019\u662f\u56e0\u70ba input
\u7e3d\u662f\u5c07\u4f60\u8f38\u5165\u9032\u4f86\u7684\u6771\u897f\u7576\u6210\u5b57\u4e32\uff0c\u4f46\u6211\u60f3\u8981\u8b93 z = x + y
\u9019\u500b\u6578\u5b78\u5f0f\u5b50\u6210\u7acb\uff0c\u6240\u4ee5\u9700\u8981\u7528 int
\u4f86\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\u518d\u9032\u884c\u904b\u7b97\u3002
\u90a3\u4e0b\u9762\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u662f\u4ec0\u9ebc\u5462?
x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = x + y\nprint(\"The sum is: \", z)\n
\u6c92\u932f\uff0c\u5b57\u4e32\u7684\u76f8\u52a0\uff0c\u5c31\u662f\u76f8\u9023\u3002\u90a3\u4f60\u8981\u4e0d\u8981\u8a66\u8a66\u770b\u76f8\u6e1b?
\u63a5\u4e0b\u4f86\u6211\u5011\u4f86\u505a\u500b\u6709\u8da3\u7684\u5be6\u9a57\uff0c\u9806\u4fbf\u8a8d\u8b58\u4e00\u4e0b f-string
a = 3.5\nb = int(a)\nprint(f'The value of b is {b}, and its type is {type(b)}')\nc = float(b)\nprint(f'The value of c is {c}, and its type is {type(c)}')\nprint(b == c)\n
\u90a3 f-string
\u662f\u4ec0\u9ebc\u6771\u897f\u5462?\u4ed6\u662f\u4e00\u7a2e\u5b57\u4e32\u683c\u5f0f\u5316(String Formatting)\u7684\u65b9\u6cd5\uff0c\u4ed6\u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u5b57\u4e32\uff0c\u4e26\u5c07\u5b57\u4e32\u4e2d\u7684 {}
\u66ff\u63db\u6210\u62ec\u865f\u5167\u7684\u6771\u897f\u3002
\u800c\u57f7\u884c\u5b8c\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u4f60\u6703\u767c\u73fe b
\u8207 c
\u7684\u985e\u5225\u4e0d\u540c\uff0c\u4f46\u4ed6\u5011\u7684\u503c\u537b\u76f8\u540c\uff0c\u9019\u662f\u56e0\u70ba int()
\u8207 float()
\u90fd\u662f\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u6574\u6578\u8207\u6d6e\u9ede\u6578\uff0c\u800c int()
\u6703\u5c07\u6d6e\u9ede\u6578\u7684\u5c0f\u6578\u9ede\u6368\u53bb\uff0c\u800c float()
\u5247\u6703\u5c07\u6574\u6578\u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002
\u90a3\u9ebc b == c
\u662f\u4ec0\u9ebc\u610f\u601d\u5462?\u5176\u4e2d ==
\u662f\u6bd4\u8f03\u904b\u7b97\u5b50(Comparison Operator)\uff0c\u4ed6\u6703\u6bd4\u8f03\u7b49\u865f\u5de6\u53f3\u5169\u908a\u7684\u6771\u897f\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5247\u56de\u50b3 True
\uff0c\u5426\u5247\u56de\u50b3 False
\u3002
\u90a3\u5982\u679c\u6211\u4eca\u5929\u60f3\u8981\u4e00\u6b21\u8f38\u5165\u597d\u5e7e\u500b\u5b57\u4e32\uff0c\u6bcf\u4e00\u500b\u5b57\u4e32\u4ee5\u7a7a\u683c\u4f86\u9694\u958b\u5462? \u4f46\u5728\u9019\u4e4b\u524d\uff0c\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5c0f\u7a0b\u5f0f :
a, b = \"Hello World\".split()\nprint(a)\nprint(b)\n
\u6211\u77e5\u9053\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u5b57\u4e32\u5f8c\u9762\u63a5\u4e86\u4e00\u500b .split()
\u5462?\u5728\u9019\u88e1\u4ed6\u5f88\u50cf\u662f\u51fd\u5f0f\uff0c\u4f46\u53c8\u4e0d\u662f\u51fd\u5f0f\uff0c\u90a3\u4ed6\u53eb\u4ec0\u9ebc\u5462?\u4ed6\u88ab\u7a31\u70ba \"Hello World\"
\u9019\u500b\u5b57\u4e32\u7684\u65b9\u6cd5(Method)\uff0c\u4f46\u672a\u4f86\u4f60\u53ef\u80fd\u9084\u6703\u807d\u5230\u985e\u5225\u65b9\u6cd5(Class Method)\uff0c\u4ee5\u53ca\u975c\u614b\u65b9\u6cd5(Static Method) \u7b49\u540d\u8a5e\uff0c\u6211\u6015\u4f60\u6703\u641e\u6df7\uff0c\u6240\u4ee5\u4f60\u5c31\u5148\u8a8d\u9017\u9ede\u5f8c\u9762\u7684\u662f\u300c\u65b9\u6cd5\u300d\u5c31\u597d\u4e86\u3002
\u800c .split()
\u6703\u628a\u5b57\u4e32\u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\u4e26\u56de\u50b3(\u90a3\u4f60\u60f3\u60f3\u62ec\u865f\u5167\u4ec0\u9ebc\u90fd\u6c92\u653e\uff0c\u9810\u8a2d\u6703\u662f\u4ec0\u9ebc?)\uff0c\u800c\u770b\u770b\u7b49\u865f\u5de6\u908a\uff0c\u6211\u7528 a, b
\u53bb\u63a5\u8457\uff0c\u9019\u7a31\u70ba\u958b\u7bb1(Unpacking)
\u90a3\u73fe\u5728\u4f60\u61c9\u8a72\u80fd\u770b\u61c2\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u4e86\uff0c\u56e0\u70ba input()
\u4e5f\u6703\u56de\u50b3\u4e00\u500b\u5b57\u4e32\uff0c\u56e0\u6b64\u4ed6\u4e5f\u80fd\u5920\u4f7f\u7528 .split()
a, b = input().split()\nprint(a)\nprint(b)\n
Note
input()
\u63a5\u53d7\u8f38\u5165\uff0c\u56de\u50b3\u4e00\u500b\u5b57\u4e32\u3002int(x)
\u5c07 x
\u8f49\u63db\u6210\u6574\u6578\u3002float(x)
\u5c07 x
\u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002str.split()
\u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\uff0c\u4e26\u56de\u50b3\u3002@EditTime : 2024-01-25 22:13
Question
print(int(input()) + int(input()))
\u6703\u5370\u51fa\u4ec0\u9ebc?124.spilt(\"1\")
\u662f\u5408\u6cd5\u7684\u55ce?bool(0)
\u8207 bool(1)
\u6703\u56de\u50b3\u4ec0\u9ebc?a
\u8207 b
\u6210\u529f\u4e92\u63db\u4e86\u5462?a, b = 4, 5\nprint(a, b)\na, b = b, a\nprint(a, b)\n
a, b = 4, 5\nprint(a, b)\ntmp = a\na = b\nb = tmp\nprint(a, b)\n
@EditTime : 2024-01-25 22:17
"},{"location":"fundamental/python/variable_and_input/#practice","title":"Practice","text":"\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:
Info
print()
\u8207 input()
\u3002type(x)
\u8207 int(x), float(x)
\u7684\u7528\u9014\u3002str.split()
\u7684\u7528\u9014\u3002f-string
\u3002\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002
ZeroJudge - a001. \u54c8\u56c9
Reference codeword = input()\nprint(f'hello, {word}')\n
@EditTime : 2024-01-27 17:02
"},{"location":"blog/archive/2024/","title":"2024","text":""},{"location":"blog/category/blog/","title":"Blog","text":""}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index 45c7d41..aae5e8d 100755 --- a/sitemap.xml +++ b/sitemap.xml @@ -50,6 +50,11 @@