# Number type

## Basic type

1. Integer type: can be positive or negative. There is no limit on the value range. There are four forms: decimal, binary (0b/0B), octal (0o/0O) and hexadecimal (0x/0X).

2. Floating point number type: numbers with decimals and decimals. There is a range in the value range. There is an uncertain mantissa in the operation between floating-point types because binary means that the decimal is infinitely close to the decimal rather than equal to the decimal.

0.1+0.2 == 0.3 #Output False round(0.1+0.2,1)==0.3 #Output True round(x,d)#Round x, d is the number of decimal places

3. Complex type: a+bj, a is the real part and b is the imaginary part. Commonly used in spatial transformation.

z = 1.23+96j a=z.real() b=z.imag()

## Digital operation

1. Numeric operator

The result of different data types is the "widest" type: integer < floating point < complex number

Operator | describe |
---|---|

x+y | Add, sum |

x-y | Subtraction |

x*y | Multiplication, quadrature |

x/y | Division, quotient 10 / 3 = 3.33335 |

x//y | Integer division, 10 / / 3 = 3 |

x%y | Remainder, modular operation, 10% 3 = 1 |

x**y | Power operation, x^y, y is the square when y is a decimal |

Enhanced operation | x+=y x-=y x*=y x/=y x//=y x%=y x**=y |

2. Digital operation function

function | describe |
---|---|

abs(x) | Find the absolute value of x |

divmod(x,y) | At the same time, the quotient and remainder of x/y are obtained |

pow(x,y[,z]) | Power remainder operation, z optional. Set z to find the y power of x divided by the remainder of z |

round(x[,d]) | Round x, d optional, rounded by default |

max(x1,x2) | Find the maximum value |

mim(x1,x2) | Find the minimum value |

int(x) | Change x to an integer and round off the decimal part |

float(x) | Change x to floating point number and increase the decimal part |

complex(x) | Change x into a complex number and increase the imaginary part |

## Example

#One thousandth of progress every day, how much progress a year? #One thousandth of every day, how much is left in a year? dayup=pow(1.001,365) daydown=pow(0.999,365) print("dayup:{:.2f}, daydown:{:.2f}".format(dayup,daydown)) #The results are: dayup:1.44, daydown:0.69 #If it's five thousandths, one percent? dayfactor=0.005 #Modifiable dayup=pow(1+dayfactor, 365) daydown=pow(1-dayfactor, 365) #If it's a 1% increase on weekdays and a 1% decrease on rest days? dayup = 1.0 dayfactor = 0.01 for i in range(365): if 1%7 in [6,0]: dayup = dayup*(1-dayfactor) else: dayup = dayup*(1+dayfactor) #How hard should you work on a weekday to be the same as working 1% every day def dayUP(df): dayuo = 1 for i in range(365): if 1%7 in [6,0]: dayup = dayup*(1-0.01) else: dayup = dayup*(1+df) return dayup dayfactor = 0.01 while dayUP(dayfactor) ＜ pow(1.01, 365) dayfactor += 0.001

# String type

## String type representation

An ordered sequence of 0 or more characters, in which the characters can be indexed.

1. Means:

- Single or double quotation marks: single line string
- Three quotation marks: multiline string

If you want the string itself to have single quotation marks / double quotation marks, you should use double quotation marks / single quotation marks on both sides to represent the string.

2. Intercept

- Positive increment sequence number: from left to right, starting from 0
- Reverse decrement sequence number: from right to left, starting from - 1

Index: < string > [M]

Slice: < string > [M: n: k]: m indicates the start position. If it is missing, it defaults to the beginning; If n indicates the end position is missing, it defaults to the end, but n is not included; K represents the step size.

3. Escape character

Used to express the original meaning of a character.

character | significance |
---|---|

\b | Back off |

\n | Line feed (cursor movement with downward header) |

\r | Enter (cursor moves to the beginning of the line) |

## String processing operation

1. Operator

Operator | describe |
---|---|

x+y | Connect two strings |

