Conditional if construct. Python: the vastness and simplicity of a language

The simplicity of Python allows beginners to learn a programming language. The presence of various methods, functions, libraries, constructs and cycles allows you to solve even the most complex problems. For example, the conditional operator if. Python offers simple, intuitive, and widely used construction syntax.

if python

Python Overview

Using Python, tasks can be completely different. Why?

  • Interpretability of the language. This means that the source code is not compiled into machine record, but is executed by the interpreter.
  • Interactivity It turns out that the program implements the commands written in the shell of the interpreter after executing the previous ones.
  • Object Orientation The language perfectly supports the principles of OOP. It is understood that the program code encapsulates commands in objects, or otherwise called special structures.

Learning Python in Russian is not possible, since the reserved words are written in English. The text displayed on the screen can be presented in a language that is understandable to the user, if you use the encoding.

Back in 2013, Python ranked first in user learning. The second position is in the Java language, and the third is in C ++. By 2017, the statistics have changed a bit. The first line is Java, the second and third are C and C ++, and the fourth is Python. Although he lost several positions in the ranking, his popularity among programmers is only growing. Some organizations are looking exclusively for pythonists. Because Python is a high-level language, productivity is maximized at the lowest cost.

Python Benefits

The popularity of this programming language is growing, since it has a number of distinctive features.

  • The simplest help () command is able to display complete information regarding the question posed.
  • Using Python, you can write simple scripts and create full-fledged applications (be it web-programming or games).
  • Cross-platform language allows you to run written applications under any operating system: Windows, Linux, Mac Os.
  • The huge standard library allows you to connect various modules. Their "activity" is aimed at solving problems with databases, web-development, complex mathematical calculations, etc.
  • On the World Wide Web you can find the answer to any question regarding Python, as a community has formed around the language with competent and experienced specialists.
  • The ability to integrate an application written in Python into any program executed in C ++, for example.
  • Each data type has Python functions and methods that make life easier for the user.
  • The distribution is distributed completely free. Some operating systems initially install it.

Types of conditional structures

The if construct (Python is considered the main development environment) implies the possibility of verifying the truth of a condition. If the expression returns True, the block is executed. Otherwise, the user prescribes a different program behavior. Most programming languages ​​contain similar mechanisms for decision making.

If Python construction is divided into the following types:

  • Syntax if. The condition is defined here. If it is true, the corresponding block is executed. If the result of the calculation is False, then the prescribed commands are skipped and the program proceeds further.

Block diagram of the if construct.

python methods

Program Example:

python methods

  • The syntax is if ... else. The reserved word else can be added to the original construct. In the block obeying this command, expressions are written. They will be executed if the initial conditions give the result False.

Block diagram of the if ... else construct.

python tasks

Program Example:

python tasks

  • The syntax is if ... elif ... else. The command located in the center allows you to check the truth of several conditions. If any of them returns True, then the corresponding block is executed. The number of elif in the conditional construct can be arbitrary, unlike else.

Block diagram of the if ... elif ... else construct.

python in russian

Program Example:

python in russian

Nested conditional constructions

The if construct (Python 3.0 and 2.7 agree on this) allows the use of checking one condition inside another. This means that when setting up a truth check, it may be necessary to iterate over several more Boolean expressions. In this case, another one is embedded in the if construct (for example, if ... elif .... else). The user needs to carefully indent as Python does not tolerate liberties in this regard.

Source: https://habr.com/ru/post/K4993/


All Articles