+ 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 RĂ©ponses
+ 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