xn nx | N is an integer, indicating that the string x is copied n times |

x in s | Returns True if x is a substring of s, False otherwise |

2. String handler

function | describe |
---|---|

len(x) | Returns the length of the string x |

str(x) | Converts any type of variable x to the corresponding string form |

hex(x) oct(x) | Form the hexadecimal or octal lowercase of integer x into a string |

chr(u) | Returns Unicode encoding to its corresponding string form |

ord(x) | Returns the string x to its Unicode encoding |

3. Treatment method

Method use | describe |
---|---|

str.lower() str.upper() | Returns all lowercase / uppercase of a string |

str.split(sep=None) | Returns a list divided by str according to sep |

str.count(sub) | Returns the number of occurrences of sub in str |

str.replace(old,new) | Replace old in the string with new |

str.center(wedth[,fillchar]) | The string is centered according to the width, fillchar is optional |

str.strip(chars) | Remove the characters in chars on the left and right sides of the string |

str.join(iter) | Add a str after every element of the iter variable except the last element |

## String type formatting

< template string >. Format (< comma separated parameter >), note that when setting the width, if the length of the string exceeds the width, it will be output according to the length of the string itself.

print("{}:computer{}of CPU The occupancy rate is{}%".format("2021.5.11","A",10)) print("{1}:computer{0}of CPU The occupancy rate is{2}%".format("A","2021.5.11",10)) #Control format: "{:<>}"#fill "{:<}"#Align left "{:>}"#Right align "{:^}"#Center alignment "{:<d>}"#width "{0:=^20}".format("python") #The width of the output string is 20, centered in python, and the rest is filled with = "{:<,>}"#thousands separator "{:.d}"#Maximum length of precision or string output "{:<leixing>}"#Integer: bcdoxX floating point number: eEf% "{0:,.2f}".format(12345.6789) #Output 12345.68

## Example

#Text progress bar: #Use string mode to print text progress bar that can change dynamically #The progress bar needs to be able to change gradually in one line #Simple framework import time scale = 10 #Approximate width and progress proportion of the text progress bar print("------Execution start------") for i in range(scale+1): a = '*'*i b = '.'*(scale-i) c = (i/scale)*100 print("{:^3.0f}%[{}->{}]".format(c,a,b)) time.sleep(0.1) #Running program print("------end of execution------") #Single line dynamic refresh (characters printed after overwrite characters before) \ r import time for i in range(101) print("\r{:3}%".format(i), end="") #\r controls the cursor to return to the beginning of the current line, and end = "" controls the output of the print() function without line wrapping. End = add information at the end after output. time.sleep(0.1) #Full effect text progress bar import time scale = 50 print("Execution start".center(scale//2, "-")) start = time.perf_counter() for i in range(scale+1): a = '*'*i b = '.'*(scale-i) c = (i/scale)*100 dur = time.perf_counter() - start print("\r{:3^.0f}%[{}->{}]{:.2f}".format(c,a,b,dur), end="") print("\n"+"end of execution".center(scale//2, '-'))

# Attached - Time library

The standard library of processing time, expressing the computer time, extracting the system time and timing accurately.

1. Get time

- time(): get the internal time value of the computer (floating point number)
- ctime(): get the readable current time (string)
- gmtime(): get the current time. The computer can read the format of the operation

2. Time format:

- strftime(tpl, ts): tpl is the format template string and defines the output effect; ts is a computer internal time type variable.

Symbol | significance |
---|---|

%Y | particular year |

%m | Month (number) |

%B,%b | Month (English, English abbreviation) |

%d | Date (number) |

%A,%a | Week (English, English abbreviation) |

%H,%h | Hour (24, 12) |

%p | Morning or afternoon identifier |

%M | minute |

%S | second |

- strptime(str,tpl): change the string into time, the time in the form of str string, and tpl format the template string.

3. Program timing:

- sleep(s): s is the sleep time in seconds. It can be a floating point number.
- perf_counter(): measure time and return a CUP level accurate time, in seconds. It is meaningful only when the difference is called continuously.