Tag: value is update copy variable content how much ISA state
Objective
1.Python uses the object model to store data, each data type has a built-in class, and each new data is actually an object, that is, all data is objects.
2.3 Properties of the object:
- Identity: Memory address, which can be obtained by ID ()
- Type: Determines the type that the object is saved in, what rules to follow, and the type () to get the datatype
- Value: The actual data saved by the object
3. Standard data types:
Numbers, strings, lists, Ganso, dictionaries
4. Other types:
Null, file, collection, function, class, module
Digital
Number types are: Int () integer, Long (), float () float, complex () complex, BOOL () Boolean
Note: Python2 is distinguished by integer type, long integer type, and no long integer type in Python3, which is called integral type.
Conversion of numeric types:
Conversion of numeric types:
int (x, base=none) converts x to an integer. Base is converted by how many binary
Float (x) converts the x to a floating-point number.
Complex (x) converts x to a complex number, the real part is x, and the imaginary part is divided into 0.
Complex (x, y) converts x and y to a complex number, the real part is x, and the imaginary part is Y. X and y are numeric expressions.
>>> Int (' 101 ', 2) 5>>> int (' 101 ') 101>>> float (' 101 ') 101.0>>> Complex (3) (3+0J) >>> Complex (3,4) (3+4j)
Numeric operations
Add (+), subtract (-), multiply (*), divide (/), withdraw (%), exponentiation (* *):
Print (9/4) # return 2.25print (9//4) # return 2print (2 * * 3) # return 8
Note : The Numeric division (/) always returns a floating-point number to get the integer use (//) operator.
Related Mathematical functions:
function |
return value (description) |
ABS (x) |
Returns the absolute value of a number, such as ABS (-10) returns 10 |
Ceil (x) |
Returns the top integer of a number, such as Math.ceil (4.1) returns 5 |
CMP (x, y) |
If x < y returns 1, if x = = y returns 0, if x > y returns 1. Python 3 is deprecated . Replace with use (x>y)-(x<y) . |
EXP (x) |
Returns the x power of E (ex), such as MATH.EXP (1) returns 2.718281828459045 |
Fabs (x) |
Returns the absolute value of a number, such as Math.fabs (-10) returns 10.0 |
Floor (x) |
Returns the lower integer of a number, such as Math.floor (4.9) returns 4 |
Log (x) |
If Math.log (MATH.E) returns 1.0,math.log (100,10) returns 2.0 |
LOG10 (x) |
Returns the logarithm of x with a base of 10, such as MATH.LOG10 (100) returns 2.0 |
Max (x1, x2,...) |
Returns the maximum value of a given parameter, which can be a sequence. |
Min (x1, x2,...) |
Returns the minimum value for a given parameter, which can be a sequence. |
MODF (x) |
Returns the integer portion of x and the fractional part, the two-part numeric symbol is the same as x, and the integer part is represented by a floating-point type. |
Pow (x, y) |
The value after the x**y operation. |
Round (x [, N]) |
Returns the rounded value of the floating-point number x, if given an n value, that represents the digits rounded to the decimal point. |
sqrt (x) |
Returns the square root of the number x, the number can be negative, and the return type is real, such as MATH.SQRT (4) returns 2+0J |
Trigonometric function:
function |
Description |
ACOs (x) |
Returns the inverse cosine radian value of x. |
ASIN (x) |
Returns the arc value of the arc sine of X. |
|
Atan (x) |
Returns the arc tangent value of x. |
Atan2 (y, x) |
Returns the inverse tangent value of the given X and Y coordinate values. |
COS (x) |
Returns the cosine of the arc of X. |
Hypot (x, y) |
Returns Euclidean norm sqrt (x*x + y*y). |
Sin (x) |
Returns the sine of the X radian. |
Tan (x) |
Returns the tangent of x radians. |
Degrees (x) |
Convert radians to angles, such as degrees (MATH.PI/2), returns 90.0 |
Radians (x) |
Convert an angle to radians |
Numeric constants:
Constants |
Description |
Pi |
Mathematical constant Pi (pi, usually denoted by π) |
E |
The mathematical constant e,e is the natural constant (natural constant). |
Comparison operation
operator |
Descriptor |
Example |
== |
Checks whether the values of the two operands are equal, and if so, the condition becomes true. |
(A = = B) is not true. |
!= |
Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. |
(A! = B) is true. |
<> |
Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. |
(a <> B) is true. This is similar to! = operator |
> |
Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. |
(A > B) is not true. |
< |
Checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. |
(A < b) is true. |
>= |
Checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. |
(a >= B) is not true. |
<= |
Checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. |
(a <= B) is true. |
Assignment operations
operator |
Descriptor |
Example |
= |
Simple assignment operator, assignment from left operand of right operand |
c = A + B will specify the value A + B to c |
+= |
The addition and assignment operator, which increases the left operand of the right operand and assigns the result to the left operand |
c + = a equals c = C + A |
-= |
Minus and assignment operator, which subtracts the right operand from the left operand and assigns the result to the left operand |
c-= a equals c = c-a |
*= |
Multiplication and assignment operator, multiplied by the right operand and left operand, and assigns the result to the left operand |
C *= a equals c = c * A |
/= |
The Division and assignment operator, which puts the left operand with the correct operand and assigns the result to the left operand |
C/= a equals = C/A |
%= |
Modulus and assignment operator, which needs to use the modulus of two operands and assign the result to the left-hand operand |
C%= A is equivalent to C = c% A |
**= |
Exponential and assignment operators, performing exponential (power) calculation operators and assigning to left operands |
C **= a equals c = c * * A |
//= |
The floor is divided and assigned a value that performs the floor in addition to the operation and assignment to the left operand |
C//= a equals c = c//A |
Bit arithmetic
A bitwise operator computes a number as a binary. The bitwise algorithms in Python are as follows:
Variable A is 60,b to 13
>>> a = # = 0011 1100>>> b = # = 0000 1101>>> A & b12>>> a ^ b 49>>> A | B61>>> ~a-61>>> a << 2240>>> a >> 215
Logical operations
operator |
Description |
Example |
and |
So-called logic and operators. If the two operands are true, then the condition is set. |
(A and B) is true. |
Or |
The so-called logical OR operator. If there are two operands that are non-0 then the condition becomes true. |
(A or B) is true. |
Not |
The so-called logical non-operator. The logical state used to invert the operand. If a condition is true, the logical non-operator returns false. |
Not (A and B) is false.
|
String (String)
String: is a collection of ordered characters that is used to store and represent basic textual information.
What is contained in the middle of ' content ' or ' content ' is called a string
Characteristics:
- Only one value can be stored
- Not variable
- Defines the character set in the left-to-right order, the subscript is accessed sequentially from 0, ordered
String method:
Serial Number |
Method and Description |
1 |
Capitalize () Converts the first character of a string to uppercase |
2 |
Center (width, Fillchar) Returns a string that specifies the width of the center, Fillchar is a filled character, and the default is a space. |
3 |
Count (str, beg= 0,end=len (String)) Returns the number of occurrences of STR in a string, if beg or end specifies that the number of STR occurrences in the specified range is returned |
4 |
Bytes.decode (encoding= "Utf-8", errors= "strict") There is no Decode method in Python3, but we can use the decode () method of the Bytes object to decode the given bytes object, which can be encoded back by bytes (). |
5 |
Encode (encoding= ' UTF-8 ', errors= ' strict ') Encodes a string in the encoded format specified by encoding, if an error defaults to a ValueError exception unless errors specifies ' ignore ' or ' replace ' |
6 |
EndsWith (suffix, beg=0, End=len (String)) Checks whether the string ends with obj, or returns False if beg or end specifies whether to end with obj in the specified range, or True if it is. |
7 |
Expandtabs (tabsize=8) Turns the tab symbol in string strings to a space, and the default number of spaces for the tab symbol is 8. |
8 |
Find (str, beg=0 End=len (String)) Detects if STR is contained in a string, if beg and end specify a range, the check is contained within the specified range, or returns 1 if the index value is returned. |
9 |
Index (str, beg=0, End=len (String)) Just like the Find () method, only if STR does not report an exception in the string. |
10 |
Isalnum () Returns True if the string has at least one character and all characters are letters or numbers, otherwise False |
11 |
Isalpha () Returns True if the string has at least one character and all characters are letters, otherwise False |
12 |
IsDigit () Returns True if the string contains only a number, otherwise False: |
13 |
Islower () Returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are lowercase, otherwise False |
14 |
IsNumeric () Returns True if the string contains only numeric characters, otherwise False |
15 |
Isspace () Returns True if the string contains only spaces, otherwise False is returned. |
16 |
Istitle () Returns True if the string is heading (see Title ()), otherwise False |
17 |
Isupper () Returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise False |
18 |
Join (SEQ) Merges all elements of the SEQ (the string representation) into a new string with the specified string as a delimiter |
19 |
Len (String) return string length |
20 |
Ljust (width[, Fillchar]) Returns a string that is left-justified by using Fillchar to fill a new string of length width, and fillchar the default is a space. |
21st |
Lower () Converts all uppercase characters in a string to lowercase. |
22 |
Lstrip () Truncate the space to the left of the string |
23 |
Maketrans () To create a conversion table of character mappings, for the simplest invocation of two parameters, the first argument is a string representing the character that needs to be converted, and the second argument is the target of the string representation of the transformation. |
24 |
Max (str) Returns the largest letter in the string str. |
25 |
Min (str) Returns the smallest letter in the string str. |
26 |
Replace (old, new [, Max]) Replace the str1 in the string with str2, and if Max specifies it, the replacement does not exceed Max times. |
27 |
RFind (str, Beg=0,end=len (String)) Similar to the Find () function, it is just looking from the right. |
28 |
Rindex (str, beg=0, End=len (String)) Similar to index (), but starting from the right. |
29 |
Rjust (width,[, Fillchar]) Returns the right alignment of an original string and fills a new string of length width with Fillchar (default space) |
30 |
Rstrip () Removes a space at the end of a string string. |
31 |
Split (str= "", Num=string.count (str)) Num=string.count (str)) intercepts a string with the Str delimiter, and only the NUM substring if NUM has a specified value |
32 |
Splitlines ([keepends]) Separated by rows (' \ r ', ' \ r \ n ', \ n '), returns a list containing the rows as elements, if the argument keepends is False, does not contain a newline character, and if true, the newline character is preserved. |
33 |
StartsWith (str, Beg=0,end=len (String)) Checks whether the string starts with obj, returns True, or False. If beg and end specify a value, the check is within the specified range. |
34 |
Strip ([chars]) Execute Lstrip () and Rstrip () on a string |
35 |
Swapcase () Convert uppercase in a string to lowercase, lowercase to uppercase |
36 |
Title () Returns the "heading" string, meaning all words start with uppercase and the remaining letters are lowercase (see istitle ()) |
37 |
Translate (table, deletechars= "") Converts a string character to a table (containing 256 characters) given by STR, and the character to be filtered out into the Deletechars parameter |
38 |
Upper () lowercase letters in a converted string are capitalized |
39 |
Zfill (width) Returns a string of length width, the original string right-aligned, front padding 0 |
40 |
Isdecimal () Checks whether the string contains only decimal characters and returns False if true. |
name = ' i\ ' am Lichengguang ' print (name) # output string, print: I ' am lichengguangprint (len name) # output Word String length print (name[0]) # output 1th character print (name[0:-1]) # output subscript 1th position to all characters of the countdown 2nd place, Print: I am Lichenggu Anprint (name[5:15]) # output subscript from 5th to 14th position characters, print: Lichengguaprint (name[5:]) # output subscript from 5th onwards all characters , Printed: Lichengguanprint (name * 2) # output 2 times string print (' Hello, ' + name) # string stitching, Print: hello,i ' am Lichenggua Nprint (' Li ' in name) # Trueprint (name.find (' Li ')) # Find, print 5, return the starting index value, otherwise return -1print (name.index (' Li ')) # Find, print 5, return the starting index value, no then throw exception print (' Li ' not in name) # Falseprint (Name.upper ()) # All uppercase print (name.lower () # All Turn lowercase print (name.capitalize ()) # capitalize the first character of the string print (Name.isspace ()) # If it contains a space print (NAME.REPLA CE (' Li ', ')) # Replace operation print (Name.split (' m ')) # split operation, print: ["I ' A", ' Lichengguan ']print (Name.strip ()) # Remove Word The left and right spaces of the string print (Name.lstrip() # Remove the left space of the string print (Name.rstrip ()) # Remove the right space from the string
Placeholder
Like Java, Python also has placeholders.
%d indicates that the position is an integer;%f represents a floating-point number;%s represents a string.
Print (' hello,%s '% ' python ') of print (' hello,%d%s%.2f '% (666, ' Python ', 9.99)) # Printing: hello,666python10.00
which
1,% is the symbol of the beginning of conversion;
2, if more than one placeholder, to be written in a parenthesis, in the middle with a comma (half-width) separated;
3,%.2f represents two decimal places, not two bits will be rounded.
String Wrapping
1, three single quotes
Python three single quotes allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters.
2, three double quotes
3, \ End
List (lists)
A list is a list of elements written in square brackets [] separated by commas, and the types of elements in the list can be different. Such as:
s = [100.0, ' LCG ', True, ' i\ ' am Lichengguang ']
accessing elements
Print (s) # output list, printing: [100.0, ' LCG ', True, ' I ' am Lichengguang ']print (len (s)) # Output list length print (s[0]) # input Out list 1th element print (S[2:5]) # output lsit subscript from 2nd to 4th element, print: [' LCG ', True, ' I ' am Lichengguang ']print (s[2:]) # output Lsit Subscript from the 2nd after all elements, print: [' LCG ', True, "I am Lichengguang"]
adding elements
S1 = [False]print (s + s1) # [+, 100.0, ' LCG ', True, ' I ' am Lichengguang ', False] # append another list at the end of the list s.extend (S1) print (' Extend add = ' + str (s)) # [+, 100.0, ' LCG ', True, "I am Lichengguang", False] # Append method stitching, add new object at the end of the list s.append (' Test stitching ') print (' Append method stitching = ' + str (s)) # [+, 100.0, ' LCG ', True, ' I am Lichengguang ', False, ' Test stitching '] s.insert (0, ' Test stitching ') # Specify position insert element print (' Insert insert = ' + str (s)) # [' Test stitching ', ' 100.0 ', ' LCG ', True, ' I ' am Lichengguang ', False, ' Test stitching ']
Updating elements
S[0] = ' 1024x768 ' Print (' update element = ' + str (s)) # update element = [' 1024x768 ', ' + ', ' 100.0 ', ' LCG ', True, ' I ' am Lichengguang ', False, ' Test stitching ']
Delete Element
S.pop () # Pop Method Delete, can specify location, default last print (s) # Print: [' 1024x768 ', ' + ', ' 100.0 ', ' LCG ', True, ' I ' am Lichengguang ', False] S.pop ( 0) # pop Specify location delete print (s) # Printing: [[[[] 100.0, ' LCG ', True, ' I ' am Lichengguang ', False] del S[0]print (s) # print: [100 .0, ' LCG ', True, "I ' am Lichengguang", False] # Removes the first occurrence of a value in the list without throwing an exception s.remove (' LCG ') print (s)
Other
Print (S * 2) # prints two times s.reverse () # Reverse list elements print (s) s2 = S.copy () # Copy print (s2) s.clear () # Empty print (s)
Tuple (tuple)
Tuples are written in parentheses (), and the elements are separated by commas, and the elements of the tuple cannot be modified, without the append (), insert () method.
You can use the index slice to take a value, and Len () can be used to find the length.
Dictionary (dictionary)
The dictionary uses the {} identifier or Dict () method to create a dictionary, which is an unordered key (key): The value pair collection. The key (key) must use the immutable type. In the same dictionary, the key (key) must be unique (if not unique, only the last value is taken, but not recommended). {} is used to create an empty dictionary.
DIC = {' name ': ' LCG ', ' site ': ' http://lcgsmile.cn/', ' Code ': 1024}
accessing elements
Print (DIC) print (dic[' site ') # Output key is the value of site
modifying elements
dic[' Code ' = 520 # modified element print (dic[' code ') # printed: 520
New element
dic[' id ' = 1314 # new element print (DIC) # Printing: {' name ': ' LCG ', ' site ': ' http://lcgsmile.cn/', ' Code ': 520, ' ID ': 1314}
Delete Element
Dic.pop (' code ') # Delete code key print (DIC) # Printing: {' name ': ' LCG ', ' site ': ' http://lcgsmile.cn/', ' ID ': 1314} dic.clear ( # Empty print (DIC) # Print: {}# del dic # Delete
Other
# Dict () method Create dictionary D = dict (id=1024, name= ' LCG ', site= ' http://lcgsmile.cn/') print (d) print (D.copy ()) # Shallow copy print ( Dic.keys ()) # output all keys, print: Dict_keys ([' Name ', ' site ', ' Code ']) print (Dic.values ()) # Output all values, print: dict_values ([' LCG ', ' http://lcgsmile.cn/', 1024])
Sets (collection)
A collection is a sequence of unordered, non-repeating elements, created with braces {} or a set () function. Creating an empty collection must be set (), because {} is used to create an empty dictionary.
s = {' name ', ' site ', ' Code ', 1, 1}print (s) # Output set, duplicate elements are automatically removed
Null value
The null value is a special value in Python, denoted by None.
Add
--not to be continued--
Python Basic Data type