MapleStory Finger Point

πŸ’Ž Python/πŸ“šν˜Όκ³΅νŒŒ

파이썬 정리 #혼곡파

HYEJU01 2024. 4. 8. 01:00

νŒŒμ΄μ¬μ‹€ν–‰ :: μΈν„°λ ‰ν‹°λΈŒ μ…Έ (λŒ€ν™” ν˜• μ…Έ)

파이썬으둜 μž‘μ„±λœ μ½”λ“œλ₯Ό μ‹€ν–‰ν•΄μ£ΌλŠ” ν”„λ‘œκ·Έλž¨ = 인터프리터

파이썬 ν”„λ‘œκ·Έλž¨μ„ 싀햏아면 μΈν„°λ ‰ν‹°λΈŒ 셸이 싀행됨

ν…μŠ€νŠΈ 에디터 :: IDLE 에디터

νŒŒμ΄μ¬μ€ ν†΅ν•©κ°œλ°œν™˜κ²½μœΌλ‘œ IDLE λ₯Ό μ œκ³΅ν•œλ‹€

!https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fd27b2f1-3e99-46bc-83ad-95812b70e0ac/Untitled.png

개발폰트

D2Coding μ΄λ‚˜ λ‚˜λˆ”κ³ λ”• μ½”λ”©

ν…μŠ€νŠΈ 에디터 :: λΉ„μ£Όμ–Ό μŠ€νŠœλ””μ˜€ μ½”λ“œ

파이썬 ν™•μž₯자

.py

shift + 였λ₯Έμͺ½λ§ˆμš°μŠ€ ⇒ Power Shell 클릭

파이썬 μš©μ–΄λ“€

ν‘œν˜„μ‹ - λ¬Έμž₯ - ν”„λ‘œκ·Έλž¨

ν‘œν˜„μ‹ : κ°’!!을 λ§Œλ“€μ–΄ λ‚΄λŠ” κ°„λ‹¨ν•œ μ½”λ“œ

λ¬Έμž₯ : ν‘œν˜„μ‹μ΄ ν•˜λ‚˜ 이상 λͺ¨μΈλ‹€

ν”„λ‘œκ·Έλž¨ : λ¬Έμž₯이 λͺ¨μΈλ‹€

ν‚€μ›Œλ“œ

ν‚€μ›Œλ“œ :: νŠΉλ³„ν•œ μ˜λ―Έκ°€ λΆ€μ—¬λœ 단어

***νŒŒμ΄μ¬μ€ λŒ€μ†Œλ¬Έμžλ₯Ό ꡬ뢄함! ex) True ≠ true

import keyword
print(keyword.kwlist)

μ‹λ³„μž

μ‹λ³„μž:: ν”„λ‘œκ·Έλž˜λ°μ–Έμ–΄μ—μ„œ 이름을 λΆ™μΌλ•Œ μ‚¬μš©ν•˜λŠ” 단어

μ‹λ³„μž 생성 κ·œμΉ™

  • ν‚€μ›Œλ“œ μ‚¬μš© x
  • νŠΉμˆ˜λ¬ΈμžλŠ” _ μ–Έλ”λ°”λ§Œ
  • 숫자 μ‹œμž‘ μ•ˆλ¨
  • 곡백 포함 μ•ˆλ¨

κ°œλ°œμžλ“€ μ‚¬μ΄μ—μ„œμ˜ κ·œμΉ™

  • μŠ€λ„€μ΄ν¬ μΌ€μ΄μŠ€ :: λ‹¨μ–΄μ‚¬μ΄μ—λŠ” (_)언더바 μ‚¬μš© **λ³€μˆ˜ or ν•¨μˆ˜ (ν•¨μˆ˜λŠ” 뒀에 κ΄„ν˜ΈμžˆμŒ)
  • 캐멀 μΌ€μ΄μŠ€ :: 단어 μ²«κΈ€μžλ₯Ό λŒ€λ¬Έμžλ‘œ λ§Œλ“¬ (νŒŒμ΄μ¬μ—μ„œλŠ” 무쑰건 μ‹Ήλ‹€ λŒ€λ¬Έμž μ‹œμž‘) ***클래슀

