So far, I have touched on lists and tuples in the fields of strings, numbers, and data structures. As mentioned in the data structure section, these were referred to as ** objects **.
So what is this object? First of all, I would like to explain the object and dig a little deeper.
First, enter the following code in the ** Python Console ** to check it.
>>> S = 'hello'
>>> type(S)
<class 'str'>
I'm assigning the string'hello' to the variable ** S **. Now, next, the ** type () ** function outputs ** \ <class'str' > **. This means that the S variable is currently a ** str type object **.
Now let's take a look at the details of the str object. You can browse from the following URL. https://docs.python.org/ja/3/library/stdtypes.html#text-sequence-type-str
Looking at the manual, the str type is written as a text sequence type. In short, it is a string type object.
Now, if you send the manual a little down, you'll see something called a "string method". For example, let's execute the * str * .capitalize () method at the very beginning. Note that * str * in this manual represents a str type object, so when you actually enter it this time, the variable name will be S.
>>> S
'hello'
>>> S.capitalize()
'Hello'
You can see that only the beginning is capitalized. See the * str * .capitalize () method manual for details.
Also, let's look at other methods. Let's execute the * str * .replace () method.
>>> S.replace('l', 'L')
'heLLo'
You can change the characters in the string with the replace method. See the manual for details.
Let's try various other methods ** while looking at the manual **. In the future, I would like to omit some of the contents written in the manual, except for some parts.
Well, not only this str type, but there are other types of objects. Let's check various people with the following commands.
The following is a ** int type object **.
>>> num1 = 100
>>> type(num1)
<class 'int'>
The following is a ** float type object **.
>>> num2 = 10.5
>>> type(num2)
<class 'float'>
The following is a ** list type object **.
>>> ls = ['Japan', 'America', 'China']
>>> type(ls)
<class 'list'>
The following is a ** tuple type object **.
>>> tp = (10, 20, 30)
>>> type(tp)
<class 'tuple'>
The following is a ** dict type object **.
>>> dc = {'jp':'Japan','us':'America', 'ch':'Chine'}
>>> type(dc)
<class 'dict'>
Each of these types of objects has a ** method **. Let's actually check it. Most of them are listed at the following URL. https://docs.python.org/ja/3/library/stdtypes.html
>>> ls = [1, 3, 3, 2, 1, 2, 2, 1, 1, 2]
>>> ls.count(2)
4
If so, ls is a ** list-type object **, and that object has ** data of "1, 3, 3, 2, 1, 2, 2, 1, 1, 2" **, It also has a ** method called count () **.
A function is also an object (** function object **).
def calc_func(x):
print('Run inside a function')
f = (x ** 2) + (3 * x) + 4
##Returns the calculation result f
return f
print(type(calc_func))
[Execution result] </ font> <class 'function'>
Earlier there were objects such as int type, str type, and list type, but in fact, you can create the object yourself. I would like to touch on this next time.
The concept of this object is so tricky that you may feel a wall at first. The point is to read various manuals that were also in the URL above. Even if you do not understand the meaning at first, you will understand the meaning by reading it, so the point is to actually execute it while looking at the manual.