+ 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?
7 Answers
+ 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.
+ 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.
+ 2
in first case we have two numbers and in second case we have an union of two strings
+ 2
ok so why use code int("7") + int("7') if you can type 7+7 and get the same result?
+ 2
7 is an integer and "7" is string simple
+ 1
7 is a number and "7" is a string
+ 1
"7" is a string. A text, not a number. You can do "7"+"@" and it will result in a 7@