주석

  • #을 λΆ™μ—¬μ„œ μ£Όμ„μ²˜λ¦¬ν•œλ‹€.

μ—°μ‚°μž

  • κ°’κ³Ό κ°’μ‚¬μ΄μ—μ„œ κΈ°λŠ₯을 μ μš©ν•  λ•Œμ‚¬μš©ν•˜λŠ”κ²ƒ

자료 = λ¦¬ν„°λŸ΄

  • κ°’μžμ²΄

좜λ ₯

  • print(274)
  • print(275,3)
  • print() == μ€„λ°”κΏˆ

 


λ¬Έμžμ—΄ λ§Œλ“€κΈ°

"" or ''

print(" 'μ•ˆλ…•ν•˜μ„Έμš”' !! ");

print("\"μ•ˆλ…•ν•˜μ„Έμš”\"!!");

\" or \' or \n or \t or \\

\

""" μ•ˆλ…•ν•˜μ„Έμš” ~~ """ [μ—¬λŸ¬μ€„λ¬Έμžμ—΄κΈ°λŠ₯ enter]

""" μ•ˆλ…•ν•˜μ„Έμš” ~~""" [enterμ€„λ°”κΏˆμ—†μ΄ 좜λ ₯ν•˜κ² λ‹€]

λ¬Έμžμ—΄ μ—°κ²° μ—°μ‚°μž +

ex )

print("μ•ˆλ…•ν•˜μ„Έμš”!!"+"호호호");

print("μ•ˆλ…•ν•˜μ„Έμš”!!"+1) → 였λ₯˜

print("μ•ˆλ…•ν•˜μ„Έμš”" + "1")

λ¬Έμžμ—΄ 반볡 μ—°μ‚°μž *

print("μ•ˆλ…•ν•˜μ„Έμš”!!" *3);

문자 선택 μ—°μ‚°μž (인덱싱) []

print("μ•ˆλ…•ν•˜μ„Έμš”!!"[0]); → μ•ˆ

print("μ•ˆλ…•ν•˜μ„Έμš”!!"[-3]); → μš”

μ•ˆ λ…• ν•˜ μ„Έ μš” ! !

-7 -6 -5 -4 -3 -2 -1

λ¬Έμžμ—΄ λ²”μœ„ 선택 μ—°μ‚°μž (μŠ¬λΌμ΄μ‹±) [:]

print("μ•ˆλ…•ν•˜μ„Έμš”"[1:4]); → λ…•ν•˜μ„Έ

[1:4] λŠ” n-1 λ²ˆμ§ΈκΉŒμ§€λ§Œ κ°€μ Έμ˜€λ―€λ‘œ 1,2,3 인덱싱 κ°’λ§Œ κ°€μ Έμ˜΄

[1: ] κ°€μž₯ λ§ˆμ§€λ§‰ μœ„μΉ˜μ˜ κ°’

[ :3] κ°€μž₯ 첫번째 μœ„μΉ˜μ˜ κ°’

λ¬Έμžμ—΄ 길이 κ΅¬ν•˜κΈ°

len() ν•¨μˆ˜

print(len("μ•ˆλ…•ν•˜μ„Έμš”")) → 5

숫자 μ’…λ₯˜

μ •μˆ˜ν˜•, μ‹€μˆ˜ν˜•(λΆ€λ™μ†Œμˆ˜μ )

숫자 μ—°μ‚°μž

사칙 μ—°μ‚° : +,-,*,/

μ •μˆ˜ λ‚˜λˆ„κΈ° : // → μ†Œμˆ˜μ  μ•„λž˜λ₯Ό 떼어버린 값이 좜λ ₯됨

print("3 / 2=",3/2) → 3 / 2 = 1.5

