We talked about binary numbers (with some theory) and base conversions. We have also impletemented an algorithm for base conversion, examined python's memory model and discussed in-place functions. Finally, we have (or will next week) analyzed the efficiency of using extend() vs. creating a new list with another element.
Before you click "Visualize Execution" button, you may want to use the following settings (can be adjusted via the drop boxes next to the textbox):
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
bin(12)
bin(12)[2:]
hex(22)
int("1010", 2) # first param is the representation in base b and the second is the base b itself
int("0b1010", 2)
int("1010")
int("0x1010", 16)
# int("0x1010", 2) # will throw an error: the prefix 0x does not go with base 2
# int(1010, 2) # will throw an error: first param has to be a string
'0b1100'
'1100'
'0x16'
10
10
1010
4112
def convert_base(n,b):
assert 2 <= b <= 36
result = ""
while n > 0:
digit = n % b
n = n//b
result = str(digit) + result
return result
convert_base(10,2)
convert_base(2,2)
convert_base(10,16)
'1010'
'10'
'10'
convert_base(0,2)
convert_base(10,16)
''
'10'
def convert_base(n,b):
assert 2 <= b <= 36
if n ==0: # fix for n = 0
return "0"
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
result = ""
while n > 0:
digit = n % b
n = n//b
# result = str(digit) + result
result = alphabet[digit] + result
return result
convert_base(10,16)
convert_base(22,16)
convert_base(0,2)
'a'
'16'
'0'
convert_base(1, 37)
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-8-d8fb5c70d025> in <module>() ----> 1 convert_base(1, 37) <ipython-input-7-3a734fbc7a54> in convert_base(n, b) 1 def convert_base(n,b): ----> 2 assert 2 <= b <= 36 3 4 if n ==0: 5 return "0" AssertionError:
def change_int (n):
n = 999
n = 10
change_int(10)
n
10
def add_to_lst(l):
l.append(999)
lst = [1,2,3]
add_to_lst(lst)
lst
[1, 2, 3, 999]
def change_lst(l):
l = [99,999,999]
lst = [1,2,3]
change_lst(lst)
lst
[1, 2, 3]
#1 basics #####################################################
a = 1000 #1
b = "hello" #2
a += len(b) #3
c = 2*b[:2] #4
if b!=c: #5
c = b #6
del c #7
#2 lists #####################################################
a = 1000 #1
d = [a,2] #2
d[1] = -1 #3
a = 1003 #4
for x in d: #5a
x = 7 #5b
#3 funcs #####################################################
a = 1000 #1
b = "hello" #2
def is_palindrom(a): #3a
b = a[::-1] #3b
return a==b #3c
is_pal = is_palindrom #4
x = is_pal(b) #5
#4 lists+funcs #####################################################
a = 1000 #1
d = [a,2] #2
def f(a,d): #3a
a = 2000 #3b
d[0] = a #3c
d = [] #3d
return d #3e
x = f(a,d) #4
#5 – lists operators and methods #####################################################
lst = [3,2,1]
id(lst)
lst = lst+[0]
id(lst)
lst.append(4) # in-place
id(lst)
lst.insert(1,5) # in-place
id(lst)
lst.extend([6,7]) # in-place
id(lst)
lst += [8] # in-place (invokes extend)
id(lst)
11034760
10849864
10849864
10849864
10849864
10849864
lst.sort()
id(lst)
lst2 = sorted(lst)
id(lst2)
10849864
10850184
import time
n = 10000
lst = []
t0 = time.clock()
for i in range(n):
lst = lst + [i]
t1 = time.clock()
print("time without extend: ", t1 - t0)
lst2 = []
t0 = time.clock()
for i in range(n):
lst2 += [i]
t1 = time.clock()
print("time with extend: ", t1 - t0)
time without extend: 0.19844970849135146 time with extend: 0.0023136734905757628
n = 20000
lst = []
t0 = time.clock()
for i in range(n):
lst = lst + [i]
t1 = time.clock()
print("time without extend: ", t1 - t0)
lst2 = []
t0 = time.clock()
for i in range(n):
lst2 += [i]
t1 = time.clock()
print("time with extend: ", t1 - t0)
time without extend: 0.8519414764165951 time with extend: 0.004375096962302649