Python test questions

(1) Fill in the blanks

1. The execution result of list (map (STR, [1, 2, 3]) is

```['1', '2', '3']
```

map() maps the specified sequence according to the provided function

2. After the execution of statement x = 3==3, 5, the value of variable x is

```True,5
```

3. If x = 3 and the return value of id(x) is 496103280, the value of expression id(x) == 496103280 after executing statement x += 6 is

```3
```

+The number operation will change the id of both the variable object and the immutable object. The + = operation will change the immutable object id and the immutable object id. the number is the immutable object.

4. The result of Python statement '. join(list('hello world!')) is

```'hello world!'
```

5. The execution result of Python statement list(range(1,10,3)) is

```[1, 4, 7]
```

6. The execution result of slicing operation list(range(6))[::2] is

```[0, 2, 4]
```

7. The output result of Python 3. X statement print(1, 2, 3, sep = ':') is

```1:2:3
```

8. Use the list derivation to get all the numbers that can be divided by 13 within 100. The code can be written

```list = [i for i in range(100) if i%13 == 0]
```

9. If vec = [[1,2], [3,4]], the value of expression [col for row in vec for col in row] is

```[1, 2, 3, 4]
```

10. The value of expression 'Hello world'. upper() is

```'HELLO WORLD'
```

11. Given x = '123' and y = '456', the value of expression x + y is

```'123456'
```

12. The value of expression 'a'. join('abc '. partition('a') is

```'aaabc'
```

13. Expression 'Hello world!' The value of [- 4:] is

```'rld!'
```

14. Given that x = (3,), the value of expression x * 3 is

```(3, 3, 3)
```

15. How to turn "1,2,3" into ['1', '2', '3']

```'1,2,3'.split(',')
```

16. How to turn ['1', '2', '3'] into [1,2,3]

```list = [int(i) for i in a]
```

17. If the list x = [1, 2, 3, 4] is known, the value of X after executing the statement del x[1] is

```[1, 3, 4]
```

18. The value of expression {1, 2, 3} < {3, 4, 5} is

```False
```

19. The value of the expression dict(zip([1, 2], [3, 4]) is

```{1: 3, 2: 4}
```

20. Given x = [[1,3,3], [2,3,1]], the value of the expression sorted(x, key=lambda item:item[0]+item[2]) is

```[[2, 3, 1],[1, 3, 3]]
```

21. The value of the expression sorted(random.sample(range(5), 5)) is

```[0, 1, 2, 3, 4]
```

22. If x = [3, 2, 3, 3, 4], then the value of the expression [index for index, value in enumerate(x) if value==3] is

```[0, 2, 3]
```

23. How to generate [1,4,9,16,25,36,49,64,81100] using list generation formula

```list = [i*i for i in range(1,11)]
```

24. If a line of code is used to realize the summation of 1 ~ 100

```sum(range(1,101))
```

25. Assuming that the value of the list object alit is [3, 4, 5, 6, 7, 9, 11, 13, 15, 17], the value obtained by slicing alit [3:7] is

```[6, 7, 9, 11]
```

(2) Judgment question

1. If a loop with else clause exits because a break statement is executed, the code in the else clause will be executed.

```wrong
```

2. Use the remove() method of the list object to delete the specified element that appears for the first time in the list. If there is no specified element to delete in the column, an exception will be thrown.

```yes
```

3. Variable sets include: list and dictionary. Immutable sets are: numbers, strings, tuples.

```wrong
```

4. When defining a Python function, if there is no return statement in the function, the null value None is returned by default.

```yes
```

5. = = the contents are equal, and is compares the memory addresses.

```yes
```

(3) Programming problem

1. Chicken and rabbit in the same cage. Assuming that there are 30 chickens and rabbits and 90 feet, ask how many chickens and rabbits there are respectively.

```for i in range(1,31):
j=30-i
if i*2+j*4 == 90:
break

print("Chicken has{0}Rabbits have{1}only".format(i,j))
```

Results: there were 15 chickens and 15 rabbits

2. Write a program to calculate the problem of buying 100 chickens for 100 money. Suppose a rooster is 5 yuan, a hen is 3 yuan and a chick is 1 yuan. Now there is 100 yuan. If you want to buy 100 chickens, how many ways do you buy them?

```for x in range(1,101):
for y in range(1,101):
z = 100-5*x-3*y
z1 = 3*z
if x+y+z1==100:
print(x,y,z1)
```

Operation results:

```4 18 78
8 11 81
12 4 84
```

So there are three.

3. Judge the number of primes between 101-200 and output all primes.

```def isPrime(n):
if n <= 1:
return False
i = 2
while i*i <= n:
if n % i == 0:
return False
i += 1
return True

num = 0
for i in range(1,101):
if isPrime(i):
num+=1
print(num)
```

Operation result: 25

4. Title: the bonus paid by the enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increased by 10%;
If the profit is higher than 100000 yuan and lower than 200000 yuan, the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan will be deducted by 10%,
Cocoa Commission 7.5%; Between 200000 and 400000 yuan, the part higher than 200000 yuan can be deducted by 5%;
3% commission can be given for the part higher than 400000 yuan between 400000 and 600000 yuan; Between 600000 and 1 million,
1.5% for the part higher than 600000 yuan, and 1% for the part higher than 1 million yuan,
Enter the profit I of the current month from the keyboard to find the total amount of bonus to be paid?

```lirun = float(input("The profit of the current month is:"))
jiangjin = 0.0
if lirun <= 100000:
jiangjin = lirun*0.1

if 100000<lirun<=200000:
jiangjin = 100000*0.1+(lirun-100000)*0.075

if 200000<lirun<=400000:
jiangjin = 100000*0.1+100000*0.075+(lirun-200000)*0.05

if 400000<lirun<=600000:
jiangjin = 100000*0.1+100000*0.075+200000*0.05+(lirun-400000)*0.03

if 600000<lirun<=1000000:
jiangjin = 100000*0.1+100000*0.075+200000*0.05+200000*0.03+(lirun-600000)*0.015

if lirun > 1000000:
jiangjin = 100000*0.1+100000*0.075+200000*0.05+200000*0.03+400000*0.015+(lirun-1000000)*0.01

print("The bonus is:",jiangjin)
```

Tags: Python

Posted on Tue, 05 Oct 2021 21:15:21 -0400 by spawnark