###### tags: python
python
===
#### print
- or
It only print B if A is false or NULL
print
- print (0 or 1) ## 1
- print(Flase or "hey") ##'hey'
- print('hi'or'hey) ##'hi'
- print([ ] or False) ## 'False'
- print(False or [ ]) ## []
- and
and only evalute the second argument if the first one is true
- print(0 and 1) ## 0
- print(1 and 0) ## 0
- print(False and 'Hey') ##False
- print('Hi' and 'Hey') ## 'Hey'
- print([ ] and False) ##[ ]
- print(False and [ ]) ## False
- operators
- & performs binary AND
- | performs binary or
- ^ Performs a binary XOR operation
- ~ performs a binary NOT operation
- << shift left operation
- ">> shift right operation
- other operator
- is
- in
- Ternay operator
- use to define conditional
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值是不變的。
- isalpha()
去確定string裡面只有charactres 且不是空的
- isalnum()
確定string裡面只有character跟digits 且不是空的
- isdecimal()
確定string 裡面只有數字且不是空的
- print("BEAU".lower())
>beau
- print("beau".upper())
>BEAU
- print('bEAu person'.title())
it make first leeter of each string become capital letter
>Beau Person
- print("Beau person".islower())
> False
- print("person".islower())
> True
is lower 是用來確定整個string是否都是小寫
- startsswith()
to check if the string starts with a specific substring
- endswith()
to check if the string ends with a specific substring
- replace()
to replace a part of a string
- split()
to split a string on a specific charactr separator
- strip()
to trim the whitespace from a string
- join ()
to append new letters to a string
- find ()
to find the postion of a substring
- 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
- turn
complex = 2+3j;
num = complex(2,3)
print(num2.real, num2.imag)
> 2.0 3.0
- abs(絕對值) and round
- print(abs(-5.5))
> 5.5
- round
- print(round(5.49, 1))
> 5.5
會收斂在小數點下面第一位
- print(round(5.4))
> 5
- print(round(5.5))
> 6
### Enum
> 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
## Basic input output
> age = input("what is your age?\n")
> print("your age is "+age)
output:
what is your age?
>55
>your age is 55
## List
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
- edit
> dogs[2]=beau
> print(dog[2])
beau
- append
dogs.append('Judah')
會把Judah加到最後面的分項去
- extend
dogs.extend(['Judah',5])
把兩格分項都加到後面去,可以加一整個list
- dogs+=['Judah',5]跟上面的結果是相同的
#### 若把Append加上list,整個list會變成最後分項,而不是多個獨立的。
#### 但若是寫成Dogs +="Judah"整個會被拆成'J','u','d','a','h',所以一定要記得加上[]
- remove(移除)
dogs.remove('Syd')
- 這樣syd會從裡面被移除,但要小心確認他有在list裡面,沒有整個program就會error。
- pop
remove the last item in the list , and print it.
And this thing will not on the list anymore.
- insert
items.insert(2,"test")
這樣item的第三項(從零開始算的)就會插入test,本來的index 2位置的,就會往後排。
#### 但你若是要插入多個的話,可以試試看這個
items[1:1]=["test1","test2"]
這樣就會從index 1開始插入test1跟test2,其他的項目往後遞延
#### sort
- items.sort()
會把items這個list由大寫的第一個字順序到小寫的第一個字順序來排序。
- items.sort(key=str.lower)
會不分大小寫,照alphabet的順序來先做排列。
- itemscopy=items[:]
可以先行複製item內容,讓值被變更的同時可以保留他的原本樣貌
- print(sorted(items,key=str.lower))
可以不變動本來item的樣子,只有輸出時來做排列改變
---
## Tuples
Tuple用()表示,Tuple一旦建立就不可再變更,要新增元素的話只能創新的Tuple後面再加。
you can't modify the original tuple
> names = ("Roger", "Syd")
> newTuple=names+("Tina","Quincy")
## Dictionaries
- dog={"name":"Roger", "age":8, "color":"green"}
print(dog.get("color","brown"))
>輸出狗的顏色,若是沒有就輸出brown
- print(dog.pop("name"))
print(dog)
>此時name就會消失,因為被pop掉了
- print(dog.popitem())
>最後被加入的會被拿掉,是Color會消失
- print(dog.keys())
>輸出所有的值
>dict_keys(['name','age,'color'])
- print(list(dog.values()))
>['Roger',8,'green']
因為使用了List所以變成[]
- dog["favorite food"]="meat"
這樣最後就可以輸出多一項favorite food, meat
- del dog['color']
這樣就可以把顏色刪除
- copy()
dogCopy = dog.copy()
這樣就可以copy dictionary
## sets