1 / 129

COMP4332/RMBI4310

COMP4332/RMBI4310. Python. Prepared by Raymond Wong Presented by Raymond Wong. Process of Data Analytics. There are the following processes in data analytics Data Collection Data Processing Data Mining (or Data Analytics) Result Presenting.

Télécharger la présentation

COMP4332/RMBI4310

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP4332/RMBI4310 Python Prepared by Raymond Wong Presented by Raymond Wong

  2. Process of Data Analytics • There are the following processes in data analytics • Data Collection • Data Processing • Data Mining (or Data Analytics) • Result Presenting

  3. In this course, we will use Python to complete most of the processes. • Thus, we will learn Python in this set of lecture notes.

  4. Overview • Python is a high-level programming language. • It is easy to learn. • It has a lot of supporting packages (e.g., Keras, TensorFlow and Scrapy). • It is very popular in both academic and industry.

  5. Overview • Python 2.7 will be faked out soon • Python 3.0 or a later version has many new features which are NOT backward-compatible.

  6. Overview • Some software like “Anaconda” includes Python and some other packages installed • We recommend you to install Python from scratch to experience how to start with the “native” Python • It is beneficial that we know how to start with the “native” Python

  7. Overview • Some examples of IDE (Integrated Development Environment) • Spyder • an IDE under the MIT license • You could write your Python code in the editor in Spyder easily • PyCharm • an IDE made by the folks at JetBrain, a team responsible for one of the most famous Java IDE, the IntelliJ IDEA • You could write your Python code in the editor in PyCharm easily

  8. Overview • Some examples of IDE (Integrated Development Environment) • jupyter notebook • An “interactive” IDE (i.e., when you type a line of code, you could see the result immediately) • Web-based application You could select which IDE to use. My recommendation: • PyCharm • jupyter notebook You could use other IDEs.

  9. Outline • Variable Type • Basic Operation • Conditional Operation • Iteration • Number Operation • String Operation • List Operation • Tuple Operation • Dictionary Operation • Date/Time Operation • Function Operation • Module Operation • File I/O Operation • Exception Operation • Class Operation

  10. 1. Variable Type • Type • Number • Boolean • Range • String • List • Tuple • Dictionary • Data Type Conversion • Number and String

  11. python Number Variables no1 = 15 no2 = 15.7 no3 = 2.14e5 print("No.1 : ", no1) print("No.2 : ", no2) print("No.3 : ", no3) Output No.1 : 15 No.2 : 15.7 No.3 : 214000.0

  12. python Boolean Variables bool1 = True bool2 = False print("bool1 : ", bool1) print("bool2 : ", bool2) Output bool1 : True bool2 : False

  13. python Range Variables range1 = range(5) range2 = range(1, 5) range3 = range(1, 5, 2) print("range(5): ", range1) print("range(1, 5) : ", range2) print("range(1, 5, 2) : ", range3) for index in range1: print(index) for index in range2: print(index) for index in range3: print(index) Print these 3 range variables Enumerating each value in range(5) Enumerating each value in range(1, 5) Enumerating each value in range(1, 5, 2)

  14. Output range(5): range(0, 5) range(1, 5) : range(1, 5) range(1, 5, 2) : range(1, 5, 2) Enumerating each value in range(5)... 0 1 2 3 4 Enumerating each value in range(1, 5) ... 1 2 3 4 Enumerating each value in range(1, 5, 2)... 1 3

  15. str1 = "Raymond" print("str1: ", str1) print("str1[0]: ", str1[0]) print("str1[2:5]: ", str1[2:5]) print("str1[2:]: ", str1[2:]) print("str1[-2]: ", str1[-2]) print("str1*2: ", str1*2) print("concatenated str: ", str1 + ", Hello!") python String Variables print a complete string str1 print the first character of the string print from the 3rd to the 5th character of the string print from the 3rd character of the string print from the second-to-last character of the string print the string two times print the concatenation between the string and ", Hello!" Output str1: Raymond str1[0]: R str1[2:5]: ymo str1[2:]: ymond str1[-2]: n str1*2: RaymondRaymond concatenated str: Raymond, Hello!

  16. list1 = ["Raymond", 4332, 15.7, "Intelligence"] list2 = [4310, "Big Data"] print("list1: ", list1) print("list1[0]: ", list1[0]) print("list[1:3]: ", list1[1:3]) print("list1[2:]: ", list1[2:]) print("list1[-2]: ", list1[-2]) print("list2: ", list2) print("list2*2: ", list2*2) print("list1+list2: ", list1 + list2) python List Variables print the complete list1 print the first element of list1 print from the 2nd to the 3rd element of list1 print from the 3rd element of list1 print from the 2nd-to-last element of list1 print the complete list2 print list2 two times print the concatenation between list1 and list2 Output list1: ['Raymond', 4332, 15.7, 'Intelligence'] list1[0]: Raymond list[1:3]: [4332, 15.7] list1[2:]: [15.7, 'Intelligence'] list1[-2]: 15.7 list2: [4310, 'Big Data'] list2*2: [4310, 'Big Data', 4310, 'Big Data'] list1+list2: ['Raymond', 4332, 15.7, 'Intelligence', 4310, 'Big Data']

  17. python Tuple Variables It is exactly the same as List Variables but Tuple Variables could not be updated. tuple1 = ("Raymond", 4332, 15.7, "Intelligence") tuple2 = (4310, "Big Data") print("tuple1: ", tuple1) print("tuple1[0]: ", tuple1[0]) print("tuple[1:3]: ", tuple1[1:3]) print("tuple1[2:]: ", tuple1[2:]) print("tuple1[-2]: ", tuple1[-2]) print("tuple2: ", tuple2) print("tuple2*2: ", tuple2*2) print("tuple1+tuple2: ", tuple1 + tuple2) print the complete tuple1 print the first element of tuple1 print from the 2nd to the 3rd element of tuple1 print from the 3rd element of tuple1 print from the 2nd-to-last element of tuple1 print the complete tuple2 print tuple2 two times print the concatenation between tuple1 and tuple2 Output tuple1: ('Raymond', 4332, 15.7, 'Intelligence') tuple1[0]: Raymond tuple[1:3]: (4332, 15.7) tuple1[2:]: (15.7, 'Intelligence') tuple1[-2]: 15.7 tuple2: (4310, 'Big Data') tuple2*2: (4310, 'Big Data', 4310, 'Big Data') tuple1+tuple2: ('Raymond', 4332, 15.7, 'Intelligence', 4310, 'Big Data')

  18. python Dictionary dict1 = {} dict1["COMP4332"] = "Big Data Mining and Management" dict1["RMBI4310"] = "Advanced Data Mining for Risk Management and Business Intelligence" dict1[123] = "One Two Three!" dict2 = {"sid": "12345678", "sname": "Raymond", "byear": 1998} print("dict1", dict1) print("dict1.keys(): ", dict1.keys()) print("dict1.values(): ", dict1.values()) print("") print(" dict1[\"COMP4332\"]: ", dict1["COMP4332"]) print(" dict1[\"RMBI4310\"]: ", dict1["RMBI4310"]) print(" dict1[123]: ", dict1[123]) print dict1 print all keys of the whole dictionary of dict1 print all values of the whole dictionary of dict1 print the dictionary value of "COMP4332" in dict1 print the dictionary value of "RMBI4310" in dict1 print the dictionary value of 123 in dict1

  19. Output dict1 {'COMP4332': 'Big Data Mining and Management', 'RMBI4310': 'Advanced Data Mining for Risk Management and Business Intelligence', 123: 'One Two Three!'} dict1.keys(): dict_keys(['COMP4332', 'RMBI4310', 123]) dict1.values(): dict_values(['Big Data Mining and Management', 'Advanced Data Mining for Risk Management and Business Intelligence', 'One Two Three!']) dict1["COMP4332"]: Big Data Mining and Management dict1["RMBI4310"]: Advanced Data Mining for Risk Management and Business Intelligence dict1[123]: One Two Three!

  20. python Dictionary (Continued) dict2 = {"sid": "12345678", "sname": "Raymond", "byear": 1998} print("dict2", dict2) print("dict2.keys(): ", dict2.keys()) print("dict2.values(): ", dict2.values()) print("") print(" dict2[\"sid\"]: ", dict2["sid"]) print(" dict2[\"sname\"]: ", dict2["sname"]) print(" dict2[\"byear\"]: ", dict2["byear"]) print dict2 print all keys of the whole dictionary of dict2 print all values of the whole dictionary of dict2 print the dictionary value of "sid" in dict2 print the dictionary value of "sname" in dict2 print the dictionary value of "byear" in dict2

  21. Output dict2 {'sid': '12345678', 'sname': 'Raymond', 'byear': 1998} dict2.keys(): dict_keys(['sid', 'sname', 'byear']) dict2.values(): dict_values(['12345678', 'Raymond', 1998]) dict2["sid"]: 12345678 dict2["sname"]: Raymond dict2["byear"]: 1998

  22. python Data Type Conversion strForNo1 = "4332" strForNo2 = "15.7" print("Converting strings to numbers...") no1 = int(strForNo1) no2 = float(strForNo2) print("no1: ", no1) print("no2: ", no2) print("Converting numbers to strings...") str1 = str(no1) str2 = str(no2) print("str1: ", str1) print("str2: ", str2) Convert a string to an integer Convert a string to a float number (a real number) print these 2 numbers Convert an integer to a string Convert a float number to a string print these 2 numbers Output Converting strings to numbers... no1: 4332 no2: 15.7 Converting numbers to strings... str1: 4332 str2: 15.7

  23. Outline • Variable Type • Basic Operation • Conditional Operation • Iteration • Number Operation • String Operation • List Operation • Tuple Operation • Dictionary Operation • Date/Time Operation • Function Operation • Module Operation • File I/O Operation • Exception Operation • Class Operation

  24. 2. Basic Operation • Arithmetic Operators • Comparison Operators • Logical Operators • Assignment Operators • Membership Operators

  25. no1 = 29 no2 = 3 print("no1: ", no1) print("no2: ", no2) result = no1 + no2 print("no1 + no2: ", result) result = no1 - no2 print("no1 - no2: ", result) result = no1*no2 print("no1*no2: ", result) result = no1/no2 print("no1/no2: ", result) result = no1//no2 print("no1//no2: ", result) result = no1%no2 print("no1%no2: ", result) result = no1**no2 print("no1**no2: ", result) python Arithmetic Operators The fractional number of the division The dividend of the division The remainder of the division no1no2

  26. Output no1: 29 no2: 3 no1 + no2: 32 no1 - no2: 26 no1*no2: 87 no1/no2: 9.666666666666666 no1//no2: 9 no1%no2: 2 no1**no2: 24389

  27. python Comparison Operators print("no1: ", no1) print("no2: ", no2) result = (no1 == no2) print("(no1 == no2): ", result) result = (no1 != no2) print("(no1 != no2): ", result) result = (no1 > no2) print("(no1 > no2): ", result) result = (no1 < no2) print("(no1 < no2): ", result) result = (no1 >= no2) print("(no1 >= no2): ", result) result = (no1 <= no2) print("(no1 <= no2): ", result)

  28. Output no1: 29 no2: 3 (no1 == no2): False (no1 != no2): True (no1 > no2): True (no1 < no2): False (no1 >= no2): True (no1 <= no2): False

  29. python Logical Operators print("no1: ", no1) print("no2: ", no2) result = ((no1 != no2) and (no1 > no2)) print("((no1 != no2) and (no1 > no2)): ", result) result = ((no1 != no2) or (no1 > no2)) print("((no1 != no2) or (no1 > no2)): ", result) result = (not((no1 != no2) and (no1 > no2))) print("(not((no1 != no2) and (no1 > no2))): ", result) Output no1: 29 no2: 3 ((no1 != no2) and (no1 > no2)): True ((no1 != no2) or (no1 > no2)): True (not((no1 != no2) and (no1 > no2))): False

  30. python Assignment Operators a = 123 print("a: ", a) print("") a += 2 print("After a += 2, we have") print("a: ", a) a -= 2 print("After a -= 2, we have") print("a: ", a) a *= 2 print("After a *= 2, we have") print("a: ", a)

  31. Output a: 123 After a += 2, we have a: 125 After a -= 2, we have a: 123 After a *= 2, we have a: 246

  32. python Assignment Operators (Continued) a /= 2 print("After a /= 2, we have") print("a: ", a) a //= 2 print("After a //= 2, we have") print("a: ", a) a %= 2 print("After a %= 2, we have") print("a: ", a) a **= 2 print("After a **= 2, we have") print("a: ", a)

  33. Output After a /= 2, we have a: 123.0 After a //= 2, we have a: 61.0 After a %= 2, we have a: 1.0 After a **= 2, we have a: 1.0

  34. python Membership Operators b = 12 c = 13 list = [1, 12, 45] print("b: ", b) print("c: ", c) print("list: ", list) result = (b in list) print("(b in list): ", result) result = (c in list) print("(c in list): ", result) result = (b not in list) print("(b not in list): ", result) result = (c not in list) print("(c not in list): ", result)

  35. Output b: 12 c: 13 list: [1, 12, 45] (b in list): True (c in list): False (b not in list): False (c not in list): True

  36. Outline • Variable Type • Basic Operation • Conditional Operation • Iteration • Number Operation • String Operation • List Operation • Tuple Operation • Dictionary Operation • Date/Time Operation • Function Operation • Module Operation • File I/O Operation • Exception Operation • Class Operation

  37. python Conditional Operation no = 10 print("no: ", no) print("-------- Conditional Statement 1 ------------") if (no == 10): print("This number is equal to 10.") print("-------- Conditional Statement 2 ------------") if (no == 10): print("This number is equal to 10.") else: print("This number is not equal to 10.") print("-------- Conditional Statement 3 ------------") if (no == 10): print("This number is equal to 10.") elif (no == 11): print("This number is equal to 11.") else: print("This number is not equal to 10 and 11.")

  38. Output no: 10 -------- Conditional Statement 1 ------------ This number is equal to 10. -------- Conditional Statement 2 ------------ This number is equal to 10. -------- Conditional Statement 3 ------------ This number is equal to 10.

  39. Outline • Variable Type • Basic Operation • Conditional Operation • Iteration • Number Operation • String Operation • List Operation • Tuple Operation • Dictionary Operation • Date/Time Operation • Function Operation • Module Operation • File I/O Operation • Exception Operation • Class Operation

  40. 4. Iteration • Basic Loop • While loop • For loop • Nested Loops

  41. python While Loop no = 1 while (no < 10): print("The number is ", no) no = no + 1 Output The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9

  42. python for loop (range(5)) for index in range(5): print("The index is ", index) Output The index is 0 The index is 1 The index is 2 The index is 3 The index is 4

  43. python for loop (range(7, 12)) for index in range(7, 12): print("The index is ", index) Output The index is 7 The index is 8 The index is 9 The index is 10 The index is 11

  44. python for loop ([1, 2, 3]) for index in [1, 2, 3]: print("The index is ", index) Output The index is 1 The index is 2 The index is 3

  45. python for loop (["Raymond", "COMP4332", "RMBI4310"]) list = ["Raymond", "COMP4332", "RMBI4310"] for element in list: print("The element is ", element) Output The element is Raymond The element is COMP4332 The element is RMBI4310

  46. python for loop (range(len(list))) for index in range(len(list)): print("The index is ", index) print("The list[index] is ", list[index]) Output The index is 0 The list[index] is Raymond The index is 1 The list[index] is COMP4332 The index is 2 The list[index] is RMBI4310

  47. python for loop (str) str = "Raymond" print("str: ", str) for letter in str: print("The letter is ", letter) Output str: Raymond The letter is R The letter is a The letter is y The letter is m The letter is o The letter is n The letter is d

  48. python Nested Loops: for loop (range(5)) for loop (range(3)) for index1 in range(5): for index2 in range(3): print("The index1 and index2 are ", index1, "and", index2) Output The index1 and index2 are 0 and 0 The index1 and index2 are 0 and 1 The index1 and index2 are 0 and 2 The index1 and index2 are 1 and 0 The index1 and index2 are 1 and 1 The index1 and index2 are 1 and 2 The index1 and index2 are 2 and 0 The index1 and index2 are 2 and 1 The index1 and index2 are 2 and 2 The index1 and index2 are 3 and 0 The index1 and index2 are 3 and 1 The index1 and index2 are 3 and 2 The index1 and index2 are 4 and 0 The index1 and index2 are 4 and 1 The index1 and index2 are 4 and 2

  49. Outline • Variable Type • Basic Operation • Conditional Operation • Iteration • Number Operation • String Operation • List Operation • Tuple Operation • Dictionary Operation • Date/Time Operation • Function Operation • Module Operation • File I/O Operation • Exception Operation • Class Operation

  50. 5. Number Operation • Standard Number Function • Angle Function • Random Number Generation

More Related