Quiz and Hands On 1

Quiz

Please answer the value and its type of objects. If the object is Function, answer "Function". If Error is expected answer answer why.


In [ ]:
def show_val_type(obj):

    if callable(obj):
        print('Function')
        return
    
    print(obj, type(obj))
    return

def dummy_func():
    pass

def x2(val):
    return val * 2

def add2(a, b):
    val = a + b
    return val

In [ ]:
# Exercise

show_val_type(1)
show_val_type(3.14)
show_val_type('123'*2)

In [ ]:
# Please think before execution 1
show_val_type(4/2)
show_val_type(5//2)
show_val_type(dummy_func)
show_val_type(dummy_func())
print()

show_val_type(x2(10))
show_val_type(x2('abc'))
show_val_type(add2(10, 20))
show_val_type(add2('abc', 'xyz'))
show_val_type(add2(0.5, 1))
show_val_type(add2('1', 1))

In [ ]:
# Please think before execution 2
show_val_type(show_val_type)
show_val_type(int(3.14))
show_val_type(str(2**10))
show_val_type(str(2**10)[3])
show_val_type(str(2**10)[1:3])
show_val_type(str(2**10)[4])

In [ ]:
# Please think before execution 3
s = 'abcdefghij'
show_val_type(s.capitalize())
show_val_type(s.capitalize)
f = s.capitalize
f()
show_val_type(s.upper())
show_val_type(s[:5])
show_val_type(s[5:])
show_val_type(s[-5:-1])

In [ ]:
# Please think before execution 4
s = '123 456 78'
show_val_type(s)
show_val_type(s[2])
show_val_type(int(s[5:8]))  # tricky, verify the behavior of int()
x = s.split()
show_val_type(x)
show_val_type(x[1])
show_val_type(int(x[0]*3))
show_val_type(x[-1] + 1)

Hands on

Please create some objects and check their value and type. As for the method of String, help(str) in the interactive shell will show the summary.


In [ ]:
help(str)

In [ ]:
# Define variables and objectys for next cell
t = (1,2,3)
u = ['a', 45, 1.234, t]
w = {'tupple': t, 'list': u, 1: 3.14, 3.14: 2**8, 256: '2**8'}

# operators for SET are  & | - ^  < <=  > >=
set1 = {2, 3, 5, 7}
set2 = {5, 7, 11,13, 17, 19}

In [ ]:
# Execute after above cell is evaluated
# IPythn is good at auto-completion, type "show" and Tab key will expand to "show_val_type"
show_val_type(w[3.14])
show_val_type(set1 ^ set2)
show_val_type(set1 | set2 > set2)
u[2] = 'replaced'
show_val_type (w['list'])