# Python 101 體驗半隻腳踏入 cs 世界的日常 **01 Intro** https://hackmd.io/@lhsueh1/python101_intro :::spoiler **02 Accumulation** **Boolean** | | | and | or| | -------- | -------- | -------- |---| |1|0|False|True |1|1|True|True |0|0|False|False |0|1|False|True **range** ```range(開頭, 結尾, 間隔)``` **conditional** ```python if (a): print("a is True!") elif(b): print("b is True") else: print("no one is True") ``` **for loop** ```python for i in range(10): print(i) # i-> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ``` **while loop** ```python while(run_below_if_true): print("forever") if(condition): break #ends while loop ``` **random** ``` python import random random.randrange(10) #random number between 0-9 ``` ::: https://hackmd.io/@lhsueh1/python101_Accumulation :::spoiler **03 Function** **function** ``` python def sum_of_square(a, b): ans = ((a + b)**2) - (2*ab) return ans def main(): #main function, good practice a = sum_of_square(2, 5) print(a) #will print 29 main() ``` ::: https://hackmd.io/@lhsueh1/python101_Function :::spoiler **04 Immutable Data Types** **string** ``` python myString = "Hello World" len(myString) #get length # strings are indexable! print(myString[0]) #prints 'H' ``` **tuple** ``` python a = (13, 5) # tuples are indexable! ``` ::: https://hackmd.io/@lhsueh1/HyxB7ac8j Indirect proof & 3n + 1 https://hackmd.io/@lhsueh1/rkfthUxwj :::spoiler **05 List** **List** ``` python mylist = [1, 2, 3, 4] # lists are indexable! print(mylist[0]) #print first element len(mylist) #get length mylist.append(4) # add 4 to the end of the list mylist.pop() # remove the last element of the list ``` ::: https://hackmd.io/@lhsueh1/BJ7Z3VFvj Recursion https://hackmd.io/@lhsueh1/HyUdJhtvj Turtle https://hackmd.io/@lhsueh1/S1zzzYzds :::spoiler **06 Dictionary** ``` python mydict = {"twelve":12, "six":6, "twenty":20} #前者 key 後者 value print(mydict["six"]) #access through key, will print 6 print(mydict.get("sixx")) #safe way to access through key mydict["one"] = 1 #新增 for i, j in mydict.items(): #access with loop print(j, i) #i: key, j: value ``` ::: https://hackmd.io/@lhsueh1/SyctFNRts :::spoiler **07 File I/O & module** - write ``` python file = open("test.txt", "w") file.write("hi") file.close() ``` - append ``` python file = open("test.txt", "a") file.write("hi_2") file.close() ``` - for loop 讀法 (一次拿一行 ``` python fref = open("test.txt", "r") for line in fref: print(line) fref.close() ``` - readline() 讀法 (一次一行 ``` python fref = open("test.txt", "r") print(fref.readline()) print(fref.readline()) print(fref.readline()) fref.close() ``` - readlines() 讀法(一次全部 ``` python fref = open("test.txt", "r") print(fref.readlines()) fref.close() ``` - read() 讀法(一次全部 ``` python fref = open("test.txt", "r") print (fref.read()) fref.close() ``` ::: https://hackmd.io/@lhsueh1/HJ5YFECKs JSON <3 https://hackmd.io/@lhsueh1/BygVYReno :::spoiler **08 Class** ``` python class Rectangle: def __init__(self): self.width = 10 self.height = 10 self.color = "red" def get_area(lines="The area is... "): return lines + str(self.width * self.height) r = Rectangle() r.width = 20 print(r.get_area()) # The area is... 200 r.height = 1 print(r.get_area("->>")) # ->>20 ``` ::: https://hackmd.io/@lhsueh1/B17iF0ghi **Pygame** https://hackmd.io/@lhsueh1/BJM6ITBTs `What's next` https://hackmd.io/@lhsueh1/r1mRinrni <br></br> --- :::spoiler Not included - Exceptions - Network - Inheritance - Testing - Git - Basic Linux - ... :::