The Application of Python Language Robert . Chi

June 8th 2021 Agenda

• Read Data from Excel

• Big Data Analysis

• Web Programming

• Mobile App Programming

• Game Programming

• Analyze Stock Prices Robert C. Chi (紀俊男)

• Education • Ph.D. Candidate / Bioinformatics National Yang-Ming University, 2003-2007 • Master / Computer Sciences Queens College, CUNY, 1994-1996 • Bachelor / Computer Sciences Fu-Jen Catholic University

• Experience • Training Director / AMI (2014-2020) • Founder / Hatch Information Co., Ltd. (2007-2013) • Research Assistant / Academia Sinica (2000-2007) • Manager of Tech Support / Trend Micro Co., Ltd. (1998-2000) • Game Developer / CG Animation Co., Ltd. (1997-1998)

• Expertise • Artificial Intelligence (AI), Embedded System, Computer Security, Game Programming.

3 Download Resources

Today’s Slides Demo Source Codes Developing Environments

PyCharm What we use today! The Most User-Friendly Environment

Spyder 200+ Packages Pre-installed

Jupyter Notebook

Good for Code Readability Read Data from Excel What to Do?

Analyze / Predict CarEvaluation.csv

Read What We Need?

How to Install • • Already Pre-installed! • conda install pandas • Other Environments • pip install pandas

How to Import • import pandas as pd Source Codes

Demo_ReadFromExcel.py Demo: Read Data from Excel

• Navigate to Working Directory.

• Write the source codes as below:

• Run to see the result. Big Data Analysis What to Do?

Statistics Heatmap Group by ‘ToBuy’ How to Calculate the Statistics?

Once you loaded data into memory…

Show numerical columns only

Show all columns (categorical + numerical) How to Draw a Heatmap?

Once you loaded data into memory…

Calculate Correlational Matrix data.corr()

annotation = False annotation = True

Draw Heatmap with seaborn.heatmap() How to Group Data by ‘ToBuy’

print(data[['Children', 'Age', 'Salary', 'ToBuy']].groupby(['ToBuy']).agg(['mean'])) → Select 'Children', 'Age', 'Salary', 'ToBuy' 4 columns

print(data[['Children', 'Age', 'Salary', 'ToBuy']].groupby(['ToBuy']).agg(['mean'])) → Group by 'ToBuy'.

print(data[['Children', 'Age', 'Salary', 'ToBuy']].groupby(['ToBuy']).agg(['mean'])) → Aggregate other data by 'mean'.

print(data[['Children', 'Age', 'Salary', 'ToBuy']].groupby(['ToBuy']).agg(['mean'])) → Print the results on screen Source Codes

Demo_BigDataAnalysis.py Demo: Big Data Analysis

• Navigate to Working Directory.

• Write the source codes as below:

• Run to see the result. Web Programming How a Web Page Works?

Web Pages 2 • HTML What we need? • CSS Name: • JavaScript Web Server Browser 1 Mail: • Python as a container • Chrome Web Pages 2 • Firefox themselves • Safari • MS-Edge

1

Name

Mail

Web Server Database • MySQL • MS-SQL Where can I find a Free Web Server?

https://geekflare.com/python-hosting-platform/

My Recommendation Create an Account at PythonAnywhere

https://www.pythonanywhere.com Create an Account at PythonAnywhere

• No dedicated domain name. Limitation of Free Accounts: • Only 1 Web Application.

• No SSH Security Access.

• No Background Running Tasks.

• CPU: 100 seconds per day.

• Priority: Low

• Storage: 512 MB Create an Account at PythonAnywhere

Enter Username, Email, and Password: Create an Account at PythonAnywhere

Confirm your Email:

Check Information Correct: Create a Web Application

Login to Your Dashboard:

Create Your First Web App: Create a Web Application

Domain Name: Python Web Framework:

Mandatory Assignment of Domain Name: cnchi1025.pythonanywhere.com Create a Web Application

Python Version: Physical Path of Your Web App:

