Scratch and Python 2017 Summer - Python Lecture 1
===
### Introduction to Python
+ What Is (Imperative) Programming?
+ A sequence of instructions
+ What Is Python?
+ A programming language
+ Python 2 or 3?
+ Interpreter
+ The best first language to learn (According to the textbook author)
+ Programmers Don't Need to Know Much Math
+ Most programming does not require math beyond basic arithmetic.
+ Programming Is a Creative Activity
+ Probably multiple solutions.
+ Downloading and Installing Python
+ [Official Website](https://www.python.org/)
+ Download Python 3
+ Optional task: install python to your own computer
+ All computers in the classroom are ready for use.
+ Installing packages: [see this](https://github.com/mzshieh/snp2017/blob/master/install.md)
+ The Interactive Shell
+ Run `python`
+ Run IDLE
+ With `>>>`: input to shell
+ Without `>>>`: input to program
+ How to Find Help
+ Search engine
+ Stackoverflow
+ Asking Smart Programming Questions
+ 解釋你想要做甚麼,不只是說你做了甚麼。能幫你的人才知道你有沒有走錯路。
+ 明確的指出甚麼地方有問題。比如說一開始就出錯,或是做了甚麼才出錯。
+ 將完整的錯誤訊息以及程式碼貼出。
+ 利用 [pastebin](http://pastebin.com/) 或 [gist](http://gist.github.com/) 來貼
+ 描述一下你已經做過哪些嘗試。代表你已經花了點力氣研究到底是怎麼一回事。
+ 這是關鍵。
+ 列出使用的 Python 版本以及開發環境。
+ Python 2 跟 Python 3 有相當差異,有很多套件可能因為平台而有所不同。
+ 如果是改完部分 code 之後才產生的問題,請明確描述改了甚麼。
+ 給出可以重現問題的情境,前提盡量清楚。
+ ERRORS ARE OKAY! You will see a lot of ERRORS and EXCEPTIONS in class.
### Python Basics
+ Values
+ Integers: `1`,`0`,`-2`
+ Decimals: `1.23`, `456.7`
+ Numbers in scientific notation: `1.234e5`, `-6.78e-2`
+ Strings: `'abc'`, `"ABC"`, `'"'`,`"'"`
+ Operators
+ Addition: `+`
+ Subtraction: `-`
+ Multiplication: `*`
+ Exponentiation: `**`
+ Division: `/`
+ Integer Division: `//`
+ Modulo: `%`
+ Negation: `-`
+ Brackets: `()`
+ [Precedence](https://automatetheboringstuff.com/chapter1/#calibre_link-101)
+ `()` > `**` > `-` (negation) > `*` = `/` = `//` = `%` > `+` = `-` (subtraction)
+ Special case: Right `**` > Left `**`
+ (Some) Data types
+ `int` Integer
+ `float` [Floating-point number](https://en.wikipedia.org/wiki/IEEE_floating_point)
+ Binary
+ `str` String
+ Addition? No, it's concatenation.
+ Multiplication? No, it's replication.
+ Storing Values in Variables
+ `=` Assignment
+ Variable is defined by assignment.
+ `x = 5`
+ Set variable `x` to `5`
+ [Variable name](https://automatetheboringstuff.com/chapter1/#calibre_link-107)
+ `tuple`
+ `tpl = (3,5)`
+ `list`
+ `lst = [1,2,3,4]`
+ `lst[1] = 9`
+ `dict`
+ `dct = {}`
+ `dct = {1:2}`
+ `dct['name'] = 'MZ'`
+ Expression
+ A valid combination of values and operators
+ Valid:
+ `(1+2)/3*4+5`
+ `3*2**0.5`
+ Invalid:
+ `1+2*`
+ `2*(3+4`
+ `(1-2)*3(4+5)`
+ After evaluation, we will get a value from the expression.
+ One of the most common building blocks in Python is assignment: `variable = expression`
+ (Some) Functions
+ A sequence of instructions
+ A mini-program in a program
+ Always return a value
+ `print()`
+ Display the value between `(` and `)`
+ Almost the only thing you can do in the first programming course.
+ `input()`
+ Read a line from the interactive shell.
+ `len()`
+ Calculate the length of a `str` / `list` / `tuple` / `dict`
+ `str()`
+ Convert things into `str`
+ `int()`
+ Convert things into `int`
+ `float()`
+ Convert things into `float`
+ `eval()`
+ Try `eval(input())`
+ The first program
+ Lines of codes
```python=
# I am a comment, and the following two lines are, too.
# A hello world example in Python 3
# Print "hello, world!"
print('hello, world!')
# Store your name in variable name
name = input("What's your name?")
# Say hello to you!
print('hello,',name)
```
+ Comments
+ Starting with `#`
+ Interpreter will ignore the comments
+ Console versus shell
### `pyautogui`
+ The computers mainly receive inputs from human via the mouse and the keyboard.
+ If your program controls the mouse and the keyboard, then it can do a lot of things (just like the human beings).
+ [Installation](https://github.com/mzshieh/snp2017/blob/master/install.md)
+ You might have trouble to install this package on your own computer.
+ Report the problem to the teaching assistants if you cannot handle it.
+ `import pyautogui`
+ Mouse
+ Get the size of the screen
+ `width, height = pyautogui.size()`
+ Get the position of the mouse
+ `x, y = pyautogui.position()`
+ The coordinate system
+ Top-left: (0,0)
+ Bottom-right: `pyautogui.size()`
+ Move
+ To: `pyautogui.moveTo(x, y, duration)`
+ You may ignore `duration`
+ Relative: `pyautogui.moveRel(x, y, duration)`
+ Click
+ `pyautogui.click(x, y)`
+ `pyautogui.rightClick(x, y)`
+ `pyautogui.doubleClick(x, y)`
+ Drag
+ To: `pyautogui.dragTo(x, y, duration)`
+ Relative: `pyautogui.dragRel(x, y, duration)`
+ Scroll
+ `pyautogui.scroll(y)`
+ Keyboard
+ Type
+ `pyautogui.typewrite(text,delay)`
+ Press
+ `pyautogui.press(key)`
+ `pyautogui.keyDown(key)`
+ `pyautogui.keyUp(key)`
+ [Key table](https://automatetheboringstuff.com/chapter18/#calibre_link-36)
+ Hot key
+ `pyautogui.hotkey(key1, key2, ..., keys)`
+ Example: `pyautogui.hotkey('ctrl', 'c')`
+ Is `pyautogui` too long?
+ Try `gui = pyautogui` then execute `gui.moveTo(5,5)`
+ Try to modify `import pyautogui` to
+ `import pyautogui as gui` then execute `gui.moveTo(5,5)`
+ `from pyautogui import moveTo` then execute `moveTo(5,5)`
+ `from pyautogui import moveTo as m2` then execute `m2(5,5)`
+ `from pyautogui import *` then execute `moveTo(5,5)`
### Task 0
+ Goal: Open Microsoft Paint and maximize it (If MacOS, maybe using sketchBook)
+ Using mouse (with your eyes?)
+ Click start
+ Find the application and click it
+ __Wait__ until Microsoft Paint is opened
+ Click the maximize button
+ Using keyboard (without your eyes?)
+ Press windows key (Click start?)
+ Type `mspaint` (Find the application?)
+ Press enter (Click it?)
+ __Wait__ until Microsoft Paint is opened
+ Hold windows key, then press up (Maximize)
+ Sample code?
+ Lecturer will post it here after demo.
```python3=
import pyautogui
# 執行(程式)
pyautogui.hotkey('win','r')
# 忍兩秒鐘等視窗跑起來,請有耐心,要忍耐。
pyautogui.moveRel(1,1,1)
# 小畫家的執行檔是 mspaint
pyautogui.typewrite('mspaint')
# 按下 enter 就會執行
pyautogui.press('enter')
# 忍兩秒鐘等小畫家跑起來,請有耐心,要忍耐。
pyautogui.moveRel(1,1,2)
# 最大化
pyautogui.hotkey('win','up')
```
+ If the task is not a provided function, then you have to program.
+ Decompose the task into instructions supported by the system environment.
### Task 1
+ Draw a triangle
+ Green
+ Filled
+ Draw a rectangle
+ Brown
+ Filled
+ Draw a Christmas tree
+ [Example](https://scratch.mit.edu/projects/115904117/)
### Task 2
+ Draw a circle
+ Green
+ Filled
+ Draw a tilt rectangle
+ Brown
+ Filled
+ Draw a tree
+ [With branches](https://scratch.mit.edu/projects/115838437)