Hướng dẫn how do you convert type in python? - làm thế nào để bạn chuyển đổi loại trong python?

Trước khi học & nbsp; chuyển đổi loại trong python, & nbsp; bạn nên có kiến ​​thức & nbsp; về & nbsp; các loại dữ liệu python.

Show

Loại chuyển đổi

Quá trình chuyển đổi giá trị của một loại dữ liệu (số nguyên, chuỗi, float, v.v.) sang kiểu dữ liệu khác được gọi là chuyển đổi loại. Python có hai loại chuyển đổi loại.

  1. Chuyển đổi loại ẩn
  2. Chuyển đổi loại rõ ràng

Chuyển đổi loại ẩn

Chuyển đổi loại rõ ràng

Trong chuyển đổi loại tiềm ẩn, Python tự động chuyển đổi một loại dữ liệu sang loại dữ liệu khác. Quá trình này không cần bất kỳ sự tham gia của người dùng.

Hãy xem một ví dụ trong đó Python thúc đẩy việc chuyển đổi loại dữ liệu thấp hơn (số nguyên) sang loại dữ liệu cao hơn (float) để tránh mất dữ liệu.

num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))

Ví dụ 1: Chuyển đổi số nguyên thành phao

Khi chúng tôi chạy chương trình trên, đầu ra sẽ là:

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 

  • Trong chương trình trên,
  • Chúng tôi thêm hai biến num_int và num_flo, lưu trữ giá trị trong num_new.
  • Chúng tôi sẽ xem xét loại dữ liệu của cả ba đối tượng tương ứng.
  • Trong đầu ra, chúng ta có thể thấy loại dữ liệu của num_int là
    datatype of num_int: 
    datatype of num_flo: 
    
    Value of num_new: 124.23
    datatype of num_new: 
    4 trong khi loại dữ liệu của num_flo là
    datatype of num_int: 
    datatype of num_flo: 
    
    Value of num_new: 124.23
    datatype of num_new: 
    5.

Ngoài ra, chúng ta có thể thấy num_new có kiểu dữ liệu

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
5 vì Python luôn chuyển đổi các loại dữ liệu nhỏ hơn thành các loại dữ liệu lớn hơn để tránh mất dữ liệu.

Bây giờ, chúng ta hãy thử thêm một chuỗi và một số nguyên, và xem Python giao dịch với nó như thế nào.

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)

Ví dụ 1: Chuyển đổi số nguyên thành phao

Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 

  • Trong chương trình trên,
  • Chúng tôi thêm hai biến num_int và num_flo, lưu trữ giá trị trong num_new.
  • Chúng tôi sẽ xem xét loại dữ liệu của cả ba đối tượng tương ứng.

Chuyển đổi loại rõ ràng

Trong chuyển đổi loại tiềm ẩn, Python tự động chuyển đổi một loại dữ liệu sang loại dữ liệu khác. Quá trình này không cần bất kỳ sự tham gia của người dùng.

Hãy xem một ví dụ trong đó Python thúc đẩy việc chuyển đổi loại dữ liệu thấp hơn (số nguyên) sang loại dữ liệu cao hơn (float) để tránh mất dữ liệu.

Ví dụ 1: Chuyển đổi số nguyên thành phao

(expression)

Khi chúng tôi chạy chương trình trên, đầu ra sẽ là:


datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))

Ví dụ 1: Chuyển đổi số nguyên thành phao

Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 

  • Trong chương trình trên,
  • Chúng tôi thêm hai biến num_int và num_flo, lưu trữ giá trị trong num_new.
  • Chúng tôi sẽ xem xét loại dữ liệu của cả ba đối tượng tương ứng.
  • Trong đầu ra, chúng ta có thể thấy loại dữ liệu của num_int là
    datatype of num_int: 
    datatype of num_flo: 
    
    Value of num_new: 124.23
    datatype of num_new: 
    4 trong khi loại dữ liệu của num_flo là
    datatype of num_int: 
    datatype of num_flo: 
    
    Value of num_new: 124.23
    datatype of num_new: 
    5.

