Python 100 Days

Day 1

  1. python shortcomings
    • Slightly lower efficiency and therefore computationally intensive tasks can be written in C / C ++.
    • Too many in the development framework that can be selected (such as Web framework there are more than 100), have a choice where there is an error.
  2. python interpreter
    • The official Python interpreter is written in C language, and it is the most widely used Python interpreter, usually called CPython. In addition, Python interpreter as well as the Java language Jython, IronPython C # language and PyPy, Brython, Pyston other versions

Day 2: Language Elements

  1. Computer hardware system consists of five parts:
    • Operator
    • Control
      operator and the controller together what we usually call a central processor, its function is to perform various calculations and control processing commands and data in the computer software.
    • Memory
    • input device
    • The output device
      2. The output format
      string print function using the output of the placeholder syntax, where% d is an integer from placeholder,% f is the fractional placeholder %% represents the percent (percent because It represents a number placeholder, the placeholder character string with the percent sign must be written to express %%), with the variable rear% after the string value will replace the placeholder and then output to the terminal.

      Day 3: branch structure

  2. Calculating the area of a triangle (Heron's formula)
    P = (A + B + C) / 2
    Area = (P * (P - A) * (P - B) * (P - C)) 0.5 **

    Day 4: cyclic structure

  3. for-in loop
    If you know exactly the number of cycles performed on a container or to iterate (will be mentioned later), then use the for-in loop
  4. while loop
    if you do not know the structure loop structure specific number of cycles, then use a while loop. capable of generating a while loop or by conversion to an expression control loop bool value, the expression is True cycle continues, the expression evaluates to False cycle ends.
  1. Greatest common divisor and least common multiple
x = int(input('x = '))
y = int(input('y = '))
if x > y:
    x, y = y, x
for factor in range(x, 0, -1):
    if x % factor == 0 and y % factor == 0:
        print('%d和%d的最大公约数是%d' % (x, y, factor))
        print('%d和%d的最小公倍数是%d' % (x, y, x * y // factor))    #最小公倍数与最大公约数的关系
        break
  1. Simple graphic print
row = 6

for i in range(row):
    for j in range(i+1):
        print("*",end="")
    #这个print()起的仅仅起换行的作用
    print()
for i in range(row):
    for j in range(row-i-1):
        print(" ",end="")
    for k in range(i+1):
        print("*",end="")
    print()
for i in range(row):
    for j in range(row-i-1):
        print(" ",end="")
    for k in range(1+2*i):
        print("*",end="")
    print()
*
**
***
****
*****
******
     *
    **
   ***
  ****
 *****
******
     *
    ***
   *****
  *******
 *********
***********
[Finished in 0.5s]

Day 5: program logic configured

1. Raymond issue one hundred chicken

"""
公鸡5元一只,母鸡3元一只,小鸡1元三只,
用100块钱买一百只鸡,问公鸡、母鸡、小鸡
各有多少只?
"""
for x in range(1,21):
    for y in range(1,33):
        z = 100-x-y
        if 100 == 5*x+3*y+(1/3)*z:
            print("公鸡有%d只;母鸡有%d只;小鸡有%d只"%(x,y,z))
#这是暴力法求解,很多时候挺有用,但是有时候不行,这个1/3就是个很大的不定因素
print(1/3)

公鸡有4只;母鸡有18只;小鸡有78只
公鸡有8只;母鸡有11只;小鸡有81只
公鸡有12只;母鸡有4只;小鸡有84只
0.3333333333333333
[Finished in 0.1s]
  1. Fibonacci number
def fib(n):
    if n==1:
        return 1
    if n==2:
        return 2
    else:
        return fib(n-1)+fib(n-2)

for i in range(1,21):    #生成前20个数
    print(fib(i),end=" ")
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 [Finished in 0.1s]

3. Number of seeking perfection

"""
完美数又称为完全数或完备数,它的所有的真因子
(即除了自身以外的因子)的和(即因子函数)恰
好等于它本身。例如:6($6=1+2+3$)和28($28
=1+2+4+7+14$)就是完美数。
"""
for num in range(1,10001):
    ls = []
    sum = 0
    for i in range(1,num):
        if num%i==0:
            ls.append(i)
    if ls!= []:
        for i in ls:
            sum += i
    if sum==num:
        print(num)
6
28
496
8128
[Finished in 4.2s]    #太慢了

And using the function modules: Day 6

Guess you like

Origin www.cnblogs.com/yu-ocean/p/12130827.html