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.
- 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
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.
- 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.
- Ví dụ 2: Bổ sung kiểu dữ liệu và kiểu dữ liệu của chuỗi [cao hơn]
- Chúng tôi thêm hai biến num_int và num_str.
- 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. - 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ự. ....
- 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 [] ..
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]
4num_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]
5num_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]
6num_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]
7num_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]
8num_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]
9Có 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]
4num_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]
5Data 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]
7num_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]
8Data 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]]
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]
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]]
4num_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]]
5num_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]]
7num_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]]
8num_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]]
9Data 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]
5Data 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]
2222num_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]
5x 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.00
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]]
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]
2 After converting to integer base 2 : 18 After converting to float : 10010.03
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]]
5num_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.06
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]
5After converting character to integer : 52 After converting 56 to hexadecimal string : 0x38 After converting 56 to octal string : 0o700
After converting character to integer : 52 After converting 56 to hexadecimal string : 0x38 After converting 56 to octal string : 0o701
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]
2222num_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]
5x 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]
5x 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
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]
datatype of num_int:
datatype of num_flo:
Value of num_new: 124.23
datatype of num_new:
4x is of type:
y is of type:
20.6
z is of type:
2This
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]
5x 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.00
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]
5x 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]
5x 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
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]
datatype of num_int:
datatype of num_flo:
Value of num_new: 124.23
datatype of num_new:
4x is of type:
y is of type:
20.6
z is of type:
2This 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]
5x 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.00
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]]
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]
2 After converting to integer base 2 : 18 After converting to float : 10010.03
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]]
5num_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.06
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]
5x 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]
5x 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
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]
After converting to integer base 2 : 18
After converting to float : 10010.0
0This 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]]
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]
2 After converting to integer base 2 : 18 After converting to float : 10010.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]
4num_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]
03num_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]
4num_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]
39Output:
datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:2