Ch. 3 Flashcards

1
Q

the value 4! is

A

factorial of 4, 4 × 3 × 2 × 1 = 24.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

4.0 / 10.0 + 3.5 * 2

A
    • and / from left to right.
    • and - from left to right.
      a) first divide 4.0 and 10.0, which equals 0.4
      0.4 + 3.5 * 2
      b) multiply 3.5 and 2, which equals 7.0
      0.4 + 7.0
      c) add, equation equals 7.4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

10 % 4 + 6 / 2

A
  1. modulo %
  2. / and + from left to right
    a) 10 % 4 equals 2 because % calculates remainder
    2 + 6 / 2
    b) divide 6 / 2, equals 3
    2 + 3
    c) add, equation equals 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

abs (4 - 20 // 3) ** 3

A
  1. integer division //
    • from left to right
  2. absolute value, abs()
  3. exponent **
    a) 20 // 3 equals 6 because // 20 divided by 3 is 6.6667, so round down to get 6
    abs(4 - 6) ** 3
    b) subtract 4 and 6, equals -2
    abs(-2) ** 3
    c) abs makes neg to pos, abs(-2) equals 2
    2 ** 3
    d) 2 ** 3 equals 8 because 2 * 2 * 2 = 8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

sqrt(4.5 - 5.0) + 7 * 3

A
  1. subtract 4.5 and 5.0, equals -0.5
    sqrt(-0.5) + 7 * 3
  2. multipy 7 and 3
    sqrt(-0.5) + 21
  3. sqrt(-0.5) equals error?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

3 * 10 // 3 + 10 % 3

A
  1. multiply first
    30 // 3 + 10 % 3
  2. divide
    10 + 10 % 3
  3. modulo, remainder of 10 divided by 3 is 1
    10 + 1
  4. add
    11
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

3 ** 3

A
  1. 3 *3 = 9
  2. 9 * 3= 27
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(3 + 5)(5)

A

(3 + 5) * 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

n(n - 1)/2

A

n * (n -1) / 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

range(5)

A

0,1,2,3,4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

range(3, 10)

A

3,4,5,6,7,8,9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

range(4, 13, 3)

A

4,7,10
start at 4, with increment of 3, and stop but not include 13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

range(15, 5, -2)

A

15,13,11,9,7
start at 15, with increment of -2, and stop but not include 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

range(5,3)

A

error bc 5 is greater than 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

for i in range(1,11):
print (i*i)

A

range is 1,2,3,4,5,6,7,8,9,10
1
4
9
16
25
36
49
64
81
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

for i in [1,3,5,7,9]:
print(i, “:”, i**3)
print(i)

A

1:1
3:27
5:125
7:343
9:729
9

17
Q

x=2
y=10
for j in range(0,y,x):
print(j, end=” “)
print(x+y)
print(“done)

A

0 12
2 12
4 12
6 12
8 12
done

18
Q

ans=0
for i in range(1,11):
ans = ans + i*i
print(i)
print(ans)

A

1
2
3
4
5
6
7
8
9
10
385

19
Q

what do you think will happen if you use a negative number in the second parameter of the round function?
Ex: print(round(314.159265, -1))

A

neg in second parameter is to round to the nearest 10, 100, 1000, etc. -1 means to round to the nearest 10th place.
output: 310
if it were -2, output would’ve been 300

20
Q

-10 // 3

A
  1. -10 // 3 equals to -3.3333
  2. round to lower number
    output: -4
21
Q

-10 % 3

A
  1. a % b = a - (a // b) * b
    -10 - (10 // 3) * 3
  2. -10 - (-4) * 3
  3. -10 - (-12)
  4. -10 + 12
  5. output is 2
22
Q

10 // -3

A
  1. 10 // -3 equals to -3.3333
  2. round to lower number
    output: -4
23
Q

10 % -3

A
  1. a % b = a - (a // b) * b
    10 - (10 // -3) * -3
  2. 10 - (-4) * -3
  3. 10 - (12)
  4. 10 - 12
  5. output is -2
24
Q

-10 // -3

A
  1. -10 divided by -3 is 3.333
  2. round to lowest int
    outcome: -4
25
Q

write a program to calculates the volume and surface area of a sphere from its radius, given as input. use given formula of volume and surface area

A

radius = float(input(“Enter the radius of the sphere: “))

pi = 3.14
volume = (4/3) * pi * radius ** 3
surface_area = 4 * pi * radius ** 2

print(“Volume of the sphere:”, volume)
print(“Surface area of the sphere:”, surface_area)

26
Q

write program that calculates the cost per square inch of a circular pizza, given its diameter and price. use formula for area

A

diameter=float(input(“enter the diameter of pizza:”))

pi = 3.14
sq_in = pi * (diameter / 2) **2

cost_per_in = sq_in * 0.60
print (“cost per square inch of pizza is”, cost_per_in)

27
Q

write a program that calculates the molecular weight of a carbohydrate. the program should prompt the user to enter the number to hydrogen atoms, the number of carbons atoms, and number of oxygen atoms. the program then prints the total combined molecular weight of all the atoms based on these individuals atom weight. weight of H = 1.00794; C = 12.0107; O = 15.9994

A

num_h = float(input(“enter number of hydrogen molecules: “))
num_c = float(input(“enter number of carbon molecules: “))
num_o = float(input(“enter number of oxygen molecules: “))

weight = (1.00794 * num_h) + (12.0107 * num_c) + (15.9994 * num_o)

print(“weight of carbohydrate molecule is”, weight)

28
Q

write a program that determines the distance to a lightning strike based on the time elapsed between the flash and the sound of thunder. the speed of sound is approximately 1100 ft/sec and 1 miles is 5280 ft.

A

time = float(input(“enter time elapsed between flash and thunder: “))

spe_sou = 1100 * time
dist = 5280 / spe_sou

print(“distance of lightning is”, dist, “ miles”)

29
Q

a coffee shop is selling coffee at $10.50 a pound plus the cost of shipping. each order ships for $0.86 per pound + $1.50 fixed cost for overhead. write a program that calculates the cost of an order

A

lbs = float(input(“enter pound of coffee”))

cof= (10.50 * lbs) + (.86 * lbs) + 1.50

print(“cost of coffee is”, cof)

30
Q

write a program to find the sum of the cubes of the first n natural numbers where the value of n is provided by the user

A

total = 0
n = int(input(“enter number: “))
for i in range(1, n + 1):
total += i ** 3
print(total)

31
Q
A