Repository navigation
Expand file tree
/
Copy path011-Implicit-Type-Casting.py
More file actions
82 lines (67 loc) · 3.33 KB
/
Copy path011-Implicit-Type-Casting.py
File metadata and controls
82 lines (67 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
===============================================================================
Implicit Type Casting
===============================================================================
Program Description:
--------------------
This program demonstrates Implicit Type Casting (Automatic Type Conversion)
in Python.
Implicit Type Casting is performed automatically by the Python interpreter
when two compatible data types are involved in an operation.
Generally, Python converts a smaller data type into a larger data type
to prevent data loss.
Example:
Integer (int) + Float (float) = Float (float)
Author : Shaik Mahaboob Basha
Repository : 13-Python
File Name : 11-Implicit-Type-Casting.py
===============================================================================
"""
# -----------------------------------------------------------------------------
# Creating an integer variable.
# -----------------------------------------------------------------------------
number1 = 10
# number1 stores the integer value 10.
# -----------------------------------------------------------------------------
# Creating a float variable.
# -----------------------------------------------------------------------------
number2 = 20.5
# number2 stores the float value 20.5.
# -----------------------------------------------------------------------------
# Displaying the value of number1.
# -----------------------------------------------------------------------------
print("Value of number1 :", number1)
# Output: Value of number1 : 10
# -----------------------------------------------------------------------------
# Displaying the data type of number1.
# -----------------------------------------------------------------------------
print("Data Type of number1 :", type(number1))
# Output: Data Type of number1 : <class 'int'>
# -----------------------------------------------------------------------------
# Displaying the value of number2.
# -----------------------------------------------------------------------------
print("Value of number2 :", number2)
# Output: Value of number2 : 20.5
# -----------------------------------------------------------------------------
# Displaying the data type of number2.
# -----------------------------------------------------------------------------
print("Data Type of number2 :", type(number2))
# Output: Data Type of number2 : <class 'float'>
# -----------------------------------------------------------------------------
# Adding an integer and a float.
# Python automatically converts the integer into a float before addition.
# This process is known as Implicit Type Casting.
# -----------------------------------------------------------------------------
result = number1 + number2
# result stores the float value 30.5.
# -----------------------------------------------------------------------------
# Displaying the result after addition.
# -----------------------------------------------------------------------------
print("Result :", result)
# Output: Result : 30.5
# -----------------------------------------------------------------------------
# Displaying the data type of the result.
# Since one operand is float, the result is also float.
# -----------------------------------------------------------------------------
print("Data Type of result :", type(result))
# Output: Data Type of result : <class 'float'>