###### tags: `Python` # CH1 數值運算與互動運算 ## Output ### print() 1. 印出變數內容 or 運算結果 2. ","區隔多個變數,列印以一個空格隔開 (sep,end可以改預設!!!) #### 基本的印出 ```python= print(1,2) print(3,4) ``` >1 2 >3 4 #### 改預設的狀況 ```python= print(1,2,end=" ",sep="") print(3,4,end=" ",sep="") # end是結束時 # sep是間隔 ``` >12 34 ## 資料型態 > Python不用宣告!!! | 整數 | 浮點數 | 字串 | |:----:|:------:|:----:| | 12 | 12.0 | '12' | | int | float | str | ## Input ### input() 1. 取得使用者鍵入資料 2. input()函數出來是字串型態,轉型要用int() or float() 3. input()函數內可以放資料,可達成互動運算的效果 #### 基本輸入 ```python= a=input() ``` #### 互動運算狀況 ```python= a=input("請輸入一個值") ``` #### 轉型 ```python= a=int(input()) #這樣a是整數 ``` ## 運算 ### 運算子 | 加 | 減 | 乘 | 除 | 求餘數 | 次方 | 整數除法(無條件捨去) | 強制轉型(取整數位) | |:---:|:---:|:---:|:---:|:------:|:----:|:--------------------:|:------------------:| | + | - | * | / | % | ** | // | int() | ### 優先順序 0. 括號 (只有小括號!!) 1. 次方 2. 乘、除、餘 3. 加、減 4. 關系運算 ### 註記 #### 求餘數 #### 運算規則(以a%b舉例) > a = (a/b)*b + (a%b) > 除式=被除式 X 商 + 餘式 | 運算式 | -10%3 | 10%-3 | -10%-3 | |:------:|:-----:|:-----:|:------:| | 答案 | 2 | -2 | -1 |
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up