Getting python Basis

Chapter python Basis

2.1 Formatting Output String

  1. % Placeholders: Statement of the type placeholder

    % S ==> String

    Or% d% i ==> Integer

    %% ==> escaped into ordinary%

  2. % () No more, no less, correspondence

  3. f "{}" content inside the braces are generally put variable strings single quotes (python3.6 version to use and above)

a = "------------------- info ----------------------"
b = "name:"
c = "age:"
d = "sex:"
e = "job:"
f = "hobby:"
g = "------------------- end ------------------------"

name = input('name:')
age = input('age:')
sex = input('sex:')
job = input('job:')
hobby = input('hobby:')

print(a + '\n' + b + name + '\n' + c + age + '\n' + d + sex + '\n' + e + job + '\n'
      + f + hobby + '\n' + g
      )

String format when no less, no more (account number and the number of filling to be consistent)

Filling time and location of the content placeholder to-one correspondence

name = input("name:")
age = input("age:")
sex = input("sex:")
job = input("job:")
hobby = input("hobby:")

print(msg%(input("name:"),input("age:"),input("sex:"),input("job:"),input("hobby:")))

% Placeholder string application

msg = '''
------------------- info ----------------------
name: %s
age:  %s
sex : %s
job:  %s
hobby: %s
------------------- end ------------------------
'''

print(msg%('alex','20','nan','it','cnb'))

%% escape (to convert it into a normal placeholder% Number)

msg = '%s,学习进度5%%'
print(msg%(input("name:")))

f string concatenation ---- 3.6 or later is required to use

msg = f'''
------------------- info ----------------------
name: {input("name:")}
age:  {input("age:")}
sex : {input("sex:")}
job:  {input("job:")}
hobby: {input("hobby:")}
------------------- end ------------------------
'''
print(msg)

2.2 while loop

grammar

while 关键字 条件:(死循环)
    循环体
条件终止循环
break       终止当前的循环
continue    跳出本次循环,继续下次循环   伪装成循环体中的最后一行

while keyword, condition (infinite loop)

while True:   # while 是关键字  条件
    print('精忠报国')
    print('团结就是力量')
    print('北京欢迎你')
    print('葫芦爷爷救娃娃')
    print('嫁衣')
    print('痒')

Control termination conditions and the number of cycles

3 > 2  条件控制循环的终止 和次数
num = 3
while num >= 1:
    print(num)
    num = num - 1
num = 5
while num > 4:
    print(1)
    print(2)
    num = num - 1

print(3)

break action: terminate the current cycle

while True:
    print(1)
    while True:
        print(123)
        break
        print(234)

1559636839343

num = 5
while num > 4:
    print(1)
    print(2)
    num = num - 1
    break
else:
    print(3)

continue out of this cycle, the next cycle continues, disguised as the last line of the loop

while True:
    print(1)
    continue
    print(2)

Operators 2.3

2.3.1 arithmetic:

/ + - Power @% ** (divisible)

print(2 ** 4) #幂运算
print(5 // 2) 整除
print(5 % 2) #取余(模)

2.3.2 logic operation

and ==> with, and or ==> or not ==> non (not)

Priority basic operations: ()> not> and> or

and

1 and 0 0 and 1

and when both sides are true it is true, as long as you take the side of false false

1and 9

Content and operations on both sides really take the time and back

False and 0

Content and take front and both sides are false operation time

or

1 or 0 0 or 1

as long as one or operator to take true true

1 or 4

or operation are both true, when taken in front of or content

False or 0

The latter two are false or operation when taking or content

2.3.3 member operator

in ==> in not in ==> not

s = 'alexTMDdsb'
print('TMD'not in s)

2.4 encoding

ASCII supports English one byte, does not support Chinese

GBK GB English one byte Chinese 2 bytes

Unicode Unicode ==> ascii gbk shift-JIS 2 Ge

Chinese English 2 bytes 4 bytes

Utf-8 ==> the most popular language least 1 byte

English one-byte Chinese (Asian) 3 bytes Europe with a minimum of 2 bytes 1 byte

Utd-16 with a minimum of 2 bytes

单位转换:

1字节 = 8位

1Byte = 8bit

1024KB = 1MB

1024MB = 1GB

1024GB = 1TB    常用的就是TB

1024TB = 1PB

1024PB = 1EB

Guess you like

Origin www.cnblogs.com/yueling314/p/10994360.html