0324 Python補足整理

2485 ワード

この4日間、学生時代に学んだPythonを再学習しました.
知っている部分は多いですが、新学の内容も多いので、これらの内容を整理したいと思います.
まずvscで領域を選択し、ctrl+kとctrl cを押してコメントします.
ctrl+k、ctrl+uでコメント処理を解除できます.

文字列関数

str_o1 = "Python"
str_o2 = "Apple"

print("Capitalize: ", str_o1.capitalize())
print("endswith?: ", str_o2.endswith("s"))

結果


Capitalize:Python文字列の最初のアルファベットは大文字で、残りは小文字に変換されます.
endswith?: False#は特定の文字で終わる文字列ですか?

スライド

str_sl = 'Nice Python'

print(str_sl[::-1])
print(str_sl[::2])

結果

nohtyP eciN
Nc yhn

文字列の書式設定

print('{:s<10}'.format('nice'))   # 10자리 확보하고 문자를 오른쪽에 붙여 출력후 나머지는 s로 채움
print('{:s>10}'.format('nice'))  # 10자리 확보하고 문자를 왼쪽에 붙여 출력후 나머지는 _ 로 채움
print('{:^10}'.format('nice'))  # 중앙 정렬
print('{:s^10}'.format('nice'))  

結果


nicessssss
ssssssnice
nice
sssnicesss
注意:Escapeコード
n:実行
t:タブ
:テキスト
":テキスト
":テキスト
000:空の文字

梱包および未梱包

t2  =1, 2, 3     #  t2 = (1, 2, 3) 괄호를 씌워주어도 튜플이고 안씌워줘도 튜플
t3 = 4,   # 여기도 t3 = (4,)  와 같다
x1, x2, x3 = t2
x4, x5, x6 = 4, 5, 6

算術、関係、論理優先度


「算術」>「リレーションシップ」>「論理順序で適用」
print('e1 : ', 3 + 12 > 7 + 3)
print('e2 : ', 5 + 10 * 3 > 7 + 3 * 20)
print('e3 : ', 5 + 10 > 3 and 7 + 3 == 10)
print('e4 : ', 5 + 10 > 0 and not 7 + 3 == 10)

for~else文法Pythonがサポートする特殊文法

numbers = [14, 3, 2, 33, 15, 34, 36, 38]

for num in numbers:
    if num == 34:  # 45
        print("Found : 34!")
        break
else:
    print("Not Found 45...")

ラムダ式の例


メモリの節約、可読性の向上、コードのシンプル化
関数を使用すると、オブジェクトの作成→リソースの割り当て(メモリ)
Ramda即時起動関数(Heap初期化)->メモリ初期化
乱発するとかえって毒性が減少する
def mul_func(x, y):
	return x * y
    
lambda x, y:x*y

input関数は変数として指定する必要はありません。また、必要に応じて宣言して、関数で使用できます。

print("FirstName - {0}, LastName - {1}".format(input("Enter first name : "), input("Enter second name : ")))

クラスとメソッド...勉强と补充を続けます!!