## 可变独立性

Python 不需要我们在使用变量和数据类型之前定义它们。这也为我们提供了范围灵活性，只要它在硬件的合理限制范围内，即无需担心整数和长整数。在内部，类型转换可以完美地执行。

## 常用函数，如排序、最小、最大、计数等。

### 例

# 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]`

## 唯一列表操作：回溯、子列表。

### 例

# 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

## 函数的参数数量灵活

### 如果其他和 for 循环易于使用

Python 的 if-else 语句允许我们在列表中搜索某个元素，而无需遍历完整列表并验证每个元素。

### 例

# 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

### 集合和字典概念

• Set 是一种无序集合数据类型，可以迭代、改变，并且不包含重复元素。它类似于没有重复元素的列表。
• 字典类似于列表，因为它的值可以使用用户定义的键而不是传统的数字索引值来检索。

### 例

# 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':  DhoniValue of 'Score':  100`