Fundamentals of Python Programming [Part 3]
Fundamentals of Python Programming [Part 3]
>>> type(3)
<class 'int'>
>>> type(3.1)
<class 'float'>
>>> type(3.0)
<class 'float'>
>>> myInt = 32
>>> type(myInt)
<class 'int'>
>>>
y = 3.44 + 6
b b 2 4ac
x
2a
import math
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
Output:
fact = 1
for factor in [6, 5, 4, 3, 2, 1]:
fact = fact * factor
fact = 1
for factor in [2, 3, 4, 5, 6]:
fact = fact * factor
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5,10))
[5, 6, 7, 8, 9]
>>> list(range(5,10,2))
[5, 7, 9]
# factorial.py
# Program to compute the factorial of a number
Example: 00000000000000000000000000111001
Sign is positive (+1)
+1∗(20 +23 +24 +25 ) = +1∗(1+8+16+32)=57
Example: 10000000000001000000100100000000
Sign is negative (-1)
−1∗(28 +211 +218 ) = −1∗(256+2048+262144)=−264448
Example: 00000000000000000000000000111001
20 +23 +24 +25 = 1+8+16+32=57
Example: 10000000000001000000100100000000
28 +211 +218 +231 = 256+2048+262144+2147483648
=2147748096
For 3 bits:
• 110+111=101, you carry a 1
• it returns 101 but actually is 1101
>>>0.3-0.2
0.09999999999999998
So for practical purposes this is usually not an issue.
The limitation on numerical accuracy rarely has an
effect on stored values
because it is very hard to obtain a scientific measurement with
this level of precision.
index … 3 2 1 0 . -1 -2 -3 …
Value … 103 102 101 100 . 10-1 10-2 10-3 …
base10
*
digit … 0 0 2 4 . 5 8 9 …
number … 0 + 0 + 2*101 + 4*100 . 5*10-1 + 8*10-2 + 9*10-3 …
index … 3 2 1 0 . -1 -2 -3 …
Value … 23 22 21 20 . 2-1 2-2 2-3 …
base2
*
digit … 0 1 0 1 . 0 0 1 …
number … 0 + 1*22 + 0 + 1*20 . 0 + 0 + 1*2-3 …
Scientific notation
(923.52)10 = 9.2352 * 102
(101011.101)2 = 1.01011101 * 25 #43.625
1.xxxxxx * 2y