ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 17 - 파괴적 연산과 비파괴적 연산
    study with Q - 파이썬 2024. 2. 26. 21:54

    #비파괴적

    +(더하기) 연산자는 : 피연산자를 바꾸지 않는다. → 비파괴적 연산자

    a = 314
    a+10
    a*10
    a/2
    a//4
    a**2
    a%5
    
    print(a)
    >>>314
    
    
    a = "크 라 임 씬 리 턴 즈"
    a.split()
    
    b = "{}만원만 생겼으면 좋겠다"
    b.format(10)
    
    print(a)
    >>>크 라 임 씬 리 턴 즈
    print(b)
    >>>{}만원만 생겼으면 좋겠다

     

    #파괴적

    =(이퀄) 연산자는 : 피연산자를 바꾼다. → 파괴적 연산자

    a = 314
    a+=10
    print(a)
    >>324
    a = 314 +a
    print(a)
    >>>638
    
    a = "크 라 임 씬 리 턴 즈"
    a = a.split()
    
    print(a)
    >>>['크', '라', '임', '씬', '리', '턴', '즈']

     

    #upper() 함수

    소문자 → 대문자

    ※ 비파괴적 함수이므로 파괴적 연산인 =을 넣어야 변환이 됨

    cs = "Crime Scene Returns"
    cs = cs.upper()
    
    print(cs)
    >>>CRIME SCENE RETURNS

     

    #lower()함수

    대문자 → 소문자

    ※ 비파괴적 함수이므로 파괴적 연산인 =을 넣어야 변환이 됨

    ep = "SeaSon4 EPisode 7"
    ep = ep.lower()
    print(ep)
    >>>season4 episode 7

     

    #strip() 함수

    문자열 양 옆의 공백을 제거 하는 함수

    • lstrip() : 문자열 왼쪽에 공백을 제거하는 함수
    • rstrip() : 문자열 오른쪽에 공백을 제거하는 함수
    ans = "       [정답] 1. reo"
    tr = "      [해석] 레오는 정말 귀여운 고영이다!!!!!\n \t       \t   "
    
    ans = ans.lstrip( )
    tr = tr.strip( )
    
    print(ans)
    >>>[정답] 1. reo
    print(tr)
    >>>[해석] 레오는 정말 귀여운 고영이다!!!!!

     

    #is어쩌구() 함수

    ※ 비파괴적 함수이므로 파괴적 연산인 =을 넣어야 변환이 됨

    • isalnum( ): 문자열이 알파벳 또는 숫자로만 구성되어 있는지 확인
    • isalpha( ): 문자열이 알파벳으로만 구성되어 있는지 확인
    • isidentifier( ): 문자열이 식별자로 사용할 수 있는 것인지 확인
    • isdecimal( ): 문자열이 정수 형태인지 확인
    • isdigit( ): 문자열이 숫자로 인식될 수 있는 것인지 확인
    • isspace( ): 문자열이 공백으로만 구성되어 있는지 확인
    • islower( ): 문자열이 소문자로만 구성되어 있는지 확인
    • isupper( ): 문자열이 대문자로만 구성되어 있는지 확인
    더보기
    eg = "reo is 고양이"
    eg = eg.isalpha()
    print(eg)
    >>>False

     

    pw = "shflshfl1"
    pw = pw.isalnum()
    print(pw)
    >>>true
    
    pw = "my pw is shflshfl1"
    pw = pw.isalnum()
    print(pw)
    >>>false

    ※ isalnum함수에 띄어쓰기가 들어가면 false로 나옴

     

    ideti = "3C2"
    ideti = ideti.isidentifier()
    print(ideti)
    >>>false
    
    ide = "s_na_ke"
    ide = ide.isidentifier()
    print(ide)
    true

     

    dig = "3.141592"
    dig = dig.isdigit()
    print(dig)
    >>>false
    
    digi = "0010"
    digi = digi.isdigit()
    print(digi)
    >>>true

    부동소수는 false

     ※ 00으로 시작해도 true 

     

    sp = ""
    sp = sp.isspace( )
    print(sp)
    >>>false
    
    spc = " \t \n"
    spc = spc.isspace( )
    print(spc)
    >>>true

    공백없이 ""는 false

     ※ \t, \n 들어가면 true 

    #find()

    왼쪽부터 찾았을 때, 왼쪽에서 몇 번째 몇번째 있는지 찾는 함수

    *0부터 카운트

    #rfind()

    오른쪽부터 찾았을 때, 왼쪽에서 몇 번째 있는지 찾는 함수

    *0부터 카운트

    fd = "01230123"
    
    print(fd.find("2"))
    >>>2
    
    print(fd.rfind("3"))
    >>>7
    
    print(fd.find("9"))
    >>>-1
    
    print(fd.rfind("9"))
    >>>-1

    ※ 없는 문자를 찾으면 -1이 출력된다

     

    #in 연산자

    print ("앞" in "뒤")로 해서 앞 !\in* 뒤 인지 확인한다.

    print("고양이 in reo")
    print("고양이" in "reo")
    >>>false
    
    print("oreo in reoreonyang")
    print("oreo" in "reoreonyang")
    >>>true

     

    #format() 함수에서 특정 칸 만큼의 출력

    print("{:#d}".format(숫자))라고 입력 시,#만큼 띄고 숫자를 출력함

    print("{:d}".format(32))
    print("{:15d}".format(32))
    print("{:20d}".format(32))
    >>>
    32
                 32
                      32

     

    print("{:0#d}".format(숫자))라고 입력 시,#만큼 0으로 채우고 숫자를 출력함

    print("{:d}".format(32))
    print("{:015d}".format(32))
    print("{:020d}".format(32))
    >>>
    32
    000000000000032
    00000000000000000032

     

    print("{:부호#d}".format(숫자))라고 입력 시

    print("{:d}".format(32))
    print("{:7d}".format(32))
    print("{:7d}".format(-32))
    print("{:=7d}".format(32))
    print("{:=7d}".format(-32))
    >>>
    32
         32
        -32
         32
    -    32

    ※ 부호가 앞으로 밀려서 출력됨

     

    print("{:부호#f}".format(숫자))라고 입력 시

    print("{:f}".format(32))
    print("{:=+12f}".format(32))
    print("{:=+12f}".format(-32))
    >>>
    32.000000
    +  32.000000
    -  32.000000

    → 부동소수점으로 출력

     

     

    print("{:부호#.nf}".format(숫자))라고 입력 시

    print("{:=+12.1f}".format(32))
    print("{:=+12.2f}".format(-32))
    print("{:=+12.6f}".format(-32))
    >>>
    +       32.0
    -      32.00
    -  32.000000

    소수 n번째 자리까지 출력

     

    print("{:=+12.1f}".format(3.141592))
    print("{:=+12.2f}".format(3.141592))
    print("{:=+12.4f}".format(3.141592))
    >>>
    +        3.1
    +       3.14
    +     3.1416

    → n+1번째 자리에서 반올림해서 나타냄

    'study with Q - 파이썬' 카테고리의 다른 글

    19 - 불 연산자, 비교 연산자, 논리 연산자  (0) 2024.03.03
    18 - 백준  (0) 2024.02.28
    16 - f-문자열  (0) 2024.02.22
    15 - format 함수  (0) 2024.02.22
    13, 14 - input 함수  (0) 2024.02.18
Designed by Tistory.