Python It's usually a sentence written one line at a time , But if the sentence is long , We can use backslash (\) To implement multiline statements , for example :
total = item_one + \
item_two + \
item_three
Python You can use multiple statements on the same line , Use semicolons... Between statements (;) Division , Here is a simple example :
import sys; x = 'runoob'; sys.stdout.write(x + '\n')
The default output is line feed
Add... At the end without newline end=""
a = b = c = 1
a, b, c = 1, 2, "runoob"
>>> 5 + 4 # Add
9
>>> 4.3 - 2 # Subtraction
2.3
>>> 3 * 7 # Multiplication
21
>>> 2 / 4 # division , Get a floating point number
0.5
>>> 2 // 4 # division , Get an integer
0
>>> 17 % 3 # Remainder
2
>>> 2 ** 5 # chengfang
32
Normal interception [:] Left closed right away
letter = ['r','o','b','o','t']
letter[0:5:2] # Not included 5, yes 5 Previous , Step length is 2
>>['r', 'b', 't']
Flip strings
The first parameter -1 Represents the last element
The second parameter is empty , Means to move to the end of the list
The third parameter is the step ,-1 It means reverseDelete list elements del
input = 'I like runoob'
inputWords = input.split(" ")
inputWords=inputWords[-1::-1]
output = ' '.join(inputWords)
output
>> runoob like I
# Delete list elements
list = ['Google', 'Runoob', 1997, 2000]
del list[2]
>> list = ['Google', 'Runoob', 2000]
Flip list elements
list.reverse()
a = [i for i in range(10)]
list.reverse(a)
>>[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
list.index(obj)
The index position corresponding to the value
a = [i for i in range(10)]
a[7] = 'a'
print(a.index('a'))
>> 7
list.count(obj)
Count the number of times an element appears in a list
list.remove(obj)
Removes the first match of a value in the list
a = [i for i in range(10)]
a.remove(7)
a
>>[0, 1, 2, 3, 4, 5, 6, 8, 9]
Sort the list
List.sort() # The default is ascending
List.sort(reverse = True) # Add parameter is in descending order
You can use braces { } perhaps set() Function to create a collection
Be careful :
To create an empty collection, you must use the set() instead of { }, because { } Is used to create an empty dictionaryMain functions: membership testing and deleting duplicate elements , Automatically delete duplicate elements
List de duplication can be used list(set(xxx))
# set You can do set operations
a = set('abracadabra')
b = set('alacazam')
print(a - b) # a and b The difference between the set
print(a | b) # a and b Union
print(a & b) # a and b Intersection
print(a ^ b) # a and b Elements that do not exist at the same time in
The elements in the dictionary are accessed by keys , Instead of accessing by offset
In the same dictionary , key (key) Must be unique
print (dict['one']) # The output key is 'one' Value
print (dict[2]) # The output key is 2 Value
print (tinydict) # Output complete dictionary
print (tinydict.keys()) # Output all keys
print (tinydict.values()) # Output all values
Delete operation
del dict['Name'] # Delete key 'Name'
dict.clear() # Empty dictionary
del dict # Delete Dictionary
dict.keys()
Returns an iterator , have access to list() To convert to list
dict.values()
Returns an iterator , have access to list() To convert to list
dict.items()
Return traversable as a list ( key , value ) Tuple array
for key,value in dict.items():
key in dict
If the key is in the dictionary dict Back in true, Otherwise return to false
ceil(x) Returns an upinteger of a number , Such as math.ceil(4.1) return 5
floor(x) Returns the rounded value of a number , Such as math.floor(4.9) return 4
round(x [,n]) Return floating point number x Round the value of , Such as given n value , Represents the number rounded to the decimal point .
round(4.3252222,2)
>> 4.33
Random function random
Sort all the elements of the sequence at random random.shuffle
a = [i for i in range(10)]
random.shuffle(a)
print(a)
Random selection of sequence elements random.choice
print(random.choice(a))
upper() Lowercase letters in the string are uppercase
capitalize() Convert the first character of the string to uppercase