# Python 入門
(北區) py 班
yjrubixcube
----
## 今天你會學到
- 輸出
- 註解
- 變數
- 資料型態
- 運算子
- 輸入
---
# 輸出
----
Python 的文字輸出
```python=
print("Goodbye World!")
```
----
如果要印其他的東西...
```python=
print("Hello World!")
print("123")
print("這句話有七個字")
```
----
為什麼要`""`
試試看單引號或不加...
```python=
print("Hi")
print('Hi')
print(Hi)
print("Hi')
```
----
如果你真的很想要印出`'`或`"`
```python=
print("Hello, it's me")
print('"Never gonna give you up", he said')
print('Hello, it\'s me')
print("⌈Hi⌋")
```
----
如果要印很長的東西
```python=
print("Somebody once told me the world is gonna roll me")
print("I ain't the sharpest tool in the shed")
print("She was lookin' kind of dumb with her finger and her thumb")
print("In the shape of an \"L\" on her forehead")
print("The years start comin' and they don't stop comin'")
print("Fed to the rules and hit the ground runnin'")
print("Didn't make sense not to live for fun")
print("Your brain gets smart but your head gets dumb")
print("So much to do so much to see")
print("So what's wrong with takin' the backstreets")
print("You'll never know if you don't go")
print("You'll never shine if you don't glow")
```
----
如果要印很長的東西
```python=
print('''Somebody once told me the world is gonna roll me
I ain't the sharpest tool in the shed
She was lookin' kind of dumb with her finger and her thumb
In the shape of an "L" on her forehead
The years start comin' and they don't stop comin'
Fed to the rules and hit the ground runnin'
Didn't make sense not to live for fun
Your brain gets smart but your head gets dumb
So much to do so much to see
So what's wrong with takin' the backstreets
You'll never know if you don't go
You'll never shine if you don't glow''')
```
單雙引號都可以
----
進階用法 `sep, end`
```python=
print("Never", 'gonna', '''give''', 'you', "up")
print("Never", 'gonna', '''give''', 'you', "up", sep="_")
print("雪莉", end="?")
print("財哥", "專業", "檳榔攤", sep="...", end=",,,")
```
----
補充`\` 跳脫字元
|字元|意義|
|-|-|
|`\n`|換行|
|`\t`|tab|
|`\\`|反斜線|
|`\'`|單引號|
|`\"`|雙引號|
[還有很多](https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences)
----
練習時間
https://neoj.sprout.tw/problem/3000/
---
# 註解
----
單行
```python=
# 這是一行註解
print("Hello") # 這也是註解
# print("Hello")
# 後面的都不理你 print("Hello")
# 我的電腦超爛
# ↑反正放在註解裡面電腦也看不到
```
----
多行
```python=
# 這是
# 一些
# 多行
# 註解
'''
這也是
多行註解
'''
```
---
# 變數
----
把東西放到盒子裡面
```python=
name = "Joe"
print("Hello", name)
```
----
可以同時有很多個變數
```python=
name = "Joe"
age = 20
print("My name is", name, "and I am", age, "years old")
```
----
賦值
```python=
x = 89
y = 64
z = x + y
print(z)
```
----
這個會輸出什麼
```python=
x = 87
x = x + 2
print(x)
```
----
`=` 跟數學課本裡面的意思不一樣
數學:兩邊的東西是一樣的
程式:把左邊的值設成右邊的值
----
也可以一次賦值很多個
```python=
a, b = 5, 10
```
----
如果想要交換兩個變數的值...
```python=
a = 5
b = 10
a = b
b = a
print(a, b)
```
----
標準的作法
```python=
tmp = a
a = b
b = tmp
```
----
還可以這樣!?
```python=
a, b = 5, 10
a, b = b, a
print(a, b)
```
![image](https://hackmd.io/_uploads/SJGVCcWaa.png)
----
變數的一些命名限制
- 不能用內建的關鍵字(通常是會變色的, if, for while...)
- 不要用內建函式(print...)
- 只能有數字、英文字母、`_`底線
- 數字不能在最前面
- 底線在最前面(有時候)會有額外的效果
- 盡量取有意義的名字
----
合適的變數名稱
```python=
name = "Joe"
age = 20
```
不合適的變數名稱
```python=
a = "Joe"
b = 20
name = 20
age = "Joe"
```
---
# 資料型態
----
基礎資料型態
- integer 整數
- floating point number 浮點數
- complex number 複數
- boolean 布林值
- string 字串
----
整數 int
```python=
i1 = 123
i2 = -8964
i3 = 1_23_456_789
```
----
浮點數 float
```python=
f1 = 1.23
f2 = 0.000000001
f3 = 1.0
f4 = 6.023e23
```
:avocado:
----
複數
注意是 `j` 不是 `i`
```python=
z1 = 1 + 2j
z2 = 8.9 + 6.4j
```
----
## 補充 - 浮點數、複數範圍限制
Python的float用64位元表達,所以會有範圍限制
大概是$\pm ( 2.2*10^{-308}, 1.8*10^{308})$跟0
https://www.ibm.com/docs/en/db2-big-sql/6.0?topic=list-numbers#d529629e288
----
布林值
```python=
True
False
```
:japanese_goblin:
----
字串
```python=
"abc" # 單行
"""
a
b
c
""" # 多行
```
:oden:
----
怎麼看資料型態 `type()`
```python=
print(type(123))
print(type(123.0))
print(type(123.))
print(type('123'))
a = 123j
print(type(a))
b = "123j"
print(type(b))
```
----
強制轉型
```python=
int("123")
float(123)
str(123+4j)
bool(123)
bool(0)
```
----
這些分別會發生甚麼事
```python=
int(123.45)
int(123 + 45j)
int("abc")
str(True)
str(float(True))
float(str(int(True)))
```
----
還有一些進階的資料型態,之後會教
- list
- dict
- set
- ...
---
# 運算子
----
數學運算子 - 以前應該見過的
```python=
a, b = 0.2, 0.1
print(a + b)
print(a - b)
print(a * b)
print(a / b)
```
----
數學運算子 - 跟你數學課本不太一樣的
```python=
a, b = 20, 3
# 20 = 6*3 + 2
print(a // b) # 商 6
print(a % b) # 餘數 2
print(a ** b) # 次方 8000
```
----
偷懶寫法
```python=
a, b = 10, 3
a += b
print(a, b)
```
其他運算子也適用
----
也可以對不是數字的資料型態做運算
```python=
a = "spr"
b = "out"
print(a + b) # sprout
print(b * 3) # outoutout
```
----
但是有些運算會出事
```python=
a = "sprout"
b = 2024
print(a + b) # 會出事
print(a + str(b)) # 強制轉型
```
---
# 輸入
----
```python=
name = "Joe"
print("My name is", name)
```
又不是所有人都叫 Joe
----
```python=
print("What is your name?: ")
name = input()
print("My name is", name)
```
輸入完記得要按enter/return
----
也可以這樣合併
```python=
name = input("What is your name?: ")
print("My name is", name)
```
----
試試看
```python=
age = input("Enter your age: ")
print("You will be", age+1, "next year")
```
----
注意資料型態
```python=
age = input("Enter your age: ")
print(type(age))
print("You will be", int(age) + 1, "next year")
```
---
# 作業
https://neoj.sprout.tw/problem/3009/
https://neoj.sprout.tw/problem/3008/
---
# 補充
- [去年講義](https://drive.google.com/file/d/1op99RyWB4z86FE7ckF4SIcAz9RxWTB6Y/view)
- [程式有問題](https://www.google.com/)
- [Python標準函式庫](https://docs.python.org/zh-tw/3/library/)
- [好康的](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
{"title":"Python入門","breaks":true,"contributors":"[{\"id\":\"54bbdba3-ffbf-423a-b026-751cb8a77149\",\"add\":6590,\"del\":926}]","description":"Python 簡介"}