# 20200409 Python ###### tags: `Learning Python`、`課程` ## Q1. Hello Word ### Ans * Team 2 ## Q2. Variables and Types ```python # change this code mystring = "hello" myfloat = 10.0 myint = 20 ``` # testing code ```python if mystring == "hello": print("String: %s" % mystring) if isinstance(myfloat, float) and myfloat == 10.0: print("Float: %f" % myfloat) if isinstance(myint, int) and myint == 20: print("Integer: %d" % myint) ``` ## Q3. Lists ```python numbers = [1, 2, 3] strings = ['hello', 'world'] names = ["John", "Eric", "Jessica"] # write your code here second_name = names[1] ``` ``` ``` ## Q4. Basic Operators ```python x = object() y = object() # TODO: change this code x_list = [x]*10 y_list = [y]*10 big_list = x_list+y_list print("x_list contains %d objects" % len(x_list)) print("y_list contains %d objects" % len(y_list)) print("big_list contains %d objects" % len(big_list)) # testing code if x_list.count(x) == 10 and y_list.count(y) == 10: print("Almost there...") if big_list.count(x) == 10 and big_list.count(y) == 10: print("Great!") ``` ## Q5. String Formatting ```python data = ("John", "Doe", 53.44) format_string = "Hello" print("%s %s %s. Your current balance is $%.4f." %(format_string,data[0],data[1],data[2])) ``` ## Q6. Condition ```python # change this code number = 16 second_number = False first_array = [1, 2, 3] second_array = [1,2] if number > 15: print("1") if first_array: print("2") if len(second_array) == 2: print("3") if len(first_array) + len(second_array) == 5: print("4") if first_array and first_array[0] == 1: print("5") if not second_number: print("6") ``` ## Q7. Loops ```python numbers = [ 951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 743, 527 ] # your code goes here for number in numbers: if(number % 2 == 0): print(number) if(number == 237): break for number in numbers: if number % 2 == 0: print(number) else: if number != 237: continue else: break ``` ## Q8.Numpy ```python weight_kg = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45] import numpy as np np_weight_kg = np.array(weight_kg) np_weight_lbs = np.array(np_weight_kg) print(np_weight_lbs) ### Create a numpy array np_weight_kg from weight_kg np_weight_kg = np.array(weight_kg) ### Create np_weight_lbs from np_weight_kg np_weight_lbs = np_weight_kg * 2.2 ### Print out np_weight_lbs print (np_weight_lbs) ``` ## Q9.Https Request ```python -- skip -- ``` ## Q10.Pandas ```python https://docs.google.com/spreadsheets/d/1dKZoIzBgE30PBUoXBWs6Lj2mgg7yjYx0I5d2k8GHDiw/ # Import cars data import pandas as pd # Import the currency.csv data: currency url = 'https://docs.google.com/spreadsheets/d/1dKZoIzBgE30PBUoXBWs6Lj2mgg7yjYx0I5d2k8GHDiw/export?format=csv' infected = pd.read_csv(url,index_col=0) # Print out observations for Australia and Egypt way1. print(infected.tail(1)) way2. print(infected.loc['4/8/2020']) ```