Ternary operator
Syntax: x if condition else y
Example
small = x if < x else y
assert
When the condition following this keyword is false, the program automatically crashes and throws an AssertionError exception. Generally used for inspection.
Example
assert 3>4
While
grammar
while condition:
loop body
for loop
grammar
for target in expression
loop body
range()
grammar
range([strart,] stop[,step=1])
1. Indicates that this BIF has three parameters, two of which are enclosed in square brackets to indicate that these two parameters are optional.
2.step = 1 means the default value of the third parameter is 1.
3. The role of the range BIF is to generate a sequence of numbers starting from the value of the start parameter and ending with the value of the stop parameter.
for i in range(5): print(i)The output is 0 1 2 3 4
for i in range(2,9): print(i)The output is 2 3 4 5 6 7 8
for i in range(1,10,2): print(i)The output is 1 3 5 7 9
break 和 continue
break out of the loop
bingo = 'I love Mario' answer = input('Please enter the sentence that Little Turtle wants to hear most:') while True: if answer == bingo: break answer = input('Sorry, wrong, please re-enter (the answer is correct to exit the game):') print('Ouch, handsome~') print('You are really a roundworm in Mario's stomach ^_^')
continue to terminate the current cycle and start the next cycle
for i in range(10): if i%2 != 0: print(i) continue i += 2 print(i)
list (an array of hormones typed)
1. Create a normal list
member = ["1","2","3"] member
2. Create a Mixed List
mix = [1,"text",3.14,[1,2,3]]
3. Create an empty list
empty = []
add element to list
append()
Can't add member.append("5","6") like this
member = ["1","2","3"] member.append("4") member
extend()
Extend a list with another list member.extend(["5","6"])
member = ["1","2","3"] member.extend(["5","6"]) member
insert()
Two parameters The first parameter represents the position in the list The second parameter represents the position of the first parameter to insert an element (that is, the element name)
member = ["1","2","3"] member.insert(0,"0") member
swap element position
member = ["4","2","3","1"] temp = member[0] member[0] = member[3] member[3] = temp member
remove element from list
1.remove() The parameter is the element name
member = ["4","2","3","1"] member.remove("2")
If there is no such element, an error is reported
2. del statement
member = ["4","2","3","1"] del member[1] member
If del member then delete the entire list
member = ["4","2","3","1"] del member member
3. pop() removes the last element and returns (assignable)
member = ["4","2","3","1"] member.pop()
Addable parameters (position number)
member = ["4","2","3","1"] member.pop(1) member4. Fragmentation (assignable)
member = ["4","2","3","1"] member[1:3] member
back 2 3
It can be abbreviated. No number before the colon means to start from the front. Similarly, no number after the colon means to get to the end. Copy the entire list without writing on both sides (assignable)
member = ["4","2","3","1","5","6"] member[:3] member[1:]
List of common operators
1. Compare
Compare the first item of an array
2. The + sign must be of the same type
3. Example of copying the list 3 times with *
list = [123,456] list*3
4. = assignment can be combined with *
list = [123,456] list *= 3 list
5. Membership operator
return true or false
list = [123,456] "123" in list "123" not in list "文字" in list "文字" not in list
Determine the elements of a list in a list
list = [012,["text","text1"],4566] "文字" in list[1]
access the value of the list in the list
list = [012,["text","text1"],4566] list[1][1]
List type built-in function BIF
you(list)
1.count counts the number of times the parameter appears in the list
list[123,123,456,789,8,,78,87,8745,64678,74,685,1321654,8,74,564,89,7,98,74,454,654,8,7,1] list.count(123)
2.index returns the position in the list, three parameters (element, start, end)
list[123,123,456,789,8,,78,87,8745,64678,74,685,1321654,8,74,564,89,7,98,74,454,654,8,7,1] list.index(123)
list[123,123,456,789,8,,78,87,8745,64678,74,685,1321654,8,74,564,89,7,98,74,454,654,8,7,1] list.count(123,0,6)
3.reverse flips the entire list
list[123,123,456,789,8,,78,87,8745,64678,74,685,1321654,8,74,564,89,7,98,74,454,654,8,7,1] list.reverse() list
4 sort is used to specify that the list is sorted from small to large by default
list = [1,3,2,5,8,78,46,4,54,531,3,48,] list.sort() list
Sort from largest to smallest
list = [1,3,2,5,8,78,46,4,54,531,3,48,] list.sort(reverse.true) list