黃芝晴 / Hedi Huang

icon picker
python

input, output, conditional branching, math, exception handling, looping, functions
終端機開啟python
%E6%88%AA%E5%9C%96_2022-07-21_%E4%B8%8B%E5%8D%884.27.52.png

REPL
Read-Eval-Print Loop
讀取 求值 輸出 循環
help()
look for every element in python
>>> help(str)
%E6%88%AA%E5%9C%96_2022-07-21_%E4%B8%8B%E5%8D%884.37.48.png
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 提供值給變數,再根據變數為賦予他們資料型態
Untitled.png
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!)
Untitled 1.png

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
Untitled 2.png
.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!)
Untitled 3.png
Untitled 4.png
list裡的variable也是可以直接assign的
Untitled 5.png
.insert
Untitled 6.png
↑讓list裡的第1個index的值是41
del & .pop(LIFO Last In First Out)
但其實都是從list裡刪掉 不過.pop可以不指定,還可以暫存
Untitled 7.png
>>> er_temperature
[39, 36]
>>> store = er_temperature.pop()
>>> store
36
.split() ft. time.sleep()
Untitled 8.png
ft. time.sleep()
Untitled.gif
.split() turns string into list
you can import time and use time.sleep() on your print
.join()
Untitled 9.png
loop
.copy
Untitled 10.png
Untitled 11.png
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.