Casting Examples All


#integer casting
x=2
y=9.8
z=5.6

print("x,y,z values are:",x,y,z)
print(type(x))
print(type(y))
print(type(z))
print("      ")

print(int(x))
print(int(y))
print(int(z))

print("int of x,y,z values are:",int(x),int(y),int(z))
print("      ")
print("type of int(x) is:", type(int(x)))
print("type of int(y) is:", type(int(y)))
print("type of int(z) is:", type(int(z)))

print("-----------------------------")


#float casting
x=1
y=2.8
z=3
w=4.2

print("x value and type of x values is:", x,",", type(x))
print("x value and type of y values is:", y,",", type(y))
print("x value and type of z values is:", z,",", type(z))
print("x value and type of w values is:", w,",", type(w))
print("      ")

print("x value, float(x), type(float(x)) value is:\n", x,",",  float(x),",",  type(float(x)))
print("      ")
print("y value, float(y), type(float(y)) value is:\n", y,",",  float(y),",",  type(float(y)))
print("      ")
print("z value, float(z), type(float(z)) value is:\n", z,",",  float(z),",",  type(float(z)))
print("      ")
print("w value, float(w), type(float(w)) value is:\n", w,",",  float(w),",",  type(float(w)))
print("      ")

print("-----------------------------")


#String casting

x="s1"
y=2.8
z=3

print("x value and type of x values is:", x,",", type(x))
print("x value and type of y values is:", y,",", type(y))
print("x value and type of z values is:", z,",", type(z))

print("      ")
print("x value, str(x), type(str(x)) value is:\n", x,",",  str(x),",",  type(str(x)))
print("      ")
print("y value, str(y), type(str(y)) value is:\n", y,",",  str(y),",",  type(str(y)))
print("      ")
print("z value, str(z), type(str(z)) value is:\n", z,",",  str(z),",",  type(str(z)))
print("      ")


o/p:

x value and type of x values is: s1 , <class 'str'>
x value and type of y values is: 2.8 , <class 'float'>
x value and type of z values is: 3 , <class 'int'>
      
x value, str(x), type(str(x)) value is:
 s1 , s1 , <class 'str'>
      
y value, str(y), type(str(y)) value is:
 2.8 , 2.8 , <class 'str'>
      
z value, str(z), type(str(z)) value is:
 3 , 3 , <class 'str'>