Skip to content

[Python] Tutorial (2) Additional, subtraction, multiplication, division, String, Variable

Variable, additional, subtraction, multiplication, division is the important concepts of Python, it’s also very important and useful for other program languages.

Today I will introduce the above concepts!

My tutorial is sorted out from here: https://docs.python.org/3.6/tutorial/

Maybe you can refer it.


Additional, subtraction, multiplication, division

Doing numerical operations in Python is very intuitive.

print(2+2)
print(50-6)
print(7*7)
print(8/2)


The above are four operations of addition, subtraction, multiplication, and division.
So, what is our results?

4
44
49
4.0


We calculate it manually:

  • 2+2 = 4
  • 50-6 = 44
  • 7*7 = 49
  • 8/2 = 4.0

The answer is correct! —— but, wait. Do you think that there is a “number” that is different for others?

In Python, we don’t need to define the data type of variables just like in other languages. (we will mention it maybe in other chapter.) Which causes us to use the type() command often in the process of debugging the program. It’s convenient to see the type of our data.

print(type(2 + 2))
print(type(50 - 6))
print(type(7 * 7))
print(type(8 / 2))


We can see the data type of the values.

<class 'int'>
<class 'int'>
<class 'int'>
<class 'float>


Oh! The last type is float! We can understand that it is a numeric data type with decimals, and int is an integer “data type“.

Python’s division symbol / automatically converts the value we output into a float data type, even if it is divisible.

In fact, we can use // symbol to divide a value.

print(100 / 7)
print(100 // 7)


Output:

14.285714285714286
14


We can see that the decimal point has been automatically discarded using the value below the integer //.

The above is the basic addition, subtraction, multiplication and division, you can try the function of numerical calculation in Python.

By the way, there is a built-in module named math, which can be import directly. I hope that I will have the opportunity to upload a teaching text about it in the future.


String、Variable

After thinking about it, I finally decided to talk the string with the variable. (Absolutely not because I feel the need to fill the page.)

String, as its name implies, is a whole string of text whose data type is str.

Let’s look at a simple example.

text = 'Today is a nice day, I want to go to play.'
print(type(text))
print(text)


Output:

<class 'str'>
Today is a nice day, I want to go to play. 


The text in the first line is the so-called “variable“. We are free to name variables and freely add, subtract, multiply and divide variables.

In Here, we assign values to a variables, that is, we give the sentence “Today is a nice day, I want to go to play.” assigned to the variable text.

Therefore, the data type of the second line of text is str.

So, when the third line is to print out the text, it is printed that “Today is a nice day, I want to go to play .” this sentence.

Now, today’s three themes have been finished.

And finally, at the end, just briefly explain some of the characteristic of the string.

First, the string type can multiplication.

text_1 = 'Today is a nice day.'
text_2 = 'I want to go to play.'
print(text_1 + text_2)


Output:

Today is a nice day. I want to go to play.


And then, we change our code.

This time we multiplied the first sentence by three.
What will be printed this time?

text_1 = 'Today is a nice day.'
text_2 = 'I want to go to play'
print(text_1 * 3)


Output:

Today is a nice day. Today is a nice day. Today is a nice day. 


The answer is, our program print the sentence tree times.

Even if doesn’t conform to the grammar, the program will execute your instructions arbitrarily.
This is hateful but fascinating part of the program.

Second, the string is a single “character” which is an “Alphabet”.

text = 'Today is a nice day. I want to go to play.'
print(text[0])
print(text[1])
print(text[2])


The above code prints the first character, second character, and the third character, respectively. (The program number we usually counted from 0.)

Below we will print separately.

T
o
d


But in fact, we can not be so troublesome.

Use the colon “:” to traverse the total string. (And you can designation [n:m] means n to m-1)

text = 'Today is a nice day. I want to go to play.'
print(text[:])


Output:

Today is a nice day. I want to go to play.


This is a very important if you need to use tools for natural language processing in the future.

The above is the tutorial about Python. It’s about addition, subtraction, multiplication, division, string, and variable.


Read More

Tags:

Leave a Reply