logo

예제를 사용한 Python의 유형 캐스팅(암시적 및 명시적)

Type Casting은 Python 변수를 변환하는 방법입니다. Python 암시적 유형 변환

산제이 더트와
  • Python 명시적 유형 변환
  • Python의 암시적 유형 변환

    이 방법에서는, 파이썬 데이터 유형을 다른 데이터 유형으로 자동으로 변환합니다. 사용자는 이 프로세스에 참여할 필요가 없습니다.



    파이썬3








    # Python program to demonstrate> # implicit type Casting> # Python automatically converts> # a to int> a>=> 7> print>(>type>(a))> # Python automatically converts> # b to float> b>=> 3.0> print>(>type>(b))> # Python automatically converts> # c to float as it is a float addition> c>=> a>+> b> print>(c)> print>(>type>(c))> # Python automatically converts> # d to float as it is a float multiplication> d>=> a>*> b> print>(d)> print>(>type>(d))>

    >

    >

    산출:

      10.0  21.0>

    Python의 명시적 유형 변환

    이 방법에서 Python은 변수 데이터 유형을 필요한 데이터 유형으로 변환하기 위해 사용자 참여가 필요합니다.

    Python의 유형 캐스팅 예

    주로 다음 데이터 유형 함수를 사용하여 유형 캐스팅을 수행할 수 있습니다.

    • 정수(): 파이썬 정수() 함수는 float 또는 string을 인수로 취하고 int 유형의 객체를 반환합니다.
    • 뜨다(): 파이썬 뜨다() 함수는 int 또는 string을 인수로 취하고 float 유형 객체를 반환합니다.
    • 문자열(): 파이썬 str() 함수는 float 또는 int를 인수로 받아 문자열 유형의 객체를 반환합니다.

    Python Int를 Float로 변환

    여기, 우리는 Python에서 Int를 Float로 변환 와 더불어 뜨다() 기능.

    파이썬3




    # Python program to demonstrate> # type Casting> # int variable> a>=> 5> # typecast to float> n>=> float>(a)> print>(n)> print>(>type>(n))>

    >

    >

    산출:

    5.0>

    Python 부동 소수점을 정수로 변환

    여기, 우리는 변환 중 Python에서 int 데이터 유형으로 부동 소수점 ~와 함께 정수() 기능.

    파이썬3




    # Python program to demonstrate> # type Casting> # int variable> a>=> 5.9> # typecast to int> n>=> int>(a)> print>(n)> print>(>type>(n))>

    >

    >

    산출:

    5>

    Python int를 문자열로 변환

    여기, 우리는 변환 중 int를 Python의 문자열 데이터 유형으로 변환 ~와 함께 str() 기능.

    파이썬3




    # Python program to demonstrate> # type Casting> # int variable> a>=> 5> # typecast to str> n>=> str>(a)> print>(n)> print>(>type>(n))>

    >

    >

    산출:

    5>

    Python 문자열을 부동 소수점으로 변환

    여기서는 문자열 데이터 유형을 부동 소수점 데이터 유형으로 캐스팅합니다. 뜨다() 기능.

    파이썬3




    # Python program to demonstrate> # type Casting> # string variable> a>=> '5.9'> # typecast to float> n>=> float>(a)> print>(n)> print>(>type>(n))>

    >

    >

    산출:

    5.9>

    Python 문자열을 int로 변환

    여기, 우리는 변환 중 Python에서 문자열을 int 데이터 유형으로 ~와 함께 정수() 기능. 주어진 문자열이 숫자가 아니면 오류가 발생합니다.

    파이썬3




    # string variable> a>=> '5'> b>=> 't'> # typecast to int> n>=> int>(a)> print>(n)> print>(>type>(n))> print>(>int>(b))> print>(>type>(b))>

    >

    >

    산출:

    5  --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[3], line 14  11 print(n)  12 print(type(n)) --->14 print(int(b)) 15 print(type(b)) ValueError: 기본이 10인 int()에 대한 리터럴이 잘못되었습니다: 't'>

    명시적 변환을 사용하여 문자열과 정수 추가

    파이썬3




    # integer variable> a>=> 5> # string variable> b>=> 't'> # typecast to int> n>=> a>+>b> print>(n)> print>(>type>(n))>

    >

    >

    산출:

    TypeError Traceback (most recent call last) Cell In[5], line 10  7 b = 't'  9 # typecast to int --->10 n = a+b 12 인쇄(n) 13 인쇄(유형(n))>