TYPE CASTING
Conversion of data type from one type to another type is called “type casting” (or) ‘type coercion”.
-
Using int() :
This function converts any type data into integer format.
Ex-1:
x=int(10.5)
print(x) # 10
Ex-2:
x=int(10+2j)
print(x) #ERROR [ We cannot convert complex to int)
Ex-3:
x=int(True)
print(x) #1
Ex-4:
x=int(“10”)
print(x) #10
Ex-5:
x=int(“10.5”)
print(x) #TYPE ERROR
-
Using Float() :
To convert any type of data into float format.
Ex-1:
x=float(10)
print(x) #10.0
Ex-2:
x=float(10+2j)
print(x) #TYPE ERROR
Ex-3:
x=float(True)
print(x) # 1.0
Ex-4:
x=float(“10.5”)
print(x) #10.5
-
COMPLEX() :
To convert any type data into complex data type.
Ex-1:
x=complex(10)
print(x) # 10+0j
Ex-2:
x=complex(“10”)
print(x) # 10+0j
Ex-3:
x=complex(10.5)
print(x) # 10.5+0j
Ex-4:
x=complex(True)
print(x) #1+0j
Ex-5:
x=complex(10,2)
print(x) #10+2j
Ex-6:
x=complex(True, True)
print(x) #1+1j
Ex-7:
x=complex(“10”, “5”)
print(x) # ERROR
Ex-8:
x=complex(“10”,5)
print(x) # 10+5j
-
BOOL() :
To convert any type data into Boolean format. We can use bool function.
Ex-1:
x=bool(10)
print(x) #True
Ex-2:
x=bool(0)
print(x) #False
Ex-3:
x=bool(10+2j)
print(x) #True
Ex-4:
x=bool(10.5)
print(x) #True
Ex-5:
x=bool(“sum”)
print(x) #True
-
STR() :
To convert any type data into string type we can use str() function.
Ex-1:
x=str(10)
print(x) #”10”
Ex-2:
x=str(10.5)
print(x) #”10.5”
Ex-3:
x=str(10+2j)
print(x) #”10+2j”
Ex-4:
x=str(“True”)
print(x) #”True”
- All fundamental data types are immutable. For integer values 0 to 256 same object will be pointed after that for every integer value new object will be created
Ex-1:
x=257
y=257
print(x is y) # False
Now object is created after 256.
- For float , complex every time new object is created.
- MODULE:
It contains pre-defined classes, variables, functions.
Ex-1:
import math
print(math.sqrt(5))
print(math.sqrt(10))
print(math.sqrt(15))
Ex-2:
import math as m
import(m.sqrt(5))
import(m.sqrt(10))
import(m.sqrt(15))
Ex-3:
from math import sqrt
print(sqrt(5))
print(sqrt(10))
print(sqrt(15))
INDENTATION
-
INPUT() :
To read the data from the keyboard input() returns string data type.
Ex-1:
a= input(“Enter a value”)
print(type(a)) # ’str’
print(a) # “10”
-
Write a program to read 2 integers from the keyword and find the sum ?
a=int”(input(“Enter 1st value”))
b=int(input(“Enter 2nd value”))
print(a+b)
-
Write a program to read 3 float values from the keyword and find the sum ?
a=float(input(“Enter 1st value”))
b=float(input(“Enter 2nd value”))
c=float(input(“Enter 3rd value”))
print(a+b+c)
-
Write a program to concatenate of two strings ?
a=input(“Enter 1st string”)
b=input(“Enter 2nd string”)
print(a+b)
-
Write a program to read roll number, name, marks, average of a student and print the same values.
a=int(input(“Enter roll number”))
b=input(“Enter name”)
c=int(input(“Enter marks”))
d=float (input(“Enter average”))
print(“ roll number=”,a)
print(“Name=”,b)
print(“Marks=”,c)
print(“Average=”,d)
-
Write a program to read 5 subjects marks of a student. Calculate total and average and print the same ?
a,b,c,d,e,f=[int(x) for x in input(“Enter 5 subjects”).split()]
s=a+ b+c+d+e
avg=s/5
print( “sum=”,s)
print( “Average=”, avg)
NOTE: Split() can take space as default separator.If we given, in between marks then we note it as “split(‘ ,’)”.
- If int is given convert to integer.
- If float is given convert to float.
- Eval() is convert the any type of data as you want. Simply It can return the data type is given data type.
-
Write a program to read “multiple value” from keyboard and print the sum.
n=[eval(x) for x in input(“Enter multiple”).split(‘,’)]
sum=0
for x in n:
sum=sum+x
print(sum)
NOTE: C> COMMAND PROMPT
Now C>py Test.py 20 30 40
If we input the data at command prompt then data values are called command lines”
argv is predefined variable and it is list type. argv is available in sys module.
-
-
-
Write a program to print the command lines?
-
C>py Test.py 20 30 40
from sys import argv
print(argv) #[ Test.py 20 30 40 ]print(argv[1]) #20
print(argv[2]) #30
print(argv[1: ]) #[20 30 40]
-
-
Write a program to add values which are taken as command line arguments.
C> Python pc.py 20 30 40 [ pc is file name, py is file extension]
from sys import argv
args=argv[1: ]
print(args)
s=0
for x in args :
y=int(x)
s=s+y
print(s) #90
Simply command line arguments is list type elements are string type . print() black line is created.
- SINGLE ARGUMENTS:
print(“purna techs”)
print(“purna”*3)
print(“purna”+3)
print(“purna”+”tech”)
- MULTIPLE ARGUMENTS:
a=10
b=20
print(a,b) #10 20
Now by using different actions in between the values.
Ex-1:
a=10
b=20
print(a,b)
print(a,b,sep=’ ,’) # 10,20
- MULTIPLE ARGUMENTS WITH FORMATTED OUTPUT :
r=10
m=10.5
n=”purna”
print(“roll=%d and marks= %f and name=%s” %(r,m,n))
-
Write a program to calculate area and perimeter of a circle.
r=eval(input(“Enter radius”))
a=3.141 * r*r
p=2*3.141*r
print(“area={}/n perimeter={}”.format(a,p))
-
Write a program to find area and perimeter of a rectangle.
l=eval(input(“Enter length”)
b=eval(input(“Enter breadth”)
a=l*b
p=2*(l+b)
print(“area={} /n perimeter={}”.format(a,p)
-
Write a program to convert foriegn heat degree to centigrade degree. formula to convert is c=5/9(F-32)
f=eval(input(“Enter f value))
c=5/9*(f-32)
print(“centigrade=”,c)
-
Write a program to find area of a triangle.
Formulas for this is s=a+b+c/2 and s= square root ( s(s-a)(s-b)(s-c))
from math import sqrt
a,b,c=[eval(x) for x in input(“Enter a,b,c”).split()]
s=(a+b+c)/2
area=sqrt(s*(s-a)*(s-b)*(s-c))
print(“area=”,area)