Udemy課程:[100 Days Of Code(Dr. Angela Yu)](https://www.udemy.com/course/100-days-of-code/) # Day 2 - Beginner - Understanding Data Types and How to Manipulate Strings ###### tags: `python` `Udemy` `100 Days Of Code` 2021.01.19(Tue.) ## ● 前言 / 心得 在講各種data types:String、Integer、Float、Boolean,聽完後的感覺是原來有那麼多細節過去沒注意到,這也應證了剛好昨天更改專題時,就是碰到因為data type的關係,而一直運作失敗,經過這堂Day 2課程後,可以更全面的理解,也更能去注意到此時的data type是甚麼。 基本上這堂課依然沒什麼難度,除了過去沒碰過的f-string跟小數最後的0也要顯示出來,而用到的format函數外,都可以輕鬆的上完不太有負擔,期待day3的課程! ## ● 上課筆記 ## 0.code > [day-2-start](https://repl.it/@tina0915tw/day-2-start#main.py) > [day-2-1-exercise](https://repl.it/@tina0915tw/day-2-1-exercise#main.py) > [day-2-2-exercise](https://repl.it/@tina0915tw/day-2-2-exercise#README.md) > [day-2-3-exercise](https://repl.it/@tina0915tw/day-2-3-exercise#README.md) > [tip-calculator-start](https://repl.it/@tina0915tw/tip-calculator-start#main.py) ## 1.large integer ```python= print(123456789) #123456789 print(123_456_789) #123456789 ``` 一般數字太大時,我們可能會習慣用comma逗號,每三位數字做隔開的動作。 而在python中,則是利用底線來做替代。 ## 2.float 有decimal point(小數點)的都屬於float ## 3.boolean 只有兩個值,True或是False ## 4.type( ) 可以用來查詢變數的data type(資料型態) ## 5.轉變data type(資料型態) * str( ):轉變成string(字串) * float( ):轉變成float(浮點數) * int( ):轉變成integer(整數) ## 6.division(除法)後的data type 在python中,除法後得到的數字,他的data type都是float,無論有沒有整除。 ## 7.小數點→整數 ```python= #int() print(int(3.14)) #3 #round(x,n) print(round(3.14))#3 print(round(3.66))#4 ``` * 利用int( ):最簡單的方法,直接去除小數點後的數字。 * 利用round(x,n):四捨五入。x是浮點數,n是指保留的小數位數,沒寫則取到整數。 ## 8.f-string 字串格式化 它的學名叫作 “Literal String Interpolation”。 * 示範: ```python= score = 100 print(f"Your score is {score}.") #Your score is 100. ``` 看到這個會想到JavaScript裡的樣板語言,利用重音符包住。 * 示範: ```javascript= var score = 100 console.log(`Your score is ${score}.`) //Your score is 100. ``` ## 9.How to round down to 2 decimals a float using Python [去google答案](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) [去google答案](https://www.itread01.com/content/1533410418.html) ```python= print(‘{:.2f}‘.format(3.1415926)) #3.14 ``` 保留兩位小數,其中.2表示精度,f表示float類型