Python第一周 学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python第一周 学习笔记相关的知识,希望对你有一定的参考价值。

习题解析


0.打印10以内偶数:位运算

for i in range(10):
    if not i & 0x01:
        print(i)

1.给定一个不超过5位的正整数,判断其有几位(使用input函数)

方法一:正常逻辑处理

a = int(input("Please enter a numer: "))
if a < 0:
    print(‘Error‘)
if a < 10:
    print(1)
elif a < 100:
    print(2)
elif a < 1000:
    print(3)
elif a < 10000:
    print(4)
elif a < 100000:
    print(5)
else:
    print(‘Error‘)

方法二:折半

a=int(input("Please enter a numer: "))
if a < 0:
    print(‘Error‘)
elif a < 1000:
    if 9 < a < 100:
        print(2)
    elif a < 10:
        print(1)
    else:
        print(3)
elif a < 10000:
    print(4)
elif a < 100000:
    print(5)
else:
    print(‘Error‘)

方法三:字符串处理

a = str(int(input("Please enter a number: ")))
print(len(a))

2.给定一个不超过5位的正整数,判断该数的位数,依次打印出个位、十位、百位、千位、万位的数字

方法一:

a = int(input("Please enter a number: "))
length = 0
if a < 0:
    print(‘Error‘)
elif a < 1000:
    if 9 < a < 100:
        length = 2
    elif a < 10:
        length = 1
    else:
        length = 3
elif a < 10000:
    length = 4
elif a < 100000:
    length = 5
else:
    print(‘Error‘)
print(length)
for i in range(length):
    print(a % 10)
    a = a // 10

方法二:

a = input("Please enter a number: ")
b = int(a)
print(len(a))
for i in range(len(a)):
    print(b % 10)
    b = b // 10

方法三:

a = int(input("Please enter a number: "))
length = 0
if a < 0:
    print(‘Error‘)
elif a < 1000:
    if 9 < a < 100:
        length = 2
    elif a < 10:
        length = 1
    else:
        length = 3
elif a < 10000:
    length = 4
elif a < 100000:
    length = 5
else:
    print(‘Error‘)
