Python
Anaconda
Spyder
x = 0 ; y=3
a = b = c = 20
age, name = 18, "林雲龍"
print(x, y)
print(a, b, c)
print(age, name)
名稱必須大小寫英文字母、數字及_。
命名時注意的第一個字母必須是大小寫字母或_,不能是數字。
大小寫有區分,不要用到保留字。
print(54168, "麥寮高中", True)
# print(str(54168) + "麥寮高中" + str(True))
# print(54168, "麥寮高中", True, sep="&", end=".")
print()顯示的字串可以使用 + 進行組合,但不同型別要先轉為文字
print("%s的成績是%d分" % ("林雲龍",80))
print("%10s的成績是%3d分" % ("Harrison",90))
print("%-10s的成績是%-3d分" % ("哈里斯",100))
print("我的BMI:%.2f" % (18))
print("我的BMI:%8.2f" % (18))
%s 字串, %d 數值, 預設靠右對齊
%3d 代表3位數字, %10s 代表10個文字字串
%-3d 或 %-10s 代表文字數字靠左對齊
%.2f 代表取2位小數,四捨五入
%8.2f 代表8位數字(5位整數,1位小數點,2位小數)
print("%s的成績是%d分" % ("林雲龍",80))
print("%10s的成績是%3d分" % ("Harrison",90))
print("%-10s的成績是%-3d分" % ("哈里斯",100))
print("我的BMI:%.2f" % (18))
print("我的BMI:%8.2f" % (18))
age = 18
abc = False
name = "harrison"
weight = 73.5
zipcode = "638"
# 測試資料型態
print(type(age))
print(type(abc))
print(type(weight))
print(type(zipcode))
整數(int)、浮點數(float)、字串(str)、布林(bool) True/False
查看資料型態 type()
資料型態轉換 int()、float()、str()
for loop:
for variable in range (start, stop, step):
#start 是序列的起始值,stop 是序列結束值(但不包括在內),step 是步長(遞增或遞減的量)
for i in range(0, 10):
print(i) # 0 1 2 3 4 5 6 7 8 9
for i in range(10):
print(i) # 0 1 2 3 4 5 6 7 8 9
for i in range(2, 8):
print(i) # 2 3 4 5 6 7
for i in range(2, 10, 2):
print(i) # 2 4 6 8
for i in range(0, -5, -1):
print(i) # 0 -1 -2 -3 -4
for i in range(10, 0, -1):
print(i) # 10 9 8 7 6 5 4 3 2 1
for i in range(1,101): # 取得- 1 ~ 100 的奇數
if i % 2 == 1: print(i) # 1 3 5 7 9 ..... 97 99
購物清單
shopping_list = ["蘋果", "香蕉", "麵包"]
for item in shopping_list:
print(item)
倒數計時
for count_down in range(5, 0, -1):
print(count_down)
print("起飛!")
倒數日期
from datetime import datetime
# 假設你的生日是 9 月 10 日
birthday = datetime(datetime.now().year, 9, 10)
current_date = datetime.now()
difference = birthday - current_date
print("距離你的生日還有", difference.days, "天")
# 要加絕對值 abs()
先要載入套件
!pip3 install ColabTurtle
滙入元件
from ColabTurtle.Turtle import *
初始化 (清螢幕)
initializeTurtle()
畫正方形 (請改成For Loop)
speed(3)
forward (100)
right (90)
forward (100)
right (90)
forward (100)
right (90)
forward (100)
#資料型態錯誤
#input()所接收的資料型態是字串(string))
scoreA = input("請輸入國文成績:")
scoreB = input("請輸入英文成績:")
scoreC = input("請輸入數學成績:")
total = screA + scoreB + screC
print(ttal)
print("你的總分數是:" + total)
=========================
提示:使用 int(), str()進行資料型態轉換
=========================
【實作練習:求平均跟總和】
讓使用者輸入任意兩個數字,能夠輸出總和(sum)及平均(average)
=========================
【實作練習:小費計算公式】
總帳單金額(Total Bill Amount):在餐廳或其他服務業所消費的總金額。
小費比例(Tip Percentage):,服務費通常介於10%到20%之間。
印出小費金額(Tip Amount):這是程式需要計算的數值。計算方式是總帳單金額乘以小費比例。
印出總金額(Total Amount):程式需要計算的數值。計算方式是總帳單金額加上小費金額。
![](https://www.google.com/images/icons/product/drive-32.png)