Numeric string

Note: python-independent variable type and name, and type of data relating to (this is where the difference between python other languages)
a numerical
type values: integer int, float float (not very accurate), decimal Decimal, score fraction
(. 1) variable declarations, assignments, using
(2) expression
Math symbols are: +, -, *, /
! (3) is greater than> Less than <, == equal, unequal = returned results results of Boolean
example: Score = 60.3
Score> = 60
output True
(4) divided /
/ indicates division, // denotes results after taking the integer part division, for example 10/4 = 2.5, 10 // 4 = 2, 4.0 = 10 // 2.0 (integer when divided by a float, the results were highly accurate value)
(5) decimal rounding --math.floor () left, math.trunc () to zero, round () rounding
the first case: rounding (rounding forward), i.e. to the left coordinate axis always
Import math # transferred to the math module
Math.floor (value) # math module with the following floor function rounded
example: math.floor (3.4) = 3, math.floor (-3.4) = - 4
second case: rounding, i.e. the coordinates To move the origin
Import math # transferred to the math module
math.trunc (value) by taking the integer math # for the following module function trunc
For example: math.trunc (3.13) = 3, math.trunc (3.94) = 3, math.trunc (-3.94) = - 3
The third case: rounded to the nearest integer
round (value)
for example: round (-3.14) = -3, round (-3.94) = -. 4
(. 6) integer: infinite precision, limited only by memory the computer configuration
(7) in binary, octal, hexadecimal,
octal: literal beginning 0o, decimal octal functions oct ()
hex: literal begins with 0x, decimal to hexadecimal function hex ()
binary: literal begins with 0b, decimal to binary bin function ()
(8) pi : Math.PI
(. 9) the square root: Math.sqrt ()
(10) decimal
Import decimal decimal transferred to the module #
decimal.Decimal ( "3.14") # with the module below decimal decimal function
result output are decimal ( "3.14 ") # 3.14 compared to a high-precision floating-
example: decimal.Decimal (" 1.1 ") + decimal.Decimal (" 2.2 ")
result is outputted Decimal (" 3.3 ")
Second, the string
1, the use of placeholder character to increase the content:
(. 1), "{0}" the format (20) shows a position 20 into the placeholder where {0};.
. (2), "{0 : .2f}" format (3.33333) 20 shows a placeholder into location {0} and 2 decimal places, float;
2, Statement - single quotes, bis quotes, triple quotes
triple quotes: acts described, the equivalent of explanatory documents differs with the content of the comment # triple quotes will not be ignored
3, the escape character: \ or \ backslash, \ n line feed, \ b backspace, \ t (corresponding to the Tab key)
For example: "what's your name" and 'what's your name' is the same meaning
path = 'c: \ abc \ xyz.txt' ( error)
path = ' c: \ abc \ xyz.txt '(right)
path R'C =: \ ABC \ xyz.yxt' (correct)
4, escape ignored: R & lt
path R'C =: \ ABC \ xyz.yxt '
. 5 basic operation
(1) obtaining a function of the length of the string: len ()
(2) 'ABC' + 'XYZ' output is' abcxyz '
(. 3) attached to play 5 times i.e.' ok '* 5, print ( ' - '* 30)
(4) each letter print
example:
S =' Hello '
for S in C:
Print ©
result is output (the default output line for each letter):
H
E
L
L
O
to the output row, each letter in the middle of custom symbols separated, then
C in S for
Print (C, End = 'custom symbol')
(5) to find a single element: in square brackets, 0 indicates the first bit, the second bit represents 1, -1 is the last one
, for example: S [0 ] = 'H'
(. 6) cutting: s [0: 3] represents a third string taken to variable s, s [:] represents the s variable taken first to the last character of a string, s [:: 2] s represents the variable taken first to the last character string, wherein each string intermediate hops to pick a number, s [:: - 1] represents a last taken variable s bit to the first bit string, wherein each string intermediate hops to pick up a number.
(7) Transcoding: ord (), chr ()
replace (8) of the string ( character string does not support changing the place ) .replace ()
, for example: H = 'Hello'
H = h.replace ( 'E', 'a')
the result is outputted Hallo
(. 9) to convert the elements inside the column element is a string
s = '' join (l) , s =. '|' .join (l) l shows a list of elements into string, wherein each string in the middle | partition
(10) dividing the plurality of strings .split ()
URL = 'coder.com, uke.cc, you.com'
L = url.split ( ',') # denotes the inside elements in url, be separated into the list l
an output result of [ 'coder.com, uke.cc, you.com' ]
beginning of the function .startswith (11) of the test string ()
( end function 12) test string .endswith ()
(13) to find if a certain element .find ()
returned result is the location of the element
(14) format string, a placeholder
'{0}> = {1 }'. Format (a, b) or ' {}> = {} '. format (a, b)
output the result are' a> = B '
' {name}> = {the salary} '. the format (name =' Tom ', the salary = 9000)
output result is 'Tom> = 9000'
(15) to exchange variables
a, B = B, a
(16) uppercase conversion .upper ()
Third, the list (in square brackets [])
substantially representation: l = [1,2 , 3,4]
nested lists: l = [1, "abc ", [3,4,5]]
acquired function element length: len ()
function of a portion taken elements: l [0] represents a first extraction element, l [1] indicates a second bit extraction element
four dictionary table (braces {})
dictionary table divided key (corresponding to the dictionary directory) and the values of the two portions.
1, showing the form: d = {key: value}, e.g., d = { "name"; "tom"}, if there are a plurality of key-value pairs may be separated by commas, d = { "name"; "tom "," Age ": 20 is};
2, (. 1) takes a value of time, D [key] of the key corresponding to the value is output;
(2) takes a value of time, represents another form: d.get ( "name"), d.get ( "gender", " M") represented by gender to find this element from the dictionary, can not find it returns the value "M";
3, 0} {use placeholders, {1} and the like additions:
For example: d = { "name": "tom", "age": 20, "job": "dev"}
Output: "Name: {0}, Age: {1}". Format ( d.get ( " name "), d.get (" age "))
output is:" name: Tom, Age: 20 "
five-tuple (tuple)
. 1, represented in the form: t = (1,2,3,4);
2, takes a value representation of time: t [0] represents a first tuple bits taken inside.
Note: The list of tuples and a value of the difference between taken, the list may be modified by truncating elements of the original, but not to modify the original tuple elements by intercepting, for example: l [1] = "xyz " shows a list which the second element modified to "xyz".
Six, Boolean
expressions: True, False integer corresponding to 1,0
for example: Score = 60.3
Score> = 60
Output True
Note: The boolean is essentially inside the python integer
isinstance (True, int) # Boolean test and the integer compatibility function
output is True
seven, a number of shortcuts
to re-execute the code: arrow
down looking for: down arrow

Released eight original articles · won praise 0 · Views 144

Guess you like

Origin blog.csdn.net/DAN_L/article/details/104065908