Control tools in Python and how to use them
4 minute(s) read
|
Published on: Oct 15, 2021
Updated on: Dec 14, 2021
|
As we said, in the Python programming-language, there are some control tools, such as for, if, functions, and so on. To be able to use them better in the Python language, we must first become familiar with its syntax and how to use them.
If in Python-programming language
If it is used for conditional loops, then we can use the if statement to be able to insert conditional statements for the desired results in the design and development of a variety of mobile applications and websites ( in Slovak: návrh a vývoj rôznych mobilných aplikácií a webových stránok ). The best way to use the if statement is as follows:
For in Python-programming language
Note that the expression for in the Python-programming language is different from for in other programming languages. In other words, the function of this phrase in other programming languages was mostly used for iteration loops and increasing or decreasing, but in Python language can be used for different types of data such as a string or a list.
Like the following:
Code that can be used to modify a set and used to repeat sets can be placed inside the for loop.
Like the following:
The range () function in the Python-programming language
To be able to create duplicate loops on numbers, we can use the range () function. Using this function, you can generate consecutive arithmetic numbers. Like the following:
Of course, it should be noted that the end point of the generated numbers is not counted. For example, Range (10) can produce ten values, which include indices 0 to 9. Thus, the number 10 itself is not part of this sequence of born numbers.
If we want the sequence of generated numbers to start from another number, we can use the step-by-step method. Like the following:
To be able to create iterations on the indices of a sequence, we can use range () and len ().
Like the following:
Note that the objects returned by the range () function act like a list, even though they are not list types at all. This saves storage space.
break in Python
Just as the term break is used in other languages inside the for or while loops, the break term can also be used in the Python programming language. If the else clause exists in the conditional and incremental loops for, it first checks the clause and if it is not correct, it goes to the else clause, which executes the clause commands after else.
This also happens for the while loop, but if I use break, the commands will generally stop running.
Like the following:
continues in Python
Like the content above and the break statement, the continue statement can be used to prevent further duplication.
Like the following:
pass in the Python-language
It should be noted that in general, the phrase pass does nothing. If we do not want to do anything in the written programs, but structurally insert the command and related code, I use the phrase pass.
Like the following:
--- while True:
... pass # Busy-wait for keyboard interrupt (Ctrl + C)
...
For small classes, if we want to use the phrase pass, which does not work, but structurally enter the commands and related code, we do the following:
In other cases, we can use the pass term if we want to hold a space condition for a function or body. Like the following:
Functions in the Python-language
Functions in programming languages can help us so that we can easily insert programs. For example, to be able to print the Fibonacci series, we can use equal functions to write its code more easily.
Like the following:
We can use the word def to define the function. After entering the word def to create the function, we must enter the name of the function and then the parameters to be inserted in it.
Examples of how to use functions in the Python-programming language
In the example above where we defined and created a function, standard_arg is a familiar form that is used. There is no limit to the arguments in this definition of the pan.
In the second function we defined pos_only_arg, a / symbol is used, which limits the function arguments.
In the third function, kwd_only_args the permissible keyword arguments are also defined using the * sign and cause it to be allowed.