Difference between 7 and "7"? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Difference between 7 and "7"?

can someone explain the difference in the outputs for 7+7 or "7"+"7"? One is 14 and one is 77 correct?

10th Jun 2018, 11:22 PM
Michael Dixon
Michael Dixon - avatar
7 ответов
+ 3
Answer to your second question: For simple calculations you would just use 7 + 7 and be happy. But you can also use int() to transform other datatypes, like strings, into integers for calculations. This can be necessary, because if you mix datatypes in a variable, you get an error. Explanation: In Python 3 you have 2 differtent datatypes for numbers: 1) integers (1) Integers are whole numbers 2) floats (1.0) Floats are decimals. If you type 7 + 7, both numers are automatically assigned to integers. For simple calculations as above that's fine. For exact calculations you would use floats. If you are unsure which kind of datatype you are dealing with use type() to find out >>> a = 7 >>> type(a) <class 'int'> This is how you could change a string into an integer: Let's say you used input('a') #input('a') always produces a string >>> cars = input('Insert number of owned cars ') Insert number of owned cars 2 #now you want to add another car to your variable cars >>> print(int(cars) + 1) 3 #the result is a string, cars is still a string and the value of cars has not changed and remains 2 Hope this clarified things a little.
11th Jun 2018, 1:51 AM
Ich
+ 13
Michael Dixon when you put 7 in double quote it becomes a string and it add both the string. and when you add 7 + 7 it adds as normal number.
10th Jun 2018, 11:26 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 2
in first case we have two numbers and in second case we have an union of two strings
10th Jun 2018, 11:27 PM
Alison.One
+ 2
ok so why use code int("7") + int("7') if you can type 7+7 and get the same result?
10th Jun 2018, 11:35 PM
Michael Dixon
Michael Dixon - avatar
+ 2
7 is an integer and "7" is string simple
11th Jun 2018, 9:23 AM
Rizi Rozi
+ 1
7 is a number and "7" is a string
11th Jun 2018, 1:51 AM
Pranjal
Pranjal - avatar
+ 1
"7" is a string. A text, not a number. You can do "7"+"@" and it will result in a 7@
12th Jun 2018, 3:15 AM
Сергей
Сергей - avatar