JavaScript Required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
黃芝晴的軟體工程師之路
黃芝晴 / Hedi Huang
想邀請你成為我踏上軟體工程師之路的貴人
解決問題的好習慣
關於我
More
Share
Explore
黃芝晴 / Hedi Huang
python
input, output, conditional branching, math, exception handling, looping, functions
終端機開啟python
REPL
Read-Eval-Print Loop
讀取 求值 輸出 循環
help()
look for every element in python
>>> help(str)
print()
>>> health = "water and good foods"
>>> print("health is" + health)
health iswater and good foods
>>> print("health is" , health)
health is water and good foods
如果要 print 的內容有換行
可以分成多個print來執行
print("I " + verb + " everyday.\n" + noun.capitalize() + " is my favorite.\nIt make me feel " + adjective + ".")
↓
print("I " + verb + " everyday.")
print(noun.capitalize() + " is my favorite.")
print("It make me feel " + adjective + ".")
input()
>>> favorite_color = input("What is your favorite color? ")
>>> print(favorite_color + " is a good color.")
What is your favorite color? blue
blue is a good color.
data type
int(integer)
float
str(string)
literal
Literals 提供值給變數,再根據變數為賦予他們資料型態
round()
to the
closest
integer
ex.
>>> round(5.551115123125783e-17)
0
>>> round(11.9)
12
>>> round(”11.9”)
12
int()
to the
closest integer which is under the number
>>> int(11.9)
11
>>> int(”11.9”)
11
float()
>>> float(11)
11.0
>>> float(”11”)
11.0
str()
>>> str(24)
'24'
operation
order(PEMDAS)
PEMDAS_Please Excuse My Dear Aunt Sally
PEMDAS_
Parenthesis(小括號)
Exponent(指數)
Multiplication(乘)
Division(除)
Addition(加)
subtraction(減)
sequence
string—>immutable
to print
>>> print(”I can’t.\nJust can’t.”)
I can't
Just can't
>>> print("""She said,
... "I can't,
... Just can't." """)
She said,
"I can't,
Just can't."
#if use """ (triple quotes), just press enter, \n is no need
>>> print("""She said,\n"I can't." """)
She said,
"I can't."
>>> health = "water" + " and good food" + "!" * 3
>>> health += " Isn't it?"
>>> print(health)
water and good food!!!
#there are no -= if wanting doing subtraction, just assign again.
#str is immutable(不可變)
to len
>>> len("Hedi Huang")
10
# space is included.
>>> name = input("name: ")
name: Hedi Huang
>>> len(name)
10
method
method is a function that is owned by an object. You can access these by using dot notation.
.upper() & .lower()
>>> name.upper()
'HEDI HUANG'
>>> name
'Hedi Huang'
>>> print(name.upper())
HEDI HUANG
>>> name.lower()
'hedi huang'
index(for string!)
booleans
>>> bool(0)
False
>>> bool(1)
True
>>> bool("")
False
>>> bool(not(0 and 1))
True
>>> "he" in "hedi"
>>> true
>>> list = ["hedi, hide"]
>>> "hedi" in list
>>> true
#If there is a value, then it's True.
#Otherwise, it's False.
#Only all are true is true.
comparison
>>> "hedi">"tina"
False
>>> "four">"one"
False
#compare the order of the words' first alphabet
#the later order is bigger
>>> "cosmos" != "cosmos"
False
# != not the same
list—>mutable
.append & .extend
>>> temperature = []
>>> temperature.append(36.5)
>>> temperature.append(36.8)
>>> temperature
[36.5, 36.8]
>>> er_temperature = [39, 40, 38]
>>> temperature.extend(er_temperature)
>>> temperature
[36.5, 36.8, 39, 40, 38]
#append => 在集合a裡新增項目
#extend => 在集合a裡加進集合b 故集合a的內容已變更,集合b內容不變
index(for list!)
list裡的variable也是可以直接assign的
.insert
↑讓list裡的第
1
個index的值是
41
del & .pop(LIFO Last In First Out)
但其實都是從list裡刪掉 不過.pop可以不指定,還可以
暫存
>>> er_temperature
[39, 36]
>>> store = er_temperature.pop()
>>> store
36
.split() ft. time.sleep()
ft.
time.sleep()
.split()
turns string into list
you can
import time
and use
time.sleep()
on your print
.join()
loop
.copy
終端機開啟python
REPL
help()
print()
input()
data type
literal
round()
int()
float()
str()
operation
order(PEMDAS)
sequence
string—>immutable
to print
to len
method
.upper() & .lower()
index(for string!)
booleans
comparison
list—>mutable
.append & .extend
index(for list!)
.insert
del & .pop(LIFO Last In First Out)
.split() ft. time.sleep()
.join()
loop
.copy
slice
loop for list remove
multidimensional list(lists inside lists / rows and columns)
sum()
tuples(immutable_list, more memory efficient than list)
sequence operation(method)
sequence method
dictionary
accessing key & value
.keys()
.values()
sorted()
update & mutate dictionary
iterate over dictionary
.items()
packing(for dictionary)
unpacking(for dictionary)
constants
function
parameter & argument (send to function)(vs return)
the time to create function:
= vs ==
scope
return (from function) (vs parameter & argument)
range(start, stop, step)
enumerate
packing
unpacking
if elif else
factor & common factor_intersection
method
.format() & template
True False
module
math
error
while loop(co→sys.exit)
for loop
iterate in str
iterate in list
good example
named unicode
OOP(Object-Oriented Programming)
python object
instantiate(instance)
isinstance()
attribute
class attribute
instance attribute
method
print(__str__)
iterate(__iter__)
__eq__
datetime(library)
timedelta
strftime
wiki_date(application of strftime & strptime)
timezone
note
Build an Application
think about user story(design function) 至少要有HELP、DONE、SHOW
write “show_help def” and show it to user at the beginning of this script
infinite loop
break
continue
dunder main
演算法
write better python
pep8
pep20
docstring
logging
pdb(python debugger)
regex
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.