What is the difference between a statement and a function in Python?

前端 未结 3 1304
礼貌的吻别
礼貌的吻别 2020-12-31 07:54

Edit: The suggested duplicate, does not answer my question, as I am primarily concerned with the difference in Python specifically. The suggested duplicate is far broade

3条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 08:07

    A statement is everything that python can understand. A function is one of those things and he have different capabilities than other statements. A function is a statement but there are many statements that aren't functions.

    The if statement is one of those that isn't a function. the for and while are also great examples. All of them are part of the Compound Statements group.

    In Python, functions have to return a value. Even if you do not use a return statement the function will return None. there are some statements that do not return any value like if, for, while, etc. how can you check if a statement is a function or not? you can use the type function which will return class 'function' for functions. You can try to ask for the type of if but you will get a SyntaxError.

    In python 2 the print statement was part of the Simple statements like import, global, break, and others. But after PEP 3105 a new print function replaced the print Simple Statement. In PEP 3105 you can read why the community decided to change the python statement, which may make it clearer why some of the statements aren't functions.

提交回复
热议问题