體驗半隻腳踏入 cs 世界的日常
01 Intro
https://hackmd.io/@lhsueh1/python101_intro
Boolean
and | or | ||
---|---|---|---|
1 | 0 | False | True |
1 | 1 | True | True |
0 | 0 | False | False |
0 | 1 | False | True |
range
range(開頭, 結尾, 間隔)
conditional
if (a):
print("a is True!")
elif(b):
print("b is True")
else:
print("no one is True")
for loop
for i in range(10):
print(i) # i-> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
while loop
while(run_below_if_true):
print("forever")
if(condition):
break #ends while loop
random
import random
random.randrange(10) #random number between 0-9
https://hackmd.io/@lhsueh1/python101_Accumulation
function
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
string
myString = "Hello World"
len(myString) #get length
# strings are indexable!
print(myString[0]) #prints 'H'
tuple
a = (13, 5)
# tuples are indexable!
https://hackmd.io/@lhsueh1/HyxB7ac8j
Indirect proof & 3n + 1
https://hackmd.io/@lhsueh1/rkfthUxwj
List
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
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
file = open("test.txt", "w")
file.write("hi")
file.close()
file = open("test.txt", "a")
file.write("hi_2")
file.close()
fref = open("test.txt", "r")
for line in fref:
print(line)
fref.close()
fref = open("test.txt", "r")
print(fref.readline())
print(fref.readline())
print(fref.readline())
fref.close()
fref = open("test.txt", "r")
print(fref.readlines())
fref.close()
fref = open("test.txt", "r")
print (fref.read())
fref.close()
https://hackmd.io/@lhsueh1/HJ5YFECKs
JSON <3
https://hackmd.io/@lhsueh1/BygVYReno
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