Hướng dẫn what does convert () do in python? - convert () làm gì 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.


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: 
    3 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: 
    4.

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: 
4 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: 
    3 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: 
    4.

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: 4 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: 
    6. 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.

Trong chuyển đổi loại rõ ràng, cần có sự tham gia của người dùng. ....

Một chuỗi thường là một chuỗi của một hoặc nhiều ký tự. ....

  1. Một số có thể được chuyển đổi thành chuỗi bằng hàm str (). ....
  2. 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 () ..

Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác hữu ích trong lập trình hàng ngày và cạnh tranh. Bài viết này nhằm mục đích cung cấp thông tin về các chức năng chuyển đổi nhất định.

Một số có thể được chuyển đổi thành chuỗi bằng hàm str (). ....

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 () ..

Example:

Python3

Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác hữu ích trong lập trình hàng ngày và cạnh tranh. Bài viết này nhằm mục đích cung cấp thông tin về các chức năng chuyển đổi nhất định.

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
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)
9

Có hai loại chuyển đổi loại trong Python:

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
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)
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
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

Chuyển đổi loại ẩn

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
(expression)
5

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

Output:

x is of type: 
y is of type: 
20.6
z is of type: 

Hãy để thảo luận chi tiết về họ.

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 () ..

Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác hữu ích trong lập trình hàng ngày và cạnh tranh. Bài viết này nhằm mục đích cung cấp thông tin về các chức năng chuyển đổi nhất định.
 

1. int (a, cơ sở): Hàm này chuyển đổi bất kỳ loại dữ liệu thành số nguyên. ‘Cơ sở chỉ định cơ sở trong đó chuỗi là nếu kiểu dữ liệu là chuỗi.2. float (): Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số điểm nổi. & nbsp; base): This function converts any data type to integer. ‘Base’ specifies the base in which string is if the data type is a string.
2. float(): This function is used to convert any data type to a floating-point number. 

Python3

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))
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)
2
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

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))
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)
2
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))
7
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))
8
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))
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: 
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)
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
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
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: 
44
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)
2222

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
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
After converting to integer base 2 : 18
After converting to float : 10010.0
0

Output: 

After converting to integer base 2 : 18
After converting to float : 10010.0

3. Ord (): Hàm này được sử dụng để chuyển đổi một ký tự thành số nguyên.4. Hex (): Hàm này là chuyển đổi số nguyên thành chuỗi thập lục phân.5. OCT (): Hàm này là chuyển đổi số nguyên thành chuỗi octal.This function is used to convert a character to integer.
4. hex() : This function is to convert integer to hexadecimal string.
5. oct() : This function is to convert integer to octal string.

Python3

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))
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)
2
After converting to integer base 2 : 18
After converting to float : 10010.0
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))
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)
2
After converting to integer base 2 : 18
After converting to float : 10010.0
6
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70
0
After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70
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)
2222

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
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
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: 
8

Output: 

After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
2
This function is used to convert to a tuple.
7. set() : This function returns the type after converting to set.
8. list() : This function is used to convert any data type to a list type.

Python3

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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
After converting to integer base 2 : 18
After converting to float : 10010.0
0

3. Ord (): Hàm này được sử dụng để chuyển đổi một ký tự thành số nguyên.4. Hex (): Hàm này là chuyển đổi số nguyên thành chuỗi thập lục phân.5. OCT (): Hàm này là chuyển đổi số nguyên thành chuỗi octal.

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
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
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: 
8

Output: 

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
0

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
2
This function is used to convert a tuple of order (key,value) into a dictionary.
10. str() : Used to convert integer into a string.
11. complex(real,imag) : This function converts real numbers to complex(real,imag) number.

Python3

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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
After converting to integer base 2 : 18
After converting to float : 10010.0
0

3. Ord (): Hàm này được sử dụng để chuyển đổi một ký tự thành số nguyên.4. Hex (): Hàm này là chuyển đổi số nguyên thành chuỗi thập lục phân.5. OCT (): Hàm này là chuyển đổi số nguyên thành chuỗi octal.

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))
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)
2
After converting to integer base 2 : 18
After converting to float : 10010.0
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))
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)
2
After converting to integer base 2 : 18
After converting to float : 10010.0
6
x is of type: 
y is of type: 
20.6
z is of type: 
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)
4
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
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: 
8

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: 
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)
2
datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
4
x is of type: 
y is of type: 
20.6
z is of type: 
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)
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
x is of type: 
y is of type: 
20.6
z is of type: 
5
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: 
44____22222

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
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: 
8

Output: 

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
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)
4
After converting to integer base 2 : 18
After converting to float : 10010.0
0
This function converts number to its corresponding ASCII character. 

Python3

3. Ord (): Hàm này được sử dụng để chuyển đổi một ký tự thành số nguyên.4. Hex (): Hàm này là chuyển đổi số nguyên thành chuỗi thập lục phân.5. OCT (): Hàm này là chuyển đổi số nguyên thành chuỗi octal.

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))
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)
2
After converting to integer base 2 : 18
After converting to float : 10010.0
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)
03

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)
39

Output: 

datatype of num_int: 
datatype of num_flo: 

Value of num_new: 124.23
datatype of num_new: 
2

Bộ chuyển đổi trong Python là gì?

Một bộ chuyển đổi là một lớp Python đơn giản với hai phương thức: to_python và to_json. Chuyển đổi hoạt động trên cơ sở Per Per Config-Path. Điều này có nghĩa là bạn có thể có một bộ chuyển đổi chuyên dụng cho một loại phức tạp cho một con đường cấu hình cụ thể.a simple Python class with two methods: to_python and to_json. Convertes works on a per config-path basis. This means that you can have a specialized converter for a complex type for an specific config-path.

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.

Tại sao chúng ta cần chuyển đổi loại?

Nói chung diễn ra khi có biểu thức nhiều hơn một kiểu dữ liệu.Trong chuyển đổi loại điều kiện như vậy (quảng cáo loại) diễn ra để tránh mất dữ liệu.Tất cả các loại dữ liệu của các biến được nâng cấp thành loại dữ liệu của biến với loại dữ liệu lớn nhất.to avoid loss of data. All the data types of the variables are upgraded to the data type of the variable with largest data type.

Làm thế nào để chúng ta chuyển đổi các loại dữ liệu trong Python?

Điều kiện tiên quyết: Kiểu dữ liệu Python ..
Trong chuyển đổi loại rõ ràng, cần có sự tham gia của người dùng.....
Một chuỗi thường là một chuỗi của một hoặc nhiều ký tự.....
Một số có thể được chuyển đổi thành chuỗi bằng hàm str ().....
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 () ..