1 Python built-in functions
Python contains the following built-in functions:
function | description | Remark |
---|---|---|
Only (item) | Count the number of elements in the container | |
del(item) | Delete variables | There are two ways del |
max(item) | Returns the maximum container element | If the dictionary, comparing only for key |
min(item) | Returns the minimum container element | If the dictionary, comparing only for key |
cmp(item1, item2) | Compare two values, less than -1 / 0 is equal to / greater than 1 | Python 3.x cancel the function cmp |
note
-
String more in line with the following rules: "0" < "A" < "a"
2 slices
description | Python expression | result | Supported data types |
---|---|---|---|
slice | "0123456789"[::-2] | "97531" | Strings, lists, tuples |
-
Slice using the index value to limit the scope, from a larger string of cut small string
-
Lists and tuples are ordered set, it can be by an index value obtained corresponding data
-
Dictionary is an unordered collection that is using a key-value pairs stored data
Operator 3
Operators | Python expression | result | description | Supported data types |
---|---|---|---|---|
+ | [1, 2] + [3, 4] | [1, 2, 3, 4] | merge | Strings, lists, tuples |
* | ["Hi!"] * 4 | [ 'Hi', 'Hi', 'Hi', 'Hi!'] | repeat | Strings, lists, tuples |
in | 3 in (1, 2, 3) | True | Element exists | Strings, lists, tuples, dictionaries |
not in | 4 not in (1, 2, 3) | True | Whether the element does not exist | Strings, lists, tuples, dictionaries |
> >= == < <= | (1, 2, 3) < (2, 2, 3) | True | Compare elements | Strings, lists, tuples |
note
-
in
Out of the dictionary operation, it is determined that the dictionary key -
in
Andnot in
it is referred to as members of the operator
Member operator
Member operator for the test sequence contains a specified member
Operators | description | Examples |
---|---|---|
in | If you find the value in the specified sequence returns True, otherwise False | 3 in (1, 2, 3) return True |
not in | If the value is not found in the specified sequence returns True, otherwise False | 3 not in (1, 2, 3) return False |
Note: for the dictionary operation, it is determined that the dictionary key
4 complete syntax for loop
-
In
Python
the completefor 循环
syntax is as follows:
for variables in the set: the loop body code for the else : no pass break exits the loop, the loop is finished, the code will be executed
Scenarios
-
In iterate through data nested type, for example, a list contains a plurality of dictionaries
-
Requirements: To determine whether there is a specified value in a dictionary
-
If there is , prompt and exit the loop
-
If not present , the whole end of the cycle after, hoping to get a unified tips
-
students = [ {"name": "阿土", "age": 20, "gender": True, "height": 1.7, "weight": 75.0}, {"name": "小美", "age": 19, "gender": False, "height": 1.6 , " weight " : 45.0 }, ] find_name = " Ado " for stu_dict in Students.: Print (stu_dict) # determines current dictionary name traversed whether find_name IF stu_dict.get ( ' name ' ) == find_name: Print ( " found " ) # If you have found a direct exit the loop, do not need to follow-up data were compared BREAK the else : Print ( " not found " ) Print (" Cycle end " )
Range
# Range () is a function that can be used to generate a sequence of natural numbers R & lt Range = (. 5) # generated such a sequence of [0,1,2,3,4] R & lt Range = (0, 10 ) R & lt Range = (10,0, -1 ) # this function takes three parameters # 1 starting position (may be omitted, the default is 0) # 2. the end position of # 3 step (can be omitted, the default is 1) # Print (List (R & lt)) # by range () to create a perform the specified number of times for the cycle # for () loop in addition to the way to create the others are and while the same, # including the else, including break continue can be circulated for use # and also easier for recycling # will use a while loop before doing exercises, and then use a for loop to complete once! for I in Range (30 ): Print (I) # for s in 'hello': # print(s)
The following highlights about the slice
Completion of a similar feeling java interception
# Slice # slicing refers to the existing list, get a list of a child # When you create a list, create a list of general, variable names will use the plural stus = [ ' Monkey King ' , ' Pig ' , ' sand monk ' , ' monk ' , ' spider ' , ' White Skeleton ' ] # index list may be negative # If the index is negative, after obtaining from the front element, -1 represents the inverse of the first, penultimate and so represents -2 print (stus [-2 ]) # acquired by slicing a specified element # syntax: list [start: end] # is acquired by slicing element, the element may include a starting position, the end position does not include the elements # When do the slicing operation, always returns a new list, will not affect the original list of # start and end positions of the indexes can be omitted # If you omit end position, it will have been intercepted in the end # If you omit the starting position , will start from the first element, taken # If the start and end positions omitted entirely, is equivalent to a copy of a list of Print (stus [. 1 :]) Print (stus [:. 3 ]) Print (stus [ 1:. 4 ]) Print (stus [1:. 5: 2 ]) Print (stus) # syntax: list [start: end: step] # step, said elements each acquisition interval, the default value is 1 # Print (stus [0: 5: 3]) # step can not be zero, but can be negative # Print (stus [:: 0]) ValueError: Slice the sTEP cAN not bE ZERO # If it is negative, it will post the list of the forward edge portion of elements having print(stus [:: -. 1 ]) # string sections can STR = ' Hello ' Print (STR) Print (STR [. 1:. 3])
E: \ PyWorkSpace \ Venv \ the Scripts \ python.exe E: / PyWorkSpace / Venv / bbb.py spider [ ' pig ' , ' sand monk ' , ' monk ' , ' spider ' , ' White Skeleton ' ] [ ' Monkey ' , ' pig ' , ' sand monk ' ] [ ' pig ' , ' sand monk ' , ' Tang ' ] [ 'Pig ', ' Monk ' ] [ ' Monkey ' , ' pig ' , ' sand monk ' , ' monk ' , ' spider ' , ' White Skeleton ' ] [ ' White Skeleton ' , ' spider ' , ' monk ' , ' sand monk ' , ' pig ' , ' Monkey '] Hello the Process finished with exit code 0