iT邦幫忙

2024 iThome 鐵人賽

DAY 2
0
Python

30天零基礎學習Python程式語言系列 第 2

Day 2: 變數與資料型別

  • 分享至 

  • xImage
  •  

變數定義:可重複使用的容器,用於儲存值(Ex:x=5,x為一變數,儲存5這個值)
簡單應用

x=5
y=6
print(x+y)

11

Ex:下圖“我的年紀是“資料型別為字串string,而age資料型別為整數integer,故需將資料型別皆改成字串才能相加並顯示。
https://ithelp.ithome.com.tw/upload/images/20240908/20169144EekUPu1xtl.png
下圖為更改過後將age加上字串括號,才能顯示age中的數字
https://ithelp.ithome.com.tw/upload/images/20240908/20169144EOv3h2KMvS.png


資料型別:

  1. Integer整數:沒有小點,Ex: age
  2. Float浮點數:有小數點,Ex: gpa
  3. String字串,Ex: name
  4. Boolean布林值,Ex: true/ false
    應用:
    a. f-strings作為字串格式化的方式。f-Strings 又稱為「string interpolation」
    b. type顯示資料型別
    Ex:
    https://ithelp.ithome.com.tw/upload/images/20240908/20169144Obk8AsKQ8E.pnghttps://ithelp.ithome.com.tw/upload/images/20240908/20169144AIgSX05i96.png

上一篇
Day 1: 環境準備(Mac)/ Hello World
下一篇
Day 3: 型別轉換 + 使用者輸入
系列文
30天零基礎學習Python程式語言21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言