Ch. 3 Flashcards
(31 cards)
the value 4! is
factorial of 4, 4 × 3 × 2 × 1 = 24.
4.0 / 10.0 + 3.5 * 2
- 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
- and - from left to right.
10 % 4 + 6 / 2
- modulo %
- / 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
abs (4 - 20 // 3) ** 3
- integer division //
- from left to right
- absolute value, abs()
- 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
sqrt(4.5 - 5.0) + 7 * 3
- subtract 4.5 and 5.0, equals -0.5
sqrt(-0.5) + 7 * 3 - multipy 7 and 3
sqrt(-0.5) + 21 - sqrt(-0.5) equals error?
3 * 10 // 3 + 10 % 3
- multiply first
30 // 3 + 10 % 3 - divide
10 + 10 % 3 - modulo, remainder of 10 divided by 3 is 1
10 + 1 - add
11
3 ** 3
- 3 *3 = 9
- 9 * 3= 27
(3 + 5)(5)
(3 + 5) * 5
n(n - 1)/2
n * (n -1) / 2
range(5)
0,1,2,3,4
range(3, 10)
3,4,5,6,7,8,9
range(4, 13, 3)
4,7,10
start at 4, with increment of 3, and stop but not include 13
range(15, 5, -2)
15,13,11,9,7
start at 15, with increment of -2, and stop but not include 5
range(5,3)
error bc 5 is greater than 3
for i in range(1,11):
print (i*i)
range is 1,2,3,4,5,6,7,8,9,10
1
4
9
16
25
36
49
64
81
100
for i in [1,3,5,7,9]:
print(i, “:”, i**3)
print(i)
1:1
3:27
5:125
7:343
9:729
9
x=2
y=10
for j in range(0,y,x):
print(j, end=” “)
print(x+y)
print(“done)
0 12
2 12
4 12
6 12
8 12
done
ans=0
for i in range(1,11):
ans = ans + i*i
print(i)
print(ans)
1
2
3
4
5
6
7
8
9
10
385
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))
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
-10 // 3
- -10 // 3 equals to -3.3333
- round to lower number
output: -4
-10 % 3
- a % b = a - (a // b) * b
-10 - (10 // 3) * 3 - -10 - (-4) * 3
- -10 - (-12)
- -10 + 12
- output is 2
10 // -3
- 10 // -3 equals to -3.3333
- round to lower number
output: -4
10 % -3
- a % b = a - (a // b) * b
10 - (10 // -3) * -3 - 10 - (-4) * -3
- 10 - (12)
- 10 - 12
- output is -2
-10 // -3
- -10 divided by -3 is 3.333
- round to lowest int
outcome: -4