(Addition, Substraction, Multiplication, Division)
Pichhle post mai hamne discuss kiya tha Python variables ke baare mai aur dekha tha ki ham python variable ko kaise declare kr skte hai. Is post mai hm baat krenge ki Python mai hm kaise addition , substraction , multiplication , division operation ka use kr skte hai.
Agar apko do number ko add krna hai to ap kaise kr skte hai , jaise mujhe 3 or 4 ko add krna hai to kaise krunga aaiye dekhte hai
a= 3
b=4
c = a+b
print(c)
output = 7
ye bilkul ek simple mathematical operation hai hmne a mai 3 ko store kiya aur b mai 4 ko store kiya , aur c mai a+b yani 7 ko store kiya aur fir print kara diya , print statement simply print krta hai jo bhi hamara output hota hai.
Kuch log soch rahe honge ki output to c aana chahiye kyoki hmne print mai c likha hai lekin nhi ap galat soch rahe ho agar maine c m koi inverted comma lagaya hota to beshak output c print ho ke aata lekin is case mai hmne c ki value print krayi hai.
" waise mai apko bta do ki agar ap python mai jakar normal 3 +4 likhte na to bhi apko outpx ut 7 mil jata bina ye variable mai value assign kiye bina , that's the beautiful magic of python"
Isi tarah ap isme substraction , multiplication , division bhi kr skte hai
x = 45
y = 4
c = x-y
print(c)
guess output?
x = 10
y = 5
z = x*y
print(z)
guess output?
x = 5
y= 2
z = x%y
print(z)
output = 1
kaise?
friends agar apko lg raha hai ki ye division ka symbol hai to ap bahaut galat hai ye division ka symbol nhi hai ise hm module operator (%) kahte hai ye remainder find krne ke liye use hota hai 5 ko agar hm 2 se divide krte hai to hame remainder kya milega obviously 1 hana that's our output is.
now difficult one
x = 3
y =2
z = x/y
print(z)
output ? (1 or 1.5)
guys, apko kya lagta hai ki output 1 hoga ki 1.5 hoga , to answer hai ki agar ap python ka second version use kr rahe hai yani Python 2 use kr rahe hai to apko output milega 1 aur agar ap Python 3 use kr rahe hai to apko output milega 1.5 aisa kyo ?
Dekhiye python 2 mai "classic" division hota tha yani decimal points ko cut kr diya jata tha , lekin python 3 mai "true" divison hota hai jisse apko result 1.5 milega . To guys apko kya lagta hai ki exact answer hame kis version mai milta hai , hme exact answer python 3 milta hai , lekin man lijeye agar ap python 2 use kr rhe ho to ap is problem ko kaise solve kr skte ho
ek tarika hai agar ap kisi bhi number ko float specify kr dete ho to apko result 1.5 mile ga
Specifying one of the numbers as a float
• 3.0/2
• 1.5
Works for either number
• 3/2.0
• 1.5
dusra tarika hai type casting ka to aayiye dekhte hai ki Python mai Type casting kaise hoti hai
Type Converison
Type conversion ka mtlb hota hai ki ek data type ki value ko dusre data type ki value mai convert karna. Python mai two type ke type conversion hote hai
1. Implicit type conversion
2. Explicit type conversion
1. Implicit type conversion mai python automatically ek datatype ko dusre datatype mai convert kr deta hai. Let's take an example jisse apko jada clear ho jayega
a = 1.5
b =1
c =a+b
print(c)
output = 2.5
ap dekh skte hai ki a float type ka hai aur b int type ka hai to jo c hoga wo kis type ka hoga float ya int , to answer hai wo float type ka hoga kyoki python automatically smaller data type ko larger data type mai convert kr deta hai, aur waise mai apko bata do ki int smaller data type hai aur float larger data type hai.
Aur agar apko c ka type check krna hai ki wo kis data type ki hai to ap aise check kr skte hai
type(c)
<class 'float'>
ek aur case lete hai aur is baar string ko integer se add karate hai aur dekhte hai ki kya milta hai
a = "123"
b=12
c = a+b
print(c)
output = Syntax error
Syntax :
<required_datatype>(expression)
to is problem ko solve krne ke liye ham explicit type conversion ka use krenge
2. Explicit type conversion ko hm type casting bhi bolte hai isme hm ek data type ko dusre data type mai convert krenge , agar upar wala example le to hm string ko int mai convert krenge , aayiye dekhte hai kaise
a = "123"
b=12
print(int(a) + b)
output
135
aur isi ko type casting kahte hai.
Agar hame string , ya float mai convert karna hai to hm aise kr skte hai --
str()
Float()
Comments
Post a Comment