# stanCode class 3 ## print(string) 可以印出想要的文字 文字:string(str) 可以用"文字" 也可以'文字'(first priority) "what's up"(若文字中有單引號,就用雙引號) ## console(會內建在電腦裡) File Path Your Prints Exit Code #為了跟她說有沒有成功 #視窗關掉後看,0代表成功、1代表無限迴圈被強迫停下來 Fun Fact:不為人知的祕密 alpaca:草尼馬 print('str') #換行print print('str', end="") #不換行print #用""或''都可以 ## Variable <function>:gives codes meaning <variable>:give data meaning ### Data Types int integer 整數 EX: 0,1,2,... float real-number 實數 EX:3.14,2.0,... str string 文字 EX:'Hi',"Hi" bool bollean 布林 True/False, 判斷正確與否 ### creating a variable(箱子/代名詞) height <---- 183(int) pi<---------3.14(float) name<-------'Jerry'(str) #有加引號的是data,沒加的是箱子 is_weekday<--True(bool) height = 183(int) pi = 3.14(float) name = 'Jerry'(str) is_weekday = True(bool) # = >>assign(右值丟左箱) # == (equaals) 可以把舊的值換掉(reassign),但舊的就找不到了 直接打新的,就可以換掉了 新定義 ten_years_ago=height-10 a=1.0 a=a+1 #2.0 a=a-1 #1.0 a=a*10 #10.0 a=a/2 #5.0 a=a+1 a+=1 a=a-1 a-=1 a=a*10 a*=10 a=a/2 a/=2 可以少寫一個a 為什麼還要有int,都有float了? float的儲存空間比較大(?但現在比較不注重空間 float沒有next的概念 要去計算就要用int,int才有可數的特性 ### naming No white spaces (use underscore) Can't start with number 避免資料與變數混合 No reserved words (有變色的地方代表被用走了) *case-sensitive 大小寫會代表不同的東西 *keep it lower case 一律用小寫!!! #### Get user inputs <Get int> meal=int(input('文字')) <Get float> tax=float(input('文字')) # prompt:透過'文字'來告訴使用者知道input什麼值 一個提示,但會等待你輸入 print內一定要文字 所以有數字要把它變成文字(變成相同type) 用+:concatenation(串起來) 前後也要加上'' ##### 小問題:電腦表達不出0.6 因為電腦是二進位 ##### concatentation print('Total:'+'total\') #Total:total print('Total:'+str(total)) #Total:550.0 ## Expression +,-,*,/,//,% <precedence> () *,/,//,% +,- ### Mod% 取餘數 7%5 == 2 2%3 == 2 turn_left()13次 13%4 == 1 odd/even x = int(input('?')) x%2 == 0 >>even x%2 == 1 >>odd ### / & // <float division> / #因為式float,所以出來一定是float 3/2 == 1.5 4/2 == 2.0 <floor division> // #int//int == 出來一定是int #因為是floor所以往下找整數取 3//2 == 1 4//2 == 2 -1//2 == -1 6//1.5 == 4.0 #int//float == 會變成float ##只要跟float運算都會變成float,會選擇用比較大的箱子 9.0//5 == 1.0 #出來會是1.8 #floor往下取,所以是1.0 ### Type Casting 用來控制出來是int還是float int(3/2) == 1 # int()做無條件捨去 float(3//2) == 1.0 Ex: float(-1//2) == -1.0 int(-1/2) == 0 #### Kahoot Q1: a1 = int(-5/2) #-2 a2 = int(-5//2) #-3 Q2: x=5 y=float(x)//2 x+=1 #除非做reassign,不然值不會變 #儘管第二行被運算,仍不會改變X的值 x = 6 y = 2.0 Q3: 2* 4//10+2* 2%4 2* 4//10 #0 2* 2%4 #0 total=0 ### Data types int float str 'bool' #### bool b1=True / b1=5>2 b2=False / b2=5<2 #### boolean operators >,<,>=,<= ==,!=,not,and,or #!=代表不等於 b3=3==3 b3=True ##### Short Circuit Evaulation and一個錯,就false or一個對,就true ###### even or odd x = int(input('Give me an int:')) if x%2==0: print("It's even!") else: print("It's odd!") ##### score score=int(input('Score')) if score>=90: print('A') else: if score>=80: print('B') else: if score>=70: print('C') else: print('fail') ###### Cascading if 疊帶 else: if condition: 寫成 elif condition: <<擇一執行>> score=82 if score>=90: print('A') elif score>=80: print('B') elif score>=70: print('C') else: print('fail') <<....>> score=82 if score>=90: print('A') if score>=80: print('B') if score>=70: print('C') else: print('fail') ### Constant 要做一個常數,要放在def main上方 PI=3.14 def main(): * All upper cases * Above def main() * 牽一髮動全身 * 把PI=3.14變成PI=3.14159265358979326 * 全部都可以變成這麼精確 * 常數除了def main()上方,其他地方不能改