Summary of the combined data types 043

First, the number and type of operation

  • Unlimited range of integer types and four kinds hexadecimal
  • Unlimited approximate range of floating point numbers, and scientific notation little-endian
  • +, -, *, /, //,%, ** binary assignment operator enhanced
  • abs()、divmod()、pow()、round()、max()、min()
  • int()、float()、complex()
# DayDayUpQ3.py

dayup = 1.0
dayfactor = 0.01
for i in range(365):
    if i % 7 in [6, 0]:
        dayup = dayup * (1 - dayfactor)
    else:
        dayup = dayup * (1 + dayfactor)
print("工作日的力量:{:.2f} ".format(dayup))  # 工作日的力量:4.63 
工作日的力量:4.63 
def dayUP(df):
    dayup = 1
    for i in range(365):
        if i % 7 in [6, 0]:
            dayup = dayup * (1 - 0.01)
        else:
            dayup = dayup * (1 + df)
    return dayup


dayfactor = 0.01
while dayUP(dayfactor) < 37.78:
    dayfactor += 0.001
print("工作日的努力参数是:{:.3f} ".format(dayfactor))  # 工作日的努力参数是:0.019 
工作日的努力参数是:0.019 

Second, the string type and operation

  • A forward sequence number increment, decrement counter number, <string> [M: N: K]
  • +、*、len()、str()、hex()、oct()、ord()、chr()
  • .lower()、.upper()、.split()、.count()、.replace()
  • .center () ,. strip () ,. join () ,. format () format
# TextProBarV1.py

import time

scale = 10
print("执行开始".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}s".format(c, a, b, dur), end='')
    time.sleep(0.1)
print("\n" + "执行结束".center(scale // 2, '-'))
-执行开始
100%[**********->]1.02s
-执行结束

Third, the branch structure of the program

  • A single limb ifbiantennary if-elseand compact form
  • Multi-branch if-elif-elserelations and conditions between
  • not and or > >= == <= < !=
  • Exception Handling try-except-else-finally
# CalBMIv3.py

height, weight = eval(input("请输入身高(米)和体重\(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))
who, nat = "", ""
if bmi < 18.5:
    who, nat = "偏瘦", "偏瘦"
elif 18.5 <= bmi < 24:
    who, nat = "正常", "正常"
elif 24 <= bmi < 25:
    who, nat = "正常", "偏胖"
elif 25 <= bmi < 28:
    who, nat = "偏胖", "偏胖"
elif 28 <= bmi < 30:
    who, nat = "偏胖", "肥胖"
else:
    who, nat = "肥胖", "肥胖"
print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat))
请输入身高(米)和体重\(公斤)[逗号隔开]: 1.8,70
BMI 数值为:21.60
BMI 指标为:国际'正常', 国内'正常'

Fourth, the structure of the program cycle

  • for…in Traversal cycle: count, strings, lists, files ...
  • whileInfinite loop
  • continueAnd breakreserved words: exit the current loop level
  • Advanced Usage of loop else: and breakrelated
# CalPiV2.py

from random import random
from time import perf_counter

DARTS = 1000 * 1000
hits = 0.0
start = perf_counter()

for i in range(1, DARTS + 1):
    x, y = random(), random()
    dist = pow(x**2 + y**2, 0.5)
    if dist <= 1.0:
        hits = hits + 1
        
pi = 4 * (hits / DARTS)
print("圆周率值是: {}".format(pi))
print("运行时间是: {:.5f}s".format(perf_counter() - start))
圆周率值是: 3.141364
运行时间是: 0.71023s

Fifth, the recursive function code reuse and

  • Modular design: loosely coupled, tightly coupled
  • Two recursive characteristic function: Examples and chain group
  • Implement recursive function: the function of a branched structure +

Six, set type and operation

  • Using the set of {} and set () function to create
  • Between set of operations: cross (&), and (|), the difference (-), complementary (^), comparison (> = <)
  • Method collection type: .add () ,. discard () ,. pop (), etc.
  • Collection types are mainly used: relationship includes a comparator, data deduplication

Seven, sequence and type of operation

  • Sequence is a base type, extended type comprising: a string, a tuple list, and
  • Tuple with () and tuple () to create, create a list with [] and set ()
  • Tuple operation sequence substantially identical to the operation
  • List of operations in sequence on the basis of the operation, an increase of more flexibility

Eight, dictionary type and operating

  • Using key mapping relationship of expression
  • {} And using a dictionary dict () to create, between the key-value pair separated by:
  • d [key] embodiment may be indexed and to be assigned
  • There are a number of dictionary type function and method of operation, the most important thing is .get ()

Guess you like

Origin www.cnblogs.com/nickchen121/p/11200519.html