Share
Explore

# python

input, output, conditional branching, math, exception handling, looping, functions

REPL

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("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(除)
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)

>>> 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