Day01

  • Introduction to Python: Functions, Variables...
# ...

# n1 = 10
# n2 = 20
# 
# n3 = -9
# 
# 
# 
# n4 = 100
# if n4 == 200 or 150: # incorrect
#     print(True)
# else:
#     print(False)
# 
# n4 = 100
# if n4 == 200 or n4 == 150:
#     print(True)
# else:
#     print(False)
# 

def divide(num1, num2):
    return num1 / num2

def get_score_grade(num1, num2):
    total_score = num1 + num2
    if total_score >= 50:
        grade = "Pass"
    else:
        grade = "Fail"
    return total_score, grade

def main():
    # about_type()
    # about_immutable_value()
    # about_boolean_expr()
    # print(divide(10, 20))
    # print(divide(20, 10))
    # print(divide(num220, num1=10))
    # print(divide(10, 20))
    score, grade = get_score_grade(40, 30)
    print(score, grade)
    result = get_score_grade(60, 40)
    print(type(result), result)
    
    # v1, v2, v3 = get_score_grade(60, 40) # Error
    
    do_work = divide
    print(type(do_work))
    print(do_work(10, 20)) # Calling divide with 10, 20

    print("End of main")

main()

Day02

  • Introduction to Python: Lists, Dictionaries, List Comprehensions...
def about_del():
    list1 = [1, 2, 4, 5, 6]
    del list1[0]
    print(list1)
    # Delete by indicating the value 
    list1.remove(5) # 5 is the value, not index
    print(list1)
    # pop is similar to remove
    value = list1.pop(0) # returns the value and remove it
    print(value)
    print(list1)

def about_list_del():
    list1 = [1, -2, -4, 5, -2, 10]
    # Use the loop to go through the list and delete all negative numbers
    for idx in range(len(list1)):
        if list1[idx] < 0:
            del list1[idx]
            print(idx, list1)
    # After the loop
    print(list1)

def about_list_del_backward():
    list1 = [1, -2, -4, 5, -2, 10]
    last = len(list1) - 1
    for idx in range(last, - 1, -1):
        if list1[idx] < 0:
            del list1[idx]
            print(idx, list1)
    # After the loop
    print(list1)

def about_list_del_2loops():
    list1 = [1, -2, -4, 5, -2, 10]
    to_be_deleted = []
    for idx in range(len(list1)):
        if list1[idx] < 0:
            to_be_deleted.append(list1[idx])
    print(to_be_deleted)
    for idx in range(len(to_be_deleted)):
        list1.remove(to_be_deleted[idx])
    print(list1)

def about_list_comprehension():
    inputStr = "12,23,45,78,34"
    numbers = []
    values = inputStr.split(",")
    # Convert each value inside values to int and save to numbers
    for v in values:
        numbers.append(int(v))
    print(numbers)
    # Apply list comprehension to get the same result
    numbers2 = [int(v) for v in values]
    print(numbers2)

    numbers3 = [int(v) for v in inputStr.split(",")]
    print(numbers3)

def about_list_comprehension2():
    raw_data = [12, 45, 23, 123, 132, 20, 98, 101]
    valid_data = []
    for each_value in raw_data:
        if each_value <= 100:
            valid_data.append(each_value)
    print(valid_data)

    valid_data2 = [value for value in raw_data if value <= 100]
    print(valid_data2)

def about_sorting():
    list1 = [12, 34, 9, 56, 23]
    list1.sort()
    print(list1)
    list1.sort(reverse=True)
    print(list1)
    list2 = [["Jurong", 40], ["Tampines", 67], ["Climenti", 55]]
    list2.sort()
    print(list2)
    list2.sort(key = sort_by_value)
    print(list2)
    list2.sort(key = sort_by_last)
    print(list2)

def sort_by_value(element):
    print(element)
    return element[1]

def sort_by_last(sub_list):
    return sub_list[2]

def about_dictionary():
    visitor = {} # Empty dictionary
    visitor["North"] = [700, 200]
    visitor["South"] = [400, 250]
    visitor["East"] = [200, 100]
    visitor["West"] = [100, 20]
    # Commonly used methods
    print("Using visitor.keys()")
    for region in visitor.keys():
        print(region)
        print(visitor[region])

    print("Using visitor.values()")
    for v_count in visitor.values():
        print(v_count)

    print("Using visitor.items()")
    for region, count in visitor.items():
        print(region, count)
    
    # Calculate the % of member visit in each region
    member_visit = []
    for region, count in visitor.items():
        member_percentage = count[1]/count[0] * 100
        member_visit.append([region, member_percentage])
    # After the loop
    for value in member_visit:
        print(value)
    
    # Use list comprehension to get the same result
    member_visit2 = [[loc, count[1]/count[0]*100] for loc, count in visitor.items()]
    for value in member_visit2:
        print(value)

def main():
    # about_del()
    # about_list_del()
    # about_list_del_backward()
    # about_list_del_2loops()
    # about_list_comprehension()
    # about_list_comprehension2()
    # about_sorting()
    about_dictionary()
    print("End of main")

main()

评论已关闭