• [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉ ๊ธฐ์ดˆ ํŠธ๋ ˆ์ด๋‹ Day2

    2024. 2. 8.

    by. @leeeun

    Day2

    a์™€ b ์ถœ๋ ฅํ•˜๊ธฐ

     

    ๋ฌธ์ œ

    ๋‘ ์ •์ˆ˜ a, b๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์˜ ๊ณ„์‚ฐ์‹์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.
    

    ํ’€์ด

    a, b = map(int, input().strip().split(' '))
    print(a,"+",b,"=",a+b)
    

    ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

    a, b = map(int, input().strip().split(' '))
    print(f"{a} + {b} = {a + b}")
    
    • f์ŠคํŠธ๋ง์ด ๋” ํŽธํ•จ

     

    ๋ฌธ์ž์—ด ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•˜๊ธฐ

     

    ๋ฌธ์ œ

    ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด str1, str2๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    ์ž…์ถœ๋ ฅ ์˜ˆ์™€ ๊ฐ™์ด str1๊ณผ str2์„ ์ด์–ด์„œ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.
    
    #์ž…๋ ฅ
    apple pen
    
    #์ถœ๋ ฅ
    applepen
    

     

    ํ’€์ด

    str1, str2 = input().strip().split(' ')
    print(str1+str2)
    

     

    ๋ฌธ์ž์—ด ๋Œ๋ฆฌ๊ธฐ

     

    ๋ฌธ์ œ

    for a in input():
        print(a)
    

     

    ํ’€์ด

    for a in input():
        print(a)
    

     

    ๋ฆฌ๋งˆ์ธ๋“œ

    • sep=” ” : ๊ตฌ๋ถ„์ž ์˜ต์…˜. ๋ฌธ์ž์—ด๋“ค ์‚ฌ์ด์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ. ๊ธฐ๋ณธ๊ฐ’์€ ๊ณต๋ฐฑ(๋„์–ด์“ฐ๊ธฐ)
    • end=” “ : print()์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด ์ถœ๋ ฅ ํ›„ ๋งˆ์ง€๋ง‰์— ๋ถ™๋Š” ๋ฌธ์ž์—ด. ๊ธฐ๋ณธ๊ฐ’์€ ์ค„๋ฐ”๊ฟˆ

    [ Python ] print ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ - ๊ธฐ์ดˆ (๋ฌธ์ž์—ด, sep, end)

     

    ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

    print('\\n'.join(input()))
    
    • join() ํ•จ์ˆ˜
      • “join(๋ฆฌ์ŠคํŠธ) ⇒ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
      • ['a', 'b', 'c'] ์ด๋Ÿฐ ์‹์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ 'abc'์˜ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์ณ์„œ ๋ฐ˜ํ™˜
      • ‘๊ตฌ๋ถ„์ž’.join(๋ฆฌ์ŠคํŠธ) : ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’๊ณผ ๊ฐ’ ์‚ฌ์ด์— ๋“ค์–ด์˜จ ๊ตฌ๋ถ€๋‚ฎ๋ฅผ ๋„ฃ์–ด์„œ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜
      • ex) '_'.join(['a', 'b', 'c']) ๋ผ ํ•˜๋ฉด "a_b_c"

     

    ํ™€์ง ๊ตฌ๋ถ„ํ•˜๊ธฐ

     

    ๋ฌธ์ œ

    ์ž์—ฐ์ˆ˜ n์ด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋งŒ์•ฝ n์ด ์ง์ˆ˜์ด๋ฉด "n is even"์„, ํ™€์ˆ˜์ด๋ฉด "n is odd"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.
    

     

    ํ’€์ด

    a = int(input())
    if a%2==0: print(a,"is even") 
    else: print(a,"is odd")
    

     

    ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

    #1 ๊ฒ๋‚˜ ์‹ ๋ฐ•
    n=int(input())
    print(f"{n} is {'eovdedn'[n&1::2]}")
    
    #2 ํ•œ์ค„๋กœ
    a = int(input())
    print(f'{a} is', 'odd' if a % 2 else 'even')
    
    • #1 n=int(input()): ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. f"{n} is {'eovdedn'[n&1::2]}": f-string์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ํฌ๋งคํŒ…ํ•ฉ๋‹ˆ๋‹ค. n&1์€ ์ž…๋ ฅ๋œ ์ •์ˆ˜ n๊ณผ 1์„ ๋น„ํŠธ ๋‹จ์œ„๋กœ AND ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด n์ด ํ™€์ˆ˜์ธ์ง€ ์ง์ˆ˜์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ 0์ด๋ฉด n์€ ์ง์ˆ˜์ด๊ณ , 1์ด๋ฉด n์€ ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค. ['eovdedn'[n&1::2]๋Š” ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ ๋๊นŒ์ง€ 2์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ ๋ฌธ์ž์—ด์„ ์Šฌ๋ผ์ด์‹ฑํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด 'eovdedn' ์ค‘์—์„œ 'o', 'v', 'e', 'n'์„ ์ฐจ๋ก€๋Œ€๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์ง์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

     

    ๋ฌธ์ž์—ด ๊ฒน์ณ์“ฐ๊ธฐ

     

    ๋ฌธ์ œ

    ๋ฌธ์ž์—ด my_string, overwrite_string๊ณผ ์ •์ˆ˜ s๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. 
    ๋ฌธ์ž์—ด my_string์˜ ์ธ๋ฑ์Šค s๋ถ€ํ„ฐ overwrite_string์˜ ๊ธธ์ด๋งŒํผ์„ ๋ฌธ์ž์—ด overwrite_string์œผ๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
    

     

    ํ’€์ด

    def solution(my_string, overwrite_string, s):
        a=len(overwrite_string)
        answer=my_string[:s]+overwrite_string+my_string[s+a:]
        return answer
    

     

    ๋ฆฌ๋งˆ์ธ๋“œ

    • ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ String[start:end:step]
    • start: ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค
    • end: ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๋ ์ธ๋ฑ
    • step: ํ˜„์žฌ ๋ฌธ์ž์—์„œ step๊ฐ„๊ฒฉ์œผ๋กœ ๋ฌธ์ž๋ฅผ ์ถ”์ถœ. step์„ ์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’์ธ 1๋กœ ์„ค์ •.
    • start ์ธ๋ฑ์Šค๋ถ€ํ„ฐ end-1๊นŒ์ง€ ์ถœ๋ ฅํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ต.

     

    ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

    def solution(my_string, overwrite_string, s):
        return my_string[:s] + overwrite_string + my_string[s + len(overwrite_string):]
    

    ๋Œ“๊ธ€