Quick actions

cmd+k|ctrl+k

Navigation

Languages

5-May-2025 Class 7-After Self-Testing-2

Snippet info

Language

Python

Visibility

public

Author

michaelluiwaihung

Created

2026-05-05T07:55:17.143472Z

Updated

2026-05-14T07:04:46.607088Z

######################### temmp 14 ###############################################
print("*********************** temp 13 & temp 14 ******************************")
for i in range(1, 20, 4):
    for j in range(5):
        print(f"j : {j}, i: {i}")
    #if i == 13:
    #   break

try:
    x = 1
    y = 1
    if x == 1: 
       raise
    result = x/y
    print(result)
except ZeroDivisionError:
    print("second operand cannot be zero")
except TypeError as msg:
    print(msg)
except :
    print("except error")

# TypeError ZeroDivisionError
else : 
    print("no error")
finally:
    print("This line will run no matter what")

print("this line will run also")
######################### temmp 14 ###############################################

#reference from another people / classmates
def a(x):
    def b(y):          
        return x + y       
    return b         
def c(f,g):
    return f(g)    # return an address with a function together with a closure
add10 = a(10)
add20 = a(20)
print(a(10), a(20))
print(add10(5), add20(5))
print(a(10)(5),a(20)(5))   
print(c(add10, 10))
print(c(add20, 10))
#reference from another people / classmates

print("#################### temp 8 ####################")
#temp 8, for tuple
a = 1,2,3,4,9,10,11,12,5,6,6,8,13,14,15
print(a.count(0),a.index(6))
print(a[0])
#a[0]=10
#a.sort()
print(a)
b = sorted(a,reverse=True)
print(a)
print(b, type(b))

b = 1,
print(b, type(b))
b = ()
print(b, type(b))

a = {1,2,"a",3,4,"",9,"",10,"b",11,12,5,6,"a",6,8,13,14,15}
print(type(a))
print(a)
print("ggg")
a = [1,2,"a",3,4,"b",9,10,"b",11,12,5,6,"a",6,8,13,14,15]
#a = set(a)
#print(type(a))
#a = list(a)
print(a)
a = list(set(a))
print(a)
a = {1,2,"a",3,4,"",9,10,"b",11,12,5,6,"a",6,8,13,14,15}
a.add(16)
print(a)
a.update(a, {17, 18})
print(a)

a = {1,2,3,4}
b = {3,4,5,6}
print(a | b)
print(a & b)
print(a - b)
print(a ^ b)


print("#################### temp 9 ####################")
#temp 9
print("Hello \
World!")

print('''Hello
Wor
ld!''')

a = (1, 
    1+1  )
print(a, type(a))
if 1:
    x = 1
    y=1

print("#################### temp 10 ####################")    
#temp 10
name = 'name'
d = {'name' : "john", # 'name' = "john"
     "age": 10+1,
}
print(d['name'])
print(d['age'])
name = 'age'
print(d['name'],d['age'])    
print(d[name])

d = {"name" : "john", 
     "age": 10+1,
     True : 12,
     (1,2): 30,
}
name = 'age'
print(d[name])
print(d[(1,2)], d[True], d)

#################################### temp 11 ####################################
print("##################### temp 11 ************************************")
c = a = [1,2,3]
b = [1,2,3]
print(id(a), id(c), id(b))
print(id(a[0]), id(c[0]), id(b[0]))

d = {"name" : "john",
     "age": 10,
     "gender" : "male"
     }
del d["gender"]
print(d)
d["name"] = "peter"
print(d)
d["mame"] = "prince building"
print(d)
print(d.keys())
for i in d.keys():
    print(i)
print(d.values())
for i in d.values():
    print(i)
print(d.items())
#################################### temp 11 ####################################

#################################### temp 12-13 ####################################
print("#################### temp 12-13 ####################")
#temp 12
#NO 1
result = 7 - 4 + 3 * 6 / 2 ** 2 % 5
print ('01', result)
result = ((7 - 4) + (((3 * 6) / (2 ** 2)) % 5))
print ('01', result)
#NO 2
result = 12 - 2 * 5 > 3 and 8 % 5 == 3 or not 4 + 1 > 7
print ('02', result)
result = (((12 - (2 * 5)) > 3) and ((8 % 5) == 3)) or (not ((4 + 1) > 7))
print ('02', result)

def a(x, y, z=2):
    result = x + y + z
    print(f"result = {result}")
    print("x =", x, "y =", y, "z =", z)
    print(f"x = {x} y = {y} z = {z}")
    return result

print(a(3, 4))
result = a(y=4,z=5,x=6) + a(7,8,9)
print(result)
print(globals())
print(a('a', 'b', 'c'))
print(a("john"," ", "lee"))
#################################### temp 12-13 ####################################
INFO