What
This is an article that summarizes what I noticed and researched when learning machine learning using Chainer. This time, I will study control syntax ʻif and
while`, which are often used in various languages.
** Thank you ** I wrote it for my own reflection, but I am surprised to see it unexpectedly and show a reaction. We would also like to thank you for taking the time to comment and point out. It is very helpful.
Finally It is written based on my understanding, so it may be incorrect. I will correct any mistakes, please forgive me.
Content
if
Familiar conditional branching type. if, elif, else. The following is a description example. Neither {}
nor ʻendis required and it is easy to see! There seems to be nothing special about if. ** Executed when the conditional expression returns
True` **
if a > 0:
print('Greater than 0')
elif a == 0:
print('0')
else:
print('Less than 0')
while
There is nothing special about the while
loop, and it is easy to read with a clean description!
ʻIfis also executed when the conditional expression returns
True`.
count = 0
while count < 3: #No conditional expression not False Conditional expression is OK
print(count)
count += 1
break #You can also force the loop out with
I was curious when I wrote it, but where is the ** 1 loop? ** If you look it up, this is the description
Python treats statements with the same indentation as the same block without using special characters.
I want to be careful about managing intensity.
Another thing, I checked if there is a conditional expression judgment method other than True
, Flase
bool type is a subclass of int type. Bool type True and False are equivalent to 1, 0
Because there was, it seems that numbers can be used. Subclass? ??
The definition of the function is also simple. You can also initialize the arguments and pass them as shown below. Since the extend is equivalent to the block, the end of the definition is not specified.
def hello(message='Welcome to the Chainer Tutorial'):
print(message)
return message #You can also specify the return value
Basically, it will be initialized when you exit the block. Certainly the same as C language.
a = 1
#Substitute 2 for a inside the function
def change():
a = 2
change()
a # =1 is output
To handle variables globally, write global
. The following is a sample sentence, but there are unclear points ...
a = 1
def change():
global a #Declaration that a is a global variable
a = 2 #Assignment to a global variable
change()
a # =2 is output
Does this mean that you first define ʻa with ʻa = 1
and then redefine it as global a
?
Or is the first ʻa and
global a` distinguished in the program?
On the other hand, when I wrote the following, I got a syntax error.
global a = 1
def change():
a = 2 #Assignment to a global variable
change()
a
I can't imagine the cause for a moment, but is the global
declaration valid only within the local block?
It's like changing from a local variable to a global variable. Isn't it possible to make a declaration that can be used globally from the beginning like C language?
If you are really in trouble, let's find out.
Class The concept of objects can be imagined through C ++ and Ruby. init is a constructor. self indicates the created instance itself.
The method automatically passes a reference to that instance as the first argument when called from the instance.
Because there is, it seems safe to think that self itself is a pointer. By the way, is self.name_plate a built-in function? (Or similar variables)
class House:
def __init__(self, name):
self.name_plate = name
my_house = House('Chainer') #Declaration
One argument. Since the first argument is given as self, it is not necessary to specify it. Not limited to init, self does not need to be specified at the time of calling.
It can be inherited like C ++. Chain is the child class and Link is the parent class.
The point is that the constructor calls the constructor of the parent class if there is no description.
Use super ()
to refer to a parent class from a child class.
The function with the same name as the parent class is overridden.
class Chain(Link):
def __init__(self):
#Of the parent class`__init__()`Call the method
super().__init__()
def true():
return True
** This is the end of Chapter 2 Python learning ** It was long ... w Next time, I will work on Chapter 3. Comment It's getting cold recently, so I took a bath for the first time this year. As the weekends get tired, I tend to get sleepy even if I try to study at the end of work, but if I take a good bath, I can secure enough concentration until bedtime. In order to continue, I think it's best to keep your body in good condition, do not overdo it, and keep working for a short time with high concentration.
Recommended Posts