Python 是否更適合某些程式設計需求?


在本文中,我們將討論 Python 是否更適合某些程式設計需求,例如競賽程式設計。

答案是肯定的;Python 更適合程式設計。它可以在短時間內用更少的程式碼行完成程式碼編寫。

產品型公司需要優秀的程式設計師,並且必須透過競賽程式設計環節才能進入面試環節。競賽程式設計是一個這樣的平臺,它將測試你的思維能力和速度。

速度是 Python 無與倫比的優勢。與 C、C++ 和 JAVA 等傳統程式語言相比,需要輸入的程式碼量大大減少。另一個重要的一點是,Python 為使用者提供了廣泛的功能、包和庫,作為程式設計師思維能力的補充。

最後,Python 最好的部分在於它非常容易上手,我們不必浪費時間在諸如輸入、輸出等不重要的事情上。它有助於我們將注意力重新集中到當前問題上。

現在我們將看到 Python 的一些特性,這些特性一定會吸引你嘗試使用 Python 進行競賽程式設計。

變數獨立性

Python 不需要我們在使用變數和資料型別之前定義它們。只要在硬體的合理限制範圍內,這也為我們提供了範圍靈活性,即無需擔心整數長整數。內部型別轉換完美無瑕。

注意

在 Python 巢狀迴圈中,我們可以使用相同的變數名在內迴圈和外迴圈變數中,而無需擔心資料不一致或錯誤。

常用函式,如 sorted、min、max、count 等。

minmax 函式分別幫助我們確定列表中的最小和最大元素。sorted 函式對列表進行排序,count 函式計算列表中特定元素出現的次數。

最好的部分是,我們可以確信 Python 庫使用了針對上述每個任務的最佳演算法。例如,sorted 函式是一種特殊的排序演算法,稱為TIMSORT,其最壞情況時間複雜度為 O(n log n),這是排序演算法所能提供的最佳效能。

示例

# input list
inputList = [10, 3, 5, 5, 1, 4, 6, 20, 5]

# printing maximum/greatest element in an input list
print("Maximum/greatest element in an input list: ",max(inputList))

# printing minimum/smallest element in an input list
print("minimum/smallest element in an input list: ",min(inputList))

# printing the sorted list
print("Sorted list: ",sorted(inputList))

# printing the Number of occurrences/frequency of a list element
print("The no. of occurrences of 5 is = ",inputList.count(5))

輸出

執行上述程式後,將生成以下輸出:

Maximum/greatest element in an input list: 20
minimum/smallest element in an input list: 1
Sorted list: [1, 3, 4, 5, 5, 5, 6, 10, 20]
The no. of occurrences of 5 is = 3

Python 列表結合了陣列和連結串列的最佳特性。

Python 列表具有獨特的刪除特定元素同時保持記憶體位置連續的能力。此功能使連結串列的概念無效。它就像一個類固醇連結串列!除此之外,可以在任何位置進行插入。

示例

# input list
inputList = [10, 3, 5, 5, 1, 4, 6, 20, 5]

# deleting element at specific index(here at 4th index) using del
del inputList[4]
print("After deleting the element at the 4th index", inputList)

# deleting specific element(6) from list using remove() function
inputList.remove(6)
print("After deleting element 6 from the list:", inputList)

# inserting at any arbitrary position
inputList[-2] = "tutorialspoint"
print("Inserting (tutorialspoint) at last second index:", inputList)

# Getting sublists
result = inputList[:2]
print(result)

輸出

執行上述程式後,將生成以下輸出:

After deleting the element at the 4th index [10, 3, 5, 5, 4, 6, 20, 5]
After deleting element 6 from the list: [10, 3, 5, 5, 4, 20, 5]
Inserting (tutorialspoint) at last second index: [10, 3, 5, 5, 4, 'tutorialspoint', 5]
[10, 3]

獨特的列表操作:回溯、子列表。

如果我們不確定列表的大小,我們可以使用索引位置 -1 獲取最後一個元素。同樣,-2 可用於倒數第二個元素,依此類推。因此,我們可以回溯。我們也不必指定列表大小;因此它可以用作動態分配陣列。

如上例所示,可以提取列表的特定部分而無需遍歷整個列表。列表可以容納多種資料型別這一事實令人驚訝。列表不再僅僅是資料元件的統一集合。

函式可以返回多個值

其他程式語言中的函式通常只返回一個值,但在 Python 中,我們可以返回多個值

示例

# creating a function that returns multiple values
def returnMultipleValues(*arr):
   el = arr[0]
   e2 = arr[1]
   return el,e2
   
x, y = returnMultipleValues(4, 6)
print(x,' ',y) 

x, y = returnMultipleValues(1, 3, 5, 8, 1)
print(x,' ',y)

輸出

執行上述程式後,將生成以下輸出:

4 6
1 3

函式引數個數靈活

函式的引數可以以列表的形式傳遞,該列表的大小在每次呼叫函式時都可能發生變化。在上例中,我們用兩個引數呼叫了該函式,然後用五個引數呼叫。

if else 和 for 迴圈易於使用

Python 的 if-else 語句允許我們在列表中搜索特定元素,而無需遍歷整個列表並驗證每個元素。

一些程式語言有一個 for each 迴圈的概念,它與 for 迴圈略有不同。它允許我們透過讓迴圈變數依次獲取列表值來遍歷列表。Python 在 for 迴圈本身中包含 each 迴圈的概念。

示例

# input list
inputList = [10, 3, 5, 5, 1, 4, 6, 20, 5]

# searching elements using if-else statements which are made easy
if 5 in inputList:
   print("True")
else:
   print("False")
   
# printing list elements using for each loop for e in inputList:
   print(e, end = ' ')

輸出

執行上述程式後,將生成以下輸出:

True

程式碼縮排

在 Python 中,程式碼塊透過縮排區分。這提高了程式碼的可讀性,並幫助我們養成縮排程式碼的習慣。

集合和字典概念

  • 集合是一種無序的集合資料型別,可以進行迭代和修改,並且不包含重複元素。它類似於沒有重複元素的列表。

  • 字典類似於列表,其值可以使用使用者定義的鍵而不是傳統的數字索引值來檢索。

示例

# input Set
inputSet = {'t','u','t','o','r','i', 'a', 'l', 's'}

# second 't' is dropped to avoid duplicates and the
# set returns elements unorderly
print("Set:", inputSet)

# input dictionary
inputDict = {'Cricketer': 'Dhoni', 'Country': 'India', 'Score': 100}
# accessing values of a dictionary using keys
print("Value of 'Cricketer': ", inputDict['Cricketer'])
print("Value of 'Score': ", inputDict['Score'])

輸出

執行上述程式後,將生成以下輸出:

Set: {'r', 'l', 't', 'a', 'i', 'o', 's', 'u'}
Value of 'Cricketer':  Dhoni
Value of 'Score':  100

更新於:2022年11月25日

127 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.