1. Operators
Assignment operator:
= - = = + etc.
Mathematical operators:
+ - * / ** % //
Member operator:
Determine whether something is included in something inside
in not in
E.g:
# String
name = "Powerpuff a" # character: N1 = "off" # sequences / substring: n2 = "Flying" IF N1 in name: Print ( "Success") the else: Print ( "Fail") # output Success IF N1 not in name: Print ( "Success") the else: Print ( "fail") # output to fail
Data types: numeric, string, Boolean (True / False)
Comparison operators:
Boolean value:
True true
False false
Equal to: ==
Greater than:>
Less than: <
Greater than or equal:> =
Less than or equal: <=
Does not mean:! = Or <>
Logical Operators:
Invert: not for example, not True and False
And: and
或:or
Sequence: a front to a rear operator
user=”ftxy" pwd="123" v= user=='ftxy' and pwd='123' or 1==1 and pwd='test' print(v) #输出True
Explain: and if it is connected to the front False, the latter expression is determined will not directly return False; or if it is connected to the front True, it is determined not everything after directly return True; So if you want to customize the order must be bracketed
2. The basic data types
Specific methods and properties of control + Right View
Digital (int):
a = 123
After a binary digit print (num.bit_length ()) # output digital conversion
int (str) string to digital
type (object) to view the object type
= A 'A' B = int (A, Base = 16) # 10 output; meaning hexadecimal string, which was converted to decimal shaping
print (b.bit_length) # output shaping share memory the median space
String (str):
str (object) into a string of strong
Commonly used methods: join, split, find, strip, upper, lower, replace (and other parts of the explanation follows)
= name "ftXy" # capitalized Print (name.capitalize ()) # all lowercase print (name.casefold ()) of the total length of the first parameter represents # string to be generated, when the second is filled the characters (with spaces if there is no default) Print (name.center (20, "*")) # put the middle name.ljust (20, "*") # put the left name.rjust (20, "*") # put the right number # statistical character appears in the character string (support sequences, the starting position may be defined) Print (name.count ( "F")) name.encode encoding name.decode decoding determining whether to start or end xxx Print (name.endswith ( "")) Print (name.startwith ( "")) # x from the position of the string to find and return, the search may be defined starting position; not found returns -1 Print (name.find ( "X")) # string variable Alternatively, as described in the case of output AM ftxy the I,. 19 Test = "the I AM {name}, Age {}" Print (test.format (name = "ftxy",=. 19 Age)) Print (test.format_map ({name: "ftxy", Age:}. 19)) # incoming Dictionary # Placeholder with Test = "the I AM {0}, {}. 1" Print (test.format ( "ftxy",. 19)) # determines whether only a string of digits and letters Print (name.isalnum ()) # Analyzing only if the string of letters, it is determined whether the number (commonly isdecimal), determines whether digital (② can be determined), it is determined whether the number (two can be determined) Print (name.isalpha (), name.isdecimal (), name. isdigit (), name.isnumeric ()) # create a table, the partition number in parentheses i.e., the string of parameters, insufficient \ t corresponding to the space filled Print (test.expandtabs (. 5)) # determines whether output as; If there \ False t this translation character returns name.isprintable () name.istitle () # determine whether the title, the first letter of each word are capitalized name.title () # becomes the title name.isupper () # judge capitalization name.islower () name.upper () # capitalized name.lower () "test" .join (name) # the name of this test separator characters spaced # pass argument string removed the same sequence; Removes the specified string name.strip ( 'XY') name.rstrip () # removed from the right name.lstrip () # removed from the left Alternatively Batch # demo, to replace the name f is J MT str.maketrans = ( "f", "J") name.translate (MT)
name.replace ( "f", "J",. 1) # The name is replaced by f j, the replacement of only a name.partition () # pass parameters in accordance with the partition, the number can not be specified, but the separator can get name.split () # partitioned into a list in accordance with parameters passed; default according separated by spaces can specify the number of the partition may not define parameters when # methods, method call parameters = (the count in the method, the parameter must Sub, start and end parameters can not) DEF count (Self, sub, start = None, end = None)
Basic operation (other data types can also be used):
Get string and the lower the index by a subscript character: name [0]
Get sequence: acquiring the first to the second sub-sequence name [0,2] to the second last acquired sequence name [1, -1]
Get string length: len (str)
for loop: for variable names in the string
Note: Once you create will exist for a long string in memory, can not be modified; any revision or spliced, regenerates the string; no one used will be recycled
End of the whole cycle: break
The end of this cycle: continue
Help create digital: range (start, end, step)
# Print user input and index name = input ( "Please enter your name:") length = len (name) for i in the Range (length): Print (i, name [i])
List (list):
Tuple (tuple):
Dictionary (dict):
Boolean value (bool):