Choose the Python Version You Want. Modify the Source Codes Modify the Source Codes

@app.rount(“/”): • All the traffics go to web root “/” → Goes to the function def hello_world() Restart Your Web Server Demo: Web Programming

• Create an Account at PythonAnywhere. • Create a Default Web Application at PythonAnywhere. • Change the following source codes, restart, and test it on browser:

• Add the following source codes, restart, and test it on browser: Mobile App Programming What to Do?

Mobile Apps Zoom & Rotate What We Need?

How to Install • Spyder • conda install kivy –c conda-forge • Other Environments • See https://bit.ly/3uXg6Nu

How to Import • from kivy.app import App Source Codes

1. Import Kivy. ① from kivy.app import App 2. Create a class SimpleApp inherited from Kivy. ⑤ from kivy.uix.label import Label 3. Define build(), which is the first function run by Kivy. ⑦ from kivy.uix.scatter import Scatter 4. Create a visual component, Label, which is a container of static text. ② class SimpleApp(App): ③ def build(self): 5. Import Label ④ lbl = Label(text="Hello!", font_size=96) 6. Create a visual container of components, Scatter, which can zoom or rotate a component. ⑥ sct = Scatter() 7. Import Scatter. ⑧ sct.add_widget(lbl) 8. Add Label into a Scatter. ⑨ return sct 9. Return the Scatter to main program. 10. Create a section for main program. ⑩ if __name__ == "__main__": SimpleApp().run() 11. Trigger SimpleApp() object to run itself.

⑪ Execution Results

Label ‘Hello’ is Draggable Simulate for Multi-touch Gestures Zoom & Rotate

Right Click for 2nd Finger Drag

Right Click for 1st Finger Close the App

Detach App with Spyder

exit() to kill the App Demo: Mobile App

• Install Kivy.

• Write the source codes as below:

• Run and Quit the App for testing. Game Programming What to Do?

Snakes

Use Arrow Keys for ← ↑ ↓ → (No Collision Detection) What We Need?

How to Install • All Platforms • pip install pygame

How to Import • import pygame Source Codes

Constants Setting Define a ■ of the Snake Source Codes

Handle Arrow Keys

Set Game Window

Create a Snake Source Codes

Make the Snake Move (Update the Snake Motion on Screen) Demo: Game Programming

• Install PyGame.

• Write the source codes as Previous Slides.

• Run the Program, use Arrow Keys to control the Snake. For More Examples…

• https://bit.ly/2TNO4XP Analyze Stock Prices What Do We Have Eventually?

Input Data

• Stock ID • Start Data • End Date What Do We Need?

• pandas-datareader • matplotlib

Yahoo Finance

Google Finance pandas_datareader matplotlib

How to Install How to Install • Spyder: conda install pandas-datareader • Spyder: (built-in, no need to install) • Others: pip install pandas-datareader • Others: pip install matplotlib

How to Import How to Import • import pandas_datareader as data • import matplotlib.pyplot as plt How Can We Get Stock Prices?

1 2 3 4

1. Must be “.TW”. 2. Could be “yahoo” (Yahoo Finance Database) or “” (Google Finance Database). 3. Start Date. Must be in the format of “YYYY-MM-DD”. 4. End Date. Must be in the format of “YYYY-MM-DD”. How Can We Parse Dates?

2021/06/01 dateutil.parser 6-1-2021 Jun 1 2021 “2021-06-01” 1 Jun 2021 … How Can We Draw a Plot?

1

3

1 2 2 4 3 4

close_price.rolling(window=20).mean().plot(label="20MA") Sum the previous 20 numbers up average plot it 2 Source Codes Demo: Analyze Stock Prices

• Install pandas-datareader, matplotlib.

• Write the source codes as Previous Slides.

• Run the Program, input the following information: • Stock ID • Start Date • End Date

• Check if you can see the plot on screen. Summary

• One Language, Multiple Purposes!

Web Crawler

Web Game Design Design

Mobile Big Data App

Embed Machine System Learning

Automate Questions & Answers