Page cover image

some additions

assignment symbol

>>> a = 4
>>> a += 3
>>> a
7
>>> 

a += 3 and a = a + 3 have the same meaning. This is an auto-increment operation.

Unlike C++ or Java, there is no such way as a++ in python, if you want a to increment by 1, then you need a += 1

All operators support auto-increment:

>>> a = 4

>>> a %= 2
>>> a
0

>>> a += 3
>>> a
3

>>> a -= 5
>>> a
-2

>>> a /= 4
>>> a
-0.5

>>> a *= 10
>>> a
-5.0

>>> a //= 6
>>> a
-1.0

>>> 

if __name__ == '__main__':

if __name__ == '__main__':
    print('hello world')

In Python, a .py file is a module. In general, the module name is the file name.

__name__ holds the current module name. When the module is run directly, the module name is __main__.

So, if __name__ == '__ main__': means: when the module is run directly, the code block below this line will be run; when the module is imported (import), the code block will not be run.

So in the main file, if __name__ == '__main__': should be added, and all your execution code should be placed under this condition. Because it makes the program more concise.

So in the package file, if __name__ == '__main__': is not necessarily required, you can add the statement that tests the function in the package file to this condition to test the correctness of the package, but when the main program calls this package file, these test statements will not be executed.

Statistics

Start time of this page: February 2, 2022

Completion time of this page: February 9, 2022

Last updated