Python Exercise analysis

Source: Internet
Author: User
Tags mul

1. Print less than 10 even: bit operation

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

2. Given a positive integer that does not exceed 5 bits, it is judged by several (using the input function)

Method One: normal logical processing

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‘)

Method Two: Binary

val=input("Please enter a numer: ")val=int(val)if val>=1000:        if val>=10000:                print("这是五位数")        else:                print("这是四位数")else:        if val>=100:                print("这是三位数")        elif val>=10:                print("这是两位数")        else:                print("这是一位数")

Method Three: String processing

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

3. Given a positive integer no more than 5 bits, determine the number of digits, then print out the digits of bits, 10, hundred, Thousand, million

Method One:

val=input(‘>>>‘)      val=int(val)print(val)if val>=1000:        if val>=10000:                num=5        else:                 num=4else:                   if val>=100:                num=3        elif val>=10:                num=2        else:                 num=1print(num)pre=0for i in range(num,0,-1):        cur=val//(10**(i-1))        print(cur-pre*10)        pre=cur

Method Two:

num =int(input("请输入一个数字:"))print(num)countnum=0while True:        print(num%10)        num=num//10        countnum+=1        if num==0:                break

Print ("The number of digits you entered is:", Countnum)
Method Three:

c=int(input(">>>"))w=10000x=lengthflag =Falsewhile w:        t=c//w        if flag:                print(t)        else:                if t:                        print(t)                        flag=True                else:                        x -= 1        c %= w        w//=10print("这个数的位数是",x)

4. Print a square with a side length of n

Method One:

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) + ‘*‘)

Method Two:

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

Method Three:

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. Ask for all odd and (2500) in 100

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

6. Judge student achievement, Grade a~e. Among them, 90 points above is ' A ', 80~89 divides into ' B ', 70~79 divides into ' C ', 60~69 divides into ' D ', 60 points below is ' 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. Ask for 1 to 5 the sum of factorial

Method One:

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

Method Two: High efficiency

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

8. Find the area and perimeter of the circle

r=int(input(">>>"))print("area="+str(3.14*r*r))print("circumference="+str(2*3.14*r))

9. Enter a number of numbers, print out the maximum value, if the input is empty, exit the program
m = Int (input (">>>"))
While True:
c = input (">>>")
If C:
n = Int (c)
If n > M:
m = n
Print ("Max is", m)
Else
Break

10. Enter the number of N to calculate the arithmetic average for each input.

count = 0sum = 0while True:    num = int(input(">>>"))    count += 1    sum += num    print("avg is ",sum / count)    if num == 0:            break

11. Give a number to determine whether it is prime (prime)

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")

12. Print 99 multiplication table

Method One:

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)

Method Two: List analytic type

[print("{}*{}={:<3}".format(j,i,i*j),end=‘\n‘ if i ==j else ‘‘) for i in range(1,10) for j in range(1,i+1)]

13. Print Diamond

Method One:

for i in range(-3,4):        if i<0:                prespace=-i        else:                prespace=i        print(‘ ‘*prespace + ‘*‘*(7-prespace*2))

Method Two:

n =int(input(">>>"))e = n//2for i in range(-e,e+1):        if i < 0 :                p = -i        else:                p =i        print(" "*p+ "*"*(n-2*p))

14. Print Lightning Graphics
Method One:

for i in range(-3,4):        if i<0:                print(‘ ‘*(-i) + ‘*‘*(4+i))        elif i>0:                print(‘ ‘*3 + ‘*‘*(4-i))        else:                print(‘*‘*7)

Method Two:

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

15. Print to the top triangle
Method One:

for i in range(-3,4):        x = -i if i < 0 else i        print(" "*(3-x)+"*"*(2*x+1))

Method Two:

n =int(input(">>>"))e = n//2for i in range(-e,e+1):        x = -i if i < 0 else i        print(" "*(e-x)+"*"*(2*x+1))

16. Print the Fibonacci sequence within 100

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

17.101th of the Fibonacci sequence 573147844013817084101
Method One:
A = 0
b = 1
Print (0,a)
Print (1,B)
index = 2
While True:
c = A + b
Print (INDEX,C)
If index = = 101:
Break
A = b
B = C
Index + = 1

Method Two:

a = 0b = 1for _ in range(100):        a, b = b , a+bprint(b)

18. Ask for all primes within 100,000 9592

Method One: (Simplify first)

for x in range(2,100):        for i in range(2,x):                if x%i==0:                        break        else:                print(x,end=" ")

Method Two:

for x in range(3,10000,2):        for i in range(3,int(x**0.5)+1):                if x%i==0:                        break        else:                print(x,end=" ")

Method Three:

import timestart= time.clock()number = 100000count = 2for num in range(4,number):        if num%6 != 1 and num%6 != 5:                continue        else:                snum = int(num**0.5+1)                for i in range(5,snum):                        if not num%i:                                break                else:                        count += 1print(count)end=time.clock()print("run:{}".format(end-start))

Method Four: (the use of prime characteristics: more than 3 of prime forms are 6n-1 and 6n+1)

import timestart= time.clock()number = 100000count = 2for num in range(4,number):        if num%6 != 1 and num%6 != 5:                continue        else:                snum = int(num**0.5+1)                for i in range(5,snum):                        if not num%i:                                break                else:                        count += 1print(count)end=time.clock()print("run:{}".format(end-start))

19. Reverse Multiplication Table:

Method One:

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)

Method Two:

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))

Method Three:

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))

20. Monkeys Eat Peach:

n=1for i in range(1,10):        n=(n+1)*2print(n)

Python Exercise analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.