print(length)
for i in range(length):
    print(a - (a // 10) * 10)
    a = a // 10

方法四:

a=int(input("Please enter a number: "))
a *= 10
while (a // 10) != 0:
    a //= 10
    print(a % 10)

3.给定一个不超过5位的正整数,判断该数的位数,依次打印出万位、千位、百位、十位、个位的数字

方法一:

a=input(‘Please enter a number: ‘)
b=int(a)
for i in range(len(a),0,-1):
    te = 10 ** (i - 1)
    c = b // te
    print(c)
    b -= (c * te)
print(len(a))    

方法二:

a = int(input("Please enter a number: "))
length = 0
if a < 0:
    print(‘Error‘)
elif a < 1000:
    if 9 < a < 100:
        length = 2
    elif a < 10:
        length = 1
    else:
        length = 3
elif a < 10000:
    length = 4
elif a < 100000:
    length = 5
else:
    print(‘Error‘)
print(length)
pre = 0
for i in range(length,0,-1):
    cur = a // (10 ** (i - 1))
    print(cur - pre * 10)
    pre = cur

方法三:

a = int(input("Please enter a number: "))
length = 0
if a < 0:
    print(‘Error‘)
elif a < 1000:
    if 9 < a < 100:
        length = 10
    elif a < 10:
        length = 1
    else:
        length = 100
elif a < 10000:
    length = 1000
elif a < 100000:
    length = 10000
else:
    print(‘Error‘)

while length:
    print(a // length)
    a %= length
    length //= 10

方法四:

c = int(input("Please enter a number: "))
w = 10000
length = 5
flag = False
while w:
    t = c // w
    if flag:  
        print(t)
    else:     
        if t:
            print(t)
            flag = True
        else:
            length -= 1
    c = c % w 
    w //= 10
print("The length of number is", length)

4.打印一个边长为n的正方形

方法一:

a = int(input("Please enter a number: "))
for i in range(a):
    if i == 0 or i == a-1:
        print(‘*‘ * a )
    else:
        print(‘*‘ + ‘ ‘ * (a - 2) + ‘*‘)

方法二:

a = int(input("Please enter a number: "))
print(‘*‘ * a)
for i in range(a - 2):
    print(‘*‘ + ‘ ‘ * (a - 2) + ‘*‘)
else:
    print(‘*‘ * a)

方法三:

a = int(input("Please enter a number: "))
top = mid = ‘*‘
for i in range(a - 1):
    top += ‘\t*‘
    mid += ‘\t‘
else:
    mid += ‘*‘
    print(top)

for i in range(a - 2):
    print(‘\n‘)
    print(mid)
else:
    print(‘\n‘)
    print(top)

5.求100内所有奇数的和(2500)

方法一:

sum = 0
for i in range(1,100,2):
    sum += i
print(sum)    

6.判断学生成绩,成绩等级A~E。其中,90分以上为‘A‘,80~89分为‘B‘,70~79分为‘C‘,60~69分为‘D‘,60分以下为‘E‘

方法一:

a = int(input(‘Please enter a score: ‘))
if a > 79:
    if a >= 90:
        print(‘A‘)
    else:
        print(‘B‘)
elif a >= 70:
    print(‘C‘)
elif 60 <= a < 69:
    print(‘D‘)
else:
    print(‘E‘)

7.求1到5阶乘之和

方法一:

sum = 0
for i in range(1,6):
    mul = 1
    for j in range(1,i + 1):
        mul *= j
    sum += mul
print(sum)

方法二:效率高

sum = 0
mul = 1
for i in range(1,6):
    mul *= i
    sum += mul
print(sum)

8.给一个数,判断它是否是素数(质数)

a=int(input("Please enter a number: "))
for i in range(2,int(a ** 0.5) + 1):
    if a % i != 0:
        continue
    elif a % i == 0:
        print(("N: %d") % (i))
        break        
else:
    print("Yes,prime")

9.打印九九乘法表

for i in range(1,10):
    line = ‘‘
    for j in range(1,i + 1):
        line = line + (‘%d*%d=%2d ‘) % (j,i,i*j)
    print(line)

10.打印菱形

方法一:

a = int(input(">>>"))
b = (a - 1) / 2
for i in range(a):
    if i <= b:
        print(‘ ‘ * ((a - (2 * i + 1)) // 2) +‘*‘ * (i * 2 + 1) + ‘ ‘ * (( a - (2 * i + 1)) // 2 ))
    else:
        m=int(2 * b - i)
        print(‘ ‘*((a - (2 * m + 1)) // 2) +‘*‘ * (m * 2 + 1) +‘ ‘ * ((a - (2 * m + 1)) // 2))

方法二:

a = int(input(">>>"))
b=(a - 1) // 2
for i in range(-b,b + 1):
    c = abs(i)
    print(‘ ‘ * c + ‘*‘ * (a - 2 * c) + ‘ ‘ * c)

11.打印100以内的斐波那契数列

a = 1
b = 1
while True:
    ne = a + b
    if ne >= 100:
        break
    a = b
    b = ne
    print(ne)

12.求斐波那契数列第101项 573147844013817084101

a = 1
b = 1
cnt = 2
while True:
    ne = a + b
    a = b
    b = ne
    cnt += 1
    if cnt == 101:
        print(b)
        break

13.求10万内的所有素数 9592

方法一:

import datetime
start=datetime.datetime.now()
cnt=1
for i in range(3,100000,2):#优化点1:排除偶数
    for j in range(3,int(i ** 0.5) + 1,2):#优化点2:排除含有因子2的数并只循环到原数的开方加1
        if i % j == 0:
            break
    else:
        #print(i)
        cnt += 1
end=(datetime.datetime.now() - start).total_seconds() 
print(end)
print(cnt)

方法二:

a = int(input(‘>>>‘))
import datetime
start=datetime.datetime.now()
cnt = 2
for i in range(4,a):
    if i % 6 != 1 and i % 6 != 5:#质数必与6的倍数相邻
        continue
    else:
        for j in range(5,int(i ** 0.5) + 1,2):
            if i % j == 0:
                break
        else:
            cnt += 1
end=(datetime.datetime.now()-start).total_seconds() 
print(end)            
print(cnt) 

方法三:

筛选法<br/>待补充<br/>

14.输入若干整数,打印最大值:

temp = 0 
while True:
    a = input(‘>>>‘) 
    if not a:
        temp = a
        break 
    else: 
        b = int(a) 
    if b >= temp:
        temp = b
print(temp)

15.输入n个数,求每次输入后的算数平均数

sum = 0
cnt = 0
while True:
    a = input(‘>>>‘)
    if not a:
        break 
    else:
        sum += float(a)
        cnt += 1
        print(sum / cnt)

16.反向乘法表:

方法一:

for i in range(1,10):
    line = ‘‘
    for j in range(i,10):
            line += ‘%d*%d=%2d ‘ % (i,j,j * i)
    print(‘ ‘ * (7 * i - 6) + line)

方法二:

for i in range(1,10):
    line = ‘‘
    for j in range(i,10):
            line += ‘%d*%d=%2d ‘ % (i,j,j * i)
    print(‘{:>63}‘.format(line))

方法三:

for i in range(1,10):
    line = ‘‘
    for j in range(i,10):
            line += ‘{}*{}={:<{}} ‘.format(i,j,j * i,2 if j<4 else 3)
    print(‘{:>70}‘.format(line))

17.打印对顶三角形:

a = int(input(">>>"))
b = (a - 1) // 2
for i in range(-b,b + 1): 
    c = int((a - (2 * abs(i) + 1)) / 2)
    print(‘ ‘ * c + ‘*‘ * (2 * abs(i) + 1) + ‘  ‘ * c)

18.打印闪电:

num = int(input(">>>"))
if num % 2 == 0:
    num += 1
half = (num - 1) // 2
for i in range(- half,half + 1):
    if i < 0:
        print(‘ ‘ * (-i) + ‘*‘ * (half + i + 1))
    elif i == 0:
        print(‘*‘ * num)
    else:
        print(‘ ‘ * half + ‘*‘ * (half - i + 1))

19.猴子吃桃:

x = 1
for i in range(9):
    x = (x + 1) * 2
print(x)

以上是关于Python第一周 学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

Python第一周 学习笔记

Python 3.5学习笔记(第一周)

Python 第一周学习笔记

MOOC《Python网络爬虫与信息提取》学习过程笔记requests库第一周1-3

Python第一周 学习笔记_待补充

第一周day1Python学习笔记