Python basis _2

Formatted output

python which use mainly used to format the output format and%

%

% s python commonly used to represent a string, in fact, it is a placeholder name or symbol format, and is used to specify what position occupy a variable position is the format (such as string, integer, float, etc.) back followed by the variable name, so that it will fill the variable data specified later in accordance with its specified format in the placeholder when the python code runs.

E.g:

name = 'zhangsan'
age = 18
job = 'student'
print('name:%s\nage:%d\njob:%s'%(name,age,job))

  

Common placeholders:

% S string (using str () display)

% R string (using the repr () display)

% C single character

% B binary integer

% D decimal integer

% I decimal integer

% O octal integer

% X hexadecimal integer

% E index (written as a substrate e)

% E index (base written as E)

% F float

% F float, on the same

Case 1: print floating-point decimal places and control

Print ( ' Product List of% .2f, discount price .2f% ' % (207.99,187.45))

Case 2: Specifies the width placeholder

print ("Name:%10s\nAge:%8d\nHeight:%8.2f"%('tom',22,1.80))

 format

Python2.6 started, a new function of formatting strings  str.format (), which enhances the string formatting functions.

By basic syntax  {} and  : instead of the previous  %.

Any format function can take arguments, the position may not be in sequence.

E.g:

Print ( ' {} {} ' .format ( ' Hello ' , ' World ' )) # matched in sequence, without specifying location 
Print ( ' {}. 1} {2} {{0}. 3 ' .format ( ' 2 ' , ' 1 ' , ' 3 ' , ' 4 ' )) # set specified position

  • Match starts from 0 at the designated position, i.e. in the embodiment 1} {- 1} {2 - 3 {0} - 2, 3} {--4

while else statements

This statement is not commonly used, but will only be used when certain requirements in its while loop, if it encounters a break out of the cycle that follows else statements are executed, otherwise it will perform.

n = 5
while n < 10:
    n = n + 1
    if n !=9:
        print(n)
    else:
        break
else:
    print(100)

If you break instead pass, it will display more than 100

The initial coding

coding:

Because the computer does not recognize human language, but only 01 identification code, so early research and development of those computers which use 01 codes correspond to which characters, that is, in a corresponding ascii code ascii code table look at each of a defined 97, and 97 to 10 decimal, which is converted to binary is, 01,100,001, so that each character has a unique code that identifies the 01, you can complete the conversion of human language and the language of the calculator.

Encoding:

ascii:

The earliest encoding, because the computer is an American invention and American English language alphabet only 26 case-sensitive also only 52, plus what some of the symbols, the use of seven memory space is sufficient, but Taking into account development, so as ascii 8 bit, that is, there are 256 unique identification.

utf-8:

After the computer development to the world, ascii 256 possible already can not meet demand, especially for Chinese, Chinese characters, but there are more of 9W, so developed a utf-8 encoding, utf-8 uses 32 bit, that is, 4 bytes (byte). Far more than its available space 9W, this is a coded way of today's most used.

Computer storage conversion:

000000001 == 8 bit == 1 byte (byte)

1024byte == 1kb

1024kb == 1MB

1024MB == 1GB

1024GB == 1TB

Operators

Math: + - * / exponentiation: ** remainder operation:% integer division: //

Logical operators: and or not

Priority: not> and> or sibling count from right to left

For example: 1

  

print(3>4 or 4>5 and 8<10 or 6<1 and 7<9)
print(3>4 and 4>5 or 8<10 and 6<1 or 7<9 and 5<10)
print(3>4 and 4>5 or 8<10 and 6<1 or 7<9 and 5>10 or 8>4)

For example: 2

# X Y or, if the bit x returns True y, and vice versa, and in the opposite or in the 
Print (. 3 or . 4 )
 Print (0 or . 1 )
 Print (. 1 or . 5 )
 Print (0 or 2 )
 Print (. 3 and . 4 )
 Print (0 and . 1 )
 Print (. 1 and . 5 )
 Print (0 and 2)

For example: 3

print(0 or 4 and 3 or 2)
print(1 > 2 and 3 or 4 and 3 < 2)
print(2 or 1 < 3 and 2)

 

Guess you like

Origin www.cnblogs.com/fudelike/p/11022721.html