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.
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.
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
Hãy để thảo luận chi tiết về họ.
Chuyển đổi loại ẩn
Chuyển đổi loại rõ ràng
Example:
Python3
Hãy để thảo luận chi tiết về họ.
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]
9Trong kiểu chuyển đổi loại ẩn của các loại dữ liệu trong Python, trình thông dịch Python tự động chuyển đổi một loại dữ liệu sang loại khác mà không có bất kỳ sự tham gia của người dùng nào. Để có được một cái nhìn rõ ràng hơn về chủ đề, hãy xem 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:",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
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]
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]
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]
3num_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
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'0____22
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'2
Output:
x is of type: y is of type: 20.6 z is of type:
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
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]
1[expression]2
[expression]3
Chuyển đổi loại rõ ràng
Hãy để thảo luận chi tiết về họ.
Trong kiểu chuyển đổi loại ẩn của các loại dữ liệu trong Python, trình thông dịch Python tự động chuyển đổi một loại dữ liệu sang loại khác mà không có bất kỳ sự tham gia của người dùng nào. Để có được một cái nhìn rõ ràng hơn về chủ đề, hãy xem các ví dụ dưới đây. 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:",type[num_str]]
print[num_int+num_str]
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]
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]
3Data 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'0____22
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'2
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
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]
1[expression]2
[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]
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
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]
5[expression]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]
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 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]]
1Như chúng ta có thể thấy loại dữ liệu của ‘Z, đã tự động thay đổi thành loại float float trong khi một biến X thuộc loại số nguyên trong khi biến Y khác là loại float. Lý do cho giá trị float không được chuyển đổi thành một số nguyên thay vào đó là do quảng cáo loại cho phép thực hiện các hoạt động bằng cách chuyển đổi dữ liệu thành một loại dữ liệu có kích thước rộng hơn mà không mất thông tin nào. Đây là một trường hợp đơn giản của chuyển đổi loại ngầm trong Python.
Trong chuyển đổi loại rõ ràng trong Python, loại dữ liệu được người dùng thay đổi theo cách thủ công theo yêu cầu của họ. Với chuyển đổi loại rõ ràng, có nguy cơ mất dữ liệu vì chúng tôi đang buộc một biểu thức phải được thay đổi trong một số loại dữ liệu cụ thể. & nbsp; các hình thức chuyển đổi loại rõ ràng khác nhau được giải thích dưới đây: & nbsp;
Output:
After converting to integer base 2 : 18 After converting to float : 10010.0
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;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 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 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
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
Output:
After converting character to integer : 52 After converting 56 to hexadecimal string : 0x38 After converting 56 to octal string : 0o70
2num_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]
After converting to integer base 2 : 18
After converting to float : 10010.0
3This
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 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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:26
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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:29
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:33
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]
222num_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
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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:41
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:45
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]
222num_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
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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:41
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:45
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]
222num_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
5num_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]
datatype of num_int:
datatype of num_flo:
Value of num_new: 124.23
datatype of num_new:
41x 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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:45
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]
222num_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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:53
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:57
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]
22229. Dict []: Hàm này được sử dụng để chuyển đổi một bộ thứ tự [khóa, giá trị] thành từ điển.10. Str []: Được sử dụng để chuyển đổi số nguyên thành chuỗi.11. phức tạp [thực, hình ảnh]: Hàm này chuyển đổi số thực thành số phức [thực, hình ảnh].
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]
2 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:65
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
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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:41
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:45
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]
222num_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
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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:41
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:45
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]
222num_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
5num_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]
datatype of num_int:
datatype of num_flo:
Value of num_new: 124.23
datatype of num_new:
41x is of type:
y is of type:
20.6
z is of type:
2This function
converts number to its corresponding ASCII character.
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]
5datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:45
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]
222num_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 datatype of num_int: datatype of num_flo: Value of num_new: 124.23 datatype of num_new:53
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]
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