Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the commonly used conditional judgment statement block in python?

Shulou Source: shulou.com Published: 2022-06-01 04:13:27 09月17日 Update

This article mainly introduces the python commonly used conditional judgment statement block is what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will get something after reading this python commonly used conditional judgment statement block, let's take a look at it.

Python commonly used conditional judgment statement block if _ _ name__ = ='_ _ main__'.

Create a new python file named py3_main.py, and write the operation code in this file:

# Today, learn the # common conditional judgment statement in python # if _ _ name__ = ='_ _ main__'# to check whether the python custom module is run directly or imported into other modules # Let's print the name directly: print ("py3_main module's name: {}" .format (_ _ name__)) # the result is: # py3_main module's name: _ _ main__# where _ _ name__ is a special variable in python

Create a new python file named py3_module.py:

# We import py3_mainimport py3_main# here we run the program found # output result is: # py3_main module's name: py3_main# because py3_main is imported module # _ _ name__ is py3_main# continue to print the nameprint of this module ("py3_module module's name: {}" .format (_ _ name__)) # py3_module module's name: _ _ main__

We adjust the print statement in py3_main to add conditional judgment:

Def main (): print ("py3_main module's name: {}" .format (_ _ name__))

# determine whether the module is imported or run directly by the condition: # to run the main () function

If _ _ name__ ='_ _ main__': main () else: print ("run module's name: {}" .format (_ _ name__))

Then, after re-executing the code of py3_module.py, it is found that the running result is:

Run module's name: py3_mainpy3_module module's name: _ _ main__

With if _ _ name__ = ='_ _ main__', we can control when the code in the module is executed, similar to the main methods of other programming languages such as java.

This is the end of the article on "what is the block of conditional judgment statements commonly used in python?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the conditional judgment sentence block commonly used in python". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Conditions statements modules runs commonly used code files knowledge results content articles or learning special value functions variables names simple operations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Xiaomi Shulou Information MariaDB