print("3 / 2=",3//2) → 3 / 2 = 1

λ‚˜λ¨Έμ§€ μ—°μ‚°μž : %

제곱 μ—°μ‚°μž : **

μ—°μ‚°μž μš°μ„ μˆœμœ„ : γ…‡γ…‡μ•Œμ•„λ‘λ©΄ μ’‹μŒ

λ³€μˆ˜

pi = 3.14159265

pi + 2

νŒŒμ΄μ¬μ€ μžλ£Œν˜•μ„ μ§€μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

같은 μžλ£Œν˜•μ— μ—¬λŸ¬ μ’…λ₯˜μ˜ μžλ£Œν˜•μ„ 넣을 수 μžˆμ§€λ§Œ λ˜λ„λ‘μ΄λ©΄ ν•˜λ‚˜μ˜ λ³€μˆ˜μ—λŠ” ν•˜λ‚˜μ˜ μžλ£Œν˜•λ§Œγ…‡γ…‡

  • λ³΅ν•©λŒ€μž…μ—°μ‚°μž :: += ~~ **=
  • λ¬Έμžμ—΄λ³΅ν•©λŒ€μž…μ—°μ‚°μž += (λ¬Έμžμ—΄μ—°κ²°ν›„ λŒ€μž…) , *= (λ¬Έμžμ—΄λ°˜λ³΅ν›„λŒ€μž…)

μ‚¬μš©μžμž…λ ₯ input()

input("인사말을 μž…λ ₯ν•˜μ‹œμ£  >")

μ‹€ν–‰ 도쀑에 μž μ‹œ λ©ˆμΆ€μƒνƒœ == λΈ”λ‘μƒνƒœ

μžλ£Œν˜• μ•Œμ•„λ³΄κΈ° type()

number = input("숫자λ₯Όμž…λ ₯ν•˜μ„Έμš”>")

12345

print(type(number))

  • input()ν•¨μˆ˜μ˜ κ²°κ³ΌλŠ” 무쑰건 λ¬Έμžμ—΄ μžλ£Œν˜•μ΄λ‹€.

λ¬Έμžμ—΄μ„ 숫자둜 λ°”κΎΈκΈ° == 캐슀트

input() ν•¨μˆ˜ μžλ£Œν˜•μ€ 항상 λ¬Έμžμ—΄ → 숫자둜 λ°”κΎΈκ³ μ‹ΆμŒ

  • int()
  • float()

string_a = input("μž…λ ₯ >")

int_a=int(string_a)

숫자λ₯Ό λ¬Έμžμ—΄λ‘œ λ°”κΎΈκΈ°

  • str(λ‹€λ₯Έμžλ£Œν˜•)

λ¬Έμžμ—΄μ˜ format()ν•¨μˆ˜

숫자λ₯Ό λ¬Έμžμ—΄λ‘œ λ°˜ν™˜

format_a = "{}λ§Œμ›".format(5000)

print(format_a)

->
5000λ§Œμ›

format() λ¬Έμžμ—΄μ΄ 가지고 μžˆλŠ” ν•¨μˆ˜

  • μ€‘κ΄„ν˜Έ{}, λ§ˆμΉ¨ν‘œ(.)
  • μ€‘κ΄„ν˜Έμ™€ formatν•¨μˆ˜ λ§€κ°œλ³€μˆ˜ κ°œμˆ˜λŠ” 같아야함
  • "{}".format(10)
  • "{}λ§Œμ›".format(100)
  • {:5d} νŠΉμ • 칸에 좜λ ₯
  • {:05d} 빈칸 μ±„μš°κΈ° 0으둜
  • {:+d} κΈ°ν˜Έμ™€ ν•¨κ»˜ 좜λ ₯ν•˜κΈ° //음수 μ–‘μˆ˜
  • {: d} μŒμˆ˜μΌλ•Œλ§Œ - λΆ€ν˜Έ ν‘œμ‹œ

float μžλ£Œν˜•

  • {:f}
  • {:15f} 15μΉΈ
  • {:+15f} λΆ€ν˜Έ
  • {:+015f} λΆ€ν˜Έ μΆ”κ°€ ν›„ 0으둜 μ±„μš°κΈ°
  • {:15.2f} μ†Œμˆ˜μ  μ•„λž˜ 자릿수
  • "{:g} μ†Œμˆ˜μ  μ œκ±°ν•˜κΈ°

λŒ€μ†Œλ¬Έμžλ°”κΎΈκΈ° uuper() lower()

λΉ„νŒŒκ΄΄μ ν•¨μˆ˜

λ¬Έμžμ—΄ μ–‘μ˜† 곡백 제거 strip() lstrip() rstrip()

""" """ μ˜λ„ν•˜μ§€ μ•Šμ€ μ€„λ°”κΏˆ μ œκ±°μ‹œ strip() ν•¨μˆ˜ μ‚¬μš© == trim이라고도 뢀름

λ¬Έμžμ—΄μ˜ ꡬ성 νŒŒμ•… isOO()

101p μ°Έκ³  μ—„μ²­ 많음 ~~

λ¬Έμžμ—΄ μ°ΎκΈ° find() rfind()

find μ™Όμͺ½λΆ€ν„° 처음 λ“±μž₯ν•˜λŠ” μœ„μΉ˜

rfind 였λ₯Έμͺ½λΆ€ν„° 처음 λ“±μž₯ν•˜λŠ” μœ„μΉ˜

λ¬Έμžμ—΄κ³Ό inμ—°μ‚°μž

λ¬Έμžμ—΄ 내뢀에 μ–΄λ–€ λ¬Έμžμ—΄μžˆλŠ”μ§€ 확인

좜λ ₯은 bool ν˜•νƒœλ‘œ λ‚˜μ˜΄.

λ¬Έμžμ—΄ 자λ₯΄κΈ° split()

κ΄„ν˜Έμ•ˆμ˜ λ¬Έμžμ—΄μ„ κΈ°μ€€μœΌλ‘œ 자λ₯Έλ‹€.

 

a = "10 20 30".split(" ")

print(a)

 

['10','20','30'] == λ¦¬μŠ€νŠΈκ°€ λ‚˜μ˜΄!

 


Boolean 뢈린, λΆˆλ¦¬μ–Έ / Bool 뢈

였직 True μ°Έ / False 거짓

λΉ„κ΅μ—°μ‚°μž

==

<

λ…Όλ¦¬μ—°μ‚°μž

not → 단항 μ—°μ‚°μž

and → λͺ¨λ‘ μ°ΈμΌλ•Œλ§Œ True

or → ν•˜λ‚˜λ§Œ 참이어도 True

if 쑰건문

if ν‘œν˜„μ‹ :
	μ°Έ λ¬Έμž₯
	거짓 λ¬Έμž₯
//λ“€μ—¬μ“°κΈ° 4번
number = input("μ •μˆ˜μž…λ ₯>")
number  int(number)

if number > 0:
	print("μ–‘μˆ˜μž…λ‹ˆλ‹€")

if number < 0:
	print("μŒμˆ˜μž…λ‹ˆλ‹€")
	

if number == 0
	print("0μž…λ‹ˆλ‹€")

λ‚ μ§œ/μ‹œκ°„ ν™œμš©

import datetime

now = datetime.datetime.now()

print(now.year, "λ…„")
print(now.month, "μ›”")
print(now.day, "일")
print(now.hour, "μ‹œ")
print(now.minute, "λΆ„")
print(now.second, "초")

λͺ¨λ“ˆ κΈ°λŠ₯을 ν™œμš©ν•˜μ—¬ datetime κΈ°λŠ₯을 κ°€μ Έμ˜΄

import datetime

now = datetime.datetime.now()

print("{}λ…„ {}μ›” {}일 {}μ‹œ {}λΆ„ {}초".format(
	now.year,
.
.
.
))

-> 2021λ…„ ~~
import datetime

now = datetime.datetime.now()

if now.hour < 12:
		print("ν˜„μž¬μ‹œκ°μ€ {}μ‹œλ‘œ μ˜€μ „ μž…λ‹ˆλ‹€!".format(now.hour))

if now.hour >= 12:
		print("ν˜„μž¬μ‹œκ°μ€ {}μ‹œλ‘œ μ˜€ν›„ μž…λ‹ˆλ‹€!".format(now.hour))

μ»΄ν“¨ν„°μ˜ 쑰건

last_character = number[-1]
last_number = int(last_character)

if last_number == 1 \\
		or last_number == 3 \\
.
.
.
print("ν™€μˆ˜μž…λ‹ˆλ‹€")

//   " \\ " μ€„λ°”κΏˆ 기호
last_character = number[-1]
last_number = int(last_character)

if last_character in "02468":
	print("ν™€μˆ˜μž…λ‹ˆλ‹€")

number = int(num)

if number % 2 == 0:
	print("짝수")

if number % 2 == 1;
	print("ν™€μˆ˜")

if ~ else elif

if 쑰건:
		쑰건 μ°Έ λ¬Έμž₯
else:
		쑰건 거짓 λ¬Έμž₯
number = int(num)

if number % 2 == 0:
	print("짝수")
else :
	print("ν™€μˆ˜")

elifꡬ문

if 쑰건A:
		Aμ°ΈμΌλ•Œ λ¬Έμž₯
elif 쑰건B:
		Bμ°ΈμΌλ•Œ λ¬Έμž₯
elif 쑰건C:
		C참일떄 λ¬Έμž₯
else:
	λͺ¨λ“  쑰건 거짓 λ¬Έμž₯

μ„Έ 개 μ΄μƒμ˜ 쑰건을 μ—°κ²°ν•΄μ„œ μ‚¬μš©

False둜 λ³€ν™˜λ˜λŠ” κ°’

if 쑰건문 λ§€κ°œλ³€μˆ˜μ— Bool μ•„λ‹Œ 값이 였면 μžλ™ Bool λ³€ν™˜ μ²˜λ¦¬ν•œλ‹€.

None, 숫자 0, 0.0 , 빈 μ»¨ν…Œμ΄λ„ˆ == false

이외 == true

pass ν‚€μ›Œλ“œ

ν”„λ‘œκ·Έλž˜λ° 골격 작기

pass ν‚€μ›Œλ“œ 의미 == 아무것도 μ•ˆν•¨, 곧 개발 μ˜ˆμ •

 

if zero == 0
		λΉˆμ€„ μ‚½μž…
else: 
		λΉˆμ€„ μ‚½μž…
number = input("μ •μˆ˜>")
number = int(number)

if number > 0:
	#μ–‘μˆ˜ μΌλ•Œ :
else:
	#음수 μΌλ•Œ :

------------------

number = input("μ •μˆ˜>")
number = int(number)

if number > 0:
	#μ–‘μˆ˜ μΌλ•Œ :
	pass
else:
#음수 μΌλ•Œ :
	pass

 

 

리슀트

 

[ ] λŒ€κ΄„ν˜Έμ— μ—¬λŸ¬μ’…λ₯˜μ˜ 자료λ₯Ό λ„£μ–΄ μ„ μ–Έν•œλ‹€.

리슀트λ₯Ό 좜λ ₯ν•˜λ©΄ λͺ¨λ“  μžλ£Œκ°€ 좜λ ₯됨

리슀트 μš”μ†Œμ— μ ‘κ·Όν•˜λŠ” 법

μΈλ±μŠ€λŠ” 0 λΆ€ν„° μ‹œμž‘ν•œλ‹€.

>>> list[0]
273

>>>list[1:3] //μ•ž 숫자 λ‘λ²ˆμ§Έ κΈ€μžκΉŒμ§€ 즉, 12
[32, 103]

>>> list[0] = "μ•ˆλ…•"
>>> llst[0]
μ•ˆλ…•

1 λŒ€κ΄„ν˜Έ μ•ˆμ— 음수λ₯Ό λ„£μ–΄ λ’€μ—μ„œ λΆ€ν„° μš”μ†Œ 선택

>>>list = [273,32,103,"hi",True,False]
>>>list[-1]
false
>>>list[-2]
True
      1. 리슀트 μ ‘κ·Ό μ—°μ‚°μžλ₯Ό μ΄μ€‘μœΌλ‘œ μ‚¬μš©κ°€λŠ₯
>>>list = [273,32,103,"hi",True,False]
>>>list[3][0]
'h'
      1. λ¦¬μŠ€νŠΈμ•ˆμ— 리슀트 μ‚¬μš©
>>>list = [[1,2,3],[4,5,6]]
>>>list[1]
[4,5,6]
>>>list[1][2]
6

쑴재 ν•˜μ§€ μ•Šμ€ μœ„μΉ˜μ—μ„œ μš”μ†Œλ₯Ό κΊΌλ‚΄λ €κ³ ν•˜λ©΄ μ˜ˆμ™Έ λ°œμƒν•˜λ‹ˆ 주의

리슀트 μ—°μ‚°μž

      1. μ—°κ²° + <λΉ„νŒŒκ΄΄μ >
list_a = [1,2,3]
list_b = [4,5,6]

list_a + list_b = [1,2,3,4,5,6]
      1. 반볡 *
list_a = [1,2,3]
list_b = [4,5,6]

list_a * 3 = [1,2,3,1,2,3,1,2,3]
      1. κΈΈμ΄κ΅¬ν•˜κΈ° len()
list_a = [1,2,3]
list_b = [4,5,6]

len(list_a) = 3
      1. μš”μ†Œ μΆ”κ°€
list_a = [1,2,3]

list_a.append(4)
print(list_a)

# [1,2,3,4]
      1. 쀑간에 μš”μ†Œ μΆ”κ°€

μš”μ†Œκ°€ λ’€λ‘œ λ°€λ¦Ό

list_a = [1,2,3]

list_a.insert(0,10)
print(list_a)

#[10,1,2,3]
      1. μ—¬λŸ¬κ°œ μš”μ†Œ <파괴적>

append 3번 반볡효과

>>>list_a = [1,2,3]
>>>list_a.extend([4,5,6])
>>>print(list_a)
[1,2,3,4,5,6]

λΉ„νŒŒκ΄΄μ  : 원본에 영ν–₯ x

파괴적 : 원본을 변경함

리슀트 μš”μ†Œ 제거

      1. del
list_a = [1,2,3,4]

del list_a[1]
print(list_a)

#[1,3,4]
---------------
>>>del list_a[1:3]
>>>list_a
[1,4] //λ§ˆμ§€λ§‰μš”μ†Œ 포함 μ•ˆν•΄μš©
>>>del list_a[:3] //3번째 κΈ°μ€€(λΆˆν¬ν•¨) 제거
[4]
>>>del list_a[2:] //3번째 κΈ°μ€€(포함) 제거
[1,2]
      1. pop

()κΈ°λ³Έκ°’ -1/ λ§ˆμ§€λ§‰ κ°’ μ œκ±°ν•¨

list_a = [1,2,3,4]

list_a.pop(2)
print(list_a)

#[1,3,4]

      1. remove

νŠΉμ •κ°’ μ§€μš°κΈ°

list.a.remove(2)

#[1,3,4]

4.clear

λͺ¨λ‘ 제거

list.a.clear()

#[]

리슀트 내뢀에 μžˆλŠ”μ§€ ν™•μΈν•˜κΉ…

      1. in
list_a = [1,2,3,4]
2 in list_a
Ture
      1. not in
list_a = [1,2,3,4]
2 not in list_a
False

 

 

for 반볡문

for i in range(100): //range() λ²”μœ„ μžλ£Œν˜•
	print("hi")

for 반볡자 in λ°˜λ³΅ν•  수 μžˆλŠ” 것:
	μ½”λ“œ
list_a = [1,2,3,4]

for element in list_a:
		print(element)

------
1
2
3
4