Python second experiment

A job

A. Short answer questions (a total of five questions, 100.0 points)
1

A plurality of x = 6+ 8j known Please write its mold, the real part and the imaginary part of the complex conjugate of the command, and write the results.

correct answer:

my answer:

x = 6 + 8j

print(abs(x))

print(x.real)

print(x.imag)

print(x.conjugate())

result:

10.0

6.0

8.0

(6-8j)

2

Known that a = [97, 98, 99,100], b = (97, 98, 99,100), c = { 'x': 97, 'y': 98, 'z': 99, 'q': 100}, d = {97, 98, 99,100}, is a [1], b [1], c [1], d [1] and c [ 'q']) operating results What are they? And explain the reasons.

correct answer:

my answer:

a= [97, 98, 99,100]

print(a[1])

b = (97, 98, 99,100)

print(b[1])

operation result:

98

98

The reason: a, is a list, b is a tuple. Lists, tuples supporting elements subscripting specified position, are numbered from 0, a [1], b [1] is the second output element, the output is 98, 98.

c = {‘x’:97, ‘y’:98, ‘z’:99,‘q’:100}

print(c[1])

operation result:

print(c[1])

KeyError: 1

The reason: c is a dictionary, dictionaries support subscript is key. Elemental form dictionary is "key: value." Error thrown when a keyword, which means using the keyword (key) mapping does not exist: the key is not in c 1, the output KeyError.

d = {97, 98, 99,100}

print(d[1])

operation result:

print(d[1])

TypeError: ‘set’ object is not subscriptable

Because:

d is a collection of the collection does not support the index, so TypeError, the type of error. A collection of commonly used in, not in determining whether the elements in the collection.

c = {‘x’:97, ‘y’:98, ‘z’:99,‘q’:100}

print (c [q])

Operating results are:

100

c [ 'q'] c, can be found in a dictionary key 'q', which corresponds to the value 100, the output is 100.

3
Run the following command to write the results and explain the reasons.

2 or 3

2 and 3

2 and 3 and True

0 and 3 and 1

0 and 3 or 1

0 and 3 or 1 or 4

0 or False and 1

correct answer:

my answer:

Operating results are:

2

3

True

0

1

1

False

Because:

Priority: ()> not> and> or, operators and, or do not necessarily return true or false, but rather to give a final calculated value of the expression.

  在python中逻辑运算符or,x or y ,如果 x or y ,x为true则返回x,如果x 为false返回y值。因为一个为真则为真,所以返回x的值,如果x的值为假,那么or运算的结果取决于y,所以返回y值。逻辑运算符and,x and y ,若x为true则返回y值,若x为false则返回x值。因为x为true,and的运算不会结束,会继续看y的值,所以此时真假取决于y的值,所以x为真则返回y的值。如果x为假,那么and运算就结束运算过程,因为一个为假则为假,所以返回x的值。

2 or 3: 2,3 is true, or: a true calculation result is True, it returns to the first operation result true value of 2

2 and 3: 2,3 is true, and: two true value is True, the second number of the calculation result being true 3

2 and 3 and True: 2,3, True are true, 2 and 3 the operation result is 3; continue operation, 3 and true, the operation result is true

0 and 3 and 1: 0 is false, true 3, 3 0 and the operation result is 0; 0 and 1,0 is false, the end of the operation, the result is 0 (0 for any result of a logical AND operation is 0)

0 and 3 or 1: 0 and 3 the operation result is 0; 0 is false, 1 is true, the return value is 0 or 1 1 (1 ORed result with any number of 1)

0 and 3 or 1 or 4: 0 is false, 0 and 3 0 returns true value, 0 or 1, 1 is the return value is true, 1 or 4 end of the operation returns a value of 1

0 or False and 1: and superior operational level or, the first operation returns a value of False and 1 False (and: a false, false), then computing 0 or False, 0 is false, the operation result is False (or : two false and false, operation results for the second false number).

4
writing small programs, enter three sides of a triangle, the output of its area, and write the results.

correct answer:

my answer:

x = int (input ( "Enter a triangle side x:"))

y = int (input ( "Enter a side of the triangle y:"))

z = int (input ( "Enter a side of the triangle z:"))

c=(x+y+z)/2

s=(c*(c-x)(c-y)(c-z)) ** 0.5

print ( 'triangle area% 0.2f'% s)

result:

Please enter one side of the triangle x: 5

Please enter one side of the triangle y: 6

Please enter one side of the triangle z: 8

Triangle area of ​​14.98

5
writing small programs, two sets setA input and set B, respectively, the output of their intersection, union, symmetric difference and difference, and write the results.

correct answer:

my answer:

Arrows = {1,2,3,4}

setB={3,4,6,8}

a=((setA) & (setB))

b = ((silk) | (SETB))

c = ((silk) ^ (SETB))

d = ((SETA) - (SETB))

e = ((setB) - (arrow))

print ( "Intersection:% s"% a)

print ( "union:% s"% b)

print ( "symmetric difference:% s"% c)

print ( "A and B set difference:% s"% d)

print ( "B and A difference set:% s"% e)

The results are:

Intersection: {3, 4}

And set: {1, 2, 3, 4, 6, 8}

Symmetric difference: {1, 2, 6, 8}

The difference between A and B set: {1, 2}

B and A difference set: {8, 6}

Released nine original articles · won praise 2 · views 70

Guess you like

Origin blog.csdn.net/weixin_41860600/article/details/104822738