Python if / else conditional statements: syntax and usage

Instruction if there is any programming language, it is used in Java, C and Assembler procedural. The if syntax is different in Python. In many ways, it is much simpler and more compact, but has its own specific elements.

if else python




If / else syntax rules in Python

Python is a scripting language, so its main task is to simplify the code and make life easier for the developer. The rule applies to all objects in the language, including the if statement . Unlike C-like languages, Python lacks curly braces, the need for a semicolon at the end of expressions. But there is one new element. This is a colon sign.

The simplest if / else example in Python:

  • >>> c = 10
  • >>> if c> 2:
  • print (c)
  • ten

c. . print().

, . .

if elif else python




, , . Python . . .





if/else Python:

  • if :
  • if :
  • else:

IDLE, . .

Python if/else . , . if , if. if else. , if, True, else.

, elif. . if/elif/else Python :

  • if <1>: # if
  • <1> #
  • elif <2>: # elif
  • <2>
  • else: # else
  • <3>

After optional instructions, a colon and a mandatory indent are put. The else part is designed to handle situations in which no matches were found in if / elif . According to the rules, each part of the code is processed sequentially. But conditional expressions force the interpreter to perform transitions. Therefore, in Python, they are also called flow control instructions.




All Articles