MapleStory Finger Point

๐Ÿ’Ž Python/๐Ÿ“šํŒŒ์ด์ฌ for Beginner (2ํŒ)

ch 7 ์—ฐ์Šต๋ฌธ์ œ ํ’€์ด - ํŒŒ์ด์ฌ for Beginner (2ํŒ)

HYEJU01 2024. 3. 28. 02:16

1) ์˜ค๋ฅ˜๋‚˜๋Š” ๋ถ€๋ถ„์„ ๊ณ ๋ฅด์‹œ์˜ค

print(aa[3]) ๋ถ€๋ถ„์ด ์˜ค๋ฅ˜๋‚œ๋‹ค.

aa = [10,20,30] ์œผ๋กœ ์ธ๋ฑ์Šค๊ฐ‘ 2๊นŒ์ง€๋งŒ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•˜๊ณ 

3์€ ์—†๋Š” ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

2) ๋ฆฌ์ŠคํŠธ 100๊ฐœ ๋งŒ๋“ค๊ณ  ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ (๋นˆ์นธ์ฑ„์šฐ๊ธฐ)

myList = []
for i in range (0, 100) :
	myList.append(0)
len(myList)

0๋ถ€ํ„ฐ 99๊นŒ์ง€ = 100๋ฒˆ append (0) ์œผ๋กœ ๋ชจ๋“  ๊ฐ’์ด 0์ธ ๋ฆฌ์ŠคํŠธ 100๊ฐœ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

len ๊ฐ’์€ 100์ด ๋จ

 

3) ์ฝ”๋“œ ์™„์„ฑํ•˜์‹œ์˜ค

myList = []
for i in range(0,3) :
	myList.append(0)
hap = 0

for i in range(0,3) :
	myList[i] = int(input(str(i+1)+"๋ฒˆ์งธ์ˆซ์ž:"))
    
for k in range(0,3):
	hap = hap + myList[k]
print("ํ•ฉ๊ณ„ => %d" % hap)

 

 

4) ๋น„์–ด์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ฝ”๋“œ , ๋งž๋Š” ๊ฒƒ๋ผ๋ฆฌ ์ด์œผ์‹œ์˜ค

aa = [] ๋ฆฌ์ŠคํŠธ

bb = {} ๋”•์…”๋„ˆ๋ฆฌ

cc = () ํŠœํ”Œ

dd = None ์ผ๋ฐ˜๋ณ€์ˆ˜

 

 

5) ๋‹ค์Œ ๋ฆฌ์ŠคํŠธ , ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธกํ•˜์‹œ์˜ค

1. aoa[2]  => ( 012 3๋ฒˆ์งธ์— ์œ„์น˜ํ•œ )  ์ง€๋ฏผ 

2. aoa[-2] => (๋’ค์—์„œ ๋‘๋ฒˆ์งธ) ๋ฏผ์•„

3. aoa[0:1] => (0๋ถ€ํ„ฐ1-1๊นŒ์ง€) ์„คํ˜„

4. aoa[6:] =>(0123456๋ถ€ํ„ฐ ๋๊นŒ์ง€) ๋ฏผ์•„,์ฐฌ๋ฏธ

5. aoa[-7:-5] =>(87654321 , ๋’ค์—์„œ 7๋ฒˆ์งธ ~ -5-1 = 7,6 ์ถœ๋ ฅ) ์ดˆ์•„ ์ง€๋ฏผ 

6. aoa[1::3] =>(1๋ถ€ํ„ฐ 3์”ฉ ๊ฑด๋„ˆ ์ถœ๋ ฅ) ์ดˆ์•„,์œ ๊ฒฝ,์ฐฌ๋ฏธ

 

6) ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘ ํ•จ์ˆ˜ ๊ณ ๋ฅด์‹œ์˜ค

๋ฆฌ์ŠคํŠธ ๋’ค์— ๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€ = append()

์ง€์ •ํ•œ ๊ฐ’ ์ œ๊ฑฐ = remove()

์ˆœ์„œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ๋งŒ๋“ฌ  = reverse()

ํ•ญ๋ชฉ์„ ๋นผ๋‚ด๊ณ  ๋นผ๋‚ธ ํ•ญ๋ชฉ์€ ์‚ญ์ œ = pop()

 

7) 2์ฐจ์› ๋ฆฌ์ŠคํŠธ, ๊ฒฐ๊ณผ ์˜ˆ์ธก

myList = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

myList[0][1] =>2 (1234 ์•ˆ์—์„œ์˜ = ๋‘๋ฒˆ์งธ ๊ฐ’)

myList[1][3] => 8 (5678์˜ 0,1,2,3  =๋„ค๋ฒˆ์งธ๊ฐ’)

myList[2] => 9,10,11,12 (0,1,2 = 3๋ฒˆ์งธ ๊ฐ’ ์ถ”์ถœ)

 

 

8) ๋‹ค์Œ ์ฝ”๋“œ ์‹คํ–‰์‹œ myData  ์ €์žฅ๋œ ๊ฐ’์€ ๋ช‡๊ฐœ์ธ๊ฐ€

myDate = {1,1,1,2,2,2,3,3,3}

myDate.add(1)

์„ธํŠธ

์ค‘๋ณต ๊ฐ’ ์ž…๋ ฅํ•ด๋„ ํ‚ค๋Š” ํ•˜๋‚˜๋งŒ ๋‚จ๋Š”๋‹ค.

์ฆ‰ 3๊ฐœ

 

 

9) myData ์— ์ €์žฅ๋œ ๊ฐ’์€?

import math

myData = [int(math.pow(2,num)) for num in range(0,5) if num % 2 != 0]

์ปดํ”„๋ฆฌํ—จ์…˜

์ˆ˜์‹ for ํ•ญ๋ชฉ in range() if ์กฐ๊ฑด์‹

 

์ˆ˜์‹ : 0 1 2 3 4 ์˜ 2^n ๊ฐ’

์กฐ๊ฑด์‹ : ์ง์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฐ’

1,3

2^1,  2^3 ๊ฐ’์ด myData์— ์ €์žฅ๋จ

์ฆ‰ [2,8]