Python Syntax

APS ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ๊ทธ ๋•Œ ๊ทธ ๋•Œ ํ•„์š”ํ•œ ๋ฌธ๋ฒ•๋“ค ์ •๋ฆฌ

์ดํ›„์— Syntax ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ๋“ค์— ๋ชจ๋‘ ์žˆ๋Š” ( ์•„๋งˆ ๊ฑฐ์˜ ? ) ๋‚ด์šฉ์ด๊ธด ํ•จ

  • ์†Œ์ˆ˜์  ํ‘œ๊ธฐ

    a=float(input())
    print("{:.6f}".format(a))
  • ๋’ค์ง‘๊ธฐ

    a=[1,2,3]
    a.reverse()
    print(a)
    
    => [3,2,1]
  • iterable ์„ ํŠน์ •ํ•œ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ธฐ

    print('!'.join(word))   ###join ์•ž์— ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๋ฅผ ๊ธฐ์ž…ํ•ด์คŒ
    print(' '.join(word))
  • .์œผ๋กœ ๋‚˜๋‰œ ์ธํ’‹ ๋ฐ›๊ณ , ์›ํ•˜๋Š” ๋งŒํผ ๊ณต๋ฐฑ๋„ฃ๊ธฐ ( https://dojang.io/mod/page/view.php?id=2300 )

    a,b,c = input().split('.')
    print("{:0>4}.{:0>2}.{:0>2}".format(a,b,c))
  • ์ง„์ˆ˜

    value = 60
    
    b = bin(value) = "0b~"
    o = oct(value) = "0o~"
    h = hex(value) = "0x~"
    
    b = "{:#b}".format(value)
    o = "{:#o}".format(value)
    h = "{:#h}".format(value)
    
    value = int("0o13",8) = 11
    value = int("0b11",2) = 3
    value = int("0xf",16) = 15
  • ํŠน์ •ํ•œ ๋ฌธ์ž ์ œ๊ฑฐํ•˜๊ธฐ

    a = oct(int(input())
    # a = '0o12'
    
    print(a.lstrip('0o')
  • ์•„์Šคํ‚ค ์ฝ”๋“œ

    print(chr(45))
    print(ord("A"))
  • ๋น„ํŠธ์—ฐ์‚ฐ

    ๋น„ํŠธ ์—ฐ์‚ฐ์ž (Bitwise Operators):
    a = 60, b = 13 ์ด๋ผ ๊ฐ€์ •ํ•œ๋‹ค.
    
    a = 0011 1100
    
    b = 0000 1101
    
    Operator    Description    Example
    &    AND ์—ฐ์‚ฐ. ๋‘˜๋‹ค ์ฐธ์ผ๋•Œ๋งŒ ๋งŒ์กฑ    (a & b) = 12 โ†’ 0000 1100
    |    OR ์—ฐ์‚ฐ. ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์ฐธ์ด์—ฌ๋„ ๋งŒ์กฑ    (a | b) = 61 โ†’ 0011 1101
    ^    XOR ์—ฐ์‚ฐ. ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์ฐธ์ผ ๋•Œ ๋งŒ์กฑ    (a ^ b) = 49 โ†’ 0011 0001
    ~    ๋ณด์ˆ˜ ์—ฐ์‚ฐ.    (~a) = -61 โ†’ 1100 0011
    <<    ์™ผ์ชฝ ์‹œํ”„ํŠธ ์—ฐ์‚ฐ์ž. ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์™ผ์ชฝ์œผ๋กœ ์ง€์ •๋œ ๋น„ํŠธ ์ˆ˜ ๋งŒํผ ์ด๋™    a << 2 = 240 โ†’ 1111 0000
    >>    ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธ ์—ฐ์‚ฐ์ž. ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ง€์ •๋œ ๋น„ํŠธ ์ˆ˜ ๋งŒํผ ์ด๋™    a >> 2 = 15 โ†’ 0000 1111
  • ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ์…˜

    array = [i for i in range(20) if i % 2 == 1 ]
    array = [i * i for i in range(1,10)]
  • ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ๊ฐ’์˜ ์›์†Œ ๋ชจ๋‘ ์ œ๊ฑฐ

    a = [1,2,3,4,5,5,5]
    remove_set = {3,5}
    
    result = [i for i in a if not in remove_set]
    print(result)
    
    [1,2,4]

Last updated