[Python]ベース構文


ノート[김왼손의 왼손코딩] 한입에 쏙 파이썬: 3시간 만에 끝내는 파이썬 기초 (저자 직강) インベントリ
[値1,値2,値3,...]
my_list = [ 1,2,3,4,'앨리스',[1.0,1.2] ]
リストには任意のデータ型が含まれますので、リストにはリストが含まれます.
list.append()
リストの最後に追加
>>> clovers =[]
>>> clovers.append('클로버1')
>>> print(clovers)
['클로버1']
>>> clovers.append('하트2')
>>> print(clovers)
['클로버1', '하트2']
>>> clovers.append('클로버3')
>>> print(clovers)
['클로버1', '하트2', '클로버3']
値の取得list[인덱스값]
>>> clovers = ['클로버1','하트2','클로버3']
>>> print(clovers[1])
하트2
クリア値
delリスト[削除するインデックス]del:キーワードの1つ.削除されたインデックスの後にある値を塗りつぶして空に押し出します.키워드:予め定められた名称
>>> clovers = ['클로버1','클로버2','클로버3']
>>> print(clovers[1])
클로버2
>>> del clovers [1]
>>> print(clovers)
['클로버1', '클로버3']
>>> print(clovers[1])
클로버3
複数の値の取得리스트[시작_인덱스:끝_인덱스+1]
>>> week = ['월','화','수','목','금','토','일']
>>> print(week)
['월', '화', '수', '목', '금', '토', '일']
>>> print(week[2:5])
['수', '목', '금']
個数리스트.count(세어볼_값)
['도도새', '오리', '체셔고양이']
>>> cards = ['하트','클로버','하트','다이아']
>>> print(cards.count('하트'))
2
>>> print(cards.count('클로버'))
1
基本構造用for変数inリスト:
実行するコマンド
>>> for num in [0,1,2]:
	print(num)

	
0
1
2
for変数in文字列:
実行するコマンド
>>> for letter in '체셔고양이':
	print(letter)

	
체
셔
고
양
이
range(끝_값+1)
>>> for num in range(3):
	print(num)

	
0
1
2
range(시작_값, 끝_값+1)
>>> for y in range(1,10):
	print(2,'x',y, '=',2*y)

	
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
if基本構造
if (조건문1):
	(실행할 명령1)
elif (조건문2:
	(실행할 명령2)
else:
	(실행할_명령3)
Example
 score= 75
 if 80 < score<=100:
	print('학점은 A입니다.')
 elif 60 < score <=80:
 	print('학점은 B입니다.')
 elif 40< score<=60:
 	print('학점은 C입니다.')
 else:
 	print('학점은 F입니다.')
複数の条件を判断
条件1 and条件2
条件1 or条件2not条件
Example
games = 12
points = 25
if games >=10 and points >=20:
	print('MVP로 선정되었습니다.')
既定の構造
while 조건:
	실행할_명령
Example
count=0
while count<3:
	count = count+1
    if count == 2:
    	break
    print(count)
入力の受信方法input():文字列で入力
トーン
(値1、値2、値3)
値1、値2、値3-かっこなし.
リストとほぼ同じですが、値は変更できません.任意のデータ型とともに保存可能
>>> my_int=(1)
>>> print(type(my_int))
<class 'int'>

>>> my_tuple = (1,)
>>> print(type(my_tuple))
<class 'tuple'>

>>> clovers = ('클로버1','하트2','클로버3')
>>> clovers[1]
'하트2'
>>> clovers[1]='클로버2'#튜플은 한번 설정하면 변경 불가
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    clovers[1]='클로버2'
TypeError: 'tuple' object does not support item assignment
梱包および未梱包패킹:複数の変数を1つの位置に配置し、パッケージの結果は常にトーン調整されます.언패킹:包装されたものを複数の変数に入れる
>>> clovers = '클로버','클로버2','클로버3' #패킹
>>> print(clovers)
('클로버', '클로버2', '클로버3')
>>> alice_blue = (240,248,255) #언패킹
>>> r,g,b=alice_blue
>>> print('R:',r,'G:',g,'B:',b)
R: 240 G: 248 B: 255
値の変更
他の言語と異なり、Pythonはtmp変数を必要とせず、2つの変数は互いに値を変換することができる.
dodo= '박하맛'
alice = '딸기맛'
print('도도새:',dodo,'앨리스:',alice)
dodo, alice = alice, dodo #오른쪽변수의  값들이  
                          #패킹 되서 왼쪽변수에 언패킹됨
print('도도새:',dodo,'앨리스:',alice)
しゅつりょく
渡鳥:ミント味アリス:イチゴ味
渡り鸟:ストロベリー味アリス:ミント味
専制的
{鍵1:値1,鍵2:値2,…}
>>> my_dict1={}
>>> print(my_dict1)
{}
>>> my_dict2={0:1,1:-2,2:3.14}
>>> print(my_dict2)
{0: 1, 1: -2, 2: 3.14}
>>> my_dict3 = {'이름': '앨리스', '나이':10,'시력':[1.0,1.2]}
>>> print(my_dict3)
{'이름': '앨리스', '나이': 10, '시력': [1.0, 1.2]}
キー値の追加딕셔너리[추가할_키] = 추가할_값:追加するキーが既に存在するキー値の場合、キー値は変更されます.存在しない場合は追加されます.
アクセス値딕셔너리.get(접근할_키)キー値の削除del 딕셔너리[제거할_키]
>>> clover = {'나이':27,'직업':'병사','번호':9}
>>> print(clover['번호'])
9
>>> clover['번호']=6
>>> print(clover['번호'])
6
>>> print(clover.get('번호'))
6
>>> del clover['나이']
>>> print(clover)
{'직업':'병사','번호':9
Python関数タイプ
組み込み関数
モジュール内の関数
カスタム関数
関数の基本構造
def 함수_이름(인수):
	실행할_명령
    return 반환값
def add(num1,num2):
	return num1+num2
    
print(add(2,3))
モジュール
関数に似たファイル.Python基本モジュールもあれば、個人が作成したモジュールもあります.import 모듈_이름 random.choice(리스트):リストからパラメータをランダムに抽出し、tupleにも適用random.sample(리스트,뽑을_개수):複数の値がリストから重複なく抽出される.random.randint(시작_값,끝_값):開始値と終了値を含む整数の1つをランダムに抽出
import random
animals = ['체셔고양이','오리','도도새']
print(random.choice(animals))
print(random.sample(anminals,2))