ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파이썬 변수
    카테고리 없음 2023. 1. 12. 11:13

     

    변수 : 데이터를 저장할 수 있는 메모리 공간

     

    작명 : 숫자로 시작하는 작명 불가능 , 대소문자 구분, 공백 불가

     

    예약 사용 불가 ( if, for, while, true, false...)

     

    a = 10
    b = 11
    print (a,b) # 10 11
    
    a,b = 11,13
    print(a,b) # 11 13
    
    a,b,c=40,'A',10.11
    print(a,b,c) # 40 A 10.11
    print(a+c) # 50.11
    print(type(a),type(b),type(c)) # <class 'int'> <class 'str'> <class 'float'>

     

     

    변환

    정수 int()

    실수 float()

    문자 str()

     

     

    ''로 처리하게 되면 문자가 되어버린다. 

    a='10.0'
    print(type(a)) # <class 'str'>

     

     

    문자 - 실수로 변경된 값이 나온다.

    a='10.0'
    a=float(a)
    print(type(a)) # <class 'float'>

     

     

    a,b = 10,10.0
    print(type(a),type(b)) # <class 'int'> <class 'float'>
    a,b = str(a),str(b)
    print(type(a),type(b)) # <class 'str'> <class 'str'>

     

     

     

    맞교환하는 방법

     

    아래처럼  해버리는 a는 b가 되어버린다. 

    a,b=10,20
    a=b
    print(a,b) # 20 20

    c라는 공간을 만들어 a를 치환하고 다시 c로 반납해버린다. 

    a,b=10,20
    c=a
    a=b
    b=c
    print(a,b) # 20 10

    파이썬에서는 맞교환을 지원해주는데, 아래처럼 하면 맞교환이 된다.

    a,b=10,20
    a,b=b,a
    print(a,b) # 20 10

     

     

     

     

    댓글

Designed by Tistory.