Ngoài ra, chúng ta có thể thấy num_new có kiểu dữ liệu datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new: 5 vì Python luôn chuyển đổi các loại dữ liệu nhỏ hơn thành các loại dữ liệu lớn hơn để tránh mất dữ liệu.

  1. Bây giờ, chúng ta hãy thử thêm một chuỗi và một số nguyên, và xem Python giao dịch với nó như thế nào.
  2. Ví dụ 2: Bổ sung kiểu dữ liệu và kiểu dữ liệu của chuỗi (cao hơn)
  3. Chúng tôi thêm hai biến num_int và num_str.
  4. Như chúng ta có thể thấy từ đầu ra, chúng ta đã nhận được
    datatype of num_int: 
    datatype of num_flo: 
    
    Value of num_new: 124.23
    datatype of num_new: 
    7. Python không thể sử dụng chuyển đổi ngầm trong các điều kiện như vậy.
  5. Tuy nhiên, Python có một giải pháp cho các loại tình huống này được gọi là chuyển đổi rõ ràng.

Điều kiện tiên quyết: Kiểu dữ liệu PythonPython Data Types

Chuyển đổi loại là quá trình chuyển đổi một loại dữ liệu sang loại dữ liệu khác. Có thể có hai loại chuyển đổi loại trong Python -

  • Chuyển đổi loại ẩn
  • Chuyển đổi loại rõ ràng

Chuyển đổi loại ẩn

Chuyển đổi loại rõ ràng

Example:

Python3

Đây là một loại chuyển đổi loại trong đó xử lý tự động chuyển đổi một loại dữ liệu sang loại dữ liệu khác mà không có bất kỳ sự tham gia của người dùng nào.

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
8

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
2
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
4

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
5

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
9
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
1

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
(expression)
3

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
(expression)
7

Output:



5.0

Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
8
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
9
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
9
(expression)
1

Chuyển đổi loại rõ ràng

Đây là một loại chuyển đổi loại trong đó xử lý tự động chuyển đổi một loại dữ liệu sang loại dữ liệu khác mà không có bất kỳ sự tham gia của người dùng nào.

num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str:",type(num_str)) print(num_int+num_str)2num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str:",type(num_str)) print(num_int+num_str)3 num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str:",type(num_str)) print(num_int+num_str)4

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
9
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
1

Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
8
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
9
Data type of num_int:  
Data type of num_str:  

Traceback (most recent call last): 
  File "python", line 7, in  
TypeError: unsupported operand type(s) for +: 'int' and 'str'
9
(expression)
1

Trong ví dụ trên, có thể thấy rằng Python xử lý tất cả các loại chuyển đổi tự động mà không cần bất kỳ sự tham gia nào của người dùng.

Example:

Python3

Trong chuyển đổi loại rõ ràng, cần có sự tham gia của người dùng. Người dùng chuyển đổi một loại dữ liệu sang loại khác theo nhu cầu của chính mình. Điều này có thể được thực hiện với sự trợ giúp của str (), int (), float (), v.v. Hãy cùng xem việc xử lý các chuyển đổi loại khác nhau.

Loại chuyển đổi với chuỗi

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
6

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
0

Output:

10

Một chuỗi thường là một chuỗi của một hoặc nhiều ký tự. Chúng tôi thường được yêu cầu chuyển đổi chuỗi thành số và ngược lại. Hãy cùng nhìn thấy từng người trong số họ một cách chi tiết.

Example:

Python3

Chuyển đổi số thành chuỗi

Một số có thể được chuyển đổi thành chuỗi bằng hàm str (). Để thực hiện điều này, hãy vượt qua một số hoặc một biến chứa giá trị số cho hàm này.

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
2
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
0

Output:

String: GFG
Number: 50

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
1
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
3
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
4

Điều này có thể hữu ích khi chúng tôi muốn in một số chuỗi chứa một số vào bảng điều khiển. Hãy xem xét các ví dụ dưới đây.

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
1
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
3
IfA string containing not containing a numeric value is passed then an error is raised.

Example:

Python3

Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
4
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
6

Is

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
10
5

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7


5.0
6

Chuyển đổi chuỗi thành số

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
String: GFG
Number: 50
5

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
String: GFG
Number: 50
9

Output:

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
0

