Mayungo Mayu Mayu
This time, as it is the 4th episode, we will look at the difference between when numbers are viewed as numbers and when they are viewed as character strings.
The same content is also published in the video, so please have a look if you like.
Like last time, I created variables with first = 114, second = 514, iti = "114", ni = "514".
first = 114
second = 514
iti = "114"
ni = "514"
card = "Monster card! !! !!"
message = "Exactly DEATH ☆ GAME! !!"
First, let's calculate with the numerical values first and second.
Addition is "+", subtraction is "-", multiplication is "*", and division is "/" and "//".
We'll talk more about this later.
print(first + second)#addition
print(first - second)#subtraction
print(first * second)#multiplication
print(first / second)#division(With decimal point)
print(first // second)#division(No decimal point)
628
-400
58596
0.22178988326848248
0
Since the numbers are different, we were able to get the calculation results for each.
Next, let's calculate with iti, which uses numbers as character strings.
print(iti + ni)#addition
print(iti - ni)#subtraction
print(iti * ni)#multiplication
print(iti / ni)#division(With decimal point)
print(iti // ni)#division(No decimal point)
114514
TypeError Traceback (most recent call last)
<ipython-input-5-c884d5dcff34> in <module>
1 print(iti + ni)#addition
----> 2 print(iti - ni)#subtraction
3 print(iti * ni)#multiplication
4 print(iti / ni)#division(With decimal point)
5 print(iti // ni)#division(No decimal point)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
When I tried to get the result with this, I got an error except for the addition "114514".
Characters can be attached to each other.
On the other hand, we can't even programmatically avoid subtraction and multiplication between words.
(Of course, this is not the case if it is between variables.)
I will add it with card and message.
If you treat a number as a character, it is essentially the same.
print(card + message)#It's okay to stick the strings together
Monster card! !! !! Exactly DEATH ☆ GAME! !!
This time, I touched on how it would be different if numbers were treated as character strings.
Next time, we will take a closer look at the calculation of numbers.
Thank you for your viewing.
print("Thank you for subscribing to the channel")
Thank you for subscribing to the channel
Thank you for subscribing to the channel.