3

If is a function or an operator in Python. Just I am curious about it. In my opinion, it is a function because it gives output and accomplishes commands. I know that it is statement.

Teymur
  • 92
  • 5
  • 7
    is your question about [`if`](https://docs.python.org/3/reference/expressions.html#if-expr) or about [`is`](https://docs.python.org/3/reference/expressions.html#is)? neither of them are functions... – hiro protagonist Apr 15 '21 at 07:14
  • I am asking about If – Teymur Apr 15 '21 at 07:18
  • Does this answer your question? https://stackoverflow.com/a/2955829/8704792 It refers to C++ but seems to be relevant nonetheless – Anson Miu Apr 15 '21 at 07:18
  • 2
    It is an element of the conditional statement or ternary operator. Per se it's just a keyword. It's certainly not a function. – bereal Apr 15 '21 at 07:24

4 Answers4

6

As seen in the Operator Precedence table, inline if-else (a if cond else b) is an operator.

The if starting a conditional block is, however, neither an operator nor a function because it is a statement, as described in Chapter 3 of the Python documentation.

Altareos
  • 843
  • 6
  • 13
2

In python, if is a statement used for conditional execution. It's not considered a function nor an operator.

I don't think you 'll find a better answer than the python docs.

Alex Metsai
  • 1,837
  • 5
  • 12
  • 24
0

The if statement is not a function, as evidenced by this list of built-in functions. It is also not an operator, about which you can find information here.

It is in fact a compound statement.

Ewran
  • 328
  • 4
  • 15
-1

Considered if is built-in construction in the core of the language I think it can be considered less of a function but more an operator. If can be described as a set of instructions but at the high level (such as python) it's operator.

red
  • 3
  • 3
  • 3
    Technically the Python language defines it as a [statement](https://docs.python.org/3/reference/compound_stmts.html#if), not an [operator](https://docs.python.org/3/reference/lexical_analysis.html?highlight=operator#operators). – Iguananaut Apr 15 '21 at 07:23