Вы находитесь на странице: 1из 4

fFactorial can be implemented as the recursive function (similar to mathematical definition) BUT also as

a looped function.
Multiple assignment are simultaneous- simultaneously updated. All the right side is the new values, all
of the left side is the old values.

These variables are local: only created while function runs, after that (outside of function) they dont
exist local scope variables.

**Dont have to use multiple assignments. Some languages dont have this capability, do them
individually.
Must make arbitrary decisions about which way to begin the factorial multiplication. (n, n-1. OR 1*2.)
x=5
factorial(x)
x=5
actual parameter assigned to formal parameter
counter variable
accumulator variable: f initialized to value if the body of the loop is not executed at ALL
for/range statements: for i in range(n) range is 0 (n-1)
Python enforces strict indentation: visual appearance corresponds to programming, want to maximize
readability.
Instead of repeatedly subtracting 6 until the other # is less than 6, youre really just computing the
remainder of the division SO you can just use modulus
u,v=v,u%v v is always assigned to the smaller value
Question: How often is B executed when gcd(6,21) is called? 3
6%21 is 6
The first time it is executed, the values are simply swapped. Then, it continues the exact same as
before.
Symbols are overloaded. Eg. Times Is both multiplication plus replication on strings. Eg. Plus is addition
and concatenation.
The meaning of the * is not determined when it is programmed: its meaning is resolved dynamically
when the program runs.

Frequently we need to convert from one data type to another. Must make sense eg. cant convert string
to integer if its not a number.
str()
int()
float()
Figure out how an object is interpreted by type()
tuple('34')
>>>('3', '4')
2*2.14
Python converted 2 to a float automatically, then multiplied them.
12345678901234567890*2
This gives the correct result
int(12345678901234567890*2.0)
Integers use as much memory as is available, but floating point numbers have limited amount of
memory. Converting from floating point to integer, there is a loss of precision (everything after dot is cut
off). Converting from integer to floating point also creates a loss of precision.
Homers method can be used for both base 10 and binary numbers.
i is the counter variable, n is the accumulator.
def binarytoInt(b:'string of 0s and 1s'):
n,i=0,0
while i<len(b):
n,i=2*n+int(b[i]),i+1
return n
def DectoInt(b:number):
n,i=0,0
while i<len(b):
n,i=10*n+int(b[i]),i+1
return n
this is a string to integer converter.

Can also program the converter by using modulo.

In python cannot combine string elements, but can concatenate the string.
return specifies the return value of function AND jumps to the end of function, end the function.

52//2=26 52%2=0
26//2=13

26%2=0

13//2=6

13%2=1

6//2=3

6%2=0

3//2=1 3%2=1
1//2=0 1%2=1
So 52 is 110100 (32, 8, 4, 2, 1)
Can use the same method for any base.

Linear search: going through each object one by one. Half open intervals exclude upper end but
include lower end.

Index is a position in any sequence/array, in this case a string.


i-clicker: if you switch the two operators, it will reach the undefined expression before it reaches the
false expression (the index goes up to the last index + 1, which means s[i]!=n is undefined! Python
checks expressions from left to right.
B and C = C if B else False
B or C=True if B else C
They are equivalent to mathematical and and or only if the operands are defined
Mathematical and and or is commutative, while computational and and or is not!
So the order of the operands MATTERS.

For Loop - SHORTCUT


def n_count(seq):
x=0
for c in seq:

if c==n:x=x+1
return x
Range Objects- SHORTCUT
range(a,b) is a sequence of integers from a to b, including a and excluding b.

>>> range(10)
range(0, 10)

>>> list(range(3,7))
[3, 4, 5, 6]

Using the statement in for loop will iterate over all values/elements in the range.

Вам также может понравиться