1.collatz序列
寫一個名為 collatz()的函數,它 有一個名為 number 的參數。如果參數是偶數, 那麼 collatz()就會印出 number // 2,並傳回該值。如果 number 是奇數,collatz()就打 印並回傳 3 * number + 1。 然後寫一個程序,讓使用者輸入一個整數,並不斷對這個數呼叫 collatz(),直 到函數回傳值1。
1 #!/usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 4 def collatz(number): 5 print(number) 6 if number ==1: 7 return number 8 elif number % 2 ==0: 9 return collatz(number//2)10 else:11 return collatz(3*number +1)12 13 A = int(input('Input a number: '))14 while True:15 if collatz(A) != 1:16 continue17 else:18 break
輸出結果:
1 Input a number: 6 2 6 3 3 4 10 5 5 6 16 7 8 8 4 9 210 1
#2.逗號代碼
假定有下面這樣的列表: spam = [' apples', 'bananas', 'tofu', 'cats'] <br> 寫一個函數,它以一個列表值作為參數,傳回一個字串。此字串包含所 有表項,表項之間以逗號和空格分隔,並在最後一個表項之前插入and。例如,將前面的 spam 列表傳遞給函數,將返回'apples, bananas, tofu, and cats'。但你的函數應該能夠處理傳遞給它的任何列表。
<br>
#!/usr/bin/env python3<br># -*- coding:utf-8 -*-<br><br>def func(spam):<br> spam[-1]='and'+ ' ' + spam[-1]<br>for i in range(len(spam)):<br>print(spam[i], end=',')<br><br><br>spam = ['apple', 'bananas', 'tofu', 'cats', 'dog']<br>func(spam)<br>#输出结果<br>apple,bananas,tofu,cats,and dog,<br>
3.字元圖網格
假定有一個列表的列表,內層列表的每個值都是包含一個字元的字串,像這樣:
grid =[['.', '.', '.', '.', '.', '.'],
['.', 'O' , 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
#['.', 'O', 'O' , 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
#['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.' , '.', '.'],
#['.', '.', '.', '.', '.', '.']]
#你可以認為grid[x][y]是一幅「圖」在x、y 座標處的字符,該圖由文字字符組成。原點(0, 0)在左上角,向右 x 座標增加,向下 y 座標增加。 複製前面的網格值,寫程式用它列印出圖像。
..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
.. .OOO...
....O....
grid = [, , , , , [, , , , , [, , , , , [, , , , , [, , , , , [, , , , , [, , , , , [, , , , , [, , , , , #嵌套循环 n m (grid[m][n], end= ()#换行 #输出结果 ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O....
#
<br>
#
以上是Collatz 序列、逗號代碼、字元圖網格的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。