Một chuỗi có thể được chuyển đổi thành một số bằng phương thức int () hoặc float (). Để thực hiện điều này, hãy vượt qua một chuỗi hợp lệ chứa giá trị số cho một trong các hàm này (tùy thuộc vào nhu cầu).

Lưu ý: Chuỗi IFA chứa không chứa giá trị số được truyền thì lỗi được nêu ra.

num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str before Type Casting:",type(num_str)) num_str = int(num_str) print("Data type of num_str after Type Casting:",type(num_str)) num_sum = num_int + num_str print("Sum of num_int and num_str:",num_sum) print("Data type of the sum:",type(num_sum))1num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str:",type(num_str)) print(num_int+num_str)3 5.0 9

Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
4
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
10
22____56

Example:

Python3

String: GFG
Number: 50
0
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
6

Nhập chuyển đổi với các số

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
10
5

Về cơ bản có hai loại số trong Python-số nguyên và số điểm nổi. Weare thường được yêu cầu thay đổi từ loại này sang loại khác. Hãy cùng xem chi tiết chuyển đổi của họ.

Output:

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
1

Điểm nổi đến số nguyên

Một điểm nổi có thể được chuyển đổi thành một số nguyên bằng hàm int (). Để thực hiện điều này vượt qua một điểm nổi bên trong phương thức int ().

Example:

Python3

String: GFG
Number: 50
0
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
02

Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
4
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
10
22____95

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
String: GFG
Number: 50
5

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7


5.0
6

Output:

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
2

Số nguyên đến điểm nổi

Một số nguyên có thể được chuyển đổi thành float bằng phương thức float (). Để thực hiện việc này vượt qua một số nguyên bên trong phương thức float ().

Example:

Python3

Data type of num_int: 
Data type of num_str before Type Casting: 

Data type of num_str after Type Casting: 

Sum of num_int and num_str: 579
Data type of the sum: 
4
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
0

String: GFG
Number: 50
0
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
5
10
5

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
String: GFG
Number: 50
9

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
53

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
57

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
58
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
3
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
60
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
61

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
63

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
5
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
6
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)
7
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
67

Output:

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
3

Làm thế nào để bạn chuyển đổi một kiểu dữ liệu trong Python?

Để chuyển đổi giữa các loại, bạn chỉ cần sử dụng tên loại làm hàm. Có một số chức năng tích hợp để thực hiện chuyển đổi từ loại dữ liệu này sang loại dữ liệu khác. Các chức năng này trả về một đối tượng mới đại diện cho giá trị được chuyển đổi.use the type name as a function. There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.

Làm thế nào để bạn chuyển đổi một kiểu dữ liệu?

Chuyển đổi một loại dữ liệu..
Chọn cột ngày, chọn Trang chủ> Biến đổi> Kiểu dữ liệu, sau đó chọn tùy chọn Ngày. Bạn có thể chuyển đổi các loại số khác, chẳng hạn như tỷ lệ phần trăm hoặc tiền tệ. ....
Để trả lại dữ liệu được chuyển đổi cho bảng tính Excel, chọn Trang chủ> Đóng & Tải ..

Làm thế nào để chuyển đổi loại hoạt động trong Python?

Trong chuyển đổi loại rõ ràng, người dùng chuyển đổi loại dữ liệu của một đối tượng thành kiểu dữ liệu cần thiết.Chúng tôi sử dụng các hàm được xác định trước như int (), float (), str (), vv để thực hiện chuyển đổi loại rõ ràng.Loại chuyển đổi này còn được gọi là typecasting vì người dùng sử dụng (thay đổi) loại dữ liệu của các đối tượng.users convert the data type of an object to required data type. We use the predefined functions like int() , float() , str() , etc to perform explicit type conversion. This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.

Làm thế nào để bạn chuyển đổi mã trong Python?

Trong Python, bạn chỉ có thể sử dụng hàm bin () để chuyển đổi từ giá trị thập phân sang giá trị nhị phân tương ứng của nó.Và tương tự, hàm int () để chuyển đổi một nhị phân thành giá trị thập phân của nó.Hàm int () lấy đối số thứ hai là cơ sở của số sẽ được chuyển đổi, đó là 2 trong trường hợp số nhị phân.use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.