Mayungo's Python Learning Episode 4: I tried to see what happens when numbers are treated as letters

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.

Click here for a list of each story.

Recommended Posts

Mayungo's Python Learning Episode 4: I tried to see what happens when numbers are treated as letters
Mayungo's Python Learning Episode 3: I tried to print numbers with print
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Mayungo's Python Learning Episode 8: I tried input
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
Mayungo's Python Learning Episode 1: I tried printing with print
Mayungo's Python Learning Episode 7: I tried printing with if, elif, else
[Question] What happens when I use% in python?
I tried to explain what a Python generator is for as easily as possible.
When I tried to introduce python3 to atom, I got stuck
What I was addicted to when using Python tornado
Introduction to AI creation with Python! Part 1 I tried to classify and predict what the numbers are from the handwritten number images.
I tried to summarize what python strong people are doing in the competition professional neighborhood
I tried to summarize useful knowledge when developing / operating machine learning services [Python x Azure]
What I was addicted to when migrating Processing users to Python
What I was addicted to when introducing ALE to Vim for Python
I tried to create a list of prime numbers with python
What to do when you get "I can't see the site !!!!"
I tried to touch Python (installation)
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to classify mnist numbers by unsupervised learning [PCA, t-SNE, k-means]
I refactored "I tried to make Othello AI when programming beginners studied python"
[Python] I tried to analyze the characteristics of thumbnails that are easy to play on YouTube by deep learning
I tried to summarize Python exception handling
I tried to implement PLSA in Python
What I referred to when studying tkinter
I installed Python 3.5.1 to study machine learning
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to implement ADALINE in Python
I tried to implement PPO in Python
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
What I was addicted to Python autorun
What I did to save Python memory
I tried to implement merge sort in Python with as few lines as possible
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
When I tried to create a virtual environment with Python, it didn't work
[Python] I tried to get the type name as a string from the type function
I tried to implement what seems to be a Windows snipping tool in Python