or
It only print B if A is false or NULL
print
and
and only evalute the second argument if the first one is true
operators
other operator
is
in
Ternay operator
def is_adult(age)
if age >18:
return True
else:
return False
def is_adult2(age)
return True if age>18 else False
print with different line
三個"可以做成很多行的輸出(若第一行的"後面沒接東西,就會變成多一行空格)
print("""Beau is
39
years old.
""")
輸出
Beau is
39
years old.
string modify
上述這些都只是輸出當下改變的string。但本身的string值是不變的。
beau
BEAU
Beau Person
False
True
is lower 是用來確定整個string是否都是小寫
global function
print(len(name))
4
name ="beau"
print("au" in name)
True
name ="Be"au"
print(name)
BE"au
\這個可以讓後面的"不再表示成本來的樣子,而是一般的string
name = 'Be"au'
print(name)
Be"au
name ='Be"'au'
print(name)
Be"'au
name = 'Be\nau'
print(name)
Be
au
name ='Be\au'
print(name)
Beu
name = 'Be\au'
print(name)
Be\au
name ="Beau"
print(name[1:2])
從1開始回值,2之前的全部丟出來(不包含2)
any([A,B])
it will return true if any value of this function is true
all([A,B])
it will return true if all of the value is true
complex
2.0 3.0
abs(絕對值) and round
5.5
round
5.5
會收斂在小數點下面第一位
5
6
from enum import Enum
class State(Enum):
INACTIVE = 0
ACTIVE = 1
print(State.ACTIVE.value)
output: 1
print(State(1))
output: State.ACTIVE
print(State['ACTIVE'].value)
output: 1
print(list(State))
output <State.INACTIVE: 0>,<State.Active: 1>
print(let(State))
2
age = input("what is your age?\n")
print("your age is "+age)
output:
what is your age?
55
your age is 55
dogs = ["Roger",1,"Syd",True]
print("If Roger is in the list just print True\n")
print("Roger" in dogs)
print(dog[:3])
['Roger',1,'Sys']
print(len(dogs))
4
append & edit
dogs[2]=beau
print(dog[2])
beau
items[1:1]=["test1","test2"]
這樣就會從index 1開始插入test1跟test2,其他的項目往後遞延
Tuple用()表示,Tuple一旦建立就不可再變更,要新增元素的話只能創新的Tuple後面再加。
you can't modify the original tuple
names = ("Roger", "Syd")
newTuple=names+("Tina","Quincy")
輸出狗的顏色,若是沒有就輸出brown
此時name就會消失,因為被pop掉了
最後被加入的會被拿掉,是Color會消失
輸出所有的值
dict_keys(['name','age,'color'])
['Roger',8,'green']
因為使用了List所以變成[]