"Python Language Programming" Wang Kai Wang Zhi Machinery Industry Press Chapter 2 Python Basic Grammar After-school Exercises Answers

2.7 Exercises after class
(1) A variable refers to the quantity whose value can change during the running of the program

(2) Given s="Python language programming", the output result of print(s[2:4]) is th, and the output result of print(s[-4:-2]) is program

(3) It is known that t=(3.5, 2, 'abcd', 4+5j, True, [3,3.5], 5.3), the output of print(t[3]) is 4+5j, print(t[ -3]) is True

(4) The result of 10/4 is 2.5, the result of 10//4 is 2, the result of 10%4 is 2, and the result of 10**4 is 10000

(5) Given x=50, the result of 10<=x and x<=30 is False

(6) Given x,y=4,5, the result of x|y is 5, and the result of x^y is 1

(7) Among the options below, the correct variable name is (C)

         A.2sum B.for C. circle area 2 D.it is

(8) After executing the Python statement "name, age='Zhang San', 20", the following statement is correct (C)

        The value of A.name is "Zhang San", the value of age is 20, and the types of the two variables are not necessarily 

        B. The program reports an error because the two variables are not defined and cannot be assigned directly

        C. Define two variables, name is a string type, the value is "Zhang San", age is an integer type, and the value is 20

        D. You cannot assign values ​​to two variables at the same time, and the program reports an error

(9) Known statement: a,b,c=12,0o12,0x12, then the output of print(a,b,c) is (A)

         A.12 10 18          B.12 12 12          C.10 8 6           D.12 18 10

(10) Given a={10,2.5, 'test',3+4j, True, 5.3,2.5}, the output of print(a) is (B)

         A.{10,2.5, 'test',3+4j,True,5.3,2.5}                  B.{True,2.5,5.3,10,3+4j, 'test'}

         C.10 2.5 'test' 3+4j True 5.3 2.5                     D. True 2.5 5.3 10 3+4j 'test'

(11) The output result of print("Name: %5s, Age: %5d, Grade: %6.2f" %("tom",19,86.5)) is (D) (Note: the option means a space)

         A. Name: tom, age: 19, score: 86.5           

         B. Name: tom, age: 19, score: 86.50

         C. Name: Kou Kou tom, Age: Kou Kou 19, Grade: 86.5

         D. Name: Kou Kou tom, Age: Kou Kou 19, Grade: Kou 86.50

(12) Knowing x, y=10, [10,20,30], then the results of x is y and x in y are (D)

          A. True True          B. False False          C.True False            D. False True

(13) Write down the running result of the following program

s1,s2="abc","def"
z1,z2=[1,2,"zhang"],[2.2,3.3,"wang"]
x1=[1,2.5,'test',3+4j,True ,[3,1.63],5.3]
print(s1+s2)
print(z1+z2)
print(s1*3)
print(z1[:])
print(x1[:3])
print(x1[3:-1 ])
 
 
#output result
abcdef
[1, 2, 'zhang', 2.2, 3.3, 'wang']
abcabcabc
[1, 2, 'zhang']
[1, 2.5, 'test']
[(3+4j), True , [3, 1.63]]
(14) The function of 1 known block is that the user inputs numbers 1~7, and outputs the string corresponding to the day of the week, such as inputting 4, outputting Thursday. Please complete the program

week="Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"
n=eval(input("Please enter the week number (1~7)"))
pos=(n-1)*3
print(week[pos:pos+3] )
(15) By setting conditions, certain statements can be executed only when the conditions are met

(16) By looping, certain statements can be repeated multiple times

(17) The number of loops in the following program segment is 11, and the value of i after the loop is -1

i=10
while i>=0:
    i-=1
print(i)
(18) Known program segment:

score=eval(input('Please enter the grade (integer between 0-100):'))
if score<60:
    print('Fail')
elif score<70:
    print('Pass')
elif score<80 :
    print('medium')
elif score<90:
    print('good')
elif score<=100:
    print('excellent')
If you input 77, the output result is medium

(19) Known program segment:

score=eval(input('Please enter the score (integer between 0-100):'))
if score<60:
    print('Your score is %d'%score)
print('Failed')
if input 55, the output result is (A)

A. Your grade is 55

    failed

B. Your grade is 55

C. failed

D.No output

(20) Known program segment:

score=eval(input('Please enter the score (an integer between 0-100):'))
if score>=60:
    pass
else:
    print('failed')
If you input 55, the output result is (B)

A.No output

B. failed

C.pass

Dc program error

(21) Known program segment:

n=eval(input('Please enter an integer:'))
if n%2==0:
    print("even number")
else:
    print("odd number")
If you input -5, the output result is (B)

A.No output

B. Odd number

C. even number

D. even number

    odd number

(22) Known sentence segment:

d={'Python':1,'C++':2,'Java':3}
for k in d:
    print('%s:%d'%(k,d[k]))
then the output result is ( c)

A.Python

   C++

    Java

B.1:Python

    2:C++

    3:Java

C.Python:1

   C++:2

   Java:3

D. None of the above is correct

(23) The output of the following program segment is (D)

ls=['Python','C++','Java']
for k,v in enumerate(ls,3):
    print(k,v)
A.Python

   C++

    Java

B.1 Python

    2 C++

    3 Java

C.Python 1

   C++ 2

   Java 3

D.3 Python

    4 C++

    5 Java

(24) The function of the known program segment is that the user inputs a number n, and uses a for loop to find n!. Please complete the program

n=eval(input('Please input an integer greater than 0:'))
s=1
for i in range(1,n+1):
    s=s*i
print(s)
 (25) The function of the following program is To find the largest integer within 100 that can be divisible by 7, please fill in the program completely

n=100
while n>=0:
    if n%7==0:
        print(n)
        break
    n-=1
(26) Program for judging prime numbers, please fill in the program completely

for n in range(2,101):
    m=int(n**0.5)
    i=2
    while i<=m:
        if n%i==0:
            break
        i+=1
    if i>m:
        print(n,end=' ')
(27) The number of daffodils is a 3-digit integer (100~999), and the sum of the cubes of each digit is equal to the number itself. The following program calculates the number of daffodils, please write the result of running the program

for n in range(100,1000):
    bai=n//100
    shi=n//10%10
    ge=n%10
    if bai**3+shi**3+ge**3==n:
        print( n)
 
 
#Output result
153
370
371
407
(28) The following program outputs the nine-nine multiplication table, please complete the program

for i in range(1,10):
    for j in range(1,i+1):
        print(j,"*",i,"=",i*j,end='  ')
    print('\n')
 

Guess you like

Origin blog.csdn.net/weixin_49647262/article/details/122027998