Python โ€œType Conversionโ€ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python โ€œType Conversionโ€

Can someone please answer the following in regards to these two conversion examples: QC-1: >>>float(โ€œ300โ€ + int(input(โ€œ4โ€))) I. How does python read this? II. What is the order in which python calculates this III. Whatโ€™s the difference between: Int(โ€œ2โ€) + (โ€œ3โ€) and Int(โ€œ2โ€ + โ€œ3โ€) ?

27th Jul 2018, 5:40 PM
Bayshawn Davis
Bayshawn Davis - avatar
6 Answers
+ 3
float("300" + int(input("4"))) => step1. ask for input and show 4 step2. convert your input to int step3. Error: "str" can't be added by "int" int("2") + ("3") => step1. convert str "2" to int 2 step2. Error: "int" can't be added by "str" int("2" + "3") => step1. "2" + "3" = "23" step2. convert str "23" to int 23 step3. return int 23
27th Jul 2018, 5:49 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
davy hermans really? let me try it i seldom use tuple so I'm not sure๐Ÿ˜† *** ok, I've tried it, you're right๐Ÿ‘
27th Jul 2018, 5:59 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 2
I agree with Flandre Scarlet but i think (correct me if i am wrong) ("3") would be a string ("3",) to make it a tuple
27th Jul 2018, 5:56 PM
davy hermans
davy hermans - avatar
+ 2
Bayshawn in python, variables can't be added between different types, except for int and float, they'll become both float therefore, int("2") + int("3") is valid int("2" + "3") is valid int("2") + "3" is invalid
27th Jul 2018, 6:10 PM
Flandre Scarlet
Flandre Scarlet - avatar
0
thanks guys I appreciate it! Flandre Scarlet and are we sure int(โ€œ2โ€) + int(โ€œ3) produces an error when both are the same?
27th Jul 2018, 6:05 PM
Bayshawn Davis
Bayshawn Davis - avatar
0
Flandre Scarlet thank you ๐Ÿ˜Š
27th Jul 2018, 6:22 PM
Bayshawn Davis
Bayshawn